From 37d2d07b5f7c61f1ec75ec6099d44c66e282437f Mon Sep 17 00:00:00 2001 From: thexai <58434170+thexai@users.noreply.github.com> Date: Sun, 28 Jun 2026 11:08:49 +0200 Subject: [PATCH 1/2] gh-152433: Windows: allow build ``mimalloc`` for UWP --- Objects/mimalloc/prim/windows/prim.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Objects/mimalloc/prim/windows/prim.c b/Objects/mimalloc/prim/windows/prim.c index a038277ad21cb0..c0aa9622c47600 100644 --- a/Objects/mimalloc/prim/windows/prim.c +++ b/Objects/mimalloc/prim/windows/prim.c @@ -13,6 +13,10 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc/prim.h" #include // fputs, stderr +// Xbox has no console IO and cannot use LoadLibrary +#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) +#define MI_WIN_DESKTOP 1 +#endif //--------------------------------------------- // Dynamically bind Windows API points for portability @@ -63,6 +67,15 @@ static PGetNumaProcessorNodeEx pGetNumaProcessorNodeEx = NULL; static PGetNumaNodeProcessorMaskEx pGetNumaNodeProcessorMaskEx = NULL; static PGetNumaProcessorNode pGetNumaProcessorNode = NULL; +// Load a library +static HMODULE mi_win_loadlibrary(const TCHAR* library) { +#if MI_WIN_DESKTOP + return LoadLibrary(library); +#else + return LoadPackagedLibrary(library, 0); +#endif +} + //--------------------------------------------- // Enable large page support dynamically (if possible) //--------------------------------------------- @@ -121,7 +134,7 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) if (si.dwAllocationGranularity > 0) { config->alloc_granularity = si.dwAllocationGranularity; } // get the VirtualAlloc2 function HINSTANCE hDll; - hDll = LoadLibrary(TEXT("kernelbase.dll")); + hDll = mi_win_loadlibrary(TEXT("kernelbase.dll")); if (hDll != NULL) { // use VirtualAlloc2FromApp if possible as it is available to Windows store apps pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2FromApp"); @@ -129,13 +142,13 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config ) FreeLibrary(hDll); } // NtAllocateVirtualMemoryEx is used for huge page allocation - hDll = LoadLibrary(TEXT("ntdll.dll")); + hDll = mi_win_loadlibrary(TEXT("ntdll.dll")); if (hDll != NULL) { pNtAllocateVirtualMemoryEx = (PNtAllocateVirtualMemoryEx)(void (*)(void))GetProcAddress(hDll, "NtAllocateVirtualMemoryEx"); FreeLibrary(hDll); } // Try to use Win7+ numa API - hDll = LoadLibrary(TEXT("kernel32.dll")); + hDll = mi_win_loadlibrary(TEXT("kernel32.dll")); if (hDll != NULL) { pGetCurrentProcessorNumberEx = (PGetCurrentProcessorNumberEx)(void (*)(void))GetProcAddress(hDll, "GetCurrentProcessorNumberEx"); pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx"); @@ -377,6 +390,7 @@ size_t _mi_prim_numa_node(void) { } size_t _mi_prim_numa_node_count(void) { +#if MI_WIN_DESKTOP ULONG numa_max = 0; GetNumaHighestNodeNumber(&numa_max); // 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) { numa_max--; } return ((size_t)numa_max + 1); +#else + return ((size_t)1); +#endif } @@ -454,7 +471,7 @@ void _mi_prim_process_info(mi_process_info_t* pinfo) // load psapi on demand if (pGetProcessMemoryInfo == NULL) { - HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll")); + HINSTANCE hDll = mi_win_loadlibrary(TEXT("psapi.dll")); if (hDll != NULL) { pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo"); } @@ -554,7 +571,7 @@ static PBCryptGenRandom pBCryptGenRandom = NULL; bool _mi_prim_random_buf(void* buf, size_t buf_len) { if (pBCryptGenRandom == NULL) { - HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll")); + HINSTANCE hDll = mi_win_loadlibrary(TEXT("bcrypt.dll")); if (hDll != NULL) { pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom"); } From d86d607ae15aff6daa0d6f701ccd2e4d4978660f Mon Sep 17 00:00:00 2001 From: thexai <58434170+thexai@users.noreply.github.com> Date: Sat, 1 Aug 2026 17:17:57 +0200 Subject: [PATCH 2/2] Fix missing header --- Objects/mimalloc/prim/windows/prim.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/mimalloc/prim/windows/prim.c b/Objects/mimalloc/prim/windows/prim.c index c0aa9622c47600..adbe956f09ef68 100644 --- a/Objects/mimalloc/prim/windows/prim.c +++ b/Objects/mimalloc/prim/windows/prim.c @@ -12,6 +12,7 @@ terms of the MIT license. A copy of the license can be found in the file #include "mimalloc/atomic.h" #include "mimalloc/prim.h" #include // fputs, stderr +#include // NTSTATUS // Xbox has no console IO and cannot use LoadLibrary #if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)