SHM replacement based on ASHMEM

情到浓时终转凉″ 提交于 2019-12-22 17:59:12

问题


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.

Is anyone aware of a shim library to map shm calls into ashmem? Google has not been very helpful.


回答1:


This is how it worked for me while working with a similar problem of porting:

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);

You can pass the base_address to your java code from the native code using a native function that returns the descriptor.

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/cozybit/aosp-frameworks-base/blob/master/core/jni/android_os_MemoryFile.cpp



回答2:


Here's a library, which you can LD_PRELOAD to simulate Linux shared memory using Android ashmem (shmget/shmat/shmdt/shmctl calls), you can also link to this library directly.

https://github.com/pelya/android-shmem



来源:https://stackoverflow.com/questions/17510157/shm-replacement-based-on-ashmem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!