背景
書接上回alloc流程圖分析中,在最后calloc分配空間,可得到空間的地址,那么calloc中系統(tǒng)是如何分配空間?如何將分配的空間與isa指針進(jìn)行綁定?isa到底是個(gè)什么東東?帶著這些疑問(wèn),下面將大致分析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()
,是否有默認(rèn)的allocWithZone方法
-
cls->ISA()
即獲取當(dāng)前class的isa指針 -
hasDefaultAWZ ()
源碼如下:
bool hasDefaultAWZ() {
return data()->flags & RW_HAS_DEFAULT_AWZ;
}
#define RW_HAS_DEFAULT_AWZ (1<<16)
RW_HAS_DEFAULT_AWZ標(biāo)識(shí)當(dāng)前class或者superclass是否有默認(rèn)的alloc/mallocWithZone:,有則直接對(duì)class進(jìn)行allocWithZone,申請(qǐng)內(nèi)存空間。
if (allocWithZone) return [cls allocWithZone:nil];
- 沒(méi)有默認(rèn)的allocWithZone方法,則
cls->canAllocFast()
再次判斷當(dāng)前的class是否支持快速alloc
- 如果可以,直接調(diào)用calloc函數(shù),申請(qǐng)一塊
bits.fastInstanceSize()
大小的內(nèi)存空間, 如果申請(qǐng)失敗,也會(huì)調(diào)用callBadAllocHandler
函數(shù);如果申請(qǐng)成功,就會(huì)obj->initInstanceIsa(cls, dtor)
初始化isa指針。
bool dtor = cls->hasCxxDtor();
bool hasCxxDtor() {
return data()->flags & RW_HAS_CXX_DTOR;
}
// 是否實(shí)現(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)建一個(gè)新的對(duì)象。
這個(gè)流程后面再詳細(xì)分析。
-
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
這個(gè)類型,下面詳解isa_t
這個(gè)結(jié)構(gòu)類型。
isa內(nèi)部結(jié)構(gòu)
isa_t
的內(nèi)部結(jié)構(gòu)如下。 當(dāng)前只分析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
是一個(gè)聯(lián)合體,即所有成員共用一個(gè)內(nèi)存空間。首先是2個(gè)構(gòu)造函數(shù)
isa_t()
默認(rèn)構(gòu)造函數(shù)和isa_t(uintptr_t value)
構(gòu)造函數(shù)。2個(gè)成員
Class cls
和uintptr_t bits
,大家都知道,實(shí)例對(duì)象的isa指針指向類對(duì)象,類對(duì)象的isa指針指向元類對(duì)象,這就是Class cls
為何存在的原因。uintptr_t
其實(shí)就是unsigned long 類型,明顯就是isa所占的空間大小。結(jié)構(gòu)體
struct
中所有成員所占的控件大小值加起來(lái)是64位,即8字節(jié),正好印證了isa指針8字節(jié)的大小,這64位空間就固定分配給了struct
中的各個(gè)成員。-
再看結(jié)構(gòu)體內(nèi)部成員,分布圖如下
bits位域分布圖.jpg-
nonpointer
表示是否對(duì) isa 指針開(kāi)啟指針優(yōu)化 -
has_assoc
是否有關(guān)聯(lián)對(duì)象 -
has_cxx_dtor
標(biāo)記對(duì)象是否使用到的C++相關(guān)內(nèi)容,在ARC環(huán)境下標(biāo)記對(duì)象是否通過(guò)ARC來(lái)管理 -
shiftcls
標(biāo)記當(dāng)前對(duì)象的類對(duì)象的指針地址 -
magic
判斷對(duì)象是否初始化完成, 是調(diào)試器判斷當(dāng)前對(duì)象是真的對(duì)象還是沒(méi)有初始化的空間。 -
weakly_referenced
標(biāo)記對(duì)象是否有弱引用指針 -
deallocating
標(biāo)記對(duì)象是否正在進(jìn)行dealloc操作 -
has_sidetable_rc
標(biāo)記是否有sitetable結(jié)構(gòu)用于存儲(chǔ)引用計(jì)數(shù) -
extra_rc
標(biāo)記對(duì)象的引用計(jì)數(shù)(首先會(huì)存儲(chǔ)在該字段中,當(dāng)?shù)竭_(dá)上限后,在存入對(duì)應(yīng)的引用計(jì)數(shù)表中)
-
從
isa_t
內(nèi)部結(jié)構(gòu)分析得出,成員shiftcls
中存放的是類對(duì)象Class的指針,這就印證了當(dāng)前對(duì)象的isa指針關(guān)聯(lián)的是其類對(duì)象。
驗(yàn)證isa與Class的關(guān)聯(lián)
-
x/4gx p
16進(jìn)制,每4段打印p指針地址 -
p/t
二進(jìn)制打印第一段地址(即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
的起始位置,此時(shí)剩余的地址只包含shiftcls
-
p/t (uintptr_t)FXPerson.class >> 3
對(duì)類對(duì)象FXPerson右移3位,同第3步,后3位抹零,同第5步得到的地址作比較,拋開(kāi)前17位,發(fā)現(xiàn)結(jié)果一模一樣,證明shiftcls
中存放的就是Class對(duì)象FXPerson。
總結(jié)
我們通過(guò)跟進(jìn)calloc的流程,跟進(jìn)obj->initInstanceIsa(cls, dtor)
這一步,調(diào)用initIsa(cls, true, hasCxxDtor)
初始化isa指針并與cls綁定時(shí),發(fā)現(xiàn)在isa_t
聯(lián)合體的位域shiftcls
中,存放就是類對(duì)象指針,最后通過(guò)示例,打印shiftcls
的二進(jìn)制值,印證了這一判斷。