最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年12月25日 16:25:54   作者:等待肉包子飛來  
這篇文章主要為大家介紹了iOS 底層alloc init new 源碼流程示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

alloc&init 的源碼流程圖

首先創(chuàng)建Person 類, 在main函數(shù)創(chuàng)建Person 實例 Person *p = [Person alloc]; 1.進入到alloc 方法的源碼實現(xiàn)

+ (id)alloc { 
return _objc_rootAlloc(self); 
}

2.跳轉(zhuǎn)到_objc_rootAlloc 源碼實現(xiàn)

id
_objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

3.跳轉(zhuǎn)至 callAlloc 的源碼實現(xiàn)

static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__ //有可用的編譯器優(yōu)化
    if (slowpath(checkNil && !cls)) return nil;
    //判斷是否自定義實現(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));

該方法中有兩個定義的宏

#define fastpath(x) (__builtin_expect(bool(x), 1))
#define slowpath(x) (__builtin_expect(bool(x), 0))

其中__builtin_expect指令由gcc 引入,目的:1. 編譯器可以對代碼進行優(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)化編譯器,達到性能優(yōu)化的目的,設(shè)置路徑: Build Settiing -> Optimization Level -> Debug -> 將None 改為fastest/smallest 4.跳轉(zhuǎn)至 _objc_rootAllocWithZone 的源碼實現(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 源碼實現(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);
}

該方法中有三個核心方法:

  • cls->instanceSize:計算所需內(nèi)存大小, 源碼實現(xiàn)
inline size_t instanceSize(size_t extraBytes) const {
// 快速計算內(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 的源碼實現(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的源碼實現(xiàn)

static inline size_t align16(size_t x) {
    return (x + size_t(15)) & ~size_t(15);
}

斷點調(diào)試此處的參數(shù)x 為8 即: align16(8)

2.calloc 申請內(nèi)存,返回地址指針 向內(nèi)存中申請大小為 instanceSize計算的內(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;
}

通過源碼實現(xiàn)可以看到 Init 就是將傳入的對象 直接返回

new 的源碼探索

日常開發(fā)中,對象的創(chuàng)建 有 alloc Init 和new , 現(xiàn)在看下new的源碼實現(xiàn)

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}

通過源碼可以看出 new 相當(dāng)于alloc init 過程,但是二者有何區(qū)別 以下是其他博主總結(jié)的, 引用一下

以上就是iOS 底層alloc init new 源碼流程示例分析的詳細內(nèi)容,更多關(guān)于iOS 底層alloc init new分析的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

晴隆县| 芦溪县| 永丰县| 永善县| 利川市| 肇源县| 兴仁县| 鲁山县| 安陆市| 上虞市| 漳平市| 佳木斯市| 台南县| 青浦区| 托克逊县| 兴宁市| 同德县| 普安县| 河南省| 沁阳市| 杭州市| 巩留县| 湟源县| 石狮市| 邵阳县| 临朐县| 宁乡县| 新源县| 澳门| 甘谷县| 巴楚县| 锡林郭勒盟| 勃利县| 阿荣旗| 和平区| 吉安县| 固安县| 武清区| 金沙县| 黑山县| 屏山县|