iOS alloc & init 探索

初探

1、新建一個Person 類 先思考打印情況 再運行

 Person *p1 = [Person alloc];
 Person *p2 = [p1 init];
 Person *p3 = [p2 init];
 NSLog(@"%@ - %p - %p",p1,p1,&p1);
 NSLog(@"%@ - %p - %p",p2,p2,&p2);
 NSLog(@"%@ - %p - %p",p3,p3,&p3);

得到結(jié)果為

<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff508
<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff500
<Person: 0x100721af0> - 0x100721af0 - 0x7ffeefbff4f8

猜測:
1、init 沒有對內(nèi)存進(jìn)行任何操作
2、前兩個打印 輸出的為打印的同一片內(nèi)存地址 說明 p1 p2 p3 指向著同一片內(nèi)存地址空間
3、&p1 &p2 &p3 不一樣 打印的是每個指針自身所在的內(nèi)存地址空間

斷點跟蹤 源碼下載 分析

我們按住command + 鼠標(biāo)點擊 發(fā)現(xiàn)只能到到達(dá) NSObject 的聲明文件 再也進(jìn)不去而且沒有注釋 這時候我們可以進(jìn)行 下符號斷點
復(fù)制alloc 添加符號斷點 繼續(xù)跟進(jìn)


截屏2020-09-09下午2.23.33.png

首先將符號斷點關(guān)掉 在 Person *p1 = [Person alloc]; 這一行進(jìn)行斷點 再打開 符號斷點alloc 繼續(xù)
85AB5774-8A02-47D9-A43F-C057CA2EA759.png

根據(jù)我們得到的信息繼續(xù)跟蹤 我們發(fā)現(xiàn) 跳到了 _objc_rootAllocWithZone


7DEC262C-E96F-4EAD-BB78-4BDD73677599.png

再次打符號斷點我們發(fā)現(xiàn) 很難讀懂 但是很好 -objc4 是開源的 我們可以去這里下載 進(jìn)行跟進(jìn) https://opensource.apple.com/tarballs/
配置可編譯源碼 https://juejin.im/post/6844904082226806792

配置好可編譯源碼我們一步一步的看源碼

+ (id)alloc

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

_objc_rootAlloc / callAlloc

static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
#if __OBJC2__
    if (slowpath(checkNil && !cls)) return nil;
    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));
}


// Base class implementation of +alloc. cls is not nil.
// Calls [cls allocWithZone:nil].
id
_objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}

_objc_rootAllocWithZone

_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)
{
    // allocWithZone under __OBJC2__ ignores the zone parameter
    return _class_createInstanceFromZone(cls, 0, nil,
                                         OBJECT_CONSTRUCT_CALL_BADALLOC);
}

_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;
   ///計算 需要多少內(nèi)存空間
    size = cls->instanceSize(extraBytes);
    if (outAllocatedSize) *outAllocatedSize = size;

    id obj;
    if (zone) {
        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
    } else {
        // alloc 開辟內(nèi)存的地方
        obj = (id)calloc(1, size);
    }
    if (slowpath(!obj)) {
        if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
            return _objc_callBadAllocHandler(cls);
        }
        return nil;
    }

    if (!zone && fast) {
       ///關(guān)聯(lián)對應(yīng)的類
        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);
}

此時我們 通過斷點及閱讀源碼總結(jié) 如下alloc流程圖 !
A9822803-A872-4AA8-8F17-F871615C5704.png

init

// Replaced by CF (throws an NSException)
+ (id)init {
    return (id)self;
}

- (id)init {
    return _objc_rootInit(self);
}



_objc_rootInit

_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;
}

new

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容