diff --git a/src/ARMJIT_Memory.cpp b/src/ARMJIT_Memory.cpp index cc0f1493..f9f82aa6 100644 --- a/src/ARMJIT_Memory.cpp +++ b/src/ARMJIT_Memory.cpp @@ -10,6 +10,12 @@ #include #endif +#if defined(__ANDROID__) +#include +#include +#include +#endif + #include "ARMJIT_Memory.h" #include "ARMJIT_Internal.h" @@ -58,6 +64,10 @@ struct FaultDescription bool FaultHandler(FaultDescription& faultDesc); } +#if defined(__ANDROID__) +#define ASHMEM_DEVICE "/dev/ashmem" +#endif + #if defined(__SWITCH__) // with LTO the symbols seem to be not properly overriden // if they're somewhere else @@ -712,14 +722,31 @@ void Init() FastMem7Start = MemoryBase + AddrSpaceSize; MemoryBase = MemoryBase + AddrSpaceSize*2; - #ifdef __APPLE__ - char* fastmemPidName = new char[snprintf(NULL, 0, "melondsfastmem%d", getpid()) + 1]; - sprintf(fastmemPidName, "melondsfastmem%d", getpid()); - MemoryFile = shm_open(fastmemPidName, O_RDWR|O_CREAT, 0600); - delete[] fastmemPidName; - #else - MemoryFile = memfd_create("melondsfastmem", 0); - #endif +#if defined(__ANDROID__) + static void* libandroid = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL); + using type_ASharedMemory_create = int(*)(const char* name, size_t size); + static void* symbol = dlsym(libandroid, "ASharedMemory_create"); + static auto shared_memory_create = reinterpret_cast(symbol); + + if (shared_memory_create) + { + MemoryFile = shared_memory_create("melondsfastmem", MemoryTotalSize); + } + else + { + int fd = open(ASHMEM_DEVICE, O_RDWR); + ioctl(fd, ASHMEM_SET_NAME, "melondsfastmem"); + ioctl(fd, ASHMEM_SET_SIZE, MemoryTotalSize); + MemoryFile = fd; + } +#elif defined(__APPLE__) + char* fastmemPidName = new char[snprintf(NULL, 0, "melondsfastmem%d", getpid()) + 1]; + sprintf(fastmemPidName, "melondsfastmem%d", getpid()); + MemoryFile = shm_open(fastmemPidName, O_RDWR|O_CREAT, 0600); + delete[] fastmemPidName; +#else + MemoryFile = memfd_create("melondsfastmem", 0); +#endif ftruncate(MemoryFile, MemoryTotalSize); struct sigaction sa;