Android Inter-process communication data (ii) implementation of------parcel

Source: Internet
Author: User

Serialize is a Java original born of things, we can see the source of Android

So look at how Android is implementing parcel, which is what inspires our own code design.

Parcel:

In Android, the parcel source code is as follows:

Frameworks/base/core/java/android/os/parcel.java

    /*** Write An integer value to the parcel at the current dataposition (), * growing datacapacity () if needed. */     Public Final voidWriteint (intval)    {Nativewriteint (Mnativeptr, Val); }    /*** Write A long integer value into the parcel in the current dataposition (), * growing datacapacity () if Neede D.*/     Public Final voidWritelong (Longval)    {Nativewritelong (Mnativeptr, Val); }

Common methods:

obtain () obtains a new parcel, which is equivalent to new Object DataSize () to get the actual storage of the current parcel object Space Datacapacity () Gets the allocated storage space for the current parcel object.>=the DataSize () value (in space-time) datapostion () Gets the offset of the current parcel object (similar to the offset of the file stream pointer) setdatapositio N () sets the offset recyle () to empty, reclaim the parcel object's memory Writeint (int) writes an integer writefloat (float) writes a floating-point number writedouble (Doublewrites a double-precision number writestring (string) to write a string of course, there are more writexxx () methods, which corresponds to Readxxx (), the specific method please          See SDK. Several notable methods are: WriteException () write an exception in the parcel Team header writeexception () Parcel Team Header Write "no exception "Readexception () reads from the parcel team header, and if the read value is an exception, the exception is thrown; otherwise, the program runs normally. 

Let's analyze the following:

status_t Parcel::writeint32 (int32_t val) {    return  writealigned (val);}
template<class t>status_t parcel::writealigned (t val) {    = = sizeof (t))    ; if ((Mdatapos+sizeof (val)) <= mdatacapacity) {restart_write:        *reinterpret_cast<t*> (mdata+ Mdatapos) = val;         return finishwrite (sizeof (Val));    }     = Growdata (sizeof (Val));     if Goto restart_write;     return err;}

Let's analyze writealigned:

First,

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

&~3:

Consider 16-in, 0x11111100 &

That is, the previous 6 bits are unchanged, and the last 2 bits are 0.

In other words, turn the last 2 bits into 0. That is, a multiple of 4.

The purpose of +3 is to take the current value up to the nearest divisible 4 value, if that is the same.

So the purpose of the macro is to press 4 bytes to align!

So writealigned is writing 4-byte aligned content.

Common is int32 and int64 and so on.

status_t parcel::growdata (size_t len) {    = ((Mdatasize+len) *3)/2;     return (newSize <= mdatasize)            ? (status_t) no_memory            : Continuewrite (newSize);}

When the memory is not enough, will apply a piece of 3/2 size of memory, so now parcel can use.

So the parcel essence is the most memory operation, and the operation bit memcpy and so on.

View the Continuewrite function, because it is very complex, it is not listed,

Look at the key places:

            size_t* objects =                (size_t*)realloc(mobjects, objectssize*sizeof(size_t));

Enlarge the memory, AHA. This is the place.

So the entire parcel read and write is operating in memory.

Android Inter-process communication data (ii) implementation of------parcel

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.