2019-01-29

2019-01-30  本文已影响5人  41c48b8df394

对象初始化流程
在对象初始化 一般都会调用alloc + init方法
alloc的关键函数调用

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

对象的创建的地方有两种方式,
一种是通过calloc开辟空间,然后通过initInstanceIsa函数初始化这块内存。
第二种是直接调用class_createInstance函数,由内部实现初始化逻辑

static ALWAYS_INLINE id
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
{
    if (slowpath(checkNil && !cls)) return nil;

#if __OBJC2__
    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
        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);
            if (slowpath(!obj)) return callBadAllocHandler(cls);
            return obj;
        }
    }
#endif

    // No shortcuts available.
    if (allocWithZone) return [cls allocWithZone:nil];
    return [cls alloc];
}

调用canAllocFast直接返回false所以只会执行上面的else代码块

 bool canAllocFast() {
        return false;
    }

初始化最终调用class_createInstance函数,class_createInstance函数的关键代码_class_createInstanceFromZone
下面代码会进入判断语句中,根据instanceSize函数返回size,听过calloc函数分配内存,并初始化isa_t指针。

id 
class_createInstance(Class cls, size_t extraBytes)
{
    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;

    id obj;
    if (!zone  &&  fast) {
        obj = (id)calloc(1, size);
        if (!obj) return nil;
        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;
}

instanceSize函数中,会通过alignedInstanceSize函数获取对象原始大小,在class_ro_t结构体中的instanceSize变量中定义。这个变量中存储对象实例化时,所有变量所在的内存大小,这个大小在编译器就已经决定了,不能运行时进行动态改变。

获取到size_t instanceSize后,对获取到的size进行地址对齐,需要注意的是。CF框架要求所有对象代销最少是16个字节,如果不够则直接定义为16个字节

   size_t instanceSize(size_t extraBytes) {
        size_t size = alignedInstanceSize() + extraBytes;
            // CF框架需要所有对象大小最少是16字节
        // CF requires all objects be at least 16 bytes.
        if (size < 16) size = 16;
        return size;
    }

initInstanceIsa;是很关键的一步,由于调用initIsa函数时,nonpointer字段传入是true,创建一个isa_t的结构体,将cls赋值给这个结构体

inline void 
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
    assert(!cls->instancesRequireRawIsa());
    assert(hasCxxDtor == cls->hasCxxDtor());

    initIsa(cls, true, hasCxxDtor);
}

inline void 
objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) 
{ 
    assert(!isTaggedPointer()); 
    
    if (!nonpointer) {
        isa.cls = 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;
    }
}

isTaggedPointer

总结

初始化调用进行创建实例对象调用栈
alloc + init
_objc_rootAlloc
callAlloc(Class cls, bool checkNil, bool allocWithZone=false)
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
initIsa(cls, true, hasCxxDtor);

上一篇下一篇

猜你喜欢

热点阅读