Bug 1966311 - Use weak android symbol instead of dlopen/dlsym for android/Ashmem.cpp r=glandium

Differential Revision: https://phabricator.services.mozilla.com/D249226
This commit is contained in:
serge-sans-paille
2025-05-16 07:04:52 +00:00
committed by sguelton@mozilla.com
parent 801ade11ce
commit f5a2f8839a

View File

@@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <cstring> #include <cstring>
#include <dlfcn.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/ashmem.h> #include <linux/ashmem.h>
#include <stdio.h> #include <stdio.h>
@@ -12,22 +11,16 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <unistd.h> #include <unistd.h>
#include <android/sharedmem.h>
#include "Ashmem.h" #include "Ashmem.h"
namespace mozilla { namespace mozilla {
namespace android { namespace android {
static void* libhandle() {
static void* handle = dlopen("libandroid.so", RTLD_LAZY | RTLD_LOCAL);
return handle;
}
int ashmem_create(const char* name, size_t size) { int ashmem_create(const char* name, size_t size) {
static auto fCreate = if (__builtin_available(android 26, *)) {
(int (*)(const char*, size_t))dlsym(libhandle(), "ASharedMemory_create"); return ASharedMemory_create(name, size);
if (fCreate) {
return fCreate(name, size);
} }
int fd = open("/" ASHMEM_NAME_DEF, O_RDWR); int fd = open("/" ASHMEM_NAME_DEF, O_RDWR);
@@ -50,20 +43,16 @@ int ashmem_create(const char* name, size_t size) {
} }
size_t ashmem_getSize(int fd) { size_t ashmem_getSize(int fd) {
static auto fGetSize = if (__builtin_available(android 26, *)) {
(size_t(*)(int))dlsym(libhandle(), "ASharedMemory_getSize"); return ASharedMemory_getSize(fd);
if (fGetSize) {
return fGetSize(fd);
} }
return (size_t)ioctl(fd, ASHMEM_GET_SIZE, nullptr); return (size_t)ioctl(fd, ASHMEM_GET_SIZE, nullptr);
} }
int ashmem_setProt(int fd, int prot) { int ashmem_setProt(int fd, int prot) {
static auto fSetProt = if (__builtin_available(android 26, *)) {
(int (*)(int, int))dlsym(libhandle(), "ASharedMemory_setProt"); return ASharedMemory_setProt(fd, prot);
if (fSetProt) {
return fSetProt(fd, prot);
} }
return ioctl(fd, ASHMEM_SET_PROT_MASK, prot); return ioctl(fd, ASHMEM_SET_PROT_MASK, prot);