mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-05 23:19:31 +03:00
cleanup on-demand loading of psapi and bcrypt on windows
This commit is contained in:
parent
59ea84cadc
commit
505a14bbd8
2 changed files with 24 additions and 25 deletions
14
src/random.c
14
src/random.c
|
@ -194,21 +194,21 @@ static bool os_random_buf(void* buf, size_t buf_len) {
|
||||||
|
|
||||||
typedef LONG (NTAPI *PBCryptGenRandom)(HANDLE, PUCHAR, ULONG, ULONG);
|
typedef LONG (NTAPI *PBCryptGenRandom)(HANDLE, PUCHAR, ULONG, ULONG);
|
||||||
static PBCryptGenRandom pBCryptGenRandom = NULL;
|
static PBCryptGenRandom pBCryptGenRandom = NULL;
|
||||||
static int BCryptGenRandom_is_initialized = 0;
|
|
||||||
|
|
||||||
static bool os_random_buf(void* buf, size_t buf_len) {
|
static bool os_random_buf(void* buf, size_t buf_len) {
|
||||||
if (!BCryptGenRandom_is_initialized) {
|
if (pBCryptGenRandom == NULL) {
|
||||||
HINSTANCE hDll;
|
HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll"));
|
||||||
hDll = LoadLibrary(TEXT("bcrypt.dll"));
|
|
||||||
if (hDll != NULL) {
|
if (hDll != NULL) {
|
||||||
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
|
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
|
||||||
}
|
}
|
||||||
BCryptGenRandom_is_initialized = 1;
|
|
||||||
}
|
}
|
||||||
if (!pBCryptGenRandom)
|
if (pBCryptGenRandom == NULL) {
|
||||||
return 0;
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
|
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
|
|
19
src/stats.c
19
src/stats.c
|
@ -489,7 +489,6 @@ typedef struct _PROCESS_MEMORY_COUNTERS {
|
||||||
typedef PROCESS_MEMORY_COUNTERS* PPROCESS_MEMORY_COUNTERS;
|
typedef PROCESS_MEMORY_COUNTERS* PPROCESS_MEMORY_COUNTERS;
|
||||||
typedef BOOL (WINAPI *PGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
|
typedef BOOL (WINAPI *PGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
|
||||||
static PGetProcessMemoryInfo pGetProcessMemoryInfo = NULL;
|
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)
|
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);
|
GetProcessTimes(GetCurrentProcess(), &ct, &et, &st, &ut);
|
||||||
*utime = filetime_msecs(&ut);
|
*utime = filetime_msecs(&ut);
|
||||||
*stime = filetime_msecs(&st);
|
*stime = filetime_msecs(&st);
|
||||||
PROCESS_MEMORY_COUNTERS info;
|
|
||||||
|
|
||||||
if (!GetProcessMemoryInfo_is_initialized) {
|
// load psapi on demand
|
||||||
HINSTANCE hDll;
|
if (pGetProcessMemoryInfo == NULL) {
|
||||||
hDll = LoadLibrary(TEXT("psapi.dll"));
|
HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll"));
|
||||||
if (hDll != NULL) {
|
if (hDll != NULL) {
|
||||||
pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo");
|
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));
|
pGetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
|
||||||
|
}
|
||||||
*current_rss = (size_t)info.WorkingSetSize;
|
*current_rss = (size_t)info.WorkingSetSize;
|
||||||
*peak_rss = (size_t)info.PeakWorkingSetSize;
|
*peak_rss = (size_t)info.PeakWorkingSetSize;
|
||||||
*current_commit = (size_t)info.PagefileUsage;
|
*current_commit = (size_t)info.PagefileUsage;
|
||||||
*peak_commit = (size_t)info.PeakPagefileUsage;
|
*peak_commit = (size_t)info.PeakPagefileUsage;
|
||||||
*page_faults = (size_t)info.PageFaultCount;
|
*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__))
|
#elif !defined(__wasi__) && (defined(__unix__) || defined(__unix) || defined(unix) || defined(__APPLE__) || defined(__HAIKU__))
|
||||||
|
|
Loading…
Add table
Reference in a new issue