Skip to content

Commit 37d2d07

Browse files
committed
gh-152433: Windows: allow build mimalloc for UWP
1 parent 38df997 commit 37d2d07

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

  • Objects/mimalloc/prim/windows

Objects/mimalloc/prim/windows/prim.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ terms of the MIT license. A copy of the license can be found in the file
1313
#include "mimalloc/prim.h"
1414
#include <stdio.h> // fputs, stderr
1515

16+
// Xbox has no console IO and cannot use LoadLibrary
17+
#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
18+
#define MI_WIN_DESKTOP 1
19+
#endif
1620

1721
//---------------------------------------------
1822
// Dynamically bind Windows API points for portability
@@ -63,6 +67,15 @@ static PGetNumaProcessorNodeEx pGetNumaProcessorNodeEx = NULL;
6367
static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL;
6468
static PGetNumaProcessorNode pGetNumaProcessorNode = NULL;
6569

70+
// Load a library
71+
static HMODULE mi_win_loadlibrary(const TCHAR* library) {
72+
#if MI_WIN_DESKTOP
73+
return LoadLibrary(library);
74+
#else
75+
return LoadPackagedLibrary(library, 0);
76+
#endif
77+
}
78+
6679
//---------------------------------------------
6780
// Enable large page support dynamically (if possible)
6881
//---------------------------------------------
@@ -121,21 +134,21 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
121134
if (si.dwAllocationGranularity > 0) { config->alloc_granularity = si.dwAllocationGranularity; }
122135
// get the VirtualAlloc2 function
123136
HINSTANCE hDll;
124-
hDll = LoadLibrary(TEXT("kernelbase.dll"));
137+
hDll = mi_win_loadlibrary(TEXT("kernelbase.dll"));
125138
if (hDll != NULL) {
126139
// use VirtualAlloc2FromApp if possible as it is available to Windows store apps
127140
pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2FromApp");
128141
if (pVirtualAlloc2==NULL) pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2");
129142
FreeLibrary(hDll);
130143
}
131144
// NtAllocateVirtualMemoryEx is used for huge page allocation
132-
hDll = LoadLibrary(TEXT("ntdll.dll"));
145+
hDll = mi_win_loadlibrary(TEXT("ntdll.dll"));
133146
if (hDll != NULL) {
134147
pNtAllocateVirtualMemoryEx = (PNtAllocateVirtualMemoryEx)(void (*)(void))GetProcAddress(hDll, "NtAllocateVirtualMemoryEx");
135148
FreeLibrary(hDll);
136149
}
137150
// Try to use Win7+ numa API
138-
hDll = LoadLibrary(TEXT("kernel32.dll"));
151+
hDll = mi_win_loadlibrary(TEXT("kernel32.dll"));
139152
if (hDll != NULL) {
140153
pGetCurrentProcessorNumberEx = (PGetCurrentProcessorNumberEx)(void (*)(void))GetProcAddress(hDll, "GetCurrentProcessorNumberEx");
141154
pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx");
@@ -377,6 +390,7 @@ size_t _mi_prim_numa_node(void) {
377390
}
378391

379392
size_t _mi_prim_numa_node_count(void) {
393+
#if MI_WIN_DESKTOP
380394
ULONG numa_max = 0;
381395
GetNumaHighestNodeNumber(&numa_max);
382396
// find the highest node number that has actual processors assigned to it. Issue #282
@@ -399,6 +413,9 @@ size_t _mi_prim_numa_node_count(void) {
399413
numa_max--;
400414
}
401415
return ((size_t)numa_max + 1);
416+
#else
417+
return ((size_t)1);
418+
#endif
402419
}
403420

404421

@@ -454,7 +471,7 @@ void _mi_prim_process_info(mi_process_info_t* pinfo)
454471

455472
// load psapi on demand
456473
if (pGetProcessMemoryInfo == NULL) {
457-
HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll"));
474+
HINSTANCE hDll = mi_win_loadlibrary(TEXT("psapi.dll"));
458475
if (hDll != NULL) {
459476
pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo");
460477
}
@@ -554,7 +571,7 @@ static PBCryptGenRandom pBCryptGenRandom = NULL;
554571

555572
bool _mi_prim_random_buf(void* buf, size_t buf_len) {
556573
if (pBCryptGenRandom == NULL) {
557-
HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll"));
574+
HINSTANCE hDll = mi_win_loadlibrary(TEXT("bcrypt.dll"));
558575
if (hDll != NULL) {
559576
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
560577
}

0 commit comments

Comments
 (0)