iOS底层原理探索—isa与类的关联

2021-05-06  本文已影响0人  十年开发初学者

今天来分析下alloc中的3个核心中的initInstanceIsa方法(将内存地址与cls绑定),首先我们先了解下联合体

联合体(union)

构造数据类型的方式由两种

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

上面代码是isa的指针类型isa_t,该类型也是由联合体来定义的,这里使用联合体主要还是考虑到内存优化。这里的内存优化是指isa指针通过char + 位域(二进制中的每一位均可表示不同的信息)的原理来实现,isa指针大小为8字节,即64位,这64位足以存放类的信息了

由以上代码可以发现,里面有一个结构体,而这个结构体中的内容如下

# 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;                                         \   //是否对isa指针开启优化
      uintptr_t has_assoc         : 1;                                         \  //是否有关联对象
      uintptr_t has_cxx_dtor      : 1;                                         \//是否有 c++相关实现
      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)

这代码中分为两种架构,一种是arm64即ios真机或者是M1芯片的模拟器,另一种是x86 即:非M1芯片的模拟器。其中: 后面的数字代表占用几位,像shiftclsarm64中占用33位,在x86中占用44

探索

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 = 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
        // bits 赋值为 0x001d800000000001
        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;
    }
}

image.png
image.png image.png

当执行到newisa.shiftcls = (uintptr_t)cls >> 3;这行代码时,发现newisa中的cls与shiftcls发生了变化。也就是这一步完成了指针与内存地址的绑定。

image.png
cls是LGperson类
shiftcls中本来存放的就是类信息

至于shiftcls为什么需要右移3位

上一篇 下一篇

猜你喜欢

热点阅读