在上一篇的文章深入底層理解alloc和init以及new中我們分析了alloc,知道了 alloc創建了對象并且分配內存;同時初始化isa屬性;我們也知道了Objective-C 對象在底層本質上是結構體,所有的對象里面都會包含有一個isa,isa的定義是一個聯合體isa_t,isa_t包含了當前對象指向類的信息,這篇文章我們來分析探究isa。
聯合體isa_t
union isa_t {
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
Class cls;
uintptr_t bits;
#if defined(ISA_BITFIELD)
struct {
ISA_BITFIELD; // defined in isa.h
};
#endif
};
# define ISA_MASK 0x00007ffffffffff8ULL
# define ISA_MAGIC_MASK 0x001f800000000001ULL
# define ISA_MAGIC_VALUE 0x001d800000000001ULL
# define ISA_BITFIELD \
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)
這是在
__x86_64__
上的實現,__arm64__
的架構會有所差別,但是這些字段都還是有的,并不影響我們對于isa_t的分析。
isa_t是一個聯合體,聯合體的特性就是內部所有的成員共用一塊內存地址空間,也就是說isa_t、cls、bits會共用同一塊內存地址空間,這塊內存地址空間大小取決于最大長度內部成員的大小即64位8字節。由此我們可以知道isa 的所占的內存空間大小為8字節。
struct {
uintptr_t indexed : 1;
uintptr_t has_assoc : 1;
uintptr_t has_cxx_dtor : 1;
uintptr_t shiftcls : 44;
uintptr_t magic : 6;
uintptr_t weakly_referenced : 1;
uintptr_t deallocating : 1;
uintptr_t has_sidetable_rc : 1;
uintptr_t extra_rc : 8;
};
如上面分析,我們已然了解到了isa_t是一個64位的聯合,isa占用8個字節的大小。下面我們來分下下isa的初始化過程。
isa的初始化
在objc的源碼中我們找到了isa的初始化方法。
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
assert(!cls->instancesRequireRawIsa());
assert(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
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;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = hasCxxDtor;
newisa.indexcls = (uintptr_t)cls->classArrayIndex();
#else
newisa.bits = ISA_MAGIC_VALUE;
// isa.magic is part of ISA_MAGIC_VALUE
// isa.nonpointer is part of ISA_MAGIC_VALUE
newisa.has_cxx_dtor = hasCxxDtor;
newisa.shiftcls = (uintptr_t)cls >> 3;
#endif
// This write must be performed in a single store in some cases
// (for example when realizing a class because other threads
// may simultaneously try to use the class).
// fixme use atomics here to guarantee single-store and to
// guarantee memory order w.r.t. the class index table
// ...but not too atomic because we don't want to hurt instantiation
isa = newisa;
}
}
由于nonpointer傳入的是true,SUPPORT_INDEXED_ISA定義為0,所以我們可以對這段代碼簡化一下。
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
isa_t newisa(0);
newisa.bits = ISA_MAGIC_VALUE;
newisa.has_cxx_dtor = hasCxxDtor;
newisa.shiftcls = (uintptr_t)cls >> 3;
isa = newisa;
}
從上面這段代碼中我們看到的是對bits的賦值ISA_MAGIC_VALUE=0x001d800000000001ULL,將此轉為二進制,在結合isa_t的結構得出如下的isa_t的初始數據圖。
從上圖中很明顯看出來對isa賦值ISA_MAGIC_VALUE初始化實際上只是設置了indexed和magic兩部分的數據。
- indexed表示 isa_t 的類型。0表示 raw isa,也就是沒有結構體的部分,訪問對象的 isa 會直接返回一個指向 cls 的指針,也就是在 iPhone 遷移到 64 位系統之前時 isa 的類型。1則表示當前 isa 不是指針,但是其中也有 cls 的信息,只是其中關于類的指針都是保存在 shiftcls 中。
- magic 用于調試器判斷當前對象是否有初始化空間。
在設置indexed和magic的值后會對has_cxx_dtor驚行設值。has_cxx_dtor表示該對象是否有 C++ 或者 Objc 的析構器,如果有析構函數,則需要做析構邏輯, 如果沒有,則可以更快的釋放對象
newisa.has_cxx_dtor = hasCxxDtor;
下一步會將當前對象的類指針存放在shiftcls中。
newisa.shiftcls = (uintptr_t)cls >> 3;
在這里對cls的地址右移動3位的目的是為了減少內存的消耗,因為類的指針需要按照8字節對齊,也就是說類的指針的大小必定是8的倍數,其二進制后三位為0 ,右移三位抹除后面的3位0并不會產生影響。
bits中的其他說明
- has_assoc 對象含有或者曾經含有關聯引用,沒有關聯引用的可以更快地釋放內存
- weakly_referenced 對象被指向或者曾經指向一個 ARC 的弱變量,沒有弱引用的對象可以更快釋放
- deallocating 對象正在釋放內存
- has_sidetable_rc 當對象引用技術大于 10 時,則需要借用該變量存儲進位。
- extra_rc 表示該對象的引用計數值,實際上是引用計數值減 1, 例如,如果對象的引用計數為 10,那么 extra_rc 為 9。如果引用計數大于 10, 則需要使用到下面的 has_sidetable_rc。
通過上面的分析我們知道了,alloc在創建對象分配內存后會進行isa的初始化,isa中存儲了對象或者類的一些信息。下面我們來分析下isa和類、對象之間的關系。
isa的走位
我們都知道Object-C的對象其本質就是結構體,前面我們也分析了每一個對象都會有一個isa。
同時我們也知道了其實類的本質也是一個結構體,而且是繼承自objc_object的。
struct objc_object {
private:
isa_t isa;
...
};
struct objc_class : objc_object {
// Class ISA;
Class superclass;
cache_t cache; // formerly cache pointer and vtable
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
...
};
所以在objc_class中也是有isa的。
struct objc_class : objc_object {
isa_t isa;
Class superclass;
cache_t cache; // formerly cache pointer and vtable
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags
...
};
我們通通過一幅圖來分析下isa、對象、類之間的關系。
- 實例對象的isa指向的是類;
- 類的isa指向的元類;
- 元類指向根元類;
- 根元類指向自己;
- NSObject的父類是nil,根元類的父類是NSObject。
下面我來驗證下他們之間的關系