isa结构分析

什么是对象?

为了了解Objective-C类在底层会编译成什么,我们先新建一个类DebugPerson

测试类DebugPerson
@interface DebugPerson : NSObject
@property (nonatomic, copy) NSString *name;
@end

@implementation DebugPerson
@end
利用clang编译main.m文件

Clang是⼀个由Apple主导编写,基于LLVM的C/C++/Objective-C编译器。源代码发布于BSD协议下。Clang将⽀持其普通lambda表达式、返回类型的简化处理以及更好的处理constexpr关键字。

clang -rewrite-objc main.m -o main.cpp
编译结果

打开main.cpp后,搜索DebugPerson关键字,会发现类已经被编译成结构体DebugPerson_IMPL,其伪继承于NSObject_IMPLstruct NSObject_IMPL NSObject_IVARS等同于isa

得出结果 对象的本质其实就是一个结构体。

#ifndef _REWRITER_typedef_DebugPerson
#define _REWRITER_typedef_DebugPerson
typedef struct objc_object DebugPerson;
typedef struct {} _objc_exc_DebugPerson;
#endif

extern "C" unsigned long OBJC_IVAR_$_DebugPerson$_name;
struct DebugPerson_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    NSString *_name;
};

以及属性变量NSString *_name,和settergetter方法。

static NSString * _I_DebugPerson_name(DebugPerson * self, SEL _cmd) { 
    return (*(NSString **)((char *)self + OBJC_IVAR_$_DebugPerson$_name)); 
}
extern "C" __declspec(dllimport) void objc_setProperty (id, SEL, long, id, bool, bool);

static void _I_DebugPerson_setName_(DebugPerson * self, SEL _cmd, NSString *name) { 
     objc_setProperty (self, _cmd, __OFFSETOFIVAR__(struct DebugPerson, _name), (id)name, 0, 1); 
}
objc_setProperty
  • objc_setProperty的实现

    我们发现name的setter方法调用objc_setProperty函数,在objc源码中查找objc_setProperty,会判断是否需要copy后,调用reallySetProperty函数。

    void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy) 
    {
        bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
        bool mutableCopy = (shouldCopy == MUTABLE_COPY);
        reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
    }
    
  • reallySetProperty的实现

    reallySetProperty函数在做赋新值操作之后,要将oldValue释放掉。

    static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
    {
        if (offset == 0) {
            object_setClass(self, newValue);
            return;
        }
    
        id oldValue;
        id *slot = (id*) ((char*)self + offset);
    
        if (copy) {
            newValue = [newValue copyWithZone:nil];
        } else if (mutableCopy) {
            newValue = [newValue mutableCopyWithZone:nil];
        } else {
            if (*slot == newValue) return;
            newValue = objc_retain(newValue);
        }
    
        if (!atomic) {
            oldValue = *slot;
            *slot = newValue;
        } else {
            spinlock_t& slotlock = PropertyLocks[slot];
            slotlock.lock();
            oldValue = *slot;
            *slot = newValue;        
            slotlock.unlock();
        }
    
        objc_release(oldValue);
    }
    
isa的结构信息
联合体

  联合体也是由不同的数据类型组成,但其变量是互斥的,所有的成员共占一段内存。而且共用体采用了内存覆盖技术同一时刻只能保存一个成员的值,如果对新的成员赋值,就会将原来成员的值覆盖掉。

  • 缺点:包容性弱

  • 优点:所有成员共用一段内存,使内存的使用更为精细灵活,同时也节省了内存空间

isa_t

isa_t类型使用联合体通过char + 位域(即二进制中每一位均可表示不同的信息)进行内存优化。

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_t提供了两个初始化方法isa_t() { }isa_t(uintptr_t value) : bits(value) { }

  • isa_tclsbits是互斥的。

  • isa的结构体用于存储类信息及其他信息。

    arm64-isa

    - ```objectivec
      #   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
    

    x86_64 isa

     #   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
    
  • nonpointer

    表示是否对isa指针开启指针优化,0纯isa指针。1isa中包含了类信息、对象的引用计数等。

  • has_assoc

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

  • has_cxx_dtor

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

  • shiftcls

    存储类的指针的值

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

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

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

  • has_sidetable_rc
    表示当对象引用计数大于10时,则需要借用该变量存储进位

  • extra_rc
    额外的引用计数,实际上是引用计数值减1

isa的存储情况如图所示

isa64情况.jpeg

你可能感兴趣的:(isa结构分析)