iOS 底層alloc init new 源碼流程示例分析
alloc&init 的源碼流程圖

首先創(chuàng)建Person 類, 在main函數(shù)創(chuàng)建Person 實(shí)例 Person *p = [Person alloc]; 1.進(jìn)入到alloc 方法的源碼實(shí)現(xiàn)
+ (id)alloc {
return _objc_rootAlloc(self);
}
2.跳轉(zhuǎn)到_objc_rootAlloc 源碼實(shí)現(xiàn)
id
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
3.跳轉(zhuǎn)至 callAlloc 的源碼實(shí)現(xiàn)
static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__ //有可用的編譯器優(yōu)化
if (slowpath(checkNil && !cls)) return nil;
//判斷是否自定義實(shí)現(xiàn)了 +allocWithZone 方法
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));
該方法中有兩個(gè)定義的宏
#define fastpath(x) (__builtin_expect(bool(x), 1)) #define slowpath(x) (__builtin_expect(bool(x), 0))
其中__builtin_expect指令由gcc 引入,目的:1. 編譯器可以對(duì)代碼進(jìn)行優(yōu)化,以減少指令跳轉(zhuǎn)帶來(lái)的性能下降,2.作用: 允許程序員將最有可能執(zhí)行的分支告訴編譯器;3.寫法為: __builtin_expect(EXP, N) , 表示 EXP == N的概率很大;
fastPath 定義的__builtin_expect(bool(x), 1) 表示x 的值為真的可能性更大;
slowpath 定義的__builtin_expect(bool(x), 0) 表示x 的值為假的可能性更大;
日常開發(fā)中可以通過(guò)設(shè)置來(lái)優(yōu)化編譯器,達(dá)到性能優(yōu)化的目的,設(shè)置路徑: Build Settiing -> Optimization Level -> Debug -> 將None 改為fastest/smallest 4.跳轉(zhuǎn)至 _objc_rootAllocWithZone 的源碼實(shí)現(xiàn)
id
_objc_rootAllocWithZone(Class cls, objc_zone_t zone __unused)
{
// allocWithZone under __OBJC2__ ignores the zone parameter
return _class_createInstanceFromZone(cls, 0, nil,
OBJECT_CONSTRUCT_CALL_BADALLOC);
}
5.跳轉(zhuǎn)至 _class_createInstanceFromZone 源碼實(shí)現(xiàn)
static ALWAYS_INLINE id
_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;
size = cls->instanceSize(extraBytes);
if (outAllocatedSize) *outAllocatedSize = size;
id obj;
#if SUPPORT_ZONES
// 支持zone
// 早期的內(nèi)存是通過(guò)zone 申請(qǐng)的 ilo89i='
if (zone) {
obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
} else {
#endif
obj = (id)calloc(1, size);
#if SUPPORT_ZONES
}
#endif
if (slowpath(!obj)) {
if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
return _objc_callBadAllocHandler(cls);
}
return nil;
}
if (!zone && fast) {
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);
}
該方法中有三個(gè)核心方法:
- cls->instanceSize:計(jì)算所需內(nèi)存大小, 源碼實(shí)現(xiàn)
inline size_t instanceSize(size_t extraBytes) const {
// 快速計(jì)算內(nèi)存大小
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;
}
fastInstanceSize 的源碼實(shí)現(xiàn)
size_t fastInstanceSize(size_t extra) const
{
ASSERT(hasFastInstanceSize(extra));
if (__builtin_constant_p(extra) && extra == 0) {
return _flags & FAST_CACHE_ALLOC_MASK16;
} else {
size_t size = _flags & FAST_CACHE_ALLOC_MASK;
// remove the FAST_CACHE_ALLOC_DELTA16 that was added
// by setFastInstanceSize
return align16(size + extra - FAST_CACHE_ALLOC_DELTA16);
}
}
align16的源碼實(shí)現(xiàn)
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
斷點(diǎn)調(diào)試此處的參數(shù)x 為8 即: align16(8)

2.calloc 申請(qǐng)內(nèi)存,返回地址指針 向內(nèi)存中申請(qǐng)大小為 instanceSize計(jì)算的內(nèi)存, 并將內(nèi)存地址的指針?lè)祷?,賦值給obj,obj = (id)calloc(1, size);
3.obj->initInstanceIsa(cls, hasCxxDtor); : 初始化isa 指針 并將類與isa 關(guān)聯(lián)
Init 源碼探索
通過(guò)查看 Init 源碼
- (id)init {
return _objc_rootInit(self);
}
id
_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;
}
通過(guò)源碼實(shí)現(xiàn)可以看到 Init 就是將傳入的對(duì)象 直接返回
new 的源碼探索
日常開發(fā)中,對(duì)象的創(chuàng)建 有 alloc Init 和new , 現(xiàn)在看下new的源碼實(shí)現(xiàn)
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
通過(guò)源碼可以看出 new 相當(dāng)于alloc init 過(guò)程,但是二者有何區(qū)別 以下是其他博主總結(jié)的, 引用一下

以上就是iOS 底層alloc init new 源碼流程示例分析的詳細(xì)內(nèi)容,更多關(guān)于iOS 底層alloc init new分析的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)的問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于iOS列表上拉(平滑加載數(shù)據(jù))自動(dòng)加載數(shù)據(jù)問(wèn)題的相關(guān)資料,本文實(shí)現(xiàn)的效果很多app都用的這種效果,文中通過(guò)圖文以及實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
實(shí)例解析iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用
這篇文章主要介紹了iOS開發(fā)中系統(tǒng)音效以及自定義音效的應(yīng)用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-10-10
iOS App使用設(shè)計(jì)模式中的模板方法模式開發(fā)的示例
這篇文章主要介紹了iOS應(yīng)用使用設(shè)計(jì)模式中的模板方法模式開發(fā)的示例,例子代碼為Objective-C語(yǔ)言,文中還與Java的相關(guān)實(shí)現(xiàn)進(jìn)行類比,需要的朋友可以參考下2016-03-03
詳解iOS中UIButton的三大UIEdgeInsets屬性用法
這篇文章主要介紹了iOS中UIButton的三大UIEdgeInsets屬性用法,分別講解了contentEdgeInsets、imageEdgeInsets和titleEdgeInsets三個(gè)屬性在創(chuàng)建UIButton時(shí)對(duì)樣式的控制,需要的朋友可以參考下2016-04-04
iOS開發(fā)避免安全隱患的要點(diǎn)總結(jié)
在本篇文章里小編給各位整理了關(guān)于iOS開發(fā)如何避免安全隱患的知識(shí)點(diǎn)總結(jié),需要的朋友們學(xué)習(xí)下。2019-07-07
iOS框架AVFoundation實(shí)現(xiàn)相機(jī)拍照、錄制視頻
這篇文章主要為大家詳細(xì)介紹了iOS框架AVFoundation實(shí)現(xiàn)相機(jī)拍照、錄制視頻功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面
最近工作中遇到一個(gè)需求,當(dāng)UITableView數(shù)據(jù)為空的時(shí)候,給出一個(gè)簡(jiǎn)單的提示頁(yè)面,通過(guò)從網(wǎng)上查找解決的方法,發(fā)現(xiàn)了兩種實(shí)現(xiàn)的方法,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面感興趣的朋友們來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11

