基于 ASHMEM 的 SHM 替换 [英] SHM replacement based on ASHMEM
问题描述
我正在开发从 *nix 到 Android 的库端口,该库使用共享内存或 shm
.Android 没有 System V shm
.相反,它使用 ashmem
.
I'm working on a library port from *nix to Android, and the library uses shared memory or shm
. Android does not have System V shm
. Instead it uses ashmem
.
是否有人知道将 shm
调用映射到 ashmem
的 shim 库?Google 的帮助不大.
Is anyone aware of a shim library to map shm
calls into ashmem
? Google has not been very helpful.
推荐答案
这是我在处理类似移植问题时的工作方式:
This is how it worked for me while working with a similar problem of porting:
我没有使用 shmfd = open(SHM_PATH, O_RDWR) 来创建和获取文件描述符,而是将其替换为
Instead of using shmfd = open(SHM_PATH, O_RDWR) for creating and getting file descriptor I replaced it with
int fd = ashmem_create_region("SharedRegionName", size);
并使用文件描述符获取基地址:
and used the file descriptor to get base address:
int base_address = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
您可以使用返回描述符的本机函数将 base_address 从本机代码传递给您的 java 代码.
You can pass the base_address to your java code from the native code using a native function that returns the descriptor.
Android 有一个名为 MemoryFile 的 Ashmem 包装类.你也可以看看.
Android has a wrapper class for Ashmem named MemoryFile. You can also have a look in that.
以下链接帮助我创建了自己的包装器:
The following links helped me to create my own wrapper:
- http://notjustburritos.tumblr.com/post/21442138796/an-introduction-to-android-shared-memory
- http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/os/MemoryFile.java
- https://github.com/舒适位/aosp-frameworks-base/blob/master/core/jni/android_os_MemoryFile.cpp
这篇关于基于 ASHMEM 的 SHM 替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!