經過下符號斷點的方式來跟蹤 [NSObject alloc]
c++
//下了兩個符號斷點
一、objc_alloc
二、+[NSObject alloc]
//結果是 1 2
複製代碼
通過編譯器一頓操做,彷佛結合和預想不是徹底吻合bash
在使用+[NSObject alloc]
的時候實際上調用的是libonjc.A.dylib objc_alloc
,並非預想的libonjc.A.dylib objc_msgSend
,這裏看一llvm
的源碼,對alloc
作了處理(第一次調用objc_alloc
,第二次調用+alloc
)函數
// Calls [cls alloc].
id
objc_alloc(Class cls)
{
return callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/);
}
複製代碼
checkNil = true
、allocWithZone = false
callAlloc
須要檢查nil
,並且不使用zone
static id callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
if (slowpath(checkNil && !cls)) return nil;
#if __OBJC2__
//是否實現 allocWithZone
if (fastpath(!cls->ISA()->hasCustomAWZ())) {
//未實現allocWithZone
// canAllocFast直接返回了false,直接看else
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 {
// 核心 建立實例
id obj = class_createInstance(cls, 0);
if (slowpath(!obj)) return callBadAllocHandler(cls);
return obj;
}
}
#endif
// No shortcuts available.
if (allocWithZone) return [cls allocWithZone:nil];
//+alloc 終於輪到你了
return [cls alloc];
}
複製代碼
hasCustomAWZ()
是檢查是否實現了 +allocWithZone
+allocWithZone
就經過 class_createInstance(cls, 0)
來建立實例+allocWithZone
,但還須要allocWithZone == true
纔會調用,首次調用必定不會觸發在objc_alloc
中傳了false
return [cls alloc];
最後這段代碼纔是主流程class_createInstance(Class cls, size_t extraBytes)
{
//調用了 私有方法 _class_createInstanceFromZone
return _class_createInstanceFromZone(cls, extraBytes, nil);
}
id
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone,
bool cxxConstruct = true,
size_t *outAllocatedSize = nil)
{
if (!cls) return nil;
assert(cls->isRealized());
// Read class's info bits all at once for performance bool hasCxxCtor = cls->hasCxxCtor(); //有c++構造函數 bool hasCxxDtor = cls->hasCxxDtor(); //有c++析構函數 bool fast = cls->canAllocNonpointer(); //快速建立,是否容許非純指針,也就是isa的優化 //計算實例大小 size_t size = cls->instanceSize(extraBytes); if (outAllocatedSize) *outAllocatedSize = size; id obj; if (!zone && fast) { //不使用zone而且支持isa優化的對象分配空間 obj = (id)calloc(1, size); if (!obj) return nil; //對象實例初始化isa obj->initInstanceIsa(cls, hasCxxDtor); } else { if (zone) { //使用zone 分配空間 obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size); } else { //不使用 zone分配空間 obj = (id)calloc(1, size); } if (!obj) return nil; //純指針的初始化 obj->initIsa(cls); } if (cxxConstruct && hasCxxCtor) { obj = _objc_constructOrFree(obj, cls); } return obj; } 複製代碼
//拿到類對象的 ro 對象的原始大小
uint32_t unalignedInstanceSize() {
assert(isRealized());
return data()->ro->instanceSize;
}
/*
字節對齊: 爲了讓CPU 尋址更簡單快速,用空間換時間
*/
uint32_t alignedInstanceSize() {
return word_align(unalignedInstanceSize());
}
//計算對象大小
size_t instanceSize(size_t extraBytes) {
size_t size = alignedInstanceSize() + extraBytes;
// CF requires all objects be at least 16 bytes.
//最小 16個字節
if (size < 16) size = 16;
return size;
}
複製代碼
關於字節對齊的內容優化
####4. InitIsaui
inline void
objc_object::initIsa(Class cls)
{
//純指針的初始化 isa 方法
initIsa(cls, false, false);
}
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
//非純指針的初始化 isa 方法
initIsa(cls, true, hasCxxDtor);
}
/*
初始化Isa
參數:
cls 類對象
nonpointer 是不是非指針,就是通過優化的isa
hasCxxDtor 是否存在c++析構
*/
inline void
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)
{
assert(!isTaggedPointer());
//
if (!nonpointer) {
isa.cls = cls;
} else {
isa_t newisa(0);
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;
isa = newisa;
}
}
複製代碼
isa的相關內容spa
+ (id)alloc {
return _objc_rootAlloc(self);
}
id
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
複製代碼
+allocWithZone
將會調用+ alloc
,而後再次調用callAlloc
,此次參數不同了不須要檢查是否爲空,使用zone
callAlloc
參數來控制執行路徑,從而實現不一樣狀況下建立對象