Skip to content

Commit 58add09

Browse files
committed
gh-152433: Windows: allow build prim.c for UWP
1 parent 95bfaff commit 58add09

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

  • Objects/mimalloc/prim/windows

Objects/mimalloc/prim/windows/prim.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ 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+
#include <psapi.h>
17+
#include <bcrypt.h>
1618

1719
//---------------------------------------------
1820
// Dynamically bind Windows API points for portability
@@ -121,21 +123,33 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
121123
if (si.dwAllocationGranularity > 0) { config->alloc_granularity = si.dwAllocationGranularity; }
122124
// get the VirtualAlloc2 function
123125
HINSTANCE hDll;
126+
#ifndef MS_WINDOWS_DESKTOP
127+
hDll = LoadPackagedLibrary(L"kernelbase", 0);
128+
#else
124129
hDll = LoadLibrary(TEXT("kernelbase.dll"));
130+
#endif
125131
if (hDll != NULL) {
126132
// use VirtualAlloc2FromApp if possible as it is available to Windows store apps
127133
pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2FromApp");
128134
if (pVirtualAlloc2==NULL) pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2");
129135
FreeLibrary(hDll);
130136
}
131137
// NtAllocateVirtualMemoryEx is used for huge page allocation
138+
#ifndef MS_WINDOWS_DESKTOP
139+
hDll = LoadPackagedLibrary(L"ntdll", 0);
140+
#else
132141
hDll = LoadLibrary(TEXT("ntdll.dll"));
142+
#endif
133143
if (hDll != NULL) {
134144
pNtAllocateVirtualMemoryEx = (PNtAllocateVirtualMemoryEx)(void (*)(void))GetProcAddress(hDll, "NtAllocateVirtualMemoryEx");
135145
FreeLibrary(hDll);
136146
}
137147
// Try to use Win7+ numa API
148+
#ifndef MS_WINDOWS_DESKTOP
149+
hDll = LoadPackagedLibrary(L"kernel32", 0);
150+
#else
138151
hDll = LoadLibrary(TEXT("kernel32.dll"));
152+
#endif
139153
if (hDll != NULL) {
140154
pGetCurrentProcessorNumberEx = (PGetCurrentProcessorNumberEx)(void (*)(void))GetProcAddress(hDll, "GetCurrentProcessorNumberEx");
141155
pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx");
@@ -377,6 +391,9 @@ size_t _mi_prim_numa_node(void) {
377391
}
378392

379393
size_t _mi_prim_numa_node_count(void) {
394+
#ifndef MS_WINDOWS_DESKTOP
395+
return ((size_t)1);
396+
#else
380397
ULONG numa_max = 0;
381398
GetNumaHighestNodeNumber(&numa_max);
382399
// find the highest node number that has actual processors assigned to it. Issue #282
@@ -399,6 +416,7 @@ size_t _mi_prim_numa_node_count(void) {
399416
numa_max--;
400417
}
401418
return ((size_t)numa_max + 1);
419+
#endif
402420
}
403421

404422

@@ -439,9 +457,6 @@ static mi_msecs_t filetime_msecs(const FILETIME* ftime) {
439457
return msecs;
440458
}
441459

442-
typedef BOOL (WINAPI *PGetProcessMemoryInfo)(HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD);
443-
static PGetProcessMemoryInfo pGetProcessMemoryInfo = NULL;
444-
445460
void _mi_prim_process_info(mi_process_info_t* pinfo)
446461
{
447462
FILETIME ct;
@@ -452,20 +467,11 @@ void _mi_prim_process_info(mi_process_info_t* pinfo)
452467
pinfo->utime = filetime_msecs(&ut);
453468
pinfo->stime = filetime_msecs(&st);
454469

455-
// load psapi on demand
456-
if (pGetProcessMemoryInfo == NULL) {
457-
HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll"));
458-
if (hDll != NULL) {
459-
pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo");
460-
}
461-
}
462-
463470
// get process info
464471
PROCESS_MEMORY_COUNTERS info;
465472
memset(&info, 0, sizeof(info));
466-
if (pGetProcessMemoryInfo != NULL) {
467-
pGetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
468-
}
473+
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
474+
469475
pinfo->current_rss = (size_t)info.WorkingSetSize;
470476
pinfo->peak_rss = (size_t)info.PeakWorkingSetSize;
471477
pinfo->current_commit = (size_t)info.PagefileUsage;
@@ -549,18 +555,8 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
549555
#define BCRYPT_USE_SYSTEM_PREFERRED_RNG 0x00000002
550556
#endif
551557

552-
typedef LONG (NTAPI *PBCryptGenRandom)(HANDLE, PUCHAR, ULONG, ULONG);
553-
static PBCryptGenRandom pBCryptGenRandom = NULL;
554-
555558
bool _mi_prim_random_buf(void* buf, size_t buf_len) {
556-
if (pBCryptGenRandom == NULL) {
557-
HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll"));
558-
if (hDll != NULL) {
559-
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
560-
}
561-
if (pBCryptGenRandom == NULL) return false;
562-
}
563-
return (pBCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
559+
return (BCryptGenRandom(NULL, (PUCHAR)buf, (ULONG)buf_len, BCRYPT_USE_SYSTEM_PREFERRED_RNG) >= 0);
564560
}
565561

566562
#endif // MI_USE_RTLGENRANDOM

0 commit comments

Comments
 (0)