summaryrefslogtreecommitdiff
path: root/Source/Core/Common/MemArenaAndroid.cpp
diff options
context:
space:
mode:
authorAdmiral H. Curtiss <pikachu025@gmail.com>2021-06-21 01:17:07 +0200
committerAdmiral H. Curtiss <pikachu025@gmail.com>2021-12-25 20:20:36 +0100
commit812cc8b6322fa72982a3084bb3de4bdb564331fe (patch)
tree72ace490d57b9e0d2947b052ee2b65ad0e333ae8 /Source/Core/Common/MemArenaAndroid.cpp
parenta29d7625ddb67f29f6dc46858b2b1488b72fd42b (diff)
MemArena: Split into three separate files for each OS.
Diffstat (limited to 'Source/Core/Common/MemArenaAndroid.cpp')
-rw-r--r--Source/Core/Common/MemArenaAndroid.cpp116
1 files changed, 116 insertions, 0 deletions
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 <cerrno>
+#include <cstddef>
+#include <cstdlib>
+#include <cstring>
+#include <set>
+#include <string>
+
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <linux/ashmem.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#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<int (*)(const char*, size_t)>(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<u8*>(base);
+}
+} // namespace Common