summaryrefslogtreecommitdiff
path: root/Source/Core/Common/MemArenaWin.cpp
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2022-02-14 22:13:13 +0100
committerGitHub <noreply@github.com>2022-02-14 22:13:13 +0100
commiteba19988cfcd324ff8574252ff674baac54a57c8 (patch)
tree272296ece196560b8da0e247dcdab6e07004e2ef /Source/Core/Common/MemArenaWin.cpp
parent7c91acb000c012ad35c37770cdfbb659afef7603 (diff)
parent4a9553bf6df683b9f2995e5d7c485bf3034502e1 (diff)
Merge pull request #10452 from AdmiralCurtiss/win7-unmap-view-of-file-ex
MemArena: Restore Windows 7 support.
Diffstat (limited to 'Source/Core/Common/MemArenaWin.cpp')
-rw-r--r--Source/Core/Common/MemArenaWin.cpp38
1 files changed, 24 insertions, 14 deletions
diff --git a/Source/Core/Common/MemArenaWin.cpp b/Source/Core/Common/MemArenaWin.cpp
index 6434627478..1e2de701c5 100644
--- a/Source/Core/Common/MemArenaWin.cpp
+++ b/Source/Core/Common/MemArenaWin.cpp
@@ -29,6 +29,8 @@ using PMapViewOfFile3 = PVOID(WINAPI*)(HANDLE FileMapping, HANDLE Process, PVOID
MEM_EXTENDED_PARAMETER* ExtendedParameters,
ULONG ParameterCount);
+using PUnmapViewOfFileEx = BOOL(WINAPI*)(PVOID BaseAddress, ULONG UnmapFlags);
+
using PIsApiSetImplemented = BOOL(APIENTRY*)(PCSTR Contract);
namespace Common
@@ -61,22 +63,31 @@ MemArena::MemArena()
if (!static_cast<PIsApiSetImplemented>(ptr_IsApiSetImplemented)("api-ms-win-core-memory-l1-1-6"))
return;
- const HMODULE handle = LoadLibraryW(L"api-ms-win-core-memory-l1-1-6.dll");
- if (!handle)
+ m_api_ms_win_core_memory_l1_1_6_handle.Open("api-ms-win-core-memory-l1-1-6.dll");
+ m_kernel32_handle.Open("Kernel32.dll");
+ if (!m_api_ms_win_core_memory_l1_1_6_handle.IsOpen() || !m_kernel32_handle.IsOpen())
+ {
+ m_api_ms_win_core_memory_l1_1_6_handle.Close();
+ m_kernel32_handle.Close();
return;
+ }
- void* const address_VirtualAlloc2 = GetProcAddress(handle, "VirtualAlloc2FromApp");
- void* const address_MapViewOfFile3 = GetProcAddress(handle, "MapViewOfFile3FromApp");
- if (address_VirtualAlloc2 && address_MapViewOfFile3)
+ void* const address_VirtualAlloc2 =
+ m_api_ms_win_core_memory_l1_1_6_handle.GetSymbolAddress("VirtualAlloc2FromApp");
+ void* const address_MapViewOfFile3 =
+ m_api_ms_win_core_memory_l1_1_6_handle.GetSymbolAddress("MapViewOfFile3FromApp");
+ void* const address_UnmapViewOfFileEx = m_kernel32_handle.GetSymbolAddress("UnmapViewOfFileEx");
+ if (address_VirtualAlloc2 && address_MapViewOfFile3 && address_UnmapViewOfFileEx)
{
- m_api_ms_win_core_memory_l1_1_6_handle = handle;
m_address_VirtualAlloc2 = address_VirtualAlloc2;
m_address_MapViewOfFile3 = address_MapViewOfFile3;
+ m_address_UnmapViewOfFileEx = address_UnmapViewOfFileEx;
}
else
{
// at least one function is not available, use legacy logic
- FreeLibrary(handle);
+ m_api_ms_win_core_memory_l1_1_6_handle.Close();
+ m_kernel32_handle.Close();
}
}
@@ -84,8 +95,6 @@ MemArena::~MemArena()
{
ReleaseMemoryRegion();
ReleaseSHMSegment();
- if (m_api_ms_win_core_memory_l1_1_6_handle)
- FreeLibrary(static_cast<HMODULE>(m_api_ms_win_core_memory_l1_1_6_handle));
}
void MemArena::GrabSHMSegment(size_t size)
@@ -123,7 +132,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
}
u8* base;
- if (m_api_ms_win_core_memory_l1_1_6_handle)
+ if (m_api_ms_win_core_memory_l1_1_6_handle.IsOpen())
{
base = static_cast<u8*>(static_cast<PVirtualAlloc2>(m_address_VirtualAlloc2)(
nullptr, nullptr, memory_size, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS,
@@ -154,7 +163,7 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size)
void MemArena::ReleaseMemoryRegion()
{
- if (m_api_ms_win_core_memory_l1_1_6_handle && m_reserved_region)
+ if (m_api_ms_win_core_memory_l1_1_6_handle.IsOpen() && m_reserved_region)
{
// user should have unmapped everything by this point, check if that's true and yell if not
// (it indicates a bug in the emulated memory mapping logic)
@@ -291,7 +300,7 @@ WindowsMemoryRegion* MemArena::EnsureSplitRegionForMapping(void* start_address,
void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base)
{
- if (m_api_ms_win_core_memory_l1_1_6_handle)
+ if (m_api_ms_win_core_memory_l1_1_6_handle.IsOpen())
{
WindowsMemoryRegion* const region = EnsureSplitRegionForMapping(base, size);
if (!region)
@@ -393,9 +402,10 @@ bool MemArena::JoinRegionsAfterUnmap(void* start_address, size_t size)
void MemArena::UnmapFromMemoryRegion(void* view, size_t size)
{
- if (m_api_ms_win_core_memory_l1_1_6_handle)
+ if (m_api_ms_win_core_memory_l1_1_6_handle.IsOpen())
{
- if (UnmapViewOfFileEx(view, MEM_PRESERVE_PLACEHOLDER))
+ if (static_cast<PUnmapViewOfFileEx>(m_address_UnmapViewOfFileEx)(view,
+ MEM_PRESERVE_PLACEHOLDER))
{
if (!JoinRegionsAfterUnmap(view, size))
PanicAlertFmt("Joining memory region failed.");