In the previous # OC low-level alloC process exploration article, we explored the alloc process, which covered a lot of ISA-related content that we will explore here

The way to create isa in the previous section is

inline void objc_object::initIsa(Class cls, bool nonpointer, bool hasCxxDtor) { ... isa_t newisa(0); . }Copy the code

isa_t

Union isa_t {isa_t() {} isa_t(uintptr_t value) : bits(value) {} uintptr_t bits; Struct {ISA_BITFIELD; #if defined(ISA_BITFIELD) // struct {ISA_BITFIELD; // defined in isa.h }; #endif };Copy the code

ISA_BITFIELD

# elif __x86_64__ # define ISA_MASK 0x00007ffffffff8ull 0 b0000000000000000011111111111111111111111111111111111111111111000 # define ISA_MAGIC_MASK x001f800000000001ull binary 0 as follows: 0 b0000000000011111100000000000000000000000000000000000000000000001 # define ISA_MAGIC_VALUE x001d800000000001ull binary 0 as follows: 0b0000000000011101100000000000000000000000000000000000000000000001 # define ISA_BITFIELD // // Uintptr_t nonpointer: 1; // Uintptr_t has_assoc: 1; Uintptr_t has_cxx_dtor: 1; // Store pointer to class object uintptr_t shiftcls: 44; /*MACH_VM_MAX_ADDRESS 0x7ffffFE00000 // Whether there is an associated weak pointer uintptr_t weakly_referenced: 1; // Whether to release the 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)Copy the code

Isa isa 64-bit, 8-byte union. Since the system is 8-byte aligned, the lower bits are 0, and Pointers to Class objects are stored from the fourth bit

Let’s verify that

Instance object, class object, metaclass object

The isa of the instance object stores a pointer to the Class object, and the ISA of the Class object stores a pointer to the metaClass object

The relationship between instance objects, class objects, and metaclass objects is shown as follows:

Simple summary

The version of ObjC2 we are currently using stores other information about ISA besides Pointers to class objects, such as whether there is an associated object, whether it is added to or released, reference counts, etc. In the older version isa is just a pointer and does not store any other information.

The ISA of the instance object stores Pointers to the class object, which is also an instance object. The ISA of the metaclass object stores Pointers to the root metaclass, and the ISA of the root metaclass stores Pointers to itself

We will explore the structure of classes in a later article

struct objc_class : objc_object { // Class ISA; Class superclass; // A pointer to a parent object... }Copy the code

After isa in the structure of a class is superclass, and the superclass pointer to the superclass points to the superclass, and the superclass pointer to NSObject points to nil.

Metaclasses also have inheritance relationships, subclass metaclasses have superclass Pointers to superclass metaclasses, superclass Pointers to root metaclasses, superclass Pointers to NSObject, NSObject’s parent class points to nil.

Now that we have a basic understanding of ISA, we will continue to explore other attributes of the class structure.

Refer to the article

Initialization of # ISA & pointing analysis