iOS OC对象底层原理之alloc

对于一个iOS开发者来说,alloc是最最频繁使用的系统方法之一.有没有想过它到底是什么呢,究竟怎么实现的呢,本篇文章就来探索这个底层原理;

准备工作

对于底层的探究,当然需要OC的底层源码.苹果也在这一方面给与了开发者一些支持.我们可以通过 opensource.apple.com 或 opensource.apple.com/tarballs/ 下载到部分苹果的开源代码;
(目前最新的是objc4-824,太新的可能跑不起来,可以用老版本试试,具体看自己机器来定,我是用的是objc4-818)

也可以用大神Cooci编译好的源码

开始探索

线上一张alloc底层流程图(画画技术有限 能看就行)

未命名文件.png

探索方法

添加符号断点 alloc –> libobjc.A.dylib`_objc_rootalloc:

首先断点在alloc行, 同时下符号断点alloc(需要设置为disable,不然会断在NSObject的alloc)
截屏2021-06-06 下午4.30.57.png
然后通过commond + setp into调试获取
截屏2021-06-06 下午4.34.01.png

通过汇编 添加符号断点: objc_alloc

先通过 Xcode–>Debug–>Debug Workflow–>Always Show Disassembly 打开混编调试模式
截屏2021-06-06 下午4.36.07.png
然后通过单步调试(control + step into) 从而得知objc_alloc为下一步调用函数
截屏2021-06-06 下午4.45.31.png
添加objc_alloc符号断点
截屏2021-06-06 下午4.37.03.png
然后再次运行 获得下一个调用函数 _objc_rootAllocWithZone 而后再设置为符号断点. 按照这个步骤可以得出alloc的运行流程
截屏2021-06-06 下午4.47.57.png

源码如下:

1.alloc

    return _objc_rootAlloc(self);
}
复制代码

3.callAlloc

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));
}
复制代码

2._objc_rootAlloc

_objc_rootAlloc(Class cls)
{
    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
复制代码

4._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);
}
复制代码

5._class_createInstanceFromZone

_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 (zone) {
        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
    } else {
        obj = (id)calloc(1, size);
    }
    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);
}

复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享