美文网首页iOS
iOS-底层原理2:alloc、init、new探析

iOS-底层原理2:alloc、init、new探析

作者: AcmenL | 来源:发表于2020-09-12 18:08 被阅读0次

    alloc、init、new是我们在开发过程中很常见的方法,但是我们并不是很清楚它内部做了些什么,这篇文章将通过苹果源码来研究下它们的底层实现。

    我们带着问题去探析:

    1、alloc方法做了些什么?
    2、init方法做了些什么?
    3、new 与 alloc init有什么区别?

    准备工作

    objc4-781项目

    alloc做了些什么?

    Person *objc = [Person alloc];
    

    step1:objc_alloc

    // Calls [cls alloc].
    id
    objc_alloc(Class cls)
    {
        return callAlloc(cls, true/*checkNil*/, false/*allocWithZone*/);
    }
    

    注意:第一步执行的并不是+ alloc类方法
    是怎么知道第一步调用这个方法的?
    通过iOS底层探索可以知道
    为什么第一步不是执行+ alloc
    移步[NSObject alloc]流程学习

    step2: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));
    }
    

    第一个if条件

    if (slowpath(checkNil && !cls))
    

    slowpath是一个宏

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

    这个宏使用了__builtin_expect(EXP, N)函数

    __builtin_expect(EXP, N)
    

    这个指令的意思是EXP==N的概率更大,即slowpath(x)表示x为0的可能性更大,即slowpath(x)为假

    第二个if条件

    if (fastpath(!cls->ISA()->hasCustomAWZ())) 
    

    cls->ISA()->hasCustomAWZ()为判断当前class或superclass 是否有自定义的alloc/allocWithZone 方法实现 ?
    如果cls->ISA()->hasCustomAWZ()返回YES,意味着有自定义的alloc/allocWithZone方法实现。这个值会存储在metaclass中。

    fastpath(x)表示x为1的可能性更大,即fastpath(x)为真。
    虽然这里是fastpath,但是对于初次创建的类是还没有默认的实现的。

    第三个if条件
    参数为false所以不会if条件

    所以继续向下执行进入到msgSend消息发送流程,调用alloc函数。

    这里说下几个比较重要的地方

    • ALWAYS_INLINE
      inline 是一种降低函数调用成本的方法,其本质是在调用声明为 inline 的函数时,会直接把函数的实现替换过去,这样减少了调用函数的成本。 是一种以空间换时间的做法。

    step3:alloc

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

    step4:_objc_rootAlloc

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

    step5:callAlloc
    step2
    此时已经在step3调用了alloc方法,所以if (fastpath(!cls->ISA()->hasCustomAWZ()))ture,所以进入_objc_rootAllocWithZone方法

    step6:_objc_rootAllocWithZone

    id
    _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)
    {
        // allocWithZone under __OBJC2__ ignores the zone parameter
        //zone 参数不再使用 类创建实例内存空间
        return _class_createInstanceFromZone(cls, 0, nil,
                                             OBJECT_CONSTRUCT_CALL_BADALLOC);
    }
    

    step7:_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;
        // 1:要开辟多少内存
        size = cls->instanceSize(extraBytes);
        if (outAllocatedSize) *outAllocatedSize = size;
    
        id obj;
        if (zone) {
            obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
        } else {
            // 2;怎么去申请内存
            obj = (id)calloc(1, size);
        }
        if (slowpath(!obj)) {
            if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
                return _objc_callBadAllocHandler(cls);
            }
            return nil;
        }
    
        // 3: ?
        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:计算所需内存大小

    流程:


    计算需要开辟内存的大小的执行流程图

    instanceSize

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

    instanceSize --> cache.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);
            }
        }
    

    fastInstanceSize --> align16

    //16字节对齐
    static inline size_t align16(size_t x) {
        return (x + size_t(15)) & ~size_t(15);
    }
    
    16字节对齐算法

    什么是16字节对齐?
    对象在开辟内存空间的时候以16字节的整数倍为开辟空间的大小。

    为什么要16字节对齐?

    • 通常内存是由一个个字节组成的,cpu在存取数据时,并不是以字节为单位存储,而是以块为单位存取,块的大小为内存存取力度。频繁存取字节未对齐的数据,会极大降低cpu的性能,所以可以通过减少存取次数来降低cpu的开销
    • 16字节对齐,是由于在一个对象中,第一个属性isa占8字节,当然一个对象肯定还有其他属性,当无属性时,会预留8字节,即16字节对齐,如果不预留,相当于这个对象的isa和其他对象的isa紧挨着,容易造成访问混乱
    • 16字节对齐后,可以加快CPU读取速度,同时使访问更安全,不会产生访问混乱的情况

    内存对齐原则

    • 数据成员对齐规则:struct 或者 union 的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员存储的起始位置要从该成员大小或者成员的子成员大小(只要该成员有子成员,比如数据、结构体等)的整数倍开始(例如int在32位机中是4字节,则要从4的整数倍地址开始存储)
    • 数据成员为结构体:如果一个结构里有某些结构体成员,则结构体成员要从其内部最大元素大小的整数倍地址开始存储(例如:struct a里面存有struct b,b里面有char、int、double等元素,则b应该从8的整数倍开始存储)
    • 结构体的整体对齐规则:结构体的总大小,即sizeof的结果,必须是其内部做大成员的整数倍,不足的要补齐

    以align(8) 为例,图解16字节对齐算法的计算过程


    • 首先将原始的内存 8size_t(15)相加,得到 8 + 15 = 23
    • size_t(15) 即 15进行~(取反)操作,~(取反)的规则是:1变为0,0变为1
    • 最后将 2315的取反结果 进行 &(与)操作,&(与)的规则是:都是1为1,反之为0,最后的结果为 16,即内存的大小是以16的倍数增加的
    calloc:申请内存,返回地址指针
    obj = (id)calloc(1, size);
    

    通过instanceSize计算的内存大小size,向内存中申请 大小 为 size的内存,并赋值给obj,因此 obj是指向内存地址的指针。

    我们可以在这行代码处打断点进行验证



    在执行这句代码后,obj指向了内存地址

    initInstanceIsa:类与isa关联

    initInstanceIsa
    objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
    {
        ASSERT(!cls->instancesRequireRawIsa());
        ASSERT(hasCxxDtor == cls->hasCxxDtor());
    
        initIsa(cls, true, hasCxxDtor);
    }
    

    initInstanceIsa --> initIsa

    inline void 
    objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) 
    { 
        ASSERT(!isTaggedPointer()); 
        
        if (!nonpointer) {
            isa = isa_t((uintptr_t)cls);
        } else {
            ASSERT(!DisableNonpointerIsa);
            ASSERT(!cls->instancesRequireRawIsa());
    
            isa_t newisa(0);
    
    #if SUPPORT_INDEXED_ISA
            ASSERT(cls->classArrayIndex() > 0);
            newisa.bits = ISA_INDEX_MAGIC_VALUE;
            // isa.magic is part of ISA_MAGIC_VALUE
            // isa.nonpointer is part of ISA_MAGIC_VALUE
            newisa.has_cxx_dtor = hasCxxDtor;
            newisa.indexcls = (uintptr_t)cls->classArrayIndex();
    #else
            newisa.bits = ISA_MAGIC_VALUE;
            // isa.magic is part of ISA_MAGIC_VALUE
            // isa.nonpointer is part of ISA_MAGIC_VALUE
            newisa.has_cxx_dtor = hasCxxDtor;
            newisa.shiftcls = (uintptr_t)cls >> 3;
    #endif
    
            // This write must be performed in a single store in some cases
            // (for example when realizing a class because other threads
            // may simultaneously try to use the class).
            // fixme use atomics here to guarantee single-store and to
            // guarantee memory order w.r.t. the class index table
            // ...but not too atomic because we don't want to hurt instantiation
            isa = newisa;
        }
    }
    
    

    综上alloc流程图

    alloc流程图

    [Person alloc] --> objc_alloc --> callAlloc --> + alloc --> objc_rootAlloc --> callAlloc --> _objc_rootAllocWithZone --> _class_createInstanceFromZone

    init函数

    从源码可以找到一个类方法、一个实例方法

    • + (id)init
    + (id)init {
        return (id)self;
    }
    

    我们来看探索一下

    LBHPerson *person = [LBHPerson init];
    

    这句代码运行直接crash, 提示cannot init a class object,那它用来做什么?

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

    我们来看探索一下

    LBHPerson *person = [[LBHPerson alloc] init];
    

    init --> _objc_rootInit

    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啥事没做,返回的是传入的self。

    new函数

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

    new 其实就等价于 [alloc init],但是一般开发中并不建议使用new,主要是因为有时会重写init方法做一些自定义的操作,例如 initWithXXX,会在这个方法中调用[super init],用new初始化可能会无法走到自定义的initWithXXX部分。

    相关文章

      网友评论

        本文标题:iOS-底层原理2:alloc、init、new探析

        本文链接:https://www.haomeiwen.com/subject/momlektx.html