美文网首页
iOS底层探索--isa位域

iOS底层探索--isa位域

作者: spyn_n | 来源:发表于2021-09-29 17:00 被阅读0次

    我们知道isa在OC的底层随处可见,非常经典的一个图是isa的走位图,可查看这篇文章:
    iOS底层探索--OC对象的本质&类的底层结构
    iOS底层探索--类底层分析这部分的前期回顾

    目录

    一、 isa
    二、位域

    typedef struct objc_class *Class;
    typedef struct objc_object *id;
    
    @interface Object { 
        Class isa; 
    }
    
    @interface NSObject <NSObject> {
        Class isa  OBJC_ISA_AVAILABILITY;
    }
    
    struct objc_object {
    private:
        isa_t isa;
    }
    
    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
    }
    
    union isa_t 
    {
        isa_t() { }
        isa_t(uintptr_t value) : bits(value) { }
        Class cls;
        uintptr_t bits;
    }
    
    

    Isa初始化方法

    inline void 
    objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) 
    {  
        if (!nonpointer) {
            isa.cls = cls;
        } else {
            isa_t newisa(0);
            newisa.bits = ISA_MAGIC_VALUE;
            newisa.has_cxx_dtor = hasCxxDtor;
            newisa.shiftcls = (uintptr_t)cls >> 3;
            isa = newisa;
        }
    }
    

    __arm64__ 架构

    # 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)
    
    arm64位域图

    __x86_64__ 架构

    # 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)
    
    x86_64位域图
    • nonpointer:表示是否对 isa 指针开启指针优化 0:纯isa指针,1:不止是类对象地址,isa 中包含了类信息、对象的引用计数等

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

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

    • shiftcls:存储类指针的值。开启指针优化的情况下,在 arm64 架构中有 \color{#0000ff}{33} 位用来存储类指针,在__x86_64__架构中用\color{#0000ff}{44}位来存储类指针

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

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

    • deallocating:标志对象是否正在释放内存

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

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

    相关文章

      网友评论

          本文标题:iOS底层探索--isa位域

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