Class data structure

Clang compiler

Clang-rewrite-objc main.m -o main.cpp compiles the object file to c++ file xcrun-sdk iphonesimulator clang-arch x86_64-rewrite-objc main.m xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m

Clang-rewrite-objc-fobjc-arc-fobjc-runtime =ios-13.0.0. Clang-rewrite-objc-fobjc-runtime =ios-13.0.0 -isysroot / Applications/Xcode.app/Contents/Developer/Platforms/ IPhoneSimulator. Platform/Developer/SDKs/iPhoneSimulator13.0. SDK. The main m installed xcode when passing the xcrun command, command on the basis of the clang xcrun for some of the packaging, To better use some xcrun-sdk iphonesimulator clang-arch arm64-rewrite-objc main.m -o main-arm64.cpp (emulator) xcrun-sdk iphoneOS Clang-arch arm64-rewrite-objc main. m-o main-arm64.cpp (handset)

  • Compile main.m into main. CPP file to view the source code

  • Drag into the project and uncompile. run

Look at the main.cpp file

  • 1. Get the LGPerson class, which is essentially a LGPerson_IMPL structure

  • 2. Struct NSObject_IMPL is also a structure

  • 3. Inheritance of structure
struct LGPerson_IMPL { struct NSObject_IMPL NSObject_IVARS; NSString *_KCName; }; -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- into struct LGPerson_IMPL {Class isa; NSString *_KCName; };Copy the code

Class

  • Class is a struct pointer to objc_class *
  • Id is a structure pointer to objC_objct *

  • Get and set
static NSString * _I_LGPerson_KCName(LGPerson * self, SEL _cmd) { return (*(NSString **)((char *)self + OBJC_IVAR_$_LGPerson$_KCName)); } static void _I_LGPerson_setKCName_(LGPerson * self, SEL _cmd, NSString *KCName) { (*(NSString **)((char *)self + OBJC_IVAR_$_LGPerson$_KCName)) = KCName; } self and _cmd are hidden arguments, so a method is written with two hidden arguments by default, which is why we can write self. get directly in the scope of set and get and other methods:  return (*(NSString **)((char *)self + OBJC_IVAR_$_LGPerson$_KCName)); Self's pointer header + address offset gets the value, wrapped as NSString *Copy the code

A consortium

  • All the variables in a struct are “co-existing” — the advantage is that they are “tolerant”,
    • Comprehensive; The disadvantage is that the allocation of struct memory space is extensive, regardless of use, full allocation.
  • In a union, the variables are mutually exclusive — the disadvantage is that they are not “inclusive” enough.
    • But the advantage is that memory usage is more delicate and flexible, and also saves memory space

ISA ISA fine and flexible one that also saves memory space.

  • To learn about the Runtime, you need to understand some of its underlying commonly used data structures, such as isa Pointers
  • Before the ARM64 architecture, ISA was a plain pointer that stored the memory addresses of Class and meta-class objects
  • Starting with the ARM64 architecture, ISA has been optimized to become a common body (Union) structure, and bitfields are used to store more information
  • What is a union?

The source code to view

  • Find this isa which is not a class but an ISA_t

  • This isa_t isa union

  • Specifically defined in ISa.h

  • So these are the schemas – different schemas have different masks and different bit operations.

Isa details – bit fields

  • nonpointer
    • 0 represents a common pointer that stores the memory address of Class and meta-class objects
    • 1, represents optimized, using bitfields to store more information
  • has_assoc
    • Is the associated object set? If not, it will be released faster
  • has_cxx_dtor
    • Does C++ destructor (.cxx_destruct) exist? If not, the release will be faster
  • shiftcls
    • The arm64 architecture has 33 bits to store Pointers to Class objects in case nonpointer=1.
  • magic
    • Used during debugging to tell if an object has not been initialized
  • weakly_referenced
    • Is there a weak reference pointing to it? If not, it will be released faster. Weak reference table.
  • deallocating
    • Whether the object is being released
  • extra_rc
    • The value stored inside is the reference counter minus 1
  • has_sidetable_rc
    • Whether the reference counter is too large to be stored in isa
    • If it is 1, the reference count is stored in an attribute of a class called SideTable
  • So the conclusion is that if you really want to know about a class you don’t have to go to ISA, you have to go to Shiftcls in ISA,
  • Isa moves three places to the left and 20 places to the right to get shiftCLs data.

  • supplement

OC underlying ->isa, superclass and class structures

OC base -> ISA detail, bit operation, enumeration expansion.