美文网首页ios底层探索
ios isa的初始化&指向分析

ios isa的初始化&指向分析

作者: Jeffery_zc | 来源:发表于2020-01-08 15:02 被阅读0次

1.isa结构

在NSObject内会自带一个属性isa,这个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
};

其中,ISA_BITFIELD作为结构体所定义的参数的宏,在isa中扮演了重要的角色,我们可以看一下在x86架构下,ISA_BITFIELD内部结构是怎么样的。

# 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 指针开启指针优化,0:纯isa指针,1:不止是类对象地址,isa 中包含了类信息、对象的引用计数等
      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;                                         \  // magic:用于调试器判断当前对象是真的对象还是没有初始化的空间
      uintptr_t weakly_referenced : 1;                                         \  //指对象是否被指向或者曾经指向一个 ARC 的弱变量,没有弱引用的对象可以更快释放
      uintptr_t deallocating      : 1;                                         \   //标志对象是否正在释放内存
      uintptr_t has_sidetable_rc  : 1;                                         \  //当对象引用技术大于 10 时,则需要借用该变量存储进位
      uintptr_t extra_rc          : 8                      // 表示该对象的引用计数值,实际上是引用计数值减 1, 例如:如果对象的引用计数为 10,那么 extra_rc 为 9。如果引用计数大于 10, 则需要使用到下面的 has_sidetable_rc。
#   define RC_ONE   (1ULL<<56)
#   define RC_HALF  (1ULL<<7)

2.isa的初始化

当我们再探究alloc底层原理的时候,我们就会发现isa初始化的时候,类和对象是通过isa联系在一起。

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.cls = 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的结构我们认识到,!nonpointer是一个纯isa指针,存储着类、元类的地址,这种情况下我们直接进行class赋值就行了,而nonpointer的情况下,则是对isa做了一系列的优化,对isa里的联合体位域进行了初始化赋值。

3.isa 指向分析

1.对象和类

   LGPerson *person = [LGPerson alloc]
   NSLog(@"preson ==== %@",person);

我们在NSLog处设一断点,lldb调试打印person的指针地址:

(lldb) x/4gx person
0x100f3d800: 0x001d800100001529 0x0000000000000000
0x100f3d810: 0x0000000100f3d8e0 0x0000000100f3db20

0x001d800100001529其实就是isa,为什么这么说呢?我们知道,在内存里面,系统会对我们定义的属性做一次二进制重排,优化我们的内存,使我们的属性位置发生变化。但是,当前对象的第一个属性必然是isa,因为当前对象的属性isa是来自于继承,继承自NSObject,而这个时候当前的对象的属性列表还没有进行编译,所以说对象的第一个属性必然是isa。
通过isa的初始化,我们知道,如果0x001d800100001529是isa,那么isa必然会关联class。
了解runtime的同学都知道有这么一个API,object_getClass(id obj),源码如下:

Class object_getClass(id obj)
{
   if (obj) return obj->getIsa();
   else return Nil;
}
objc_object::getIsa() 
{
    if (!isTaggedPointer()) return ISA();
    //注:一般情况下都不是isTaggedPointer,一般都不走下面
    uintptr_t ptr = (uintptr_t)this;
    if (isExtTaggedPointer()) {
        uintptr_t slot = 
            (ptr >> _OBJC_TAG_EXT_SLOT_SHIFT) & _OBJC_TAG_EXT_SLOT_MASK;
        return objc_tag_ext_classes[slot];
    } else {
        uintptr_t slot = 
            (ptr >> _OBJC_TAG_SLOT_SHIFT) & _OBJC_TAG_SLOT_MASK;
        return objc_tag_classes[slot];
    }
}
objc_object::ISA() 
{
    assert(!isTaggedPointer()); 
#if SUPPORT_INDEXED_ISA  // 0
    if (isa.nonpointer) {
        uintptr_t slot = isa.indexcls;
        return classForIndex((unsigned)slot);
    }
    return (Class)isa.bits;
#else
    return (Class)(isa.bits & ISA_MASK);   //#define ISA_MASK    0x00007ffffffffff8ULL

#endif
}

从源码中很容易可以看出,对象是通过isa关联到了当前的类。下面我们通过lldb调试证实一下。首先,我们打印一下LGPerson.class的指针地址得到:

(lldb) p/x LGPerson.class
(Class) $12 = 0x0000000100001528 LGPerson

接着,通过源码objc_object::ISA() 方法接着调试:

(lldb) p/x 0x001d800100001529 & 0x00007ffffffffff8
(long) $13 = 0x0000000100001528
(lldb) po 0x0000000100001528
LGPerson

打印出来最后的结果正好是LGPerson,正好证明了0x001d800100001529就是isa,也证明了isa关联了对象和类。

2.类与元类

对象的isa在通过掩码ISA_MASK处理之后,就会指向类的内存空间,那么类呢?我们继续通过lldb调试来看一下:

(lldb) x/4gx LGPerson.class
0x100001530: 0x001d800100001509 0x0000000100afd140
0x100001540: 0x00000001003a0280 0x0000000000000000

接着,我们打印一下0x001d800100001509,

(lldb) p 0x001d800100001509
(long) $4 = 8303516107937033

发现竟然打印不出来,那么我们就来打印一下地址

(lldb) po 0x100001530
LGPerson
发现类的内存竟然是LGPerson,难道类里的指针又指回了自己吗?这肯定会出现问题的,所以肯定不是LGPerson。其实,类里的第一个内存指向的是元类。所以,对象的isa指向了类,类的isa指向了元类。那么元类呢?我们接着用lldb调试看一下 lldb调试.png
通过调试我们发现根元类的isa还是指向了根元类,由此我们可以理解到apple官方给出的isa的走位指向图。 isa流程图.png

相关文章

网友评论

    本文标题:ios isa的初始化&指向分析

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