一. 探究對象本質(zhì)
需要用到Clang, 利用Clang 編譯OC文件
Clang是?個C語?、C++、Objective-C語?的輕量級編譯器。
源代碼發(fā)布于BSD協(xié)議下。Clang將?持其普通lambda表達(dá)式、返回類型的簡化處理以及更好的處理constexpr關(guān)鍵字。
Clang是?個由Apple主導(dǎo)編寫,基于LLVM的C/C++/Objective-C編譯器。
我們通過xcrun 編譯
編譯 main.m
xcrun -sdk iphonesimulator clang -rewrite-objc TObjec.mt
@interface TObject : NSObject
@property (nonatomic, copy) NSString *prString;
@property (nonatomic, assign) int prInt1;
@property (nonatomic, assign) int prInt2;
@end
#ifndef _REWRITER_typedef_TObject
#define _REWRITER_typedef_TObject
typedef struct objc_object TObject;
typedef struct {} _objc_exc_TObject;
#endif
extern "C" unsigned long OBJC_IVAR_$_TObject$_prString;
extern "C" unsigned long OBJC_IVAR_$_TObject$_prInt1;
extern "C" unsigned long OBJC_IVAR_$_TObject$_prInt2;
struct TObject_IMPL {
struct NSObject_IMPL NSObject_IVARS;
int _prInt1;
int _prInt2;
NSString * _Nonnull _prString;
};
// @property (nonatomic, copy) NSString *prString;
// @property (nonatomic, assign) int prInt1;
// @property (nonatomic, assign) int prInt2;
/* @end */
#pragma clang assume_nonnull end
對象的 prString 等屬性也得到了體現(xiàn)
所以對象的本質(zhì)是結(jié)構(gòu)體
對象結(jié)構(gòu)
typedef struct objc_object TObject; 表示 TObject 就是一個objc_object 類型的結(jié)構(gòu)體,也就是說 每個對象在底層都是一個 objc_object 類型的結(jié)構(gòu)體
/// An opaque type that represents an Objective-C class.
typedef struct objc_class *Class;
/// Represents an instance of a class.
struct objc_object {
Class _Nonnull isa OBJC_ISA_AVAILABILITY;
};
每個對象都有一個 isa 指針, isa 指向的是一個 objc_class 的結(jié)構(gòu)體
二. isa 是什么
Objective-C 是一門面向?qū)ο蟮恼Z言,所以每個OC對象都有一個隱藏的數(shù)據(jù)結(jié)構(gòu),這個結(jié)構(gòu)就是OC對象的第一個成員變量,他就是isa指針,指針指向的是內(nèi)存空間的一片地址
isa的類型
結(jié)合第二篇篇 對象創(chuàng)建過程 ,會調(diào)用 initInstanceIsa 初始化指針對象
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 = isa_t((uintptr_t)cls); // 初始化isa
} 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 是一個isa_t的類型
union isa_t {
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
Class cls; //表示isa 關(guān)聯(lián)類的類型
uintptr_t bits;
// cls 和 bits 兩者是互斥的關(guān)系
#if defined(ISA_BITFIELD)
struct {
ISA_BITFIELD; // defined in isa.h
};
#endif
};
// 下面就是 ISA_BITFIELD 的定義
# 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
- isa_t 使用的是聯(lián)合體 union 類型,聯(lián)合體類型內(nèi)存使用精細(xì)靈活,節(jié)省空間,也稱作共用體,共用一個內(nèi)存首地址
- Class cls 表示 isa 所關(guān)聯(lián)的類的類型
- uintptr_t bits 這是保存著一段isa 通過 位域偏移后的數(shù)據(jù)
nonpointer. 對 isa 指針優(yōu)化,0表示純 isa 指針,1表示包含了類信息,對象引用計數(shù)等
has_assoc:關(guān)聯(lián)對象標(biāo)志位,0沒有,1存在;
has_cxx_dtor:該對象是否有 C++ 或者 Objc 的析構(gòu)器,如果有析構(gòu)函數(shù),則需要做析構(gòu)邏輯, 如果沒有,則可以更快的釋放對象;
shiftcls:存儲類指針的值。開啟指針優(yōu)化的情況下,在 arm64 架構(gòu)中有 33 位用來存儲類指針;
magic:用于調(diào)試器判斷當(dāng)前對象是真的對象還是沒有初始化的空間;
weakly_referenced:指對象是否被指向或者曾經(jīng)指向一個 ARC 的弱變量,沒有弱引用的對象可以更快釋放;
deallocating:標(biāo)志對象是否正在釋放內(nèi)存;
has_sidetable_rc:如果為1,代表引用計數(shù)過大無法存儲在 isa 中,
那么超出的引用計數(shù)會存儲在一個叫 SideTable 結(jié)構(gòu)體的RefCountMap(引用計數(shù)表)散列表中
extra_rc:里面存儲的值是對象本身之外的引用計數(shù)的數(shù)量,這 19 位如果不夠存儲,has_sidetable_rc的值就會變?yōu)?1;
驗證 shiftcls 存儲的的類信息
x/4g: 以十六進(jìn)制打印person的4段內(nèi)存情況,其中第一段就是 isa
>> 3, <<3 就是將 右邊3字節(jié)清除,也就是清除 nonpointer,has_assoc, has_cxx_dtor 這三個東東
<<17 ,>>17 原因同上
最終位移計算出來的地址 就是 shiftcls 這個東西
這也驗證了 shiftcls 就是存儲類的信息

image.png
所以 isa 內(nèi)存結(jié)構(gòu)如下

image.png
·