網(wǎng)站首頁 編程語言 正文
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. 編譯器可以對代碼進(jìn)行優(yōu)化,以減少指令跳轉(zhuǎn)帶來的性能下降,2.作用: 允許程序員將最有可能執(zhí)行的分支告訴編譯器;3.寫法為: __builtin_expect(EXP, N) , 表示 EXP == N的概率很大;
fastPath 定義的__builtin_expect(bool(x), 1) 表示x 的值為真的可能性更大;
slowpath 定義的__builtin_expect(bool(x), 0) 表示x 的值為假的可能性更大;
日常開發(fā)中可以通過設(shè)置來優(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)存是通過zone 申請的 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
申請內(nèi)存,返回地址指針 向內(nèi)存中申請大小為 instanceSize
計(jì)算的內(nèi)存, 并將內(nèi)存地址的指針返回,賦值給obj,obj = (id)calloc(1, size);
3.obj->initInstanceIsa(cls, hasCxxDtor);
: 初始化isa 指針 并將類與isa 關(guān)聯(lián)
Init 源碼探索
通過查看 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; }
通過源碼實(shí)現(xiàn)可以看到 Init 就是將傳入的對象 直接返回
new 的源碼探索
日常開發(fā)中,對象的創(chuàng)建 有 alloc Init 和new , 現(xiàn)在看下new的源碼實(shí)現(xiàn)
+ (id)new { return [callAlloc(self, false/*checkNil*/) init]; }
通過源碼可以看出 new 相當(dāng)于alloc init 過程,但是二者有何區(qū)別 以下是其他博主總結(jié)的, 引用一下
原文鏈接:https://juejin.cn/post/7180564100001300537
相關(guān)推薦
- 2023-11-25 消息的訂閱與發(fā)布機(jī)制
- 2023-05-15 Android?onMeasure與onDraw及自定義屬性使用示例_Android
- 2023-01-03 C#短消息提示窗口位置及窗口大小詳解_C#教程
- 2023-02-05 Python?面向?qū)ο缶幊淘斀鈅python
- 2022-06-16 Zabbix自定義腳本監(jiān)控nginx以及微信告警的全過程_zabbix
- 2023-02-09 Python關(guān)鍵字?asynico基本用法_python
- 2022-06-24 C#利用itext實(shí)現(xiàn)PDF頁面處理與切分_C#教程
- 2022-10-08 Python中集合創(chuàng)建與使用詳解_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支