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 的值為假的可能性更大;
日常開(kāi)發(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 的源碼探索
日常開(kāi)發(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中利用CAGradientLayer繪制漸變色的方法實(shí)例
有時(shí)候iOS開(kāi)發(fā)中需要使用到漸變色,來(lái)給圖片或者view蓋上一層,使其顯示效果更好,所以這篇文章主要給大家介紹了關(guān)于iOS中利用CAGradientLayer繪制漸變色的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11
ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果
本篇文章主要介紹了ios基于MJRefresh實(shí)現(xiàn)上拉刷新和下拉加載動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
ios利用RunLoop原理實(shí)現(xiàn)去監(jiān)控卡頓實(shí)例詳解
這篇文章主要為大家介紹了ios利用RunLoop原理實(shí)現(xiàn)去監(jiān)控卡頓實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
iOS中讓多個(gè)cell上都出現(xiàn)倒計(jì)時(shí)的分析與實(shí)現(xiàn)
這篇文章主要給大家介紹了在iOS中每個(gè)cell上都出現(xiàn)倒計(jì)時(shí)的分析與實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
ios獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例
下面小編就為大家?guī)?lái)一篇ios獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
iOS中無(wú)限循環(huán)滾動(dòng)簡(jiǎn)單處理實(shí)現(xiàn)原理分析
這篇文章主要介紹了iOS中無(wú)限循環(huán)滾動(dòng)簡(jiǎn)單處理實(shí)現(xiàn)原理分析,需要的朋友可以參考下2017-12-12
IOS 打包出現(xiàn)Missing Push Notification Entitlement 問(wèn)題解決方案
這篇文章主要介紹了IOS 打包出現(xiàn)Missing Push Notification Entitlement 問(wèn)題解決方案的相關(guān)資料,需要的朋友可以參考下2016-12-12

