前言
在前面的文章中我們探索了在iOS中alloc執(zhí)行流程,在流程中最后的_class_createInstanceFromZone()方法中,主要執(zhí)行如下:
-
size = cls->instanceSize(extraBytes);確認要開辟的空間大小 -
obj = (id)calloc(1, size);開辟空間 -
obj->initInstanceIsa(cls, hasCxxDtor);建立cls與isa的綁定。
這里文章就重點探索下initInstanceIsa()的內(nèi)部實現(xiàn)。
聯(lián)合體位域
聯(lián)合體:在進行某些算法的C語言編程的時候,需要使幾種不同類型的變量存放到同一段內(nèi)存單元中。也就是使用覆蓋技術(shù),幾個變量互相覆蓋。這種幾個不同的變量共同占用一段內(nèi)存的結(jié)構(gòu),在C語言中,被稱作“共用體”類型結(jié)構(gòu),簡稱共用體,也叫聯(lián)合體。
union 共用體名{
成員列表
};
共用體有時也被稱為聯(lián)合或者聯(lián)合體,這也是 Union 這個單詞的本意。
struct與union的區(qū)別
struct |
union |
|---|---|
| 各個成員會占用不同的內(nèi)存,互相之間沒有影響 | 所有成員占用同一段內(nèi)存,修改一個成員會影響其余所有成員 |
| 結(jié)構(gòu)體占用的內(nèi)存大于等于所有成員占用的內(nèi)存的總和(成員之間可能會存在縫隙) | 共用體占用的內(nèi)存等于最長的成員占用的內(nèi)存 |
位域:信息在存儲時,并不需要占用一個完整的字節(jié), 而只需占幾個或一個二進制位。例如在存放一個開關(guān)量時,只有0和1 兩種狀態(tài), 用一位二進位即可。為了節(jié)省存儲空間,并使處理簡便,C語言又提供了一種數(shù)據(jù)結(jié)構(gòu),稱為“位域”或“位段”。所謂“位域”是把一個字節(jié)中的二進位劃分為幾 個不同的區(qū)域, 并說明每個區(qū)域的位數(shù)。每個域有一個域名,允許在程序中按域名進行操作。 這樣就可以把幾個不同的對象用一個字節(jié)的二進制位域來表示。
聯(lián)合體+位域
// 聯(lián)合體
union
{
uint8_t value;
//位域
struct
{
uint8_t lowbit : 2;
uint8_t middlebit : 3;
uint8_t highbit : 3;
}byte;
}data;
value和byte共用一個字節(jié)的內(nèi)存空間,改變value的值,那么byte的值也就改變了,同樣改變byte中的位(lowbit占2位, middlebit占3位,highbit也占3位;)value的值也就改變了;一般情況下,我們要得到value中的高三位的值,需要得到這樣(暫定高三位的值為x)x=(value>>5)&0x03,但是使用了位域,就可以直接得到了,省去了這樣的一個計算的過程。
int main(int argc, const char * argv[]) {
@autoreleasepool {
//聯(lián)合體
union {
uint8_t value;
// 位域
struct {
uint8_t bit0:2;
uint8_t bit1:3;
uint8_t bit2:3;
};
}data1,data2;
data1.value = 20;
data2.bit0 = 3;
data2.bit1 = 3;
data2.bit2 = 0;
printf("bit0 = %d\r\n",data1.bit0);
printf("bit1 = %d\r\n",data1.bit1);
printf("bit2 = %d\r\n",data1.bit2);
printf("data1.value = %d\r\n",data1.value);
printf("data2.value = %d\r\n",data2.value);
}
}
console=>
bit0 = 0
bit1 = 5
bit2 = 0
data1.value = 20
data2.value = 15
回到OC
obj->initInstanceIsa(cls, hasCxxDtor):
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
ASSERT(!cls->instancesRequireRawIsa());
ASSERT(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
核心進入 -> initIsa()
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
ASSERT(!isTaggedPointer());
if (!nonpointer) {
isa = isa_t((uintptr_t)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;
}
}
isa_t:一個典型的聯(lián)合體位域結(jié)構(gòu)
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
};
ISA_BITFIELD:位域結(jié)構(gòu)分布宏定義(arm64和x86_64)
# if __arm64__
# define ISA_MASK 0x0000000ffffffff8ULL
# define ISA_MAGIC_MASK 0x000003f000000001ULL
# define ISA_MAGIC_VALUE 0x000001a000000001ULL
# define ISA_BITFIELD \
uintptr_t nonpointer : 1; \
uintptr_t has_assoc : 1; \
uintptr_t has_cxx_dtor : 1; \
uintptr_t shiftcls : 33; /*MACH_VM_MAX_ADDRESS 0x1000000000*/ \
uintptr_t magic : 6; \
uintptr_t weakly_referenced : 1; \
uintptr_t deallocating : 1; \
uintptr_t has_sidetable_rc : 1; \
uintptr_t extra_rc : 19
# define RC_ONE (1ULL<<45)
# define RC_HALF (1ULL<<18)
# elif __x86_64__
# 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)
# else
# error unknown architecture for packed isa
# endif

以下是各段位字段注釋:
nonpointer:表示是否對 isa 指針開啟指針優(yōu)化 0:純isa指針,1:不止是類對象地址,isa 中包含了類信息、對象的引用計數(shù)等。has_assoc:關(guān)聯(lián)對象標志位,0沒有,1存在。has_cxx_dtor:該對象是否有 C++ 或者 Objc 的析構(gòu)器,如果有析構(gòu)函數(shù),則需要做析構(gòu)邏輯, 如果沒有,則可以更快的釋放對象shiftcls:存儲類指針的值。開啟指針優(yōu)化的情況下,在 arm64 架構(gòu)中有 33 位用來存儲類指針。(重要)magic:用于調(diào)試器判斷當前對象是真的對象還是沒有初始化的空間weakly_referenced:標志對象是否被指向或者曾經(jīng)指向一個 ARC 的弱變量,沒有弱引用的對象可以更快釋放。deallocating:標志對象是否正在釋放內(nèi)存has_sidetable_rc:當對象引用計數(shù)大于 10 時,則需要借用該變量存儲進位。extra_rc:當表示該對象的引用計數(shù)值,實際上是引用計數(shù)值減 1, 例如,如果對象的引用計數(shù)為 10,那么 extra_rc 為 9。如果引用計數(shù)大于 10, 則需要使用到上面的 has_sidetable_rc。
到此,我們可以看出isa在其64bit的長度中,存儲了很多的信息;而廣義上的我們說isa其實讀取的是shiftcls段位的信息。如下:
inline Class
objc_object::ISA()
{
ASSERT(!isTaggedPointer());
#if SUPPORT_INDEXED_ISA
if (isa.nonpointer) {
uintptr_t slot = isa.indexcls;
return classForIndex((unsigned)slot);
}
return (Class)isa.bits;
#else
return (Class)(isa.bits & ISA_MASK);
#endif
}
isa經(jīng)典走位圖

總結(jié)
以上僅為羅列筆記。