美文网首页
iOS底层原理探索—isa与类的关联

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

作者: 十年开发初学者 | 来源:发表于2021-05-06 15:01 被阅读0次

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

联合体(union)

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

  • 结构体(struct)
    • 结构体是把不同的数据组合成一个整体,里面所有的变量是共存的,所有的变量都会分配内存
    • 假设结构体中有多个变量,但是只用到了其中一个,会造成内存的浪费
    • 但是结构体 容量大、包容性强,不会影响其他的变量
    • 结构体的内存是大于等于所有变量之和的(变量之间可能有间隙)
  • 联合体 (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

探索

  • 首先进入initInstanceIsa函数中
inline void 
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
    ASSERT(!cls->instancesRequireRawIsa());
    ASSERT(hasCxxDtor == cls->hasCxxDtor());

    initIsa(cls, true, hasCxxDtor);
}

  • 然后进入initIsa函数中
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
  • 根据以上两个断点,在给newisa.bits设置了ISA_MAGIC_VALUE x86下为0x001d800000000001ULL
    image.png
    由上图可知,ISA_MAGIC_VALUEcls的地址,并且lldb中打印发现与bits的值相同,由此我们断定newisa.bits = ISA_MAGIC_VALUE;是同时给bits与cls(默认值)赋值
image.png

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

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

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

  • 是因为shiftcls处于isa指针地址的中间部分,前面还有3个位域,为了不影响前面3个位域的数据,需要右移将其抹零

相关文章

网友评论

      本文标题:iOS底层原理探索—isa与类的关联

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