背景
書接上回alloc流程圖分析中,在最后calloc分配空間,可得到空間的地址,那么calloc中系統(tǒng)是如何分配空間?如何將分配的空間與isa指針進行綁定?isa到底是個什么東東?帶著這些疑問,下面將大致分析calloc的處理流程,以及所涉及到isa內(nèi)部結(jié)構(gòu)的分析。
calloc大致流程
先上源碼
static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
if (slowpath(checkNil && !cls)) return nil;
#if __OBJC2__
if (fastpath(!cls->ISA()->hasCustomAWZ())) {
// No alloc/allocWithZone implementation. Go straight to the allocator.
// fixme store hasCustomAWZ in the non-meta class and
// add it to canAllocFast's summary
if (fastpath(cls->canAllocFast())) {
// No ctors, raw isa, etc. Go straight to the metal.
bool dtor = cls->hasCxxDtor();
id obj = (id)calloc(1, cls->bits.fastInstanceSize());
if (slowpath(!obj)) return callBadAllocHandler(cls);
obj->initInstanceIsa(cls, dtor);
return obj;
}
else {
// Has ctor or raw isa or something. Use the slower path.
id obj = class_createInstance(cls, 0);
if (slowpath(!obj)) return callBadAllocHandler(cls);
return obj;
}
}
#endif
// No shortcuts available.
if (allocWithZone) return [cls allocWithZone:nil];
return [cls alloc];
}
大致流程如下:
- 首先判斷
cls->ISA()->hasCustomAWZ()
,是否有默認的allocWithZone方法
-
cls->ISA()
即獲取當前class的isa指針 -
hasDefaultAWZ ()
源碼如下:
bool hasDefaultAWZ() {
return data()->flags & RW_HAS_DEFAULT_AWZ;
}
#define RW_HAS_DEFAULT_AWZ (1<<16)
RW_HAS_DEFAULT_AWZ標識當前class或者superclass是否有默認的alloc/mallocWithZone:,有則直接對class進行allocWithZone,申請內(nèi)存空間。
if (allocWithZone) return [cls allocWithZone:nil];
- 沒有默認的allocWithZone方法,則
cls->canAllocFast()
再次判斷當前的class是否支持快速alloc
- 如果可以,直接調(diào)用calloc函數(shù),申請一塊
bits.fastInstanceSize()
大小的內(nèi)存空間, 如果申請失敗,也會調(diào)用callBadAllocHandler
函數(shù);如果申請成功,就會obj->initInstanceIsa(cls, dtor)
初始化isa指針。
bool dtor = cls->hasCxxDtor();
bool hasCxxDtor() {
return data()->flags & RW_HAS_CXX_DTOR;
}
// 是否實現(xiàn)了析構(gòu)函數(shù)
// class or superclass has .cxx_destruct implementation
#define RW_HAS_CXX_DTOR (1<<17)
- 不能快速alloc的話,則調(diào)用
class_createInstance(cls, 0)
乖乖的去創(chuàng)建一個新的對象。
這個流程后面再詳細分析。
-
obj->initInstanceIsa(cls, dtor)
初始化isa指針
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
assert(!cls->instancesRequireRawIsa());
assert(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
調(diào)用initIsa(cls, true, hasCxxDtor)
初始化isa指針,并與cls綁定。
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
assert(!isTaggedPointer());
if (!nonpointer) {
isa.cls = cls;
} else {
assert(!DisableNonpointerIsa);
assert(!cls->instancesRequireRawIsa());
isa_t newisa(0);
#if SUPPORT_INDEXED_ISA
assert(cls->classArrayIndex() > 0);
newisa.bits = ISA_INDEX_MAGIC_VALUE;
newisa.has_cxx_dtor = hasCxxDtor;
newisa.indexcls = (uintptr_t)cls->classArrayIndex();
#else
newisa.bits = ISA_MAGIC_VALUE;
newisa.has_cxx_dtor = hasCxxDtor;
newisa.shiftcls = (uintptr_t)cls >> 3;
#endif
isa = newisa;
}
}
既然是初始化isa,那么緊跟isa,發(fā)現(xiàn)是將newisa賦值給isa,newisa是isa_t
這個類型,下面詳解isa_t
這個結(jié)構(gòu)類型。
isa內(nèi)部結(jié)構(gòu)
isa_t
的內(nèi)部結(jié)構(gòu)如下。 當前只分析x86_64架構(gòu),其它架構(gòu)同理。
union isa_t
{
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
Class cls;
uintptr_t bits;
#if SUPPORT_PACKED_ISA
# if __arm64__
# define ISA_MASK 0x0000000ffffffff8ULL
# define ISA_MAGIC_MASK 0x000003f000000001ULL
# define ISA_MAGIC_VALUE 0x000001a000000001ULL
struct {
uintptr_t nonpointer : 1;
uintptr_t has_assoc : 1;
uintptr_t has_cxx_dtor : 1;
uintptr_t shiftcls : 44; // MACH_VM_MAX_ADDRESS 0x7fffffe00000
uintptr_t magic : 6;
uintptr_t weakly_referenced : 1;
uintptr_t deallocating : 1;
uintptr_t has_sidetable_rc : 1;
uintptr_t extra_rc : 8;
# define RC_ONE (1ULL<<56)
# define RC_HALF (1ULL<<7)
};
# else
# error unknown architecture for packed isa
# endif
// SUPPORT_PACKED_ISA
#endif
};
分析:
isa_t
是一個聯(lián)合體,即所有成員共用一個內(nèi)存空間。首先是2個構(gòu)造函數(shù)
isa_t()
默認構(gòu)造函數(shù)和isa_t(uintptr_t value)
構(gòu)造函數(shù)。2個成員
Class cls
和uintptr_t bits
,大家都知道,實例對象的isa指針指向類對象,類對象的isa指針指向元類對象,這就是Class cls
為何存在的原因。uintptr_t
其實就是unsigned long 類型,明顯就是isa所占的空間大小。結(jié)構(gòu)體
struct
中所有成員所占的控件大小值加起來是64位,即8字節(jié),正好印證了isa指針8字節(jié)的大小,這64位空間就固定分配給了struct
中的各個成員。-
再看結(jié)構(gòu)體內(nèi)部成員,分布圖如下
bits位域分布圖.jpg-
nonpointer
表示是否對 isa 指針開啟指針優(yōu)化 -
has_assoc
是否有關(guān)聯(lián)對象 -
has_cxx_dtor
標記對象是否使用到的C++相關(guān)內(nèi)容,在ARC環(huán)境下標記對象是否通過ARC來管理 -
shiftcls
標記當前對象的類對象的指針地址 -
magic
判斷對象是否初始化完成, 是調(diào)試器判斷當前對象是真的對象還是沒有初始化的空間。 -
weakly_referenced
標記對象是否有弱引用指針 -
deallocating
標記對象是否正在進行dealloc操作 -
has_sidetable_rc
標記是否有sitetable結(jié)構(gòu)用于存儲引用計數(shù) -
extra_rc
標記對象的引用計數(shù)(首先會存儲在該字段中,當?shù)竭_上限后,在存入對應的引用計數(shù)表中)
-
從
isa_t
內(nèi)部結(jié)構(gòu)分析得出,成員shiftcls
中存放的是類對象Class的指針,這就印證了當前對象的isa指針關(guān)聯(lián)的是其類對象。
驗證isa與Class的關(guān)聯(lián)
-
x/4gx p
16進制,每4段打印p指針地址 -
p/t
二進制打印第一段地址(即isa指針)的內(nèi)存值 -
p/t $1>>3
右移3位,代表抹零nonpointer
has_assoc
和has_cxx_dtor
-
p/t $2<<17
左移17位,代表抹零magic
weakly_referenced
deallocating
和
extra_rc
-
p/t $3>>17
再右移17位,還原shiftcls
的起始位置,此時剩余的地址只包含shiftcls
-
p/t (uintptr_t)FXPerson.class >> 3
對類對象FXPerson右移3位,同第3步,后3位抹零,同第5步得到的地址作比較,拋開前17位,發(fā)現(xiàn)結(jié)果一模一樣,證明shiftcls
中存放的就是Class對象FXPerson。
總結(jié)
我們通過跟進calloc的流程,跟進obj->initInstanceIsa(cls, dtor)
這一步,調(diào)用initIsa(cls, true, hasCxxDtor)
初始化isa指針并與cls綁定時,發(fā)現(xiàn)在isa_t
聯(lián)合體的位域shiftcls
中,存放就是類對象指針,最后通過示例,打印shiftcls
的二進制值,印證了這一判斷。