iOS底层原理

iOS底层原理-005 isa

2021-05-10  本文已影响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存储的信息

图示

isa64情况

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

验证

总结

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

上一篇 下一篇

猜你喜欢

热点阅读