How does Clang compile object files into c++ files
The sample
What is the object?
The underlying nature of an object is a structure
The bottom layer of NSObject is objc_Object, so the bottom layer of ID is objc_Object *;
At the bottom of Class is objc_class *
The lower levels of the set and GET methods:
What is a bitfield?
- Let’s start with a structure that takes up 4 bytes of memory based on the alignment of the structure, but we could have used only 1 bit to store a Boolean value.
- So we can use bitfields, where the 1 in the diagram represents 1 bit, so that the whole structure is only 1 byte, why 1 byte? Since there is no half a byte, the structure is denoted as 0000 1111, which is a significant memory savings.
What is a union?
The structure of the union is shown in the figure. The member variables in the union are mutually exclusive. For example, when name is assigned, there is no data or dirty data in age and height, which can greatly save memory space on special occasions.
Advantages and disadvantages of structures and unions
Used in combination with unions and bitfields
It can save memory space, but is not recommended because it costs a lot of time. Therefore, you are advised to use it only in special cases.
InitIsa role
Binds the structure pointer to the current heap memory request to the class.
Isa underlying structure
1. Initialize Isa
2. InitIsa internal
3. It can be seen from the above that ISA is obtained through the initialization of ISA_T. Inside ISA_T, the union and structure are used in combination, as shown in the following figure
4. Therefore, the actual internal structure of ISA is in ISA_BITFIELD, as shown in the figure below
5. As can be seen from the figure above, isa mainly stores the following contents:
- Nonpointer: indicates whether pointer optimization is enabled for isa Pointers. 0 indicates pure ISA Pointers. 1 indicates that ISA contains not only the address of class objects but also the class information and reference count of objects.
- Has_assoc: flag bit of the associated object. 0 does not exist and 1 exists.
- Has_cxx_dtor: does the object have a destructor for c++ or Objc? If it has a destructor, the destructor logic needs to be done. If not, the object can be freed faster.
- Shiftcls: Stores the value of the class pointer; With pointer optimization enabled, there are 33 bits of storage 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: Indicates whether an object is pointed to or has been pointed to an ARC weak variable. Objects without weak references can be released faster.
- Deallocation: Indicates whether the object is freeing memory.
- Has_sidetable_rc: When the object reference count is greater than 10, it needs to borrow this variable to store carry.
- Extra_rc: When representing the reference count of this object, the reference count is actually subtracted by 1. For example, if the object’s reference count is 10, the extra_rc is 9. If the reference count is greater than 10, the following has_sideTABLE_rc is used.
Reverse class through ISA
Bit operation of isa address and mask
mask
Isa address
Do an operation
Bit operations are performed using information from the Isa consortium
Shiftcls stores class addresses, so you only need to move the ISA address 3 bits to the left, 20 bits to the right, and 17 bits to the left to get the class address, as shown in the figure below
The significance of the Init
The init method doesn’t actually do anything internally, it just provides an interface for you to expand, right
The meaning of the New
The new method is just alloc + init