👉🏻
// isa.h#L68
# if __arm64__
# if TARGET_OS_EXCLAVEKIT
uintptr_t nonpointer : 1; // 0:普通指针,1: TaggedPointer 优化
uintptr_t has_assoc : 1; // 是否包含有关联引用
uintptr_t weakly_referenced : 1; // 是否有弱引用
uintptr_t shiftcls_and_sig : 61; //
# elif __has_feature(ptrauth_calls) || TARGET_OS_SIMULATOR
uintptr_t nonpointer : 1;
uintptr_t has_assoc : 1;
uintptr_t weakly_referenced : 1;
uintptr_t shiftcls_and_sig : 52;
uintptr_t has_sidetable_rc : 1;
uintptr_t extra_rc : 8
# else
uintptr_t nonpointer : 1;
uintptr_t has_assoc : 1;
uintptr_t has_cxx_dtor : 1; // 是否包含 C++析构函数或 OC 的 dealloc
uintptr_t shiftcls : 33;
uintptr_t magic : 6;
uintptr_t weakly_referenced : 1;
uintptr_t unused : 1;
uintptr_t has_sidetable_rc : 1;
uintptr_t extra_rc : 19
# endif
# elif __x86_64__
uintptr_t nonpointer : 1;
uintptr_t has_assoc : 1;
uintptr_t has_cxx_dtor : 1;
uintptr_t shiftcls : 44;
uintptr_t magic : 6;
uintptr_t weakly_referenced : 1;
uintptr_t unused : 1;
uintptr_t has_sidetable_rc : 1;
uintptr_t extra_rc : 8
# else
# error unknown architecture for packed isa
# endif
👉🏻
// objc-private.h#L83
union isa_t {
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
uintptr_t bits;
private:
// Accessing the class requires custom ptrauth operations, so force clients to go through setClass/getClass by making this private.
Class cls; // 访问类需要自定义 ptrauth 操作,将它设为私有,这样就可以强制客户端通过 setClass/getClass 来访问
}
👉🏻
// objc-private.h#L75
// 类对象指针
typedef struct objc_class *Class;
👉🏻
// objc-private.h#L76
// 实例对象指针
typedef struct objc_object *id;
👉🏻
// objc-private.h#121
// 类实例结构体定义
struct objc_object {
private:
char isa_storage[sizeof(isa_t)];
// reinterpret_cast : C++中的一种强制类型转换运算符,允许讲任意指针类型转化为其它类型指针
isa_t &isa() { return *reinterpret_cast<isa_t *>(isa_storage); }
const isa_t &isa() const { return *reinterpret_cast<const isa_t *>(isa_storage); }
}
👉🏻
// objc-runtime-new.h#L2254
// 类结构体定义
struct objc_class : objc_object {
// `= delete` : c++ 11 语法,禁止编译器生成构造函数、赋值运算符,保证 objc_class 操作安全性
objc_class(const objc_class&) = delete;
objc_class(objc_class&&) = delete;
void operator=(const objc_class&) = delete;
void operator=(objc_class&&) = delete;
// Class ISA;
Class superclass;
cache_t cache; // formerly cache pointer and vtable 缓存指针和虚函数表(优化性能)
class_data_bits_t bits; // class_rw_t * plus custom rr/alloc flags 存放类数据
class_rw_t *data() const { // 获取类数据(可读可写)
return bits.data();
}
const class_ro_t *safe_ro() const { // 获取的安全的只读数据
return bits.safe_ro();
}
}
👉🏻
// objc-runtime.mm#L204
// 通过类名获取 metaClass
/***********************************************************************
* objc_getMetaClass. Return the id of the meta class the named class.
* Warning: doesn't work if aClassName is the name of a posed-for class's isa!
**********************************************************************/
Class objc_getMetaClass(const char *aClassName)
{
Class cls;
if (!aClassName) return Nil;
cls = objc_getClass (aClassName); // 通过类名获取类对象
if (!cls)
{
_objc_inform ("class `%s' not linked into application", aClassName);
return Nil;
}
return cls->ISA(); // 返回指向 metaClass 的 isa 指针
}
👉🏻
// objc-runtime-new.h#L2005
// 类数据存放结构体
struct class_data_bits_t {
friend objc_class;
}
👉🏻
// objc-runtime-new.h#L1505
// 类只读结构体定义
struct class_ro_t {
uint32_t flags;
uint32_t instanceStart; // 实例内存开始位置
uint32_t instanceSize; // 实例内存大小
#ifdef __LP64__
uint32_t reserved;
#endif
union {
const uint8_t * ivarLayout;
Class nonMetaclass;
};
// 类名
explicit_atomic<const char *> name;
// 基础的方法列表
objc::PointerUnion<method_list_t, relative_list_list_t<method_list_t>, method_list_t::Ptrauth, method_list_t::Ptrauth> baseMethods;
// 基础的协议列表
objc::PointerUnion<protocol_list_t, relative_list_list_t<protocol_list_t>, PtrauthRaw, PtrauthRaw> baseProtocols;
// 实例变量列表
const ivar_list_t * ivars;
// 弱引用实例变量
const uint8_t * weakIvarLayout;
// 基础的属性列表
objc::PointerUnion<property_list_t, relative_list_list_t<property_list_t>, PtrauthRaw, PtrauthRaw> baseProperties;
}
👉🏻
// objc-runtime-new.h#L1873
// 类可读可写扩展结构体定义
struct class_rw_ext_t {
DECLARE_AUTHED_PTR_TEMPLATE(class_ro_t)
class_ro_t_authed_ptr<const class_ro_t> ro; // 指向只读类的指针
method_array_t methods; // 方法列表
property_array_t properties; // 属性列表
protocol_array_t protocols; // 协议列表
const char *demangledName;
uint32_t version;
};
👉🏻
// objc-runtime-new.h#L1883
// 类可读可写结构体定义
struct class_rw_t {
// Be warned that Symbolication knows the layout of this structure.
uint32_t flags;
uint16_t witness;
#if SUPPORT_INDEXED_ISA
uint16_t index;
#endif
explicit_atomic<uintptr_t> ro_or_rw_ext; // 原子操作指针,用于指向只读类或者类扩展
Class firstSubclass; // 第一个子类
Class nextSiblingClass; // 下一个兄弟类
private: // 联合指针,指向 class_ro_t 或者 class_rw_ext_t
using ro_or_rw_ext_t = objc::PointerUnion<const class_ro_t, class_rw_ext_t, PTRAUTH_STR("class_ro_t"), PTRAUTH_STR("class_rw_ext_t")>;
}
👉🏻
// objc-runtime-new.h#L2787
struct swift_class_t : objc_class {
uint32_t flags;
uint32_t instanceAddressOffset;
uint32_t instanceSize;
uint16_t instanceAlignMask;
uint16_t reserved;
uint32_t classSize;
uint32_t classAddressOffset;
void *description;
// ...
void *baseAddress() {
return (void *)((uint8_t *)this - classAddressOffset);
}
};