Android anonymous shared memory Java INTERFACE ANALYSIS

Source: Internet
Author: User

In the Android anonymous shared memory driver source code analysis, this article introduces the implementation process of the anonymous shared memory driver. Based on the Android anonymous shared memory driver, this article introduces the Android anonymous shared memory. The main body of the external Android anonymous shared memory sub-system is implemented in the kernel space in the form of a driver, and a Java call interface is provided at the application framework layer. At the Android Application Framework layer, a MemoryFile interface is provided to encapsulate the creation and use of anonymous shared memory files, it is implemented in frameworks/base/core/java/android/OS/MemoryFile. java [cpp] public MemoryFile (String name, int length) throws IOException {mLength = length; // open the "/dev/ashmem" Device File mFD = native_open (name, length ); if (length> 0) {// map the opened "/dev/ashmem" device file to the virtual address space of the Process mAddress = native_mmap (mFD, length, PROT_READ | PROT_WRITE);} else {mAddress = 0 ;}} native _ The open function is a local function implemented in the C ++ layer through JNI. The code is located in frameworks \ base \ core \ jni \ android_ OS _MemoryFile.cpp [cpp] static jobject android_ OS _MemoryFile_open (JNIEnv * env, jobject clazz, jstring name, jint length) {// String Conversion const char * namestr = (name? Env-> GetStringUTFChars (name, NULL): NULL); // open the device file "/dev/ashmem ", and modify the device file name and shared memory size int result = ashmem_create_region (namestr, length); if (name) env-> ReleaseStringUTFChars (name, namestr); if (result <0) {jniThrowException (env, "java/io/IOException", "ashmem_create_region failed"); return NULL;} // convert the device file handle to return jniCreateFileDescriptor (env, result );} the function first converts the shared memory name passed by the Java layer to the string of the C ++ layer, and then calls the ashme The m_create_region function creates an anonymous shared memory named dev/ashmem/and modifies the name and size of the shared memory, then, return the file handle value of the created anonymous shared memory device to the Java space. The ashmem_create_region function has detailed analysis in the C interface Analysis of Android anonymous shared memory. This function is used to create an anonymous shared memory. When constructing a MemoryFile object in the Java space, first open the/dev/ashmem device file and create an ashmem_area in the kernel space, next, you need to map the shared memory address allocated by the kernel space to the virtual address space of the process. The ing process is completed through the native_mmap function. [Cpp] static jint Parser (JNIEnv * env, jobject clazz, jobject fileDescriptor, jint length, jint prot) {int fd = encode (env, fileDescriptor); jint result = (jint) mmap (NULL, length, prot, MAP_SHARED, fd, 0); if (! Result) jniThrowException (env, "java/io/IOException", "mmap failed"); return result;} This function calls mmap directly to implement address space ing. Note that the flag is MAP_SHARED, indicates that the buffer is mapped in a shared manner. The ashing process is implemented by the Ashmem driver. The source code analysis of the Android anonymous shared memory driver analyzes the implementation process of the Android anonymous shared memory. When constructing a MemoryFile object, the process of creating an anonymous shared memory and ing the address space is completed. The size of the created anonymous shared memory is saved to the MemoryFile member variable mLength, the member variable mFD stores the file descriptor of the created anonymous shared memory, and the member variable mAddress saves the initial address of the anonymous shared memory mapped to the process address space. With this information, you can directly use the anonymous shared memory. The read operation on the anonymous shared memory is encapsulated into a MemoryInputStream in the Java space. This class inherits from the input stream InputStream and provides the read method, which is defined as follows: [cpp] @ Override public int read () throws IOException {if (mSingleByte = null) {mSingleByte = new byte [1];} int result = read (mSingleByte, 0, 1); if (result! = 1) {return-1;} return mSingleByte [0];} @ Override public int read (byte buffer [], int offset, int count) throws IOException {if (offset <0 | count <0 | offset + count> buffer. length) {// readBytes () also does this check, but we need to do it before // changing count. throw new IndexOutOfBoundsException ();} count = Math. min (count, available (); if (count <1) {return-1;} int result = ReadBytes (buffer, mOffset, offset, count); if (result> 0) {mOffset + = result;} return result;} The MemoryInputStream class provides two read overload methods, the first non-parameter read method calls the parameter read method to read 1 byte of data, the Data Reading Process with the parameter read method calls the readBytes method of MemoryInputStream's external class MemoryFile to implement the read process of anonymous shared memory data. [Cpp] public int readBytes (byte [] buffer, int srcOffset, int destOffset, int count) throws IOException {if (isDeactivated ()) {throw new IOException ("Can't read from deactivated memory file. ");} if (destOffset <0 | destOffset> buffer. length | count <0 | count> buffer. length-destOffset | srcOffset <0 | srcOffset> mLength | count> mLength-srcOffset) {throw new IndexOutOfBoundsExce Ption ();} return native_read (mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);} This function only makes some judgments, then, call the local method native_read to complete data reading in the C ++ space. when constructing the MemoryFile object, the dev/ashmem device file has been opened and mapped, therefore, the file handle value obtained by opening the file on the device is directly transferred to the C ++ space to correctly read the content in the specified anonymous shared memory, mAddress maps the anonymous shared memory to the starting address in the process address space. [Cpp] static jint evaluate (JNIEnv * env, jobject clazz, jobject fileDescriptor, jint address, jbyteArray buffer, jint srcOffset, jint destOffset, jint count, jboolean unpinned) {int fd = encode (env, fileDescriptor); if (unpinned & ashmem_pin_region (fd, 0, 0) = ASHMEM_WAS_PURGED) {ashmem_unpin_region (fd, 0, 0 ); jniThrowException (env, "java/io/IOException "," Ashmem region was purged "); return-1;} env-> SetByteArrayRegion (buffer, destOffset, count, (const jbyte *) address + srcOffset); if (unpinned) {ashmem_unpin_region (fd, 0, 0);} return count;} write the specified data to the anonymous shared memory, memoryOutputStream is used to encapsulate write operations on anonymous shared memory. This class provides two overloaded write methods, one for writing multi-byte data to anonymous shared memory, and the other for writing only one byte data. Here is a brief introduction to the multi-byte data writing process: [cpp] public void write (byte buffer [], int offset, int count) throws IOException {writeBytes (buffer, offset, mOffset, count); mOffset + = count;} the buffer parameter refers to the byte array written into the anonymous shared memory. offset specifies the offset of the data buffer to start writing, the count parameter specifies the length of bytes written to the anonymous shared memory. The function calls the writeBytes function of MemoryFile to write data. [Cpp] public void writeBytes (byte [] buffer, int srcOffset, int destOffset, int count) throws IOException {if (isDeactivated ()) {throw new IOException ("Can't write to deactivated memory file. ");} if (srcOffset <0 | srcOffset> buffer. length | count <0 | count> buffer. length-srcOffset | destOffset <0 | destOffset> mLength | count> mLength-destOffset) {throw new IndexOutOfBoundsEx Ception ();} native_write (mFD, mAddress, buffer, srcOffset, destOffset, count, mAllowPurging);} This function first checks the correctness of the parameter, then call the native method native_write to transfer data to C ++ through JNI. The first parameter is the file descriptor of the anonymous shared memory, the second parameter is the base value mapped from the anonymous shared memory to the process address space. The following three parameters have already been described, the last mAllowPurging parameter indicates whether to allow memory collection to describe the write process of anonymous shared memory, essentially, the data starting at the specified position in the buffer is copied to the specified offset position in the anonymous shared memory, frameworks \ base \ core \ jni \ android_ OS _MemoryFile.cpp [cpp] static jint android_ OS _MemoryFile_write (JNIEnv * e Nv, jobject clazz, jobject fileDescriptor, jint address, jbyteArray buffer, jint srcOffset, jint destOffset, jint count, jboolean unpinned) {int fd = encode (env, fileDescriptor ); if (unpinned & ashmem_pin_region (fd, 0, 0) = ASHMEM_WAS_PURGED) {ashmem_unpin_region (fd, 0, 0); jniThrowException (env, "java/io/IOException ", "ashmem region was purged"); return-1;} env-> GetByteA RrayRegion (buffer, srcOffset, count, (jbyte *) address + destOffset); if (unpinned) {ashmem_unpin_region (fd, 0, 0);} return count ;} the JNI function GetByteArrayRegion is used to copy data.

Related Article

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.