Android 下层提供了一些内存共享工具类,比方 MemoryFile。你应用过吗?晓得它的实现原理吗?

MemoryFile 是 Java 层对 Ashmem 的一个封装,上面来一起学习 MemoryFile,把握它的应用姿态和底层原理。

MemoryFile 应用办法大抵如下:

「过程 A 中申请一块共享内存写入数据,并筹备好文件描述符:」

MemoryFile memoryFile = new MemoryFile(name, size);memoryFile.getOutputStream().write(data);Method method = MemoryFile.class.getDeclaredMethod("getFileDescriptor");FileDescriptor des = (FileDescriptor) method.invoke(memoryFile);ParcelFileDescriptor pfd = ParcelFileDescriptor.dup(des);

「过程 B 中通过 binder 拿到 A 过程中筹备好的文件描述符,而后间接读取数据:」

FileDescriptor descriptor = pfd.getFileDescriptor();FileInputStream fileInputStream = new FileInputStream(descriptor);fileInputStream.read(data);

如上代码所示,应用起来和文件读写一样很简略,但咱们不能只停留在仅会应用的通俗层面。接着来看 MemoryFile 是怎么从 Java API 调用到 Ashmem 驱动函数的,先来看 MemoryFile 的构造函数:

public MemoryFile(String name, int length) throws IOException {    try { mSharedMemory = SharedMemory.create(name, length);        mMapping = mSharedMemory.mapReadWrite();    } catch (ErrnoException ex) {        ex.rethrowAsIOException();    }}

能够看到结构 MemoryFile 时通过 SharedMemory create 办法申请了一块匿名共享内存,SharedMemory create 办法中调用了 nCreate native 办法:

private static native FileDescriptor nCreate(String name, int size) throws ErrnoException;

对应的 native 实现在 android_os_SharedMemory.cpp 中,实现如下:

static jobject SharedMemory_create(JNIEnv* env, jobject, jstring jname, jint size) {    const char* name = jname ? envGetStringUTFChars(jname, nullptr) : nullptr;   int fd = ashmem_create_region(name, size); //创立匿名共享内存    ...    return jniCreateFileDescriptor(env, fd);}

接着来看 ashmem_create_region 办法,它的对应实现在 ashmem-dev.cpp 中,如下:

int ashmem_create_region(const char *name, size_t size){    int ret, save_errno;    //创立匿名共享内存int fd = __ashmem_open();     if (fd < 0) { return fd;}    if (name) {        char buf[ASHMEM_NAME_LEN] = {0};        strlcpy(buf, name, sizeof(buf));        // 设置 Ashmem 名字ret = TEMP_FAILURE_RETRY(ioctl(fd, ASHMEM_SET_NAME, buf));         if (ret < 0) {goto error; }    }}

如上设置 Ashmem 名字执行了 ioctl 零碎调用, 它会进一步调用到 「ashmem_ioctl」 驱动函数。接着来看 __ashmem_open 办法:

static int __ashmem_open(){int fd;    pthread_mutex_lock(&__ashmem_lock);    fd = __ashmem_open_locked(); //创立匿名共享内存 pthread_mutex_unlock(&__ashmem_lock);    return fd;}

进一步调用到 __ashmem_open_locked 办法,如下:

#define ASHMEM_DEVICE "/dev/ashmem" //Ashmem 设施驱动static int __ashmem_open_locked(){int ret;    struct stat st;    int fd = TEMP_FAILURE_RETRY(                open(ASHMEM_DEVICE, O_RDWR | O_CLOEXEC)); //创立匿名共享内存    ...    return fd;}

在 __ashmem_open_locked 办法中调用了 open() 零碎调用,和 ioctl 一样,对应到 Ashmem 设施驱动函数了,即 「ashmem_open」 驱动函数。

通过下面的剖析晓得 Ashmem 驱动的 ashmem_open 函数是由 SharedMemory 的 create 办法触发一步一步调用到的,期间还调用了 ashmem_ioctl 函数。

而 ashmem_mmap 驱动函数是通过 SharedMemory 的 mapReadWrite 办法触发,上面来剖析这个过程:

//android.os.SharedMemory.javapublic @NonNull ByteBuffer mapReadWrite() throws ErrnoException {    return map(OsConstants.PROT_READ | OsConstants.PROT_WRITE, 0, mSize);}public @NonNull ByteBuffer map(int prot, int offset, int length) throws ErrnoException {    ...    long address = Os.mmap(0, length, prot, OsConstants.MAP_SHARED, mFileDescriptor, offset);    ...    return new DirectByteBuffer(length, address, mFileDescriptor, unmapper, readOnly);}

比拟要害的是 mFileDescriptor,它是执行 SharedMemory create 办法申请匿名共享内存后,返回的文件描述符。持续来跟踪 mmap 调用:

//android.system.Os.javapublic static long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException {    return Libcore.os.mmap(address, byteCount, prot, flags, fd, offset);}//libcore.io.Libcore.javapublic final class Libcore {    private Libcore() { }    public static Os rawOs = new Linux();    public static Os os = new BlockGuardOs(rawOs);}//libcore.io.Linux.javapublic native long mmap(long address, long byteCount, int prot, int flags, FileDescriptor fd, long offset) throws ErrnoException;

Libcore 中应用 BlockGuardOs 对 Linux 进行了一层包装,但理论还是通过 Linux 来执行的,最初调用到 Linux 中的 native mmap 办法,native 中对应的实现是 mmap.cpp:

void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {    return mmap64(addr, size, prot, flags, fd, static_cast<off64_t>(offset));}

到此为止,由 SharedMemory 的 mapReadWrite 办法调用到 native mmap 函数,传递的要害参数是文件描述符,后续它将这样调用到 ashmem_mmap:

  1. 通过 fd 能够找到所属设施,也就是 Ashmem 设施
  2. 调用 Ashmem 设施的 ashmem_mmap 驱动函数

要害代码如下:

if(file){    ...    error = file->f_op->mmap(file,vma);    ...}

file 代表文件或设施驱动,这里指的就是 Ashmem 设施,f_op 就是 Ashmem 设施驱动函数集,也就是注册的 Ashmem 设备描述,至此便是 ashmem_mmap 驱动函数的调用过程。

关注我,每天分享常识干货~