I want to have a shared memory block (an ashmem region) that\'s mapped and accessed from native code. I also want this block to be used by several applications.
This is how it worked for me while working with a similar problem:
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);
// write data You can pass the base_address to your java code from the native code using a native function that returns the descriptor.
Then I create a Service with aidl interface and used this interface to bind this service from another process. From the Service I have used a ParcelFileDescriptor object to return to another process. You can create ParcelFileDescriptor by:
ParcelFileDescriptor desc = ParcelFileDescriptor.fromFd(fd);