From 812cc8b6322fa72982a3084bb3de4bdb564331fe Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 21 Jun 2021 01:17:07 +0200 Subject: MemArena: Split into three separate files for each OS. --- Source/Core/Common/MemArenaAndroid.cpp | 116 +++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Source/Core/Common/MemArenaAndroid.cpp (limited to 'Source/Core/Common/MemArenaAndroid.cpp') diff --git a/Source/Core/Common/MemArenaAndroid.cpp b/Source/Core/Common/MemArenaAndroid.cpp new file mode 100644 index 0000000000..053b14411f --- /dev/null +++ b/Source/Core/Common/MemArenaAndroid.cpp @@ -0,0 +1,116 @@ +// Copyright 2008 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "Common/MemArena.h" + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "Common/CommonFuncs.h" +#include "Common/CommonTypes.h" +#include "Common/Logging/Log.h" +#include "Common/MsgHandler.h" +#include "Common/StringUtil.h" + +namespace Common +{ +#define ASHMEM_DEVICE "/dev/ashmem" + +static int AshmemCreateFileMapping(const char* name, size_t size) +{ + // ASharedMemory path - works on API >= 26 and falls through on API < 26: + + // We can't call ASharedMemory_create the normal way without increasing the + // minimum version requirement to API 26, so we use dlopen/dlsym instead + static void* libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + static auto shared_memory_create = + reinterpret_cast(dlsym(libandroid, "ASharedMemory_create")); + if (shared_memory_create) + return shared_memory_create(name, size); + + // /dev/ashmem path - works on API < 29: + + int fd, ret; + fd = open(ASHMEM_DEVICE, O_RDWR); + if (fd < 0) + return fd; + + // We don't really care if we can't set the name, it is optional + ioctl(fd, ASHMEM_SET_NAME, name); + + ret = ioctl(fd, ASHMEM_SET_SIZE, size); + if (ret < 0) + { + close(fd); + NOTICE_LOG_FMT(MEMMAP, "Ashmem returned error: {:#010x}", ret); + return ret; + } + return fd; +} + +void MemArena::GrabSHMSegment(size_t size) +{ + fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size); + if (fd < 0) + NOTICE_LOG_FMT(MEMMAP, "Ashmem allocation failed"); +} + +void MemArena::ReleaseSHMSegment() +{ + close(fd); +} + +void* MemArena::CreateView(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); + + if (retval == MAP_FAILED) + { + NOTICE_LOG_FMT(MEMMAP, "mmap failed"); + return nullptr; + } + else + { + return retval; + } +} + +void MemArena::ReleaseView(void* view, size_t size) +{ + munmap(view, size); +} + +u8* MemArena::FindMemoryBase() +{ +#if _ARCH_32 + const size_t memory_size = 0x31000000; +#else + const size_t memory_size = 0x400000000; +#endif + + // Android 4.3 changed how mmap works. + // if we map it private and then munmap it, we can't use the base returned. + // This may be due to changes in them to support a full SELinux implementation. + const int flags = MAP_ANON | MAP_SHARED; + void* base = mmap(nullptr, memory_size, PROT_NONE, flags, -1, 0); + if (base == MAP_FAILED) + { + PanicAlertFmt("Failed to map enough memory space: {}", LastStrerrorString()); + return nullptr; + } + munmap(base, memory_size); + return static_cast(base); +} +} // namespace Common -- cgit v1.2.3 From 155412856072c07521536d5a07117c400c0ce131 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Mon, 21 Jun 2021 02:13:00 +0200 Subject: MemArena: Prepare interface for proper memory reservation. --- Source/Core/Common/MemArenaAndroid.cpp | 53 ++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 21 deletions(-) (limited to 'Source/Core/Common/MemArenaAndroid.cpp') diff --git a/Source/Core/Common/MemArenaAndroid.cpp b/Source/Core/Common/MemArenaAndroid.cpp index 053b14411f..aca94db173 100644 --- a/Source/Core/Common/MemArenaAndroid.cpp +++ b/Source/Core/Common/MemArenaAndroid.cpp @@ -59,6 +59,9 @@ static int AshmemCreateFileMapping(const char* name, size_t size) return fd; } +MemArena::MemArena() = default; +MemArena::~MemArena() = default; + void MemArena::GrabSHMSegment(size_t size) { fd = AshmemCreateFileMapping(("dolphin-emu." + std::to_string(getpid())).c_str(), size); @@ -71,35 +74,18 @@ void MemArena::ReleaseSHMSegment() close(fd); } -void* MemArena::CreateView(s64 offset, size_t size, void* base) +void* MemArena::CreateView(s64 offset, size_t size) { - void* retval = mmap(base, size, PROT_READ | PROT_WRITE, - MAP_SHARED | ((base == nullptr) ? 0 : MAP_FIXED), fd, offset); - - if (retval == MAP_FAILED) - { - NOTICE_LOG_FMT(MEMMAP, "mmap failed"); - return nullptr; - } - else - { - return retval; - } + return MapInMemoryRegion(offset, size, nullptr); } void MemArena::ReleaseView(void* view, size_t size) { - munmap(view, size); + UnmapFromMemoryRegion(view, size); } -u8* MemArena::FindMemoryBase() +u8* MemArena::ReserveMemoryRegion(size_t memory_size) { -#if _ARCH_32 - const size_t memory_size = 0x31000000; -#else - const size_t memory_size = 0x400000000; -#endif - // Android 4.3 changed how mmap works. // if we map it private and then munmap it, we can't use the base returned. // This may be due to changes in them to support a full SELinux implementation. @@ -113,4 +99,29 @@ u8* MemArena::FindMemoryBase() munmap(base, memory_size); return static_cast(base); } + +void MemArena::ReleaseMemoryRegion() +{ +} + +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); + + if (retval == MAP_FAILED) + { + NOTICE_LOG_FMT(MEMMAP, "mmap failed"); + return nullptr; + } + else + { + return retval; + } +} + +void MemArena::UnmapFromMemoryRegion(void* view, size_t size) +{ + munmap(view, size); +} } // namespace Common -- cgit v1.2.3