OC创建对象alloc流程图

2021-09-24  本文已影响0人  竖着走的大闸蟹

开局先上alloc流程图

alloc流程图.png

1.在项目Demo中:创建对象 [HSPerson alloc]将调用底层的alloc函数创建对象


+ (id)alloc {

return_objc_rootAlloc(self);

}

  1. alloc创建对象的时候会调用内部 _objc_rootAlloc函数
// Base class implementation of +alloc. cls is not nil.

// Calls [cls allocWithZone:nil].

id

_objc_rootAlloc(Class cls)

{

    return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);

}

3. 从_objc_rootAlloc进一步调用callAlloc函数、当前对象没有自定义的allocWithZone方法的话、会走快速创建方法;有自定义的allocWithZone方法,调用之后也会走 _objc_rootAllocWithZone方法

// Call [cls alloc] or [cls allocWithZone:nil], with appropriate

// shortcutting optimizations.

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())) {//hasCustomAWZ :自定义的allocWithZone

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

}

4.经过快速创建继续走 _objc_rootAllocWithZone方法、

NEVER_INLINE

id

_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)

{

    // allocWithZone under __OBJC2__ ignores the zone parameter

    return _class_createInstanceFromZone(cls, 0, nil,

                                        OBJECT_CONSTRUCT_CALL_BADALLOC);

}

5.而最终创建对象落在了 _class_createInstanceFromZone方法上

cls->instanceSize 计算需要的内存空间大小

calloc 向系统申请开辟内存,返回地址指针

obj->initInstanceIsa 关联到相应的类

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;

    //计算出需要的内存空间大小

    size = cls->instanceSize(extraBytes);

    if (outAllocatedSize) *outAllocatedSize = size;

    id obj;

    if (zone) {

        obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);

    } else {

    //向系统申请开辟内存,返回地址指针

        obj = (id)calloc(1, size);

    }

    if (slowpath(!obj)) {

        if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {

            return _objc_callBadAllocHandler(cls);

        }

        return nil;

    }

    //关联到相应的类

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

}

6、关联到相应的类过程

inline void

objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)

{

    ASSERT(!cls->instancesRequireRawIsa());

    ASSERT(hasCxxDtor == cls->hasCxxDtor());

    initIsa(cls, true, hasCxxDtor);

}

其他:zone存在时、走 obj->initIsa(cls) 方法

inline void

objc_object::initIsa(Class cls)

{

    initIsa(cls, false, false);

}

7、最终initIsa方法做了些赋值操作

inline void

objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor)

{

    ASSERT(!isTaggedPointer());

    //nonpointer 表示是否对isa指针开启指针优化 0 纯isa指针、1:不止是类对象地址,isa包含了类对象,对象的引用等

    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;

    }

}

8、在 initIsa方法中如果对指针开启指针优化、那么其中的涉及到联合体和位域、

union isa_t {//联合体

    isa_t() { }

    isa_t(uintptr_t value) : bits(value) { }

    Class cls;

    uintptr_t bits;

#if defined(ISA_BITFIELD)

    struct {//位域:告诉位置区域

        ISA_BITFIELD;  // defined in isa.h

    };

#endif

};

9、ISA_BITFIELD的定义在下方显示、uintptr_t定义为下

typedef unsigned long uintptr_t;

SUPPORT_PACKED_ISA 判断当前设备为arm64移动结构还是 x86_64电脑设备、这两类设备类型中无符号长整型 uintptr_t 对应的数据值相加均为 64

#if SUPPORT_PACKED_ISA

    // extra_rc must be the MSB-most field (so it matches carry/overflow flags)

    // nonpointer must be the LSB (fixme or get rid of it)

    // shiftcls must occupy the same bits that a real class pointer would

    // bits + RC_ONE is equivalent to extra_rc + 1

    // RC_HALF is the high bit of extra_rc (i.e. half of its range)

    // future expansion:

    // uintptr_t fast_rr : 1;    // no r/r overrides

    // uintptr_t lock : 2;        // lock for atomic property, @synch

    // uintptr_t extraBytes : 1;  // allocated with extra bytes

# if __arm64__

#  define ISA_MASK        0x0000000ffffffff8ULL

#  define ISA_MAGIC_MASK  0x000003f000000001ULL

#  define ISA_MAGIC_VALUE 0x000001a000000001ULL

#  define ISA_BITFIELD                                                      \

      uintptr_t nonpointer        : 1;                                      \

      uintptr_t has_assoc        : 1;                                      \

      uintptr_t has_cxx_dtor      : 1;                                      \

      uintptr_t shiftcls          : 33; /*MACH_VM_MAX_ADDRESS 0x1000000000*/ \

      uintptr_t magic            : 6;                                      \

      uintptr_t weakly_referenced : 1;                                      \

      uintptr_t deallocating      : 1;                                      \

      uintptr_t has_sidetable_rc  : 1;                                      \

      uintptr_t extra_rc          : 19

#  define RC_ONE  (1ULL<<45)

#  define RC_HALF  (1ULL<<18)

# elif __x86_64__

#  define ISA_MASK        0x00007ffffffffff8ULL

#  define ISA_MAGIC_MASK  0x001f800000000001ULL

#  define ISA_MAGIC_VALUE 0x001d800000000001ULL

#  define ISA_BITFIELD                                                        \

      uintptr_t nonpointer        : 1;                                        \

      uintptr_t has_assoc        : 1;                                        \

      uintptr_t has_cxx_dtor      : 1;                                        \

      uintptr_t shiftcls          : 44; /*MACH_VM_MAX_ADDRESS 0x7fffffe00000*/ \

      uintptr_t magic            : 6;                                        \

      uintptr_t weakly_referenced : 1;                                        \

      uintptr_t deallocating      : 1;                                        \

      uintptr_t has_sidetable_rc  : 1;                                        \

      uintptr_t extra_rc          : 8

#  define RC_ONE  (1ULL<<56)

#  define RC_HALF  (1ULL<<7)

# else

#  error unknown architecture for packed isa

# endif

// SUPPORT_PACKED_ISA

#endif

10、ISA_BITFIELD涉及的变量含义

nonpointer: 表示是否对指针开启指针优化、0:纯isa指针,1:不止是类对象地址,isa中包含了类信息,对象的引用计数等。

has_assoc: 关联对象标志位,0没有,1存在

has_cxx_dtor: 该对象是否有C++或者Objc的析构器,如果有析构函数,则需要做析构逻辑,如果没有,则可以更快的释放对象。

shiftcls: 存储类指针的值。开启指针优化的情况下,在arm64架构中有33位用来存储类指针。

magic: 用于调试器判断当前对象是真的对象还是没有初始化的空间。

weakly_referenced:标志对象是否被指向或者曾经指向一个ARC的弱变量,没有弱引用的对象可以更快释放。

deallocating: 标志对象是否正在释放对象。

has_sidetable_rc:当对象引用计数大于10时,则需要借用该变量存储进位

extra_rc: 当表示该对象的引用计数值,实际上是引用计数减1。如果对象的引用计数为10,那么extra_rc为9。如果引用计数大于10,则需要使用has_sidetable_rc。

上一篇 下一篇

猜你喜欢

热点阅读