Starting with the Alloc implementation process (1), I finally saw some underlying substantive source code, and then continued to explore the internal implementation of instanceSize.
Center of this paper:
Alloc
How much memory should be allocated for instance objects?- Based on the size of the class object, calculate the size to be cleared using the 16-byte alignment principle.
- Class objectWhat is the size of? What are the factors? How do you calculate that?
- The factor that affects the size of a class is the number of members/attributes inside it, not the method.
- In accordance with the principle of internal alignment, the OC layer ultimately calculates the class size by 8-byte alignment.
- Memory alignment principles for structs.
- This sentence can not be summarized, write the following. 👇 👇 🏻 👇 🏼 👇 🏾 👇 🏿
- Brief analysis of 8 – and 16-byte memory alignment reasons.
- Simple said: space for time, optimize reading speed, reduce CUP digestion, fault tolerance.
Theory to serve
1.1. Follow the instanceSize() process
Before executing calloc(), follow instanceSize to see how the internal methods work to align memory sizes.
Call againalloc
Before, it will execute firstrealizeClassWithoutSwift
Initializing the memory associated with the class object, which includessetFastInstanceSize
So this is thetacache
The size of the class in.(This will be a separate chapter to expand on)
fast
The key agent isalign16()
, according to the_flag
Pick up toClass objectThe size, in order to16-byte alignmentThe principle calculates the size to be opened.
Add the same algorithm in the figure below (align16-> objC-OS.h): essentially, x adds 15, then >>3 removes the low position,<<3 fills the bit to see if the high position can reach the next 16-byte alignment point of X, if it can reach it, it will rise, if it can’t reach it, it will remain the original size.
An 🌰 :
If x = 24, then x 16 byte alignment ladder is 32: first: x = 24, binary: 01 1000, then: x + 15, binary: 10 0111, left and right displacement: binary: 100 0000, decimal: 32 if again 😡 = 40, binary: 10 1000, calculate whether to rise?
1.2. Internal alignment of structures
These are the three pure theories. Class sizes are calculated using the 8-byte alignment principle based on the existing alignment principle in the structure.
1. Pure data member alignment
The first data member of a Struct or Union starts at 0, and then each data member I is stored at an address that is an integer multiple of the byte size of I itself.
2. Structure is nested and aligned
Struct (A) contains other struct (B) whose data members are stored at integer multiples of the largest element in the struct (B).
3. Total size of the structure
The total size of the structure (A) must be an integer multiple of the largest member of the structure (A), which is usually 8.
Such as principle 1+3: |
---|
Struct and StructOne although the internal member of the data type is exactly the same, but according to the calculation of the size of the gap is not the same. |
Like principle 2+3: |
---|
StructTwo members contain Struct, where the largest element of Struct a is 8 bytes, so the location of Struct E should start from 24. |
Practice to serve
1. Internal alignment of structures
To verify my big 🌰, run the code directly and see if the output is consistent with the theory.
The output of practice verifies the correctness of the theory.
2. Class size
The size of the class is calculated according to the internal alignment of the structure, but at the OC level a blank class will also have an ISA pointer of at least 8 bytes. I added a char to UIPerson, which should be 8+1, but after the alignment algorithm it should be 16. Let’s see what happens.
Well, the truth is that the algorithm uses macros, 64-bit, to differentiate between operating systems
#define WORD_MASK 7UL
3. Factors affecting class size
The factor that affects the size of a class is the number of members/attributes inside it, not the method. To test this claim, set up three scenarios to see how the class size changes.
- There’s only one attribute.
- Case 1 + 1 member variable.
- Case 2 + 1 instance method.
This is… And here it is. We don’t have to go into details, do we?
4. How much memory the instance object Alloc has
Do the UIPerson above a favor and verify that the instance object’s alloc memory size is calculated using the 16-byte alignment rule based on the size of the class object.
This trial is now a full loop, sinceinstanceSize
byinstanceSize
Finally, center — > theory — > practice, perfect ~
conclusion
Object size and memory alignment are clear, but the exploratory process also brings up some information about classes and ISA, which I will update in another post (which I will).
Collect resources
- All the image capture code is from
objc4(818)
In theobjc-runtime-new
andobjc-os
, the line number is shown in the figure. - Crazy Exploration of OC Bottom layer -Alloc Implementation process (I)