iOS-isa结构

1,344 阅读2分钟

对象在底层是一个struct,isa为Class类型,实为联合体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的位域(bit field)标志

 #   define ISA_BITFIELD                                                        \
        uintptr_t nonpointer        : 1; //是否对isa指针开启优化 0:纯isa指针 1:不仅类对象地址,还包含类信息,引用计数等\
        uintptr_t has_assoc         : 1; //关联对象标志位 0:没有 1:存在                                         \
        uintptr_t has_cxx_dtor      : 1; //是否有C++或Objc的析构器,有则需要做析构逻辑,无则可以更快释放对象\
        uintptr_t shiftcls          : 44; /*MACH_VM_MAX_ADDRESS 0x7fffffe00000*/ \//存储类指针的值。优化情况下arm64架构中有33位用来存储类指针
        uintptr_t magic             : 6; //用于调试器判断当前对象是真的对象还是没有初始化的空间                                        \
        uintptr_t weakly_referenced : 1; //标志对象是否被指向或曾经指向一个ARC的弱变量,没有弱引用的对象可以更快释放                                         \
        uintptr_t deallocating      : 1; //对象是否正在释放内存                                         \
        uintptr_t has_sidetable_rc  : 1; //当对象引用计数大于10时,则需要借用该变量存储进位                                        \
        uintptr_t extra_rc          : 8  //当表示该对象的引用计数值,实际上是引用计数值减一。大于256时 取一半存到计数表中...。
# if __arm64__
#   define ISA_MASK        0x0000000ffffffff8ULL     //isa掩码
#   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    //isa掩码
#   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