上一篇我们对 Parcel 进行了简单的介绍。这里我们选两个代表性的数据类型(String 和 Binder)来分析 Parcel 的处理流程,对应的接口分别是:writeString() / readString() 和 writeStrongBinder() / readStrongBinder() 。接着上一篇从 Parcel C++ 类的构造过程开始分析。
一、源码分析
1、Parcel.cpp
源码位置:frameworks/native/libs/binder/Parcel.cpp。
Parcel::Parcel()
{
LOG_ALLOC("Parcel %p: constructing", this);
initState();
}
void Parcel::initState()
{
LOG_ALLOC("Parcel %p: initState", this);
mError = NO_ERROR; // 错误码
mData = nullptr; // Parcel中存储的数据,注意它是一个uint8_t类型指针
mDataSize = 0; // Parcel中已经存储的数据大小
mDataCapacity = 0; // 最大存储能力
mDataPos = 0; //数据指针
ALOGV("initState Setting data size of %p to %zu", this, mDataSize);
ALOGV("initState Setting data pos of %p to %zu", this, mDataPos);
mSession = nullptr;
mObjects = nullptr;
mObjectsSize = 0;
mO