美文网首页iOS底层原理
iOS底层原理-005 isa

iOS底层原理-005 isa

作者: 杨奇 | 来源:发表于2021-05-10 18:16 被阅读0次

我们知道OC对象都是结构体。那怎么验证呢

生成cpp文件验证

创建一个工程,创建一个Person类

@interface Person : NSObject
@property (nonatomic,strong)NSString *PersonName;
@end
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
    }
    return 0;
}

编译cpp文件的方法

1、clang clang -rewrite-objc xxx.m -o xxx.cpp
2、clang clang -rewrite-objc -fobjc-arc -fobjc-runtime=ios-13.0.0 -isysroot /
Applications/Xcode.app/Contents/Developer/Platforms/
iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.0.sdk xxx.m
3、xcrun xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 xxx.m

struct NSPersonNameComponents_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    NSString * _PersonName;
};

可以看到在底层PersonName变异成了结构体类型。验证成功

isa

探索alloc中已经知道obj->initInstanceIsa函数是在开辟空间后需要把开辟的空间和相关的类进行绑定,那么指针和类是怎么关联的呢?isa到底是什么结构?保存了什么信息?下面来一一解惑。

源码跟进到关联步骤

    if (!zone && fast) {
        obj->initInstanceIsa(cls, hasCxxDtor);
    } else {
        // Use raw pointer isa on the assumption that they might be
        // doing something weird with the zone or RR.
        obj->initIsa(cls);
    }

跟进obj->initInstanceIsa后有一个函数是initIsa

跟进initIsa源码

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 = 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
        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

在SUPPORT_INDEXED_ISA阶段就是cls和hasCxxDtor的关联
那么出现的newisa.bits,newisa.has_cxx_dtor,newisa.indexcls ,magic,nonpointer,shiftcls这些都是什么呢,继续跟进isa

isa定义

跟进 isa = isa_t((uintptr_t)cls);

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
};

1、可以看到isa是一个联合体union,包含成员cls, bits, 还有一个struct。联合体位域可以通过联合体位域了解
2、cls是一个结构体指针

typedef struct objc_class *Class;

3、 bits是 unsigned long

typedef unsigned long           uintptr_t;

4、这里ISA_BITFIELD就是一个位域,它有两个版本,分别对应arm64x86_64,即iOS和macOS:

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

# else
#   error unknown architecture for packed isa
# endif

// SUPPORT_PACKED_ISA
#endif

以arm64(真机)为例 isa存储的信息

  • nonpointer(存储在第0位):表示是否对isa 指针开启指针优化0:纯isa指针,1:不止是类对象地址,isa 中包含了类信息、对象的引用计数等。

  • has_assoc(存储在第1位):关联对象标志位,0没有,1存在。

  • has_cxx_dtor(存储在第2位):表示该对象是否有 C++ 或者 Objc 的析构器,如果有析构函数,则需要做析构逻辑, 如果没有,则可以更快的释放对象。

  • shiftcls(存储在第3-35位):存储类指针的值。开启指针优化的情况下,在 arm64 架构中有 33 位用来存储类指针。

  • magic(存储在第36-41位):用于调试器判断当前对象是真的对象还是没有初始化的空间。

  • weakly_referenced(存储在第42位):对象被指向或者曾经指向一个 ARC 的弱变量,没有弱引用的对象可以更快释放。

  • deallocating(存储在第43位):标志对象是否正在释放内存

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

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

  • 联合体采用内存覆盖机制,只有一块变量存储区,只能存一个变量的值,新的成员赋值会把原本存储的成员信息替换掉。也就是Class cls和uintptr_t bits只能set赋值其中一个。而内存使用的精细灵活,体现在以位域(即二进制中每一位均可表示不同的信息)存储成员数据,也就是以计算机二进制存储的方式,位bit为单位,用1和0标记数据,数据的尺寸大小是以占用多少bit,而不是以每个数据成员数据类型的尺寸大小(多少字节byte)存储。isa的bits占用的内存大小是8字节,即64位,可以存储足够多的信息,很大节省了内存。

图示

isa64情况

这样就验证了objc_object::initIsa中的关联属性。都是在于isa内部的信息进行绑定。

验证

  • 和alloc一样,我们在main.m中初始化MuPerson。由alloc-> _objc_rootAlloc -> callAlloc -> _objc_rootAllocWithZone > _class_createInstanceFromZone > initInstanceIsa > initIsa 到上面这份核心代码,打上断点,开始调试,我们来看系统是如何对isa的成员赋值。
  • 初始化isa指针,这里依据nonpointer来分别通过cls和bits对isa初始化,也从侧面验证了cls和bits互斥的原则,不能同时setter。
  • 断点过isa_t newisa(0),对isa初始化,到bits赋值之前,我们通过p打印newisa结果如下


  • 断点过newisa.bits = ISA_MAGIC_VALUE,对bits赋值之后,我们通过p打印newisa结果如下


  • 点到isa = newisa,对isa赋值之后,我们通过p打印newisa结果如下


  • 三次断点p打印的newisa结果做下对比如下


  • 通过几次断点,可以看到isa中的成员是如何赋值

总结

1、oc对象利用isa变量存储类对象指针
2、每一个对象都有isa指针。
3、 isa是一个union类型,利用了结构体位域技术,定义了多个数据段,除了class指针还存储额外信息,提高了空间利用率。

相关文章

网友评论

    本文标题:iOS底层原理-005 isa

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