cleanup on-demand loading of psapi and bcrypt on windows

This commit is contained in:
Daan Leijen 2022-12-19 16:50:13 -08:00
parent 59ea84cadc
commit 505a14bbd8
2 changed files with 24 additions and 25 deletions

View file

@ -194,20 +194,20 @@ static bool os_random_buf(void* buf, size_t buf_len) {
typedef LONG (NTAPI *PBCryptGenRandom)(HANDLE, PUCHAR, ULONG, ULONG);
static PBCryptGenRandom pBCryptGenRandom = NULL;
static int BCryptGenRandom_is_initialized = 0;
static bool os_random_buf(void* buf, size_t buf_len) {
if (!BCryptGenRandom_is_initialized) {
HINSTANCE hDll;
hDll = LoadLibrary(TEXT("bcrypt.dll"));
if (pBCryptGenRandom == NULL) {
HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll"));
if (hDll != NULL) {
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
}
BCryptGenRandom_is_initialized = 1;
}
if (!pBCryptGenRandom)
return 0;
if (pBCryptGenRandom == NULL) {
return false;
}
else {
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
}
}
#endif

View file

@ -489,7 +489,6 @@ typedef struct _PROCESS_MEMORY_COUNTERS {
typedef PROCESS_MEMORY_COUNTERS* PPROCESS_MEMORY_COUNTERS;
typedef BOOL (WINAPI *PGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
static PGetProcessMemoryInfo pGetProcessMemoryInfo = NULL;
static int GetProcessMemoryInfo_is_initialized = 0;
static void mi_stat_process_info(mi_msecs_t* elapsed, mi_msecs_t* utime, mi_msecs_t* stime, size_t* current_rss, size_t* peak_rss, size_t* current_commit, size_t* peak_commit, size_t* page_faults)
{
@ -501,26 +500,26 @@ static void mi_stat_process_info(mi_msecs_t* elapsed, mi_msecs_t* utime, mi_msec
GetProcessTimes(GetCurrentProcess(), &ct, &et, &st, &ut);
*utime = filetime_msecs(&ut);
*stime = filetime_msecs(&st);
PROCESS_MEMORY_COUNTERS info;
if (!GetProcessMemoryInfo_is_initialized) {
HINSTANCE hDll;
hDll = LoadLibrary(TEXT("psapi.dll"));
// load psapi on demand
if (pGetProcessMemoryInfo == NULL) {
HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll"));
if (hDll != NULL) {
pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo");
}
GetProcessMemoryInfo_is_initialized = 1;
}
if (pGetProcessMemoryInfo) {
// get process info
PROCESS_MEMORY_COUNTERS info;
memset(&info, 0, sizeof(info));
if (pGetProcessMemoryInfo != NULL) {
pGetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
}
*current_rss = (size_t)info.WorkingSetSize;
*peak_rss = (size_t)info.PeakWorkingSetSize;
*current_commit = (size_t)info.PagefileUsage;
*peak_commit = (size_t)info.PeakPagefileUsage;
*page_faults = (size_t)info.PageFaultCount;
} else {
*current_rss = *peak_rss = *current_commit = *peak_commit = *page_faults = 0;
}
}
#elif !defined(__wasi__) && (defined(__unix__) || defined(__unix) || defined(unix) || defined(__APPLE__) || defined(__HAIKU__))