美文网首页
alloc+init 与 new 有什么区别

alloc+init 与 new 有什么区别

作者: 初灬终 | 来源:发表于2019-10-14 11:47 被阅读0次
    + (id)alloc {
        return _objc_rootAlloc(self);
    }
    
    id _objc_rootAlloc(Class cls)
    {
        return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
    }
    
    + (id)new {
        //类方法的self是class
        return [callAlloc(self, false/*checkNil*/) init];
    }
    

    alloc+init与new出了callAlloc方法的第三个参数不一样,其他都一致。

    id callAlloc(Class cls, bool checkNil, bool allocWithZone=false)

    static ALWAYS_INLINE id
    callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
    {
        if (slowpath(checkNil && !cls)) return nil;
    
    #if __OBJC2__
        //#define RW_HAS_DEFAULT_AWZ    (1<<16)
        //有没有alloc/allocWithZone
        if (fastpath(!cls->ISA()->hasCustomAWZ())) {
            // No alloc/allocWithZone implementation. Go straight to the allocator.
            // fixme store hasCustomAWZ in the non-meta class and 
            // add it to canAllocFast's summary
    
            // 始终返回false,bool canAllocFast() { return false; }
            // fastpath,slowpath去掉不影响逻辑。fastpath表示大概率,slowpath表示小概率。
            // if...else...中标明大概率,小概率,编译器会优化这个判断逻辑。
            if (fastpath(cls->canAllocFast())) {
                // No ctors, raw isa, etc. Go straight to the metal.
                bool dtor = cls->hasCxxDtor();
                id obj = (id)calloc(1, cls->bits.fastInstanceSize());
                if (slowpath(!obj)) return callBadAllocHandler(cls);
                obj->initInstanceIsa(cls, dtor);
                return obj;
            }
            else {
                // Has ctor or raw isa or something. Use the slower path.
                id obj = class_createInstance(cls, 0);
                // 如果obj为nil,则回调内存分配错误
                if (slowpath(!obj)) return callBadAllocHandler(cls);
                return obj;
            }
        }
    #endif
    
        // No shortcuts available.
        if (allocWithZone) return [cls allocWithZone:nil];
        return [cls alloc];
    }
    

    callAlloc方法的第三个参数allocWithZone在方法实现中,根本没有用到过。传值false,true没有影响。

    allocWithZone

    //zone固定传值nil
    + (id)allocWithZone:(struct _NSZone *)zone {
        return _objc_rootAllocWithZone(self, (malloc_zone_t *)zone);
    }
    
    id
    _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone)
    {
        id obj;
    
    #if __OBJC2__
        // allocWithZone under __OBJC2__ ignores the zone parameter
        (void)zone;
        obj = class_createInstance(cls, 0);
    #else
        if (!zone) {
            obj = class_createInstance(cls, 0);
        }
        else {
            obj = class_createInstanceFromZone(cls, 0, zone);
        }
    #endif
    
        if (slowpath(!obj)) obj = callBadAllocHandler(cls);
        return obj;
    }
    

    class_createInstance(Class cls, size_t extraBytes)

    id 
    class_createInstance(Class cls, size_t extraBytes)
    {
        //zone参数固定传递 nil
        return _class_createInstanceFromZone(cls, extraBytes, nil);
    }
    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());
    
        // Read class's info bits all at once for performance
        bool hasCxxCtor = cls->hasCxxCtor();
        bool hasCxxDtor = cls->hasCxxDtor();
        bool fast = cls->canAllocNonpointer();
    
        size_t size = cls->instanceSize(extraBytes);
        if (outAllocatedSize) *outAllocatedSize = size;
    
        // zone为nil且可以馥佩非空内存空间
        id obj;
        if (!zone  &&  fast) {
            // 分配内存
            obj = (id)calloc(1, size);
            if (!obj) return nil;
            // 初始化isa指针等信息
            obj->initInstanceIsa(cls, hasCxxDtor);
        } 
        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);
        }
    
        if (cxxConstruct && hasCxxCtor) {
            obj = _objc_constructOrFree(obj, cls);
        }
    
        return obj;
    }
    

    总结

    OBJC2情况下,alloc+init 与 new一样,没有区别。

    相关文章

      网友评论

          本文标题:alloc+init 与 new 有什么区别

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