Memoryheapbase memory allocation, ashmem_create_region MMAP application shared memory

Source: Internet
Author: User
Tags tmp file

From: http://blog.csdn.net/zjc0888/article/details/6400624

The header files of several memory-related classes are as follows:
Imemory. h: Defines the interface of memory-related classes, indicating the heap memory class imemoryheap and bnmemoryheap, indicating the memory class imemory and bnmemory.
Memoryheapbase. h: Defines the class memoryheapbase and inherits and implements bnmemoryheap.
Memorybase. h: Defines the class memorybase and inherits and implements bnmemory.
In general, memoryheapbase class is usually used to allocate a heap memory (similar to malloc), while memorybase indicates that a part of the heap memory is allocated from one block.

Class audiotrackjnistorage {

Public:

Sp <memoryheapbase> mmemheap; // these two memories are very important.

Sp <memorybase> mmembase;

Audiotrack_callback_cookie mcallbackdata;

Int mstreamtype;

 

Bool allocsharedmem (INT sizeinbytes ){

Mmemheap = new memoryheapbase (sizeinbytes, 0, "audiotrack heap base ");

Mmembase = new memorybase (mmemheap, 0, sizeinbytes );

// Pay attention to usage. first obtain a heapbase and then pass heapbase to memorybase.

Return true;

}

};

2 memoryheapbase

Memroyheapbase is also a set of classes for memory operations based on the Binder Mechanism developed by Android. Since it is a binder mechanism, there must be a server (bnxxx) and a proxy bpxxx. Let's take a look at the definition of memoryheapbase:

Class memoryheapbase: Public Virtual bnmemoryheap

{

Sure enough, it is derived from bnmemoryheap, that is, bn. In this way, it is hooked to the binder.

// The functions called by the BP end are eventually adjusted to bn.

For details about the Binder Mechanism, refer:

Http://blog.csdn.net/Innost/archive/2011/01/08/6124685.aspx

There are several constructors. Let's take a look at what we use:

Memoryheapbase: memoryheapbase (size_t size, uint32_t flags, char const * name)

: MFD (-1), msize (0), mbase (map_failed), mflags (flags ),

Mdevice (0), mneedunmap (false)

{

Const size_t pagesize = getpagesize ();

Size = (size + pagesize-1 )&~ (Pagesize-1 ));

// Create shared memory. ashmem_create_region is provided by the system.

// The/dev/ashmem device is opened on the device, while the tmp file is opened on the host.

Int FD = ashmem_create_region (name = NULL? "Memoryheapbase": name, size );

Mapfd (FD, size); // obtain a piece of memory by using the MMAP method of the FD.

// Do not understand. Check man MMAP.

After mapfd is complete, the mbase variable points to the starting position of the memory. msize indicates the size of the allocated memory, and MFD indicates

File descriptor returned by ashmem_create_region

 

}

Memoryheapbase provides several functions to obtain the size and location of the shared memory.

Getbaseid () ---> MFD is returned. If the value is negative, it indicates that the shared memory has failed to be created.

Getbase ()-> return mbase, memory location

Getsize ()-> return msize, memory size

With memoryheapbase, another memorybase is created, which is a class linked to the Binder Mechanism.

Alas, this is probably a convenient class on memoryheapbase, right? Because I saw the offset

It is estimated that this class is a convenient class that can return the write position (that is, offset) in the current buffer.

In this way, you do not need to calculate the read/write location everywhere.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.