1. The nature of the object

Clang

  • Clang is aC + +C/C++/Objective-C/Objective-C++ lightweight compiler published under the LLVM BSD protocol license
  • Almost complete compatibility with the GNU C language specification, and adds additional syntax features such as C function overloading (which modifies functions with __attribute__(overloadable))
  • Clang will support its normal Iambda expressions, simplified handling of return types, and better handlingconstexprThe keyword
  • Clang command
    • Clang rewrite-objc main.m(object file) -o main.cpp(compiler file)Compile the object file into a C++ file
    • fatal error: ‘UIKit/ uikit. h’ file not found: clang-rewrite-objc-fobjc-arc-fobjc-Runtime = ios-13.0.0-isysroot / Applications/Xcode. App/Contents/Developer/Platforms/iPhoneSimulator platform/Developer/SDKs/iPhoneSimulator13.0. The SDK main.m

Xcrun

  • ‘Xcode’ is also installed with the ‘xcrun’ command, which is packaged with ‘Clang’ to make it easier to use

  • Xcrun -sdk iphonesimulator clang -arch x86_64 -rewrite-objc main.m -o main-arm64.cpp

  • Xcrun – SDK iphoneOS clang -arch arm64 -rewrite-objc main.m(object file) -o main-arm64.cpp(custom compiler file)

Nature of object

As you can see from the compiled file above, an object is essentially a structure

  • Underlying compiled content

2, structure, commonwealth (common body), bit domain

The structure of the bodystruct

  • All variables are coexisting
    • Advantages: comprehensive, can be assigned
    • Disadvantages: Memory space allocation is extensive, does not need to be fully allocated

A consortiumunion

  • The variables are mutually exclusive
    • Disadvantages: Only the most recently assigned variable works, the previous variable memory is no longer used as dirty memory
    • Advantages: More fine memory usage, saving memory space

A domain:n

  • Member followed by :n(n is the number of bits you want to occupy)

3, a pointerIsa

inline void 
objc_object::initIsa(Class cls)
{
    initIsa(cls, false, false);
}
Copy the code
inline void objc_object::initIsa(Class cls, bool nonpointer, UNUSED_WITHOUT_INDEXED_ISA_AND_DTOR_BIT bool hasCxxDtor) { ASSERT(! isTaggedPointer()); isa_t newisa(0); // if (! nonpointer) { newisa.setClass(cls, this); } else { ASSERT(! DisableNonpointerIsa); ASSERT(! cls->instancesRequireRawIsa()); #if SUPPORT_INDEXED_ISA ASSERT(cls->classArrayIndex() > 0); newisa.bits = ISA_INDEX_MAGIC_VALUE; // isa.magic is part of ISA_MAGIC_VALUE // isa.nonpointer is part of ISA_MAGIC_VALUE newisa.has_cxx_dtor = hasCxxDtor; newisa.indexcls = (uintptr_t)cls->classArrayIndex(); #else newisa.bits = ISA_MAGIC_VALUE; // isa.magic is part of ISA_MAGIC_VALUE // isa.nonpointer is part of ISA_MAGIC_VALUE # if ISA_HAS_CXX_DTOR_BIT newisa.has_cxx_dtor = hasCxxDtor; # endif newisa.setClass(cls, this); #endif newisa.extra_rc = 1; } // This write must be performed in a single store in some cases // (for example when realizing a class because other threads // may simultaneously try to use the class). // fixme use atomics here to guarantee single-store and to // guarantee memory order w.r.t. the class index table // ... but not too atomic because we don't want to hurt instantiation isa = newisa; }Copy the code
//isa_t isa union 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; public: #if defined(ISA_BITFIELD) struct { ISA_BITFIELD; // defined in isa.h }; bool isDeallocating() { return extra_rc == 0 && has_sidetable_rc == 0; } void setDeallocating() { extra_rc = 0; has_sidetable_rc = 0; } #endif void setClass(Class cls, objc_object *obj); Class getClass(bool authenticated); Class getDecodedClass(bool authenticated); };Copy the code

nonPointerIsa

  • Class Pointers are 8 bytes and 64 bits, which is too wasteful to store only addresses, so Apple optimizes memory by storing reference counts, weak, associated objects, whether they are being released, destructors, and other information in the 64 bit Pointers
  • nonpointorSaid:Whether to enable pointer optimization for ISA Pointers
    • 0: pure ISA pointer
    • 1: Not only class object address, ISA contains class information, object reference count, etc
  • has_assocSaid:Associated object flag bit0: no,1: exists
  • has_cxx_dtor: this objectIs there a destructor for C++ or ObjcIf there is a destructor, then the destructor logic is required. If there is no destructor, the object can be freed faster
  • shiftcls: Store the value of the class pointer. With pointer optimization enabled, 33 bits are used to store the class pointer in the ARM64 architecture
  • magic: used by the debugger to determine if the current object is a real object or if there is no initialization space
  • weakly_referenced: indicates whether an object is or has been referred to an ARC weak variable. Objects without weak variable references can be freed faster
  • deallocating: indicates whether the object is freeing memory
  • has_sidetable_rc: When the object reference count is greater than 10, the variable is borrowed to store the carry
  • extra_rcHas_sidetable_rc is used when the referential count value of this object is -1. If the referential count value is greater than 10, has_sidetable_rc is used

Isa derived Class

  • Object through its ISA with a maskISA_MASKDo the “and” operation to get the class information inside the pointer

  • After the memory is not released, the position is the same, so to getClass pointer shiftclBy performing bit operations on the pointer, the three bits on the right are cleared, and the 17 bits on the left are also cleared and restoredClass pointer shiftclAfter the position is obtained :(on the objectx/4gxView the memory 0x10068AC90Memory overview: 0x011d800100008475The first address is a pointer 0x0000000000000000Contents stored)

  • Isa pointer 64-bit specific distribution