preface

In the last article we studied the object of memory

So to summarize memory

LGPerson *person = [LGPerson alloc]; 1. Sizeof (person): this operator only returns the sizeof the type, not the actual memory because the isa structure pointer takes 8 bytes

2. Class_getInstanceSize ([LGPerson class]): Pass in a class object and return the minimum amount of memory required for an instance of the object

3. Malloc_size ((__bridge const void *)(person)): return the actual memory size allocated by the system. #import

is required

Now let’s look at the essence of the object what is the essence of the object

I. The essence of the object

1.clangWhat is?

Clang is a lightweight compiler for C, C++, and Objective-C. The source code is distributed under the BSD protocol. Clang will support its normal lambda expressions, simplified handling of return types, and better handling of constexpr keywords.

Clang is an Apple-led, LLVM-based C/C++/Objective-C compiler

As of April 2013,Clang has fully supported the C++11 standard and started implementing C++1y features (also known as C++14, the next minor update to C++). Clang will support its normal lambda expressions, simplified handling of return types, and better handling of constexpr keywords.

Clang is a C/C++/ objective-C/objective-C ++ compiler written in C++, based on LLVM and published under the LLVM BSD license. It is almost completely compatible with the GNU C specification (although there are some incompatibilities, including a slightly different compilation command option), and adds additional syntactic features such as C function overloading (modifying functions with __attribute__((overloadable)), Its goal (among other things) is to surpass GCC.

2. Use clang

Clang-rewrite-objc main.m -o main. CPP compiles object files into C++ files

UIKit is reporting an error

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

Xcode is installed with the xcrun command, which is a bit of a wrapper around clang to make it easier to use

Xcrun - SDK iphonesimulator clang -arch arm64-rewrite-objc main.m -o main-arm64.cpp (emulator)

Xcrun - SDK iphoneos clang -arch arm64 - rerewrite -objc main.m -o mainarm64.cpp

3. A clang

Into the projectmainFolder addingXWTestObjects are shown below

Enter the project through the terminal and run clang-rewrite-objc main.m -o main. CPP to generate the main. CPP file

View the main. CPP file source object in the underlying nature is the structure of the global search XWTest found here as shown in the figure below

The global search NSObject_IMPL is known as ISA

The essence of an object isobjc_objecttype

Id person why do we always write that

The operation of assigning the get value set by obtaining the first address of an object and offsetting it

2. A domain

1. Bit-field memory optimization For example, front:1 indicates that front occupies 1 bit

2. Mutually exclusive combinations coexist

All variables in a struct “coexist” — the advantage is that it is “tolerant” and comprehensive; The disadvantage is that struct memory space allocation is extensive, whether used or not, all allocation. In a union, the variables are mutually exclusive — the disadvantage is that they are not “inclusive”; Logic education, however, has the advantage of more subtle and flexible memory usage, which also saves memory space

Analysis of nonPointerIsa

The class we usually use is also a pointer 8 bytes *8 = 64 bits which is a huge waste of space. Apple optimizes to see if we are releasing the reference count. The destructor of the associated object will be determined by the bit field

Into theinitIsa

Enter the initIsa

Enter the isa_t

Click to enter

Nonpointer: indicates whether pointer optimization is enabled on the ISA pointer. 0: pure ISA pointer. 1: Isa contains more than the address of a class object

Has_assoc: associated object flag bit, 0 does not exist, 1 exists

Has_cxx_dtor: whether the object has a C++ or Objc destructor. If so, the destructor logic needs to be done. If not, the object can be released faster

Shiftcls: Stores the value of the class pointer. With pointer optimization enabled, 33 bits are used to store class Pointers in the ARM64 architecture

Magic: Used by the debugger to determine whether the current object is a real object or has no space to initialize

Weakly_referenced: Specifies whether an object is or has been referred to an ARC weak variable. Objects without a weak reference can be released faster.

Deallocating: indicates whether the object is freeing memory

Has_sidetable_rc: If the object reference technique is greater than 10, this variable needs to be borrowed to store carries

Extra_rc: When representing the object’s reference count, you’re actually subtracting the reference count by 1. For example, if the object’s reference count is 10, extra_RC is 9. If the reference count is greater than 10, the following has_sidetable_RC is used.

Iv. Isa pushes to class

The first address & mask address of the object gives the address and mask address of the class:define ISA_MASK 0x00007ffffffffff8ULL

Iv. Bit operations for ISA