美文网首页
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 有什么区别

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

  • iOS - Runtime面试题

    1、alloc+init与new区别 从OC2.0开始没区别。需要注意的是数组类型的走的是 allocWithZo...

  • ListNode初始化

    ListNode * p = NULL;与ListNode * p = new ListNode();有什么区别用...

  • Python:__ new __ & __ init __ 的区

    1. 引言 new 和 init 的代码示例如下: name new 和 init 有什么区别呢,各自的作用又是什...

  • java之String类

    1,String s = "abc";和String s1 = new String("abc");有什么区别?答...

  • C++面试问题汇总

    C++语言本身问题 代码在内存中的分布都有哪些区,宏定义哪个区域?堆栈有什么区别?malloc和new有什么区别?...

  • C++编程:细说 new与 malloc 的 10 点区别

    前言 几个星期前去面试C++研发的实习岗位,面试官问了个问题: new与malloc有什么区别? 这是个老生常谈的...

  • 12. new

    new问题一:new 的原理是什么?通过 new 的方式创建对象和通过字面量创建有什么区别? 在调用new的过程中...

  • 1和new Number(1)有什么区别

    1和new Number(1)有什么区别author: @Tiffanysbear 总结,两者的区别就是原始类型和...

  • golang make和new有什么区别

    make、new操作 make用于内建类型(map、slice 和channel)的内存分配。new用于各种类型的...

网友评论

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

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