Technology Sharing

The isa in Objective-C is no longer a simple structure pointer

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Learn about Objective-C isa Pointer memory structure

In 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.

what is 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 .

Early isa Pointer structure

In 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; // 指向父类对象的指针
    // 其他类相关的元数据
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

In this structure:

  • Object isa Pointer to class object.
  • Class Object isa Pointer to the metaclass object.
  • Metaclass object isa Pointer to the root metaclass object.

modern isa Pointer structure

On 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; // 额外的引用计数
    };
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Structure field explanation

  • nonpointer:instruct isa Whether it is a non-pointer type (optimize memory layout and store additional information).
  • has_assoc: Whether the object has associated references (Associative References).
  • has_cxx_dtor: Whether the object has a C++ destructor and needs to call the destructor.
  • shiftcls: Class pointer, which stores the class information of the object (after displacement and compression).
  • magic: Magic number used for debugging and runtime verification.
  • weakly_referenced: Whether the object is pointed to by a weak reference.
  • deallocating: Whether the object is being released.
  • has_sidetable_rc: Whether the object's reference count is stored in the side table.
  • extra_rc: Additional reference counts to optimize memory usage.

Storage and management of reference counts

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; // 引用计数
};
  • 1
  • 2
  • 3
  • 4

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.

  • Inline Reference Counting: Some reference counting information is stored in isa In the optimized structure of pointers, for exampleextra_rc field.
  • Side Table: When the reference count exceeds 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.

Modern isa Advantages of pointers

  • Memory optimization: By storing more information (such as reference counts, flags) in isa In the pointer, access to other memory areas is reduced, improving performance.
  • Performance Improvements: Reduces memory read operations because more information can be obtained in one memory read.
  • Richer metadata: Can contain more runtime information, which helps improve runtime flexibility and efficiency.

Usage Examples

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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Summarize

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.