In this lab, you’ll create a service to allocate and manage shared memory (ashmem)
- Create an empty android application with C++ support – the service application
- Add an android library module
- Create a new aidl file – IShmService
- Declare an interface with the following functions:
ParcelFileDescriptor createFD(in String name, int size); ParcelFileDescriptor getFD(in String name)
Service Application
Implement the above interface using native code. To create an ashmem object use the following IOCTLs (taken from the kernel code)
you can include the file from the kernel (~/kernel3.4/goldfish)
jint fd = open("/dev/ashmem",O_RDWR); ioctl(fd,ASHMEM_SET_NAME,name); ioctl(fd,ASHMEM_SET_SIZE,size); map = (int *)mmap(0,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
In the service implementation java code create and return ParcelFileDescriptor (using the file descriptor number returning from the native library)
Client ApplicationĀ
Add a client application:
- Bind to the service
- Use the service to create and memory region
- Use native code to mmap the region and test your work