iOS进阶学习

OC对象原理(二)— isa指针详解

2019-12-28  本文已影响0人  小满豆

OC是一门面向对象的语言,上篇文章我们讲解了对象创建alloc的流程,知道了每个对象都有一个isa指针,那么我们接着上篇文章详细讲解一下isa初始化过程、isa内部结构和isa指向分析。

isa的初始化

通过上篇文章知道isa的初始化都会调用initisa方法,如下:

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); //创建isa

#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的结构

//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是一个联合体,包含了clsbits,因为是联合体,clsbits不会被同时赋值。cls存储的是对象的类信息,即!nonpointer直接设置clsbits则是一个位域ISA_BITFIELDISA_BITFIELD结构如下:

# 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;                                         \
      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)

ISA_BITFIELD的结构和系统有关,我们调试的环境是x86。ISA_BITFIELD的结构也就代表了isa的内部结构分布。

x/4gx十六进制输出对象内存前四个8字节内存,4代表前4个。
p/t二进制输出。
p/d十进制输出。
p/o八进制输出。
p/x十六进制输出。
>>3<<3 先右移三位,再左移三位。相当于把最右边三位删除,变为0。

从上图可以看出person对象的类信息存储在了isa的4->47位内存中,即shiftcls

isa的指向分析

其实object_getClass方法是通过掩码ISA_MASK来获取对象的类信息的。

//object_getClass获取对象类信息,最终会进入到这里
inline Class 
objc_object::ISA() 
{
    assert(!isTaggedPointer()); 
#if SUPPORT_INDEXED_ISA
    if (isa.nonpointer) {
        uintptr_t slot = isa.indexcls;
        return classForIndex((unsigned)slot);
    }
    return (Class)isa.bits;
#else
    //通过掩码获取isa的类信息
    return (Class)(isa.bits & ISA_MASK);
#endif
}

我们也可以通过ISA_MASK追踪一下isa的指向。
首先,我们创建LGPersonLGTeacher两个类,其中LGTeacher继承与LGPersonLGPerson继承与NSObject

创建类 创建LGTeacher对象,打断点。
断点 isa指向流程分析
isa指向分析
isa的指向和superclass指向流程图
isa&superClass指向图

总结

上一篇 下一篇

猜你喜欢

热点阅读