From ab98db445d67491ac63196686b73fb279f71984f Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Thu, 11 May 2023 02:41:40 +0200 Subject: [PATCH] Common/MemArenaAndroid: Keep fastmem region mapped. --- Source/Core/Common/MemArena.h | 10 +++----- Source/Core/Common/MemArenaAndroid.cpp | 35 ++++++++++++++++++-------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Source/Core/Common/MemArena.h b/Source/Core/Common/MemArena.h index e365b78e0b..01b1b5c06b 100644 --- a/Source/Core/Common/MemArena.h +++ b/Source/Core/Common/MemArena.h @@ -113,13 +113,9 @@ private: void* m_address_VirtualAlloc2 = nullptr; void* m_address_MapViewOfFile3 = nullptr; #else -#ifdef ANDROID - int fd; -#else - int m_shm_fd; - void* m_reserved_region; - std::size_t m_reserved_region_size; -#endif + int m_shm_fd = 0; + void* m_reserved_region = nullptr; + std::size_t m_reserved_region_size = 0; #endif }; diff --git a/Source/Core/Common/MemArenaAndroid.cpp b/Source/Core/Common/MemArenaAndroid.cpp index aca94db173..721925d213 100644 --- a/Source/Core/Common/MemArenaAndroid.cpp +++ b/Source/Core/Common/MemArenaAndroid.cpp @@ -64,24 +64,33 @@ MemArena::~MemArena() = default; void MemArena::GrabSHMSegment(size_t size) { - fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size); - if (fd < 0) + m_shm_fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size); + if (m_shm_fd < 0) NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed"); } void MemArena::ReleaseSHMSegment() { - close(fd); + close(m_shm_fd); } void* MemArena::CreateView(s64 offset, size_t size) { - return MapInMemoryRegion(offset, size, nullptr); + void* retval = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, m_shm_fd, offset); + if (retval == MAP_FAILED) + { + NOTICE_LOG_FMT(MEMMAP, "mmap failed"); + return nullptr; + } + else + { + return retval; + } } void MemArena::ReleaseView(void* view, size_t size) { - UnmapFromMemoryRegion(view, size); + munmap(view, size); } u8* MemArena::ReserveMemoryRegion(size_t memory_size) @@ -96,19 +105,23 @@ u8* MemArena::ReserveMemoryRegion(size_t memory_size) PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString()); return nullptr; } - munmap(base, memory_size); + m_reserved_region = base; + m_reserved_region_size = memory_size; return static_cast(base); } void MemArena::ReleaseMemoryRegion() { + if (m_reserved_region) + { + munmap(m_reserved_region, m_reserved_region_size); + m_reserved_region = nullptr; + } } void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base) { - void* retval = mmap(base, size, PROT_READ | PROT_WRITE, - MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset); - + void* retval = mmap(base, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, m_shm_fd, offset); if (retval == MAP_FAILED) { NOTICE_LOG_FMT(MEMMAP, "mmap failed"); @@ -122,6 +135,8 @@ void* MemArena::MapInMemoryRegion(s64 offset, size_t size, void* base) void MemArena::UnmapFromMemoryRegion(void* view, size_t size) { - munmap(view, size); + void* retval = mmap(view, size, PROT_NONE, MAP_SHARED | MAP_ANONYMOUS | MAP_FIXED, -1, 0); + if (retval == MAP_FAILED) + NOTICE_LOG_FMT(MEMMAP, "mmap failed"); } } // namespace Common