Basic principle of

The android system provides two C++ classes MemoryHeapBase and MemoryBase in the application framework layer to create and manage anonymous shared memory. If a process needs to share an entire block of anonymous shared memory with another process, it can create this block of anonymous shared memory by using the MemoryHeapBase class. If a process has created a block of anonymous shared memory and only wants to share part of it with other processes, the MemoryBase class can be used to create this block of anonymous shared memory.

Imemery. h: Defines an interface for memory-related classes, representing IMemoryHeap and BnMemoryHeap for heap memory, and IMemory and BnMemory for general memory. MemoryHeapBase. H: Defines the class MemoryHeapBase, inherits and implements BnMemoryHeap MemoryBase.

The Android system provides the Java class MemoryFile in the application framework layer to create and manage anonymous shared memory. Anonymous shared memory created using the Java class MemoryFile can be shared between different Android applications.

Java Code parsing

The anonymous shared memory Java class MemoryFile is implemented in source\ Frameworks \ Base \core\ Java \ Android \ OS \ memoryfile.java.

// Constructor of anonymous shared memory. Parameter 1 indicates the name to create anonymous shared memory, and parameter 2 indicates the size to create anonymous shared memory
    public MemoryFile(String name, int length) throws IOException {
        mLength = length;
        if (length >= 0) {
            // Open anonymous shared memory by calling jNI's interface
            mFD = native_open(name, length);
        } else {
            throw new IOException("Invalid length: " + length);
        }

        if (length > 0) {
            // Map
            mAddress = native_mmap(mFD, length, PROT_READ | PROT_WRITE);
        } else {
            mAddress = 0; }}Copy the code

C++ key function parsing

// Implementation of the MemoryHeapBase constructor
MemoryHeapBase::MemoryHeapBase(const char* device, size_t size, uint32_t flags)
    : mFD(- 1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
      mDevice(0), mNeedUnmap(false), mOffset(0)
{
    int open_flags = O_RDWR;
    if (flags & NO_CACHING)
        open_flags |= O_SYNC;
	// Open the anonymous shared memory device file by calling open
    int fd = open(device, open_flags);
    ALOGE_IF(fd<0."error opening %s: %s", device, strerror(errno));
    if (fd >= 0) {
		// Specify anonymous shared memory size by page alignment
        const size_t pagesize = getpagesize(a); size = ((size + pagesize- 1) & ~(pagesize- 1));
		// Anonymous shared memory is mapped to the current process address space
        if (mapfd(fd, size) == NO_ERROR) { mDevice = device; }}}Copy the code
// The MemoryHeapBase constructor
MemoryHeapBase::MemoryHeapBase(size_t size, uint32_t flags, char const * name)
    : mFD(-1), mSize(0), mBase(MAP_FAILED), mFlags(flags),
      mDevice(0), mNeedUnmap(false), mOffset(0)
{
	// Get the memory size of the page in the system
    const size_t pagesize = getpagesize();
	// Memory page alignment
    size = ((size + pagesize-1) & ~(pagesize-1));
	Create a block of anonymous shared memory
    int fd = ashmem_create_region(name == NULL ? "MemoryHeapBase" : name, size);
    ALOGE_IF(fd<0."error creating ashmem region: %s", strerror(errno));
    if (fd >= 0) {
		// Create anonymous shared memory mapped to the current process address space
        if (mapfd(fd, size) == NO_ERROR) {
            if (flags & READ_ONLY) {// If the address mapping succeeds, modify the access attribute of anonymous shared memoryashmem_set_prot_region(fd, PROT_READ); }}}}Copy the code