4

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.

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

3

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:

jww
  • 97,681
  • 90
  • 411
  • 885
ahsanul_k
  • 161
  • 8
3

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

pelya
  • 4,326
  • 2
  • 24
  • 23