Android Shared Memory (ASHMEM)
Used to share memory b/w process
Ashmem uses virtual memory,
Whereas PMEM uses physically contiguous memory. pmem is used to manage large (1-16+MB)
1) Ashmem introduced the concept of pining and unpinning.
[A] Pinned pages of shared memory can not be reclaimable in memory pressure,
[B] Unpinned pages can be reclaimable.
[C] Pinning/Unpinning works by manipulating the ashmem range.
Ashmem range structure holds list of unpinned pages of all shared memory regions.
struct ashmem_range {
struct list_head lru;
struct list_head unpinned;
struct ashmem_area *asma;
size_t pgstart;
size_t pgend;
unsigned int purged;
};
Ashmem Usage:-
ASHMEM is allocated and used as follows:
fd = ashmem_create_region("my_shm_region", size);
if(fd < 0)
return -1;
// Use fd to mmap from offset "0" to size mentioned below,
data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if(data == MAP_FAILED)
goto out;
Through binder this fd is shared, not the "my_shm_region"
PMEM Usage:-
char *pmaddr = pmem_map(fd, size))
PMEM deals with physical memory, but to maintain the ref count in
"struct file", pmem has to link/maintain both phy addr & fd.
Thats why pmem can't maintain refcount.
The process which creates shared memory through pmem should hold
file descriptor untill all the refernces are closed.
Ashmem, maintains a ref-counted object for each FD (shared region), it represents how many
process are currently accessing shared memory region.
If reference count is zero, then no
process accessing that shared memory region.
3. Some of the functions available in android shared memory
(a) ashmem create region(“myfile”,size) this function creates shared memory
file in /dev/ashmem/myfile
(b) ashmem set prot region(fd,prot) It helps to set protection mechanism on
shared memory.
(c) ashmem pin region(fd, offset, length) this helps to process protect the pages
from kernel to reclaim in low memory.
(d) ashmem unpin region(fd, offset, length) this function helps tell to android
shared memory subsystem to reclaim these pages in low memory scenarios. A
process can request for pining on already unpinned pages to android shared
memory sub system.