OC底层-ISA的前生今世

2020-09-14  本文已影响0人  浅笑慕

在前面的OC底层-对象的alloc流程探究文章中,alloc
的流程中,我们知道了OC底层是通过initInstanceIsa把我们的类clsisa关联起来,我们顺着initInstanceIsa去对今天的主角isa一探究竟。

isa是什么

isa的类型

struct objc_object 
private:
    isa_t isa;

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

可以看到在objc_object结构体中,isaisa_t类型。同时我们找到了isa_t的定义。union是联合体,这里我们的isa显然就是联合体类型。

unionstruct

union

联合体(也称共用体)是由不同的数据类型组成,但其成员是互斥的,所有的成员共占一段内存。联合体采用了内存覆盖技术,同一时刻只能保存一个成员的值,如果对新的成员赋值,就会将原来成员的值覆盖掉。

struct

结构体是指把不同的数据组合成一个整体,其成员是共存的,成员不管是否使用,都会分配内存

两者的区别

isa_t 的成员分析

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


# 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

通过联合体结构体的特性,不难分析出isa_t采用联合体是基于内存优化的考虑。isa指针的内存大小是8字节,即64bit,也就是64位。isa通过char+位域的设计,巧妙的用二进制的每一位来存储和表示联合体成员的信息。

下面是x86_64macOS平台的isa_t的成员存储情况
[图片上传失败...(image-519ad0-1600051522675)]

isa做了什么

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

isa成员赋值

通过断点,我们清晰的看到isa中的成员是如何一步一步的被赋值。

isa关联

image

通过对isa成员赋值分析,isa和我们的关联关键在于isa中的成员shiftcls,即newisa.shiftcls = (uintptr_t)cls >> 3这句关键代码。这里>> 3是为了通过位运算把cls的信息准确的放在shiftcls的存储位置上。以这样一种巧妙的方式,就把isa关联起来。

isa是否和类关联的验证

(lldb) po cls
MuPerson

(lldb) po obj
<MuPerson: 0x1007076b0>

(lldb) x/4gx obj
0x1007076b0: 0x001d800100002255 0x0000000000000000
0x1007076c0: 0x0000000000000000 0x0000000000000000
(lldb) po 0x001d800100002255 & 0x00007ffffffffff8ULL
MuPerson

(lldb) 

object_getClass -> Class object_getClass -> inline Class objc_object::getIsa() -> inline Class objc_object::ISA()

inline Class 
objc_object::ISA() 
{
    ASSERT(!isTaggedPointer()); 
#if SUPPORT_INDEXED_ISA
    if (isa.nonpointer) {
        uintptr_t slot = isa.indexcls;
        return classForIndex((unsigned)slot);
    }
    return (Class)isa.bits;
#else
    return (Class)(isa.bits & ISA_MASK);
#endif
}
(lldb) p cls
(Class) $7 = MuPerson
(lldb) p/x cls
(Class) $8 = 0x0000000100002250 MuPerson
(lldb) p obj
(MuPerson *) $9 = 0x000000010192c440
(lldb) x/4gx obj
0x10192c440: 0x001d800100002255 0x0000000000000000
0x10192c450: 0x0000000000000000 0x0000000000000000
(lldb) p/x 0x001d800100002255 >> 3
(long) $11 = 0x0003b0002000044a
(lldb) p/x $11 << 20
(long) $12 = 0x0002000044a00000
(lldb) p/x $12 >> 17
(long) $13 = 0x0000000100002250
(lldb) 

为什么设计isa

isa走位流程图

这里放上业界经典isa走位流程图

image

objc_class & objc_object

struct NSObject_IMPL {
    Class isa;
};

typedef struct objc_class *Class;

struct objc_class : objc_object {
    // Class ISA;
    Class superclass;
    cache_t cache;             // formerly cache pointer and vtable
    class_data_bits_t bits;    // class_rw_t * plus custom rr/alloc flags

    class_rw_t *data() const {
        return bits.data();
    }
    void setData(class_rw_t *newData) {
        bits.setData(newData);
    }
    ///此处省略一万行
}


struct objc_object {
private:
    isa_t isa;
    ///此处省略一万行
}

贯穿全局的isa

上面我们微观的分析了isa做了什么,isa对象关联起来。宏观的看,isa贯穿了我们的类对象元类根元类。在OC底层系统为我们设计了objc_objectobjc_class,OC上层由NSObject作为根类持有isa特性。isa完美的将底层OC串联起来。

结语:万物皆对象,万物皆有isa

上一篇 下一篇

猜你喜欢

热点阅读