Skip to content

Commit 5e71dfc

Browse files
committed
check dynamically for getPhysicallyInstalledSystemMemory on windows (issue #992)
1 parent 14b4f67 commit 5e71dfc

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/prim/windows/prim.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ static PGetNumaProcessorNodeEx pGetNumaProcessorNodeEx = NULL;
6262
static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL;
6363
static PGetNumaProcessorNode pGetNumaProcessorNode = NULL;
6464

65+
// Available after Windows XP
66+
typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory( PULONGLONG TotalMemoryInKilobytes );
67+
6568
//---------------------------------------------
6669
// Enable large page support dynamically (if possible)
6770
//---------------------------------------------
@@ -123,13 +126,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
123126
const size_t vbits = MI_INTPTR_BITS - mi_clz((uintptr_t)si.lpMaximumApplicationAddress);
124127
config->virtual_address_bits = vbits;
125128
}
126-
// get physical memory
127-
ULONGLONG memInKiB = 0;
128-
if (GetPhysicallyInstalledSystemMemory(&memInKiB)) {
129-
if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) {
130-
config->physical_memory = (size_t)memInKiB * MI_KiB;
131-
}
132-
}
129+
133130
// get the VirtualAlloc2 function
134131
HINSTANCE hDll;
135132
hDll = LoadLibrary(TEXT("kernelbase.dll"));
@@ -152,8 +149,19 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
152149
pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx");
153150
pGetNumaNodeProcessorMaskEx = (PGetNumaNodeProcessorMaskEx)(void (*)(void))GetProcAddress(hDll, "GetNumaNodeProcessorMaskEx");
154151
pGetNumaProcessorNode = (PGetNumaProcessorNode)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNode");
152+
// Get physical memory (not available on XP, so check dynamically)
153+
PGetPhysicallyInstalledSystemMemory pGetPhysicallyInstalledSystemMemory = (PGetPhysicallyInstalledSystemMemory)(void (*)(void))GetProcAddress(hDll,"GetPhysicallyInstalledSystemMemory");
154+
if (pGetPhysicallyInstalledSystemMemory != NULL) {
155+
ULONGLONG memInKiB = 0;
156+
if ((*pGetPhysicallyInstalledSystemMemory)(&memInKiB)) {
157+
if (memInKiB > 0 && memInKiB < (SIZE_MAX / MI_KiB)) {
158+
config->physical_memory = (size_t)memInKiB * MI_KiB;
159+
}
160+
}
161+
}
155162
FreeLibrary(hDll);
156163
}
164+
// Enable large/huge OS page support?
157165
if (mi_option_is_enabled(mi_option_allow_large_os_pages) || mi_option_is_enabled(mi_option_reserve_huge_os_pages)) {
158166
win_enable_large_os_pages(&config->large_page_size);
159167
}

0 commit comments

Comments
 (0)