iOS 底层探索- alloc流程

2020-09-05  本文已影响0人  Johnny_Z

一、 OC的alloc初探

代码准备,我们先对一个类alloc一个对象出来

 NSObject *objc = [NSObject alloc];

在这里下好断点,打开汇编调试(Debug->Debug workflow->Always Show Disassembly) 运行,

图1

我们可以看到下一条指令的汇编注释symbol stub for: objc_alloc得知立即调用存根符号为objc_alloc的函数,所以我们不妨增加一个objc_alloc的符号断点,接着运行可以看到

图2

所以可以看到在调用NSObject *objc = [NSObject alloc];时,其实调用的是objc中的objc_alloc方法。

二、OC 的 alloc 深入探究

经过上面一步,我们就能浅尝辄止吗?当然不能;所以我搞来一份objc4源码继续我们的探究。
看到源码 NSObject.mm 文件,里面的alloc方法

图3
他会调用_objc_rootAlloc
图4
static void 
fixupMessageRef(message_ref_t *msg)
{    
    msg->sel = sel_registerName((const char *)msg->sel);

    if (msg->imp == &objc_msgSend_fixup) { 
        if (msg->sel == @selector(alloc)) {
            msg->imp = (IMP)&objc_alloc;
        } else if (msg->sel == @selector(allocWithZone:)) {
            msg->imp = (IMP)&objc_allocWithZone;
        } else if (msg->sel == @selector(retain)) {
            msg->imp = (IMP)&objc_retain;
        } else if (msg->sel == @selector(release)) {
            msg->imp = (IMP)&objc_release;
        } else if (msg->sel == @selector(autorelease)) {
            msg->imp = (IMP)&objc_autorelease;
        } else {
            msg->imp = &objc_msgSend_fixedup;
        }
    } 
    else if (msg->imp == &objc_msgSendSuper2_fixup) { 
        msg->imp = &objc_msgSendSuper2_fixedup;
    } 
    else if (msg->imp == &objc_msgSend_stret_fixup) { 
        msg->imp = &objc_msgSend_stret_fixedup;
    } 
    else if (msg->imp == &objc_msgSendSuper2_stret_fixup) { 
        msg->imp = &objc_msgSendSuper2_stret_fixedup;
    } 
#if defined(__i386__)  ||  defined(__x86_64__)
    else if (msg->imp == &objc_msgSend_fpret_fixup) { 
        msg->imp = &objc_msgSend_fpret_fixedup;
    } 
#endif
#if defined(__x86_64__)
    else if (msg->imp == &objc_msgSend_fp2ret_fixup) { 
        msg->imp = &objc_msgSend_fp2ret_fixedup;
    } 
#endif
}

发现罅隙发送时,当msg->imp == &objc_msgSend_fixup 进行了方法实现(IMP)的替换。

什么时候msg->imp == &objc_msgSend_fixup成立还没有确定的探究,后续了解会进一步更新。从源码的运行看[NSObject alloc]会走到objc_alloc;但是其子类的[LGPerson alloc]会调用_objc_rootAlloc

不管是_objc_rootAlloc或者objc_alloc从图4看都会进入callAlloc这个函数,不过最后两个参数不同;我们姑且往下走,进入callAlloc去看看究竟。代码如下

图5

那么函数此时应来到_objc_rootAllocWithZone

unsigned 
class_createInstances(Class cls, size_t extraBytes, 
                      id *results, unsigned num_requested)
{
    return _class_createInstancesFromZone(cls, extraBytes, nil, 
                                          results, num_requested);
}

那么我们去看看_class_createInstancesFromZone

unsigned
_class_createInstancesFromZone(Class cls, size_t extraBytes, void *zone, 
                               id *results, unsigned num_requested)
{
    unsigned num_allocated;
    if (!cls) return 0;

    size_t size = cls->instanceSize(extraBytes);

    num_allocated = 
        malloc_zone_batch_malloc((malloc_zone_t *)(zone ? zone : malloc_default_zone()), 
                                 size, (void**)results, num_requested);
    for (unsigned i = 0; i < num_allocated; i++) {
        bzero(results[i], size);
    }

    // Construct each object, and delete any that fail construction.

    unsigned shift = 0;
    bool ctor = cls->hasCxxCtor();
    for (unsigned i = 0; i < num_allocated; i++) {
        id obj = results[i];
        obj->initIsa(cls);    // fixme allow nonpointer
        if (ctor) {
            obj = object_cxxConstructFromClass(obj, cls,
                                               OBJECT_CONSTRUCT_FREE_ONFAILURE);
        }
        if (obj) {
            results[i-shift] = obj;
        } else {
            shift++;
        }
    }

    return num_allocated - shift;    
}

这里的逻辑大体分为

size_t size = cls->instanceSize(extraBytes);
num_allocated = 
        malloc_zone_batch_malloc((malloc_zone_t *)(zone ? zone : malloc_default_zone()), 
                                 size, (void**)results, num_requested);

3、绑定isa到类

obj->initIsa(cls);    // fixme allow nonpointer

接下来返回对应类的首地址

三、总结

上一篇 下一篇

猜你喜欢

热点阅读