2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
isa
Pointer memory structureIn Objective-C,isa
Pointers are an important bridge between objects and classes. They not only help the runtime system identify the type of the object, but also participate in some memory and performance optimizations. This article will explain in depthisa
The memory structure of pointers, including its evolution in early and modern implementations.
isa
pointer?Every Objective-C object has a isa
A pointer to an object's class object. The class object itself is also an object.isa
The pointer points to a meta-class object. The meta-class object stores class methods, and itsisa
The pointer ultimately points to the root metaclass (usuallyNSObject
of the .
isa
Pointer structureIn early Objective-C implementations,isa
The pointer simply points to the structure of the class object. Here is a typical early implementation example:
struct objc_object {
Class isa; // 指向类对象的指针
};
typedef struct objc_class *Class; // Class 的本质是 objc_class 类型的结构体指针
struct objc_class {
Class isa; // 指向元类对象的指针
Class super_class; // 指向父类对象的指针
// 其他类相关的元数据
};
In this structure:
isa
Pointer to class object.isa
Pointer to the metaclass object.isa
Pointer to the root metaclass object.isa
Pointer structureOn 64-bit systems and modern Objective-C runtimes,isa
The pointer was redesigned into a more complex union (union isa_t
), which contains not only a pointer to the class object, but also other flags and information to optimize memory usage and performance. The following is isa_t
A simplified example of the structure:
union isa_t {
isa_t() { }
isa_t(uintptr_t value) : bits(value) { }
Class cls; // 指向类对象的指针
uintptr_t bits; // 包含位域信息的位模式
struct {
uintptr_t nonpointer : 1; // 是否启用优化的 non-pointer isa
uintptr_t has_assoc : 1; // 是否有关联对象
uintptr_t has_cxx_dtor : 1; // 是否有 C++ 析构函数
uintptr_t shiftcls : 33; // 类指针(经过位移和压缩)
uintptr_t magic : 6; // 调试用的魔数
uintptr_t weakly_referenced : 1; // 是否被弱引用
uintptr_t deallocating : 1; // 是否正在释放
uintptr_t has_sidetable_rc : 1; // 是否有辅助引用计数表
uintptr_t extra_rc : 19; // 额外的引用计数
};
};
isa
Whether it is a non-pointer type (optimize memory layout and store additional information).In early Objective-C implementations, reference counts were often stored directly on objects as part of the object structure. For example:
struct objc_object {
Class isa; // 指向类对象的指针
uintptr_t retainCount; // 引用计数
};
In modern Objective-C runtimes, reference counting is done via isa
The pointer optimization structure and Side Table auxiliary data structure are used for management.
isa
In the optimized structure of pointers, for exampleextra_rc
field.isa
When the pointer is within the range that can be represented, the reference count is stored in an auxiliary data structure called Side Table.isa
Advantages of pointersisa
In the pointer, access to other memory areas is reduced, improving performance.Although developers do not usually work directly with isa
Pointer interaction, but understanding its structure is helpful for debugging and optimizing performance. The following is an example of usage, which displays the type of an object by accessing the class information of the object:
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
MyClass *obj = [[MyClass alloc] init];
Class cls = object_getClass(obj);
NSLog(@"Class name: %s", class_getName(cls));
// 访问 isa 指针信息(需要通过运行时函数)
NSLog(@"isa pointer: %p", *(uintptr_t *)obj);
}
return 0;
}
isa
Pointers play an important role in the Objective-C runtime, from the early simple pointers to class objects to the modern complexisa_t
structure, which helps optimize memory usage and performance.isa
The evolution of pointers and memory structures can help us better understand the runtime mechanism of Objective-C and write efficient code.
I hope this article can help you gain a deeper understanding of Objective-C. isa
The memory structure of the pointer. If you have any questions or suggestions, please leave a message for discussion.