@@ -29,6 +29,8 @@ using PMapViewOfFile3 = PVOID(WINAPI*)(HANDLE FileMapping, HANDLE Process, PVOID
29
29
MEM_EXTENDED_PARAMETER* ExtendedParameters,
30
30
ULONG ParameterCount);
31
31
32
+ using PUnmapViewOfFileEx = BOOL(WINAPI*)(PVOID BaseAddress, ULONG UnmapFlags);
33
+
32
34
using PIsApiSetImplemented = BOOL(APIENTRY*)(PCSTR Contract);
33
35
34
36
namespace Common
@@ -61,31 +63,38 @@ MemArena::MemArena()
61
63
if (!static_cast <PIsApiSetImplemented>(ptr_IsApiSetImplemented)(" api-ms-win-core-memory-l1-1-6" ))
62
64
return ;
63
65
64
- const HMODULE handle = LoadLibraryW (L" api-ms-win-core-memory-l1-1-6.dll" );
65
- if (!handle)
66
+ m_api_ms_win_core_memory_l1_1_6_handle.Open (" api-ms-win-core-memory-l1-1-6.dll" );
67
+ m_kernel32_handle.Open (" Kernel32.dll" );
68
+ if (!m_api_ms_win_core_memory_l1_1_6_handle.IsOpen () || !m_kernel32_handle.IsOpen ())
69
+ {
70
+ m_api_ms_win_core_memory_l1_1_6_handle.Close ();
71
+ m_kernel32_handle.Close ();
66
72
return ;
73
+ }
67
74
68
- void * const address_VirtualAlloc2 = GetProcAddress (handle, " VirtualAlloc2FromApp" );
69
- void * const address_MapViewOfFile3 = GetProcAddress (handle, " MapViewOfFile3FromApp" );
70
- if (address_VirtualAlloc2 && address_MapViewOfFile3)
75
+ void * const address_VirtualAlloc2 =
76
+ m_api_ms_win_core_memory_l1_1_6_handle.GetSymbolAddress (" VirtualAlloc2FromApp" );
77
+ void * const address_MapViewOfFile3 =
78
+ m_api_ms_win_core_memory_l1_1_6_handle.GetSymbolAddress (" MapViewOfFile3FromApp" );
79
+ void * const address_UnmapViewOfFileEx = m_kernel32_handle.GetSymbolAddress (" UnmapViewOfFileEx" );
80
+ if (address_VirtualAlloc2 && address_MapViewOfFile3 && address_UnmapViewOfFileEx)
71
81
{
72
- m_api_ms_win_core_memory_l1_1_6_handle = handle;
73
82
m_address_VirtualAlloc2 = address_VirtualAlloc2;
74
83
m_address_MapViewOfFile3 = address_MapViewOfFile3;
84
+ m_address_UnmapViewOfFileEx = address_UnmapViewOfFileEx;
75
85
}
76
86
else
77
87
{
78
88
// at least one function is not available, use legacy logic
79
- FreeLibrary (handle);
89
+ m_api_ms_win_core_memory_l1_1_6_handle.Close ();
90
+ m_kernel32_handle.Close ();
80
91
}
81
92
}
82
93
83
94
MemArena::~MemArena ()
84
95
{
85
96
ReleaseMemoryRegion ();
86
97
ReleaseSHMSegment ();
87
- if (m_api_ms_win_core_memory_l1_1_6_handle)
88
- FreeLibrary (static_cast <HMODULE>(m_api_ms_win_core_memory_l1_1_6_handle));
89
98
}
90
99
91
100
void MemArena::GrabSHMSegment (size_t size)
@@ -123,7 +132,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
123
132
}
124
133
125
134
u8 * base;
126
- if (m_api_ms_win_core_memory_l1_1_6_handle)
135
+ if (m_api_ms_win_core_memory_l1_1_6_handle. IsOpen () )
127
136
{
128
137
base = static_cast <u8 *>(static_cast <PVirtualAlloc2>(m_address_VirtualAlloc2)(
129
138
nullptr , nullptr , memory_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS,
@@ -154,7 +163,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
154
163
155
164
void MemArena::ReleaseMemoryRegion ()
156
165
{
157
- if (m_api_ms_win_core_memory_l1_1_6_handle && m_reserved_region)
166
+ if (m_api_ms_win_core_memory_l1_1_6_handle. IsOpen () && m_reserved_region)
158
167
{
159
168
// user should have unmapped everything by this point, check if that's true and yell if not
160
169
// (it indicates a bug in the emulated memory mapping logic)
@@ -291,7 +300,7 @@ WindowsMemoryRegion* MemArena::EnsureSplitRegionForMapping(void* start_address,
291
300
292
301
void * MemArena::MapInMemoryRegion (s64 offset, size_t size, void * base)
293
302
{
294
- if (m_api_ms_win_core_memory_l1_1_6_handle)
303
+ if (m_api_ms_win_core_memory_l1_1_6_handle. IsOpen () )
295
304
{
296
305
WindowsMemoryRegion* const region = EnsureSplitRegionForMapping (base, size);
297
306
if (!region)
@@ -393,9 +402,10 @@ bool MemArena::JoinRegionsAfterUnmap(void* start_address, size_t size)
393
402
394
403
void MemArena::UnmapFromMemoryRegion (void * view, size_t size)
395
404
{
396
- if (m_api_ms_win_core_memory_l1_1_6_handle)
405
+ if (m_api_ms_win_core_memory_l1_1_6_handle. IsOpen () )
397
406
{
398
- if (UnmapViewOfFileEx (view, MEM_PRESERVE_PLACEHOLDER))
407
+ if (static_cast <PUnmapViewOfFileEx>(m_address_UnmapViewOfFileEx)(view,
408
+ MEM_PRESERVE_PLACEHOLDER))
399
409
{
400
410
if (!JoinRegionsAfterUnmap (view, size))
401
411
PanicAlertFmt (" Joining memory region failed." );
0 commit comments