網站首頁 編程語言 正文
alloc&init 的源碼流程圖
首先創建Person 類, 在main函數創建Person 實例 Person *p = [Person alloc];
1.進入到alloc 方法的源碼實現
+ (id)alloc { return _objc_rootAlloc(self); }
2.跳轉到_objc_rootAlloc
源碼實現
id _objc_rootAlloc(Class cls) { return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/); }
3.跳轉至 callAlloc
的源碼實現
static ALWAYS_INLINE id callAlloc(Class cls, bool checkNil, bool allocWithZone=false) { #if __OBJC2__ //有可用的編譯器優化 if (slowpath(checkNil && !cls)) return nil; //判斷是否自定義實現了 +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));
該方法中有兩個定義的宏
#define fastpath(x) (__builtin_expect(bool(x), 1)) #define slowpath(x) (__builtin_expect(bool(x), 0))
其中__builtin_expect指令由gcc 引入,目的:1. 編譯器可以對代碼進行優化,以減少指令跳轉帶來的性能下降,2.作用: 允許程序員將最有可能執行的分支告訴編譯器;3.寫法為: __builtin_expect(EXP, N) , 表示 EXP == N的概率很大;
fastPath 定義的__builtin_expect(bool(x), 1) 表示x 的值為真的可能性更大;
slowpath 定義的__builtin_expect(bool(x), 0) 表示x 的值為假的可能性更大;
日常開發中可以通過設置來優化編譯器,達到性能優化的目的,設置路徑: Build Settiing -> Optimization Level -> Debug -> 將None 改為fastest/smallest 4.跳轉至 _objc_rootAllocWithZone
的源碼實現
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.跳轉至 _class_createInstanceFromZone
源碼實現
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 // 早期的內存是通過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); }
該方法中有三個核心方法:
- cls->instanceSize:計算所需內存大小, 源碼實現
inline 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; }
fastInstanceSize
的源碼實現
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
的源碼實現
static inline size_t align16(size_t x) { return (x + size_t(15)) & ~size_t(15); }
斷點調試此處的參數x 為8 即: align16(8)
2.calloc
申請內存,返回地址指針 向內存中申請大小為 instanceSize
計算的內存, 并將內存地址的指針返回,賦值給obj,obj = (id)calloc(1, size);
3.obj->initInstanceIsa(cls, hasCxxDtor);
: 初始化isa 指針 并將類與isa 關聯
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; }
通過源碼實現可以看到 Init 就是將傳入的對象 直接返回
new 的源碼探索
日常開發中,對象的創建 有 alloc Init 和new , 現在看下new的源碼實現
+ (id)new { return [callAlloc(self, false/*checkNil*/) init]; }
通過源碼可以看出 new 相當于alloc init 過程,但是二者有何區別 以下是其他博主總結的, 引用一下
原文鏈接:https://juejin.cn/post/7180564100001300537
相關推薦
- 2022-04-09 Solr 檢索結果集List<SolrDocument> 轉換為指定業務對象總結
- 2022-09-30 LeetCode189輪轉數組python示例_python
- 2022-04-12 Error: Rule can only have one resource source (pro
- 2022-12-12 python?使用?with?open()?as?讀寫文件的操作方法_python
- 2021-12-07 基于Kubernetes實現前后端應用的金絲雀發布(兩種方案)_C#教程
- 2022-11-25 Go實現快速生成固定長度的隨機字符串_Golang
- 2022-07-31 教你用python實現一個加密的文字處理器_python
- 2022-08-07 Android?無障礙服務?performAction?調用過程分析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支