前言:從對象的創(chuàng)建 , 去探究對象的本質(zhì) , 創(chuàng)建的流程 , 一路上會遇到 isa
, 對象
-> 類
->元類
, cache_t
, 內(nèi)存對齊
, 分類
,taggedPoint
, 方法緩存
, 方法查找
, 消息轉(zhuǎn)發(fā)
, 內(nèi)存管理
等內(nèi)容虚吟。對于iOS開發(fā)人員來說:alloc
& init
& new
是非常熟悉的鳞溉,但是是否對于它的原理也一的熟悉!磨总,今天我們就一起探索一下這個我們熟悉的陌生人誓篱。流程圖如下:
底層流程圖
-
1.1 objc_alloc 與 alloc
對象調(diào)用alloc
CPPerson *objc1 = [LGPerson alloc];
? alloc
類方法源碼如下 :
+ (id)alloc {
return _objc_rootAlloc(self);
}
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
? objc_alloc
函數(shù)如下 :
objc_alloc(Class cls)
{
return callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/);
}
我們可以看到不管是 alloc
還是 objc_alloc
, 都會進(jìn)入callAlloc
這個函數(shù) , 只是最后兩個參數(shù)傳入的不同 . 那么我們就繼續(xù)往下看 .
-
1.2callAlloc
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__
if (slowpath(checkNil && !cls)) return nil;
if (fastpath(!cls->ISA()->hasCustomAWZ())) {
return _objc_rootAllocWithZone(cls, nil);
}
#endif
// No shortcuts available.
if (allocWithZone) {
return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
}
return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
}
首先我們注意到了兩個宏定義的函數(shù) : fastpath
與slowpath
.
// x 很可能不為 0朋贬,希望編譯器進(jìn)行優(yōu)化
#define fastpath(x) (__builtin_expect(bool(x), 1))
// x 很可能為 0,希望編譯器進(jìn)行優(yōu)化
#define slowpath(x) (__builtin_expect(bool(x), 0))
-
1.2.1 _objc_rootAllocWithZone
源碼如下:
_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)
{
// allocWithZone under __OBJC2__ ignores the zone parameter
return _class_createInstanceFromZone(cls, 0, nil,
OBJECT_CONSTRUCT_CALL_BADALLOC);
}
-
1.2.2. objc_msgSend
源碼如下:
if (allocWithZone) {
return ((id(*)(id, SEL, struct _NSZone *))objc_msgSend)(cls, @selector(allocWithZone:), nil);
}
return ((id(*)(id, SEL))objc_msgSend)(cls, @selector(alloc));
-
1.3. _class_createInstanceFromZone
源碼如下:
_class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone,
int construct_flags = OBJECT_CONSTRUCT_NONE,
bool cxxConstruct = true,
size_t *outAllocatedSize = nil)
{
ASSERT(cls->isRealized());
// Read class's info bits all at once for performance
bool hasCxxCtor = cxxConstruct && cls->hasCxxCtor();
bool hasCxxDtor = cls->hasCxxDtor();
bool fast = cls->canAllocNonpointer();
size_t size;
//計算出需要的內(nèi)存空間大小
size = cls->instanceSize(extraBytes);
if (outAllocatedSize) *outAllocatedSize = size;
id obj;
if (zone) {
obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
} else {
// alloc 向系統(tǒng)申請開辟內(nèi)存窜骄,返回地址指針
obj = (id)calloc(1, size);
}
if (slowpath(!obj)) {
if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
return _objc_callBadAllocHandler(cls);
}
return nil;
}
if (!zone && fast) {
//關(guān)聯(lián)到相應(yīng)的類
obj->initInstanceIsa(cls, hasCxxDtor);
} else {
// Use raw pointer isa on the assumption that they might be
// doing something weird with the zone or RR.
obj->initIsa(cls);
}
if (fastpath(!hasCxxCtor)) {
return obj;
}
construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
return object_cxxConstructFromClass(obj, cls, construct_flags);
}
-
1.3.1 instanceSize 計算內(nèi)存空間
源碼
size_t instanceSize(size_t extraBytes) const {
if (fastpath(cache.hasFastInstanceSize(extraBytes))) {
return cache.fastInstanceSize(extraBytes);
}
size_t size = alignedInstanceSize() + extraBytes;
// CF requires all objects be at least 16 bytes.
if (size < 16) size = 16;
return size;
}
-
1.3.2 calloc向內(nèi)存申請空間锦募,返回指針
void *calloc(size_t __count, size_t __size) __result_use_check __alloc_size(1,2);
-
1.3.3 nitInstanceIsa(cls, hasCxxDtor);關(guān)聯(lián)類
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
ASSERT(!cls->instancesRequireRawIsa());
ASSERT(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
-
2.1 init
源碼
- (id)init {
return _objc_rootInit(self);
}
_objc_rootInit(id obj)
{
// In practice, it will be hard to rely on this function.
// Many classes do not properly chain -init calls.
return obj;
}
可以看到init
默認(rèn)返回方法調(diào)用者 . 這個設(shè)計其實(shí)是為了方便工程設(shè)計 , 以便于在初始化對象時做一些初始化或者賦值操作 .
-
3.1 new
源碼
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
new
相當(dāng)于 alloc
+init
. 但是使用new
并不能調(diào)用我們所重寫的各種 init
工廠方法 .