From 5e71dfc33641bbf2853e9a64825fcd02bb8c95a3 Mon Sep 17 00:00:00 2001 From: Daan Date: Wed, 22 Jan 2025 12:03:58 -0800 Subject: [PATCH 1/2] check dynamically for getPhysicallyInstalledSystemMemory on windows (issue #992) --- src/prim/windows/prim.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c index b25b8f2d..9a8610b0 100644 --- a/src/prim/windows/prim.c +++ b/src/prim/windows/prim.c @@ -62,6 +62,9 @@ static PGetNumaProcessorNodeEx pGetNumaProcessorNodeEx = NULL; static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL; static PGetNumaProcessorNode pGetNumaProcessorNode = NULL; +// Available after Windows XP +typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory( PULONGLONG TotalMemoryInKilobytes ); + //--------------------------------------------- // Enable large page support dynamically (if possible) //--------------------------------------------- @@ -123,13 +126,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) const size_t vbits = MI_INTPTR_BITS - mi_clz((uintptr_t)si.lpMaximumApplicationAddress); config->virtual_address_bits = vbits; } - // get physical memory - ULONGLONG memInKiB = 0; - if (GetPhysicallyInstalledSystemMemory(&memInKiB)) { - if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) { - config->physical_memory = (size_t)memInKiB * MI_KiB; - } - } + // get the VirtualAlloc2 function HINSTANCE hDll; hDll = LoadLibrary(TEXT("kernelbase.dll")); @@ -152,8 +149,19 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx"); pGetNumaNodeProcessorMaskEx = (PGetNumaNodeProcessorMaskEx)(void (*)(void))GetProcAddress(hDll, "GetNumaNodeProcessorMaskEx"); pGetNumaProcessorNode = (PGetNumaProcessorNode)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNode"); + // Get physical memory (not available on XP, so check dynamically) + PGetPhysicallyInstalledSystemMemory pGetPhysicallyInstalledSystemMemory = (PGetPhysicallyInstalledSystemMemory)(void (*)(void))GetProcAddress(hDll,"GetPhysicallyInstalledSystemMemory"); + if (pGetPhysicallyInstalledSystemMemory != NULL) { + ULONGLONG memInKiB = 0; + if ((*pGetPhysicallyInstalledSystemMemory)(&memInKiB)) { + if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) { + config->physical_memory = (size_t)memInKiB * MI_KiB; + } + } + } FreeLibrary(hDll); } + // Enable large/huge OS page support? if (mi_option_is_enabled(mi_option_allow_large_os_pages) || mi_option_is_enabled(mi_option_reserve_huge_os_pages)) { win_enable_large_os_pages(&config->large_page_size); } From 90b7a694eb7cc755a25f7a0d2204262045aebf24 Mon Sep 17 00:00:00 2001 From: Daan Date: Wed, 22 Jan 2025 12:29:41 -0800 Subject: [PATCH 2/2] syntax error fix (#992) --- src/prim/windows/prim.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c index 9a8610b0..e3ad1e57 100644 --- a/src/prim/windows/prim.c +++ b/src/prim/windows/prim.c @@ -63,7 +63,7 @@ static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL; static PGetNumaProcessorNode pGetNumaProcessorNode = NULL; // Available after Windows XP -typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory( PULONGLONG TotalMemoryInKilobytes ); +typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory)( PULONGLONG TotalMemoryInKilobytes ); //--------------------------------------------- // Enable large page support dynamically (if possible)