Skip to content

Commit c9c86c7

Browse files
committed
allow build for universal windows platform. PR #1340 by @THEXIA
1 parent 78cc89d commit c9c86c7

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

‎src/prim/windows/prim.c‎

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

16-
// xbox has no console IO
17-
#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP | WINAPI_PARTITION_SYSTEM)
18-
#define MI_HAS_CONSOLE_IO
16+
// xbox has no console IO and cannot use mi_win_loadlibrary
17+
#if !defined(WINAPI_FAMILY_PARTITION) || WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
18+
#define MI_WIN_DESKTOP 1
1919
#endif
2020

2121
//---------------------------------------------
@@ -78,6 +78,14 @@ static PGetLargePageMinimum pGetLargePageMinimum = NULL;
7878
// Available after Windows XP
7979
typedef BOOL (__stdcall *PGetPhysicallyInstalledSystemMemory)( PULONGLONG TotalMemoryInKilobytes );
8080

81+
// Load a library
82+
static HMODULE mi_win_loadlibrary(const TCHAR* library) {
83+
#if MI_WIN_DESKTOP
84+
return LoadLibrary(library);
85+
#else
86+
return LoadPackagedLibrary(library, 0);
87+
#endif
88+
}
8189

8290
//---------------------------------------------
8391
// Enable large page support dynamically (if possible)
@@ -151,21 +159,21 @@ void _mi_prim_mem_init( mi_os_mem_config_t* config )
151159

152160
// get the VirtualAlloc2 function
153161
HINSTANCE hDll;
154-
hDll = LoadLibrary(TEXT("kernelbase.dll"));
162+
hDll = mi_win_loadlibrary(TEXT("kernelbase.dll"));
155163
if (hDll != NULL) {
156164
// use VirtualAlloc2FromApp if possible as it is available to Windows store apps
157165
pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2FromApp");
158166
if (pVirtualAlloc2==NULL) pVirtualAlloc2 = (PVirtualAlloc2)(void (*)(void))GetProcAddress(hDll, "VirtualAlloc2");
159167
FreeLibrary(hDll);
160168
}
161169
// NtAllocateVirtualMemoryEx is used for huge page allocation
162-
hDll = LoadLibrary(TEXT("ntdll.dll"));
170+
hDll = mi_win_loadlibrary(TEXT("ntdll.dll"));
163171
if (hDll != NULL) {
164172
pNtAllocateVirtualMemoryEx = (PNtAllocateVirtualMemoryEx)(void (*)(void))GetProcAddress(hDll, "NtAllocateVirtualMemoryEx");
165173
FreeLibrary(hDll);
166174
}
167175
// Try to use Win7+ numa API
168-
hDll = LoadLibrary(TEXT("kernel32.dll"));
176+
hDll = mi_win_loadlibrary(TEXT("kernel32.dll"));
169177
if (hDll != NULL) {
170178
pGetCurrentProcessorNumberEx = (PGetCurrentProcessorNumberEx)(void (*)(void))GetProcAddress(hDll, "GetCurrentProcessorNumberEx");
171179
pGetNumaProcessorNodeEx = (PGetNumaProcessorNodeEx)(void (*)(void))GetProcAddress(hDll, "GetNumaProcessorNodeEx");
@@ -554,7 +562,7 @@ void _mi_prim_process_info(mi_process_info_t* pinfo)
554562

555563
// load psapi on demand
556564
mi_atomic_do_once {
557-
HINSTANCE hDll = LoadLibrary(TEXT("psapi.dll"));
565+
HINSTANCE hDll = mi_win_loadlibrary(TEXT("psapi.dll"));
558566
if (hDll != NULL) {
559567
pGetProcessMemoryInfo = (PGetProcessMemoryInfo)(void (*)(void))GetProcAddress(hDll, "GetProcessMemoryInfo");
560568
// FreeLibrary(hDll); // don't free
@@ -588,7 +596,7 @@ void _mi_prim_out_stderr( const char* msg )
588596
static bool hconIsConsole = false;
589597
if (hcon == INVALID_HANDLE_VALUE) {
590598
hcon = GetStdHandle(STD_ERROR_HANDLE);
591-
#ifdef MI_HAS_CONSOLE_IO
599+
#if MI_WIN_DESKTOP
592600
CONSOLE_SCREEN_BUFFER_INFO sbi;
593601
hconIsConsole = ((hcon != INVALID_HANDLE_VALUE) && GetConsoleScreenBufferInfo(hcon, &sbi));
594602
#endif
@@ -597,7 +605,7 @@ void _mi_prim_out_stderr( const char* msg )
597605
if (len > 0 && len < UINT32_MAX) {
598606
DWORD written = 0;
599607
if (hconIsConsole) {
600-
#ifdef MI_HAS_CONSOLE_IO
608+
#if MI_WIN_DESKTOP
601609
WriteConsoleA(hcon, msg, (DWORD)len, &written, NULL);
602610
#endif
603611
}
@@ -660,7 +668,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
660668
mi_assert(buf_len <= ULONG_MAX);
661669
if (buf_len > ULONG_MAX) return false;
662670
mi_atomic_do_once {
663-
HINSTANCE hDll = LoadLibrary(TEXT("bcrypt.dll"));
671+
HINSTANCE hDll = mi_win_loadlibrary(TEXT("bcrypt.dll"));
664672
if (hDll != NULL) {
665673
pBCryptGenRandom = (PBCryptGenRandom)(void (*)(void))GetProcAddress(hDll, "BCryptGenRandom");
666674
// FreeLibrary(hDll); // don't free

0 commit comments

Comments
 (0)