Android inter-process communication data (2) ------ Implementation of parcel, androidparcel

Source: Internet
Author: User

Android inter-process communication data (2) ------ Implementation of parcel, androidparcel

Serialize is something that comes with java Native. We can see the android source code.

So let's take a look at how android implements parcel, which inspires our own code design.

Parcel:

In android, the source code of parcel is as follows:

Frameworks/base/core/java/android/OS/Parcel. java

    /**     * Write an integer value into the parcel at the current dataPosition(),     * growing dataCapacity() if needed.     */    public final void writeInt(int val) {        nativeWriteInt(mNativePtr, val);    }    /**     * Write a long integer value into the parcel at the current dataPosition(),     * growing dataCapacity() if needed.     */    public final void writeLong(long val) {        nativeWriteLong(mNativePtr, val);    }

Common Methods:

Obtain () gets a new parcel, which is equivalent to a new object dataSize () to get the actual storage space of the current parcel object dataCapacity () to get the allocated storage space of the current parcel object,> = dataSize () value (for time in space) dataPostion () Get the offset of the current parcel object (similar to the offset of the file stream pointer) setDataPosition () set the offset recyle () clear and reclaim the memory writeInt (int) of the parcel object and write an integer writeFloat (float) to a floating point number writeDouble (double) to write a double number writeString (string) to write a string of course, there are more writeXXX () methods, which correspond to readXXX (). For details, see SDK. Several noteworthy methods are as follows: writeException () writes an exception writeException () to the Parcel header and writes "no exception" readException () to read in the Parcel header, if the read value is an exception, this exception is thrown; otherwise, the program runs normally.

Let's analyze:

status_t Parcel::writeInt32(int32_t val){    return writeAligned(val);}
template<class T>status_t Parcel::writeAligned(T val) {    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE(sizeof(T)) == sizeof(T));    if ((mDataPos+sizeof(val)) <= mDataCapacity) {restart_write:        *reinterpret_cast<T*>(mData+mDataPos) = val;        return finishWrite(sizeof(val));    }    status_t err = growData(sizeof(val));    if (err == NO_ERROR) goto restart_write;    return err;}

Let's analyze writeAligned:

First

#define PAD_SIZE(s) (((s)+3)&~3)

&~ 3:

Consider hexadecimal ,~ 3-> 0x11111100 In Progress &

That is to say, the first six digits remain unchanged, and the last two digits are 0.

Or, change the last two digits to 0, that is, a multiple of 4.

The purpose of + 3 is to take the nearest value except 4 from the current value. If it is itself, it will not change.

So the purpose of the macro is to align it in 4 bytes!

Therefore, writeAligned writes 4-byte aligned content.

Optional values: int32 and int64.

status_t Parcel::growData(size_t len){    size_t newSize = ((mDataSize+len)*3)/2;    return (newSize <= mDataSize)            ? (status_t) NO_MEMORY            : continueWrite(newSize);}

When the memory is insufficient, a memory of 3/2 blocks will be applied for so that parcel can be used now.

Therefore, parcel is essentially the most memory operation, and the operation bit is memcpy.

Check the continueWrite function. It is not listed due to its complexity,

Let's take a look at the key points:

            size_t* objects =                (size_t*)realloc(mObjects, objectsSize*sizeof(size_t));

Expand the memory. This is the place.

Therefore, the entire parcel read and write operations are in the memory.

 

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.