Merge pull request #8955 from JosJuice/android-no-ashmem

Android: Don't access /dev/ashmem on newer Android versions
This commit is contained in:
LC 2020-07-16 14:23:51 -04:00 committed by GitHub
commit c59648337a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,6 +23,7 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <unistd.h> #include <unistd.h>
#ifdef ANDROID #ifdef ANDROID
#include <dlfcn.h>
#include <linux/ashmem.h> #include <linux/ashmem.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#endif #endif
@ -35,6 +36,18 @@ namespace Common
static int AshmemCreateFileMapping(const char* name, size_t size) 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; int fd, ret;
fd = open(ASHMEM_DEVICE, O_RDWR); fd = open(ASHMEM_DEVICE, O_RDWR);
if (fd < 0) if (fd < 0)