From ff5bcba966372d5071c080234866e5fab23ae647 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 11 May 2023 01:35:23 +0200 Subject: [PATCH 1/2] Common/MemArenaWin: Handle file mappings >= 4GB. --- Source/Core/Common/MemArenaWin.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/Source/Core/Common/MemArenaWin.cpp b/Source/Core/Common/MemArenaWin.cpp index 1e2de701c5..b055477a85 100644 --- a/Source/Core/Common/MemArenaWin.cpp +++ b/Source/Core/Common/MemArenaWin.cpp @@ -97,11 +97,22 @@ MemArena::~MemArena() ReleaseSHMSegment(); } +static DWORD GetHighDWORD(u64 value) +{ + return static_cast(value >> 32); +} + +static DWORD GetLowDWORD(u64 value) +{ + return static_cast(value); +} + void MemArena::GrabSHMSegment(size_t size) { const std::string name = "dolphin-emu." + std::to_string(GetCurrentProcessId()); - m_memory_handle = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, - static_cast(size), UTF8ToTStr(name).c_str()); + m_memory_handle = + CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, GetHighDWORD(size), + GetLowDWORD(size), UTF8ToTStr(name).c_str()); } void MemArena::ReleaseSHMSegment() @@ -114,8 +125,9 @@ void MemArena::ReleaseSHMSegment() void* MemArena::CreateView(s64 offset, size_t size) { - return MapViewOfFileEx(m_memory_handle, FILE_MAP_ALL_ACCESS, 0, (DWORD)((u64)offset), size, - nullptr); + const u64 off = static_cast(offset); + return MapViewOfFileEx(m_memory_handle, FILE_MAP_ALL_ACCESS, GetHighDWORD(off), GetLowDWORD(off), + size, nullptr); } void MemArena::ReleaseView(void* view, size_t size) From 8342164dbdf6d862556de017f67ff28ecde0f0ec Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 11 May 2023 01:51:43 +0200 Subject: [PATCH 2/2] Common/MemArena: Pass shared memory base file name to GrabSHMSegment(). --- Source/Core/Common/MemArena.h | 5 ++++- Source/Core/Common/MemArenaAndroid.cpp | 7 +++++-- Source/Core/Common/MemArenaUnix.cpp | 6 ++++-- Source/Core/Common/MemArenaWin.cpp | 6 ++++-- Source/Core/Core/HW/Memmap.cpp | 2 +- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Source/Core/Common/MemArena.h b/Source/Core/Common/MemArena.h index 01b1b5c06b..adb944acf9 100644 --- a/Source/Core/Common/MemArena.h +++ b/Source/Core/Common/MemArena.h @@ -4,6 +4,7 @@ #pragma once #include +#include #include #include "Common/CommonTypes.h" @@ -34,8 +35,10 @@ public: /// CreateView() and ReleaseView(). Used to make a mappable region for emulated memory. /// /// @param size The amount of bytes that should be allocated in this region. + /// @param base_name A base name for the shared memory region, if applicable for this platform. + /// Will be extended with the process ID. /// - void GrabSHMSegment(size_t size); + void GrabSHMSegment(size_t size, std::string_view base_name); /// /// Release the memory segment previously allocated with GrabSHMSegment(). diff --git a/Source/Core/Common/MemArenaAndroid.cpp b/Source/Core/Common/MemArenaAndroid.cpp index 721925d213..01b210100a 100644 --- a/Source/Core/Common/MemArenaAndroid.cpp +++ b/Source/Core/Common/MemArenaAndroid.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include #include #include @@ -62,9 +64,10 @@ static int AshmemCreateFileMapping(const char* name, size_t size) MemArena::MemArena() = default; MemArena::~MemArena() = default; -void MemArena::GrabSHMSegment(size_t size) +void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) { - m_shm_fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size); + const std::string name = fmt::format("{}.{}", base_name, getpid()); + m_shm_fd = AshmemCreateFileMapping(name.c_str(), size); if (m_shm_fd < 0) NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed"); } diff --git a/Source/Core/Common/MemArenaUnix.cpp b/Source/Core/Common/MemArenaUnix.cpp index 549ce09cfd..1532699276 100644 --- a/Source/Core/Common/MemArenaUnix.cpp +++ b/Source/Core/Common/MemArenaUnix.cpp @@ -10,6 +10,8 @@ #include #include +#include + #include #include #include @@ -25,9 +27,9 @@ namespace Common MemArena::MemArena() = default; MemArena::~MemArena() = default; -void MemArena::GrabSHMSegment(size_t size) +void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) { - const std::string file_name = "/dolphin-emu." + std::to_string(getpid()); + const std::string file_name = fmt::format("/{}.{}", base_name, getpid()); m_shm_fd = shm_open(file_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600); if (m_shm_fd == -1) { diff --git a/Source/Core/Common/MemArenaWin.cpp b/Source/Core/Common/MemArenaWin.cpp index b055477a85..9d4a88d54a 100644 --- a/Source/Core/Common/MemArenaWin.cpp +++ b/Source/Core/Common/MemArenaWin.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include #include "Common/Assert.h" @@ -107,9 +109,9 @@ static DWORD GetLowDWORD(u64 value) return static_cast(value); } -void MemArena::GrabSHMSegment(size_t size) +void MemArena::GrabSHMSegment(size_t size, std::string_view base_name) { - const std::string name = "dolphin-emu." + std::to_string(GetCurrentProcessId()); + const std::string name = fmt::format("{}.{}", base_name, GetCurrentProcessId()); m_memory_handle = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, GetHighDWORD(size), GetLowDWORD(size), UTF8ToTStr(name).c_str()); diff --git a/Source/Core/Core/HW/Memmap.cpp b/Source/Core/Core/HW/Memmap.cpp index 5a63405d17..d66f77febf 100644 --- a/Source/Core/Core/HW/Memmap.cpp +++ b/Source/Core/Core/HW/Memmap.cpp @@ -121,7 +121,7 @@ void MemoryManager::Init() region.active = true; mem_size += region.size; } - m_arena.GrabSHMSegment(mem_size); + m_arena.GrabSHMSegment(mem_size, "dolphin-emu"); m_physical_page_mappings.fill(nullptr);