美文网首页
alloc&init底层探索

alloc&init底层探索

作者: Keyon_9468 | 来源:发表于2019-11-05 14:43 被阅读0次

    [[NSObject alloc] init]两段式构造

    1.对象分配,方法有alloc何allocWithZone:

    流程图:

    image

    经过上面的一系列判断,过程最终是_class_createInstanceFromZone函数

    static __attribute__((always_inline)) 
    id
    _class_createInstanceFromZone(Class cls, size_t extraBytes, void *zone, 
                                  bool cxxConstruct = true, 
                                  size_t *outAllocatedSize = nil)
    {
        if (!cls) return nil;
    
        assert(cls->isRealized());
    
        bool hasCxxCtor = cls->hasCxxCtor();// 判断当前class或者superclass 是否有.cxx_construct构造方法的实现
        bool hasCxxDtor = cls->hasCxxDtor();// 判断判断当前class或者superclass 是否有.cxx_destruct析构方法的实现
        bool fast = cls->canAllocNonpointer();// 是对 isa 的类型的区分,如果一个类和它父类的实例不能使用isa_t 类型的 isa 的话,fast 就为 false,但是在 Objective-C 2.0 中,大部分类都是支持的
    
        // 获得分配的内存的大小
        size_t size = cls->instanceSize(extraBytes);
        if (outAllocatedSize) *outAllocatedSize = size;
    
        id obj;
        if (!zone  &&  fast) {
            obj = (id)calloc(1, size); // 分配内存空间,calloc( )函数会默认的把申请出来的空间初始化为0或者nil
            if (!obj) return nil;
            obj->initInstanceIsa(cls, hasCxxDtor); // 初始化Isa指针
        } 
        else {
            if (zone) {
                obj = (id)malloc_zone_calloc ((malloc_zone_t *)zone, 1, size);
            } else {
                obj = (id)calloc(1, size);
            }
            if (!obj) return nil;
    
            // Use raw pointer isa on the assumption that they might be 
            // doing something weird with the zone or RR.
            obj->initIsa(cls); // 初始化Isa指针
        }
    
        if (cxxConstruct && hasCxxCtor) {
            obj = _objc_constructOrFree(obj, cls);
        }
    
        return obj;
    }
    
    inline void 
    objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
    {
        initIsa(cls, true, hasCxxDtor);
    }
    
    inline void 
    objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) 
    { 
        if (!nonpointer) {
            isa.cls = cls;
        } else {
            isa_t newisa(0);
            newisa.bits = ISA_MAGIC_VALUE;
            newisa.has_cxx_dtor = hasCxxDtor;
            newisa.shiftcls = (uintptr_t)cls >> 3;
            isa = newisa;
        }
    }
    

    当我们通过alloc或者allocWithZone方法创建对象时做以下3件事情:
    1、分配内存,会遍历该对象所有的成员变量,通过成员变量的类型来计算所需占用的内存
    2、将该新对象的引用计数 (Retain Count) 设置成 1。
    3、将该新对象的 isa 成员变量指向它的类对象。
    4、将该新对象的所有其它成员变量的值设置成零。(根据成员变量类型,零有可能是指 nil 或 Nil 或 0.0)

    2、对象初始化,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;
    }
    

    _objc_rootInit 函数直接就将 obj 返回了,所以 -init 方法其实什么都没有做。看起来应该是个历史遗留问题,从上面对 +alloc 的分析也能看出,+alloc 把所有的工作都做完了

    3、new方法 new实际上是集alloc和init于一身,它创建了对象并初始化了对象。它的实现如下:
    // 源码
    + (id)new {
        return [callAlloc(self, false/*checkNil*/) init];
    }
    
    // 可以这么理解
    + (instancetype)new {
      return [[self alloc] init];
    }
    
    

    相关文章

      网友评论

          本文标题:alloc&init底层探索

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