Skip to content

Commit eba1998

Browse files
authored
Merge pull request #10452 from AdmiralCurtiss/win7-unmap-view-of-file-ex
MemArena: Restore Windows 7 support.
2 parents 7c91acb + 4a9553b commit eba1998

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

Source/Core/Common/MemArena.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <vector>
88

99
#include "Common/CommonTypes.h"
10+
#include "Common/DynamicLibrary.h"
1011

1112
namespace Common
1213
{
@@ -106,7 +107,9 @@ class MemArena final
106107
std::vector<WindowsMemoryRegion> m_regions;
107108
void* m_reserved_region = nullptr;
108109
void* m_memory_handle = nullptr;
109-
void* m_api_ms_win_core_memory_l1_1_6_handle = nullptr;
110+
Common::DynamicLibrary m_kernel32_handle;
111+
Common::DynamicLibrary m_api_ms_win_core_memory_l1_1_6_handle;
112+
void* m_address_UnmapViewOfFileEx = nullptr;
110113
void* m_address_VirtualAlloc2 = nullptr;
111114
void* m_address_MapViewOfFile3 = nullptr;
112115
#else

Source/Core/Common/MemArenaWin.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ using PMapViewOfFile3 = PVOID(WINAPI*)(HANDLE FileMapping, HANDLE Process, PVOID
2929
MEM_EXTENDED_PARAMETER* ExtendedParameters,
3030
ULONG ParameterCount);
3131

32+
using PUnmapViewOfFileEx = BOOL(WINAPI*)(PVOID BaseAddress, ULONG UnmapFlags);
33+
3234
using PIsApiSetImplemented = BOOL(APIENTRY*)(PCSTR Contract);
3335

3436
namespace Common
@@ -61,31 +63,38 @@ MemArena::MemArena()
6163
if (!static_cast<PIsApiSetImplemented>(ptr_IsApiSetImplemented)("api-ms-win-core-memory-l1-1-6"))
6264
return;
6365

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();
6672
return;
73+
}
6774

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)
7181
{
72-
m_api_ms_win_core_memory_l1_1_6_handle = handle;
7382
m_address_VirtualAlloc2 = address_VirtualAlloc2;
7483
m_address_MapViewOfFile3 = address_MapViewOfFile3;
84+
m_address_UnmapViewOfFileEx = address_UnmapViewOfFileEx;
7585
}
7686
else
7787
{
7888
// 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();
8091
}
8192
}
8293

8394
MemArena::~MemArena()
8495
{
8596
ReleaseMemoryRegion();
8697
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));
8998
}
9099

91100
void MemArena::GrabSHMSegment(size_t size)
@@ -123,7 +132,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
123132
}
124133

125134
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())
127136
{
128137
base = static_cast<u8*>(static_cast<PVirtualAlloc2>(m_address_VirtualAlloc2)(
129138
nullptr, nullptr, memory_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS,
@@ -154,7 +163,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
154163

155164
void MemArena::ReleaseMemoryRegion()
156165
{
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)
158167
{
159168
// user should have unmapped everything by this point, check if that's true and yell if not
160169
// (it indicates a bug in the emulated memory mapping logic)
@@ -291,7 +300,7 @@ WindowsMemoryRegion* MemArena::EnsureSplitRegionForMapping(void* start_address,
291300

292301
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
293302
{
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())
295304
{
296305
WindowsMemoryRegion* const region = EnsureSplitRegionForMapping(base, size);
297306
if (!region)
@@ -393,9 +402,10 @@ bool MemArena::JoinRegionsAfterUnmap(void* start_address, size_t size)
393402

394403
void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
395404
{
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())
397406
{
398-
if (UnmapViewOfFileEx(view, MEM_PRESERVE_PLACEHOLDER))
407+
if (static_cast<PUnmapViewOfFileEx>(m_address_UnmapViewOfFileEx)(view,
408+
MEM_PRESERVE_PLACEHOLDER))
399409
{
400410
if (!JoinRegionsAfterUnmap(view, size))
401411
PanicAlertFmt("Joining memory region failed.");

0 commit comments

Comments
 (0)