IOS underlying principles + reverse article summary
In iOS- Underlying Principles 02: Alloc & Init & New source code analysis article, there are three core operations in alloc, one of which is calloc, which is allocating memory, and that’s what we need to explore today, but the essence of the exploration is to verify that the actual alignment of objects in iOS is 8 bytes
Analyze the calloc source code in objC4
- First from alloc into the objc source code, find
obj = (id)calloc(1, size);
Operation, the order of methods involved isalloc --> _objc_rootAlloc --> callAlloc --> _objc_rootAllocWithZone --> _class_createInstanceFromZone
To explore Calloc here, you need to switch to the libmalloc source code, which is available here to download the latest version, and move on
Libmalloc calloc source analysis
- Define a compilable target in compilable libmalloc, to be used in main
calloc
Create a pointer
- Enter the source code of Calloc, where the key code is
1713 line malloc_zone_calloc
- Among them
default_zone
Is a default zone whose purpose is to boot the program into a create real zonezone
The process of
- Among them
- Enter the
malloc_zone_calloc
The key code is 1441 lineszone->calloc
- the zone passed in by 'zone->calloc' is' default_zone 'from the previous step - the purpose of this key code is to' request a pointer and return the address of the pointer 'Copy the code
- When entering
zone->alloc
Source code, found is acalloc
At this point, the source code can not continue to follow up
So here comes the point!! To follow up the source code, you can:
-
PTR = zone->calloc(zone, num_items, size); , add a breakpoint, and run
-
If you want to enter the zone->calloc source code, there are two ways:
-
Press control + step into, enter the source implementation of Calloc
-
Then through the LLDB command p zone->callocde to find the source implementation, through the print that zone->calloc source implementation in default_zone_calloc method, and then global search default_zone_calloc method, find the specific implementation
-
- Enter the source code of Calloc, which is mainly operated by two parts
- Create real
zone
, i.e.,runtime_default_zone
methods - Use real
zone
forcalloc
- Create real
The breakpoint is in the zone. It is not possible to run the LLDB command p zone->alloc because zone has not been assigned
Validation where zone is not assigned
- Enter the
runtime_default_zone
Source code implementation of
- Enter the
inline_malloc_default_zone
Source code implementation, through the viewmalloc_zones
The value discovery of isNULL
So, we can get theta at this pointZone has not been assigned
Continue tracking source code
- Go back to
default_zone_calloc
Method, continue to execute, break inzone->calloc
In this case, you can enter the source code implementation of Calloc through either of the above methodsnano_calloc
- Enter the
nano_calloc
Method, where the key code is 878, where p is pointerPointer to the
As with the previous PTR, there are two main parts of logic- If I have less space to open up
NANO_MAX_SIZE
, then proceednanozone_t
themalloc
- Otherwise, proceed
helper_zone
process
- If I have less space to open up
- Enter the
_nano_malloc_check_clear
Source code, fold if else, look at the main process- Among them
segregated_next_block
The pointer memory opening algorithm is to find the appropriate memory and return slot_bytes
It’s the encryption algorithmsalt
(The idea is to make encryption algorithms more secure, and it’s essentially a string of custom numbers.)
- Among them
- Enter the
segregated_size_to_fit
Encryption algorithm source code, through algorithm logic, you can see that its nature will be 16 byte alignment algorithm
#define SHIFT_NANO_QUANTUM 4 #define NANO_REGIME_QUANTA_SIZE (1 << SHIFT_NANO_QUANTUM) // 16 static MALLOC_INLINE size_t segregated_size_to_fit(nanozone_t *nanozone, size_t size, size_t *pKey) { size_t k, slot_bytes; //k + 15 >> 4 << 4 -- k + 15 << 4 -- k + 15 << 4 -- k + 15 << 4 -- k + 15 << 4 -- k + 16 * 16 If (0 == size) {size = NANO_REGIME_QUANTA_SIZE; // Historical behavior } k = (size + NANO_REGIME_QUANTA_SIZE - 1) >> SHIFT_NANO_QUANTUM; // round up and shift for number of quanta slot_bytes = k << SHIFT_NANO_QUANTUM; // multiply by power of two quanta size *pKey = k - 1; // Zero-based! return slot_bytes; }Copy the code
This algorithm has been mentioned at the end of ios-Underlying Principle 05: Memory Alignment principle, and will not be explained here
- Go back to
_nano_malloc_check_clear
Method, entersegregated_next_block
Source code, this method is mainlyGet memory pointer
- But if it’s the first time
segregated_next_block
Function, the band does not exist, the cache will not exist, so it will be calledsegregated_band_grow
To break new groundband
- But if it’s the first time
- Enter the
segregated_band_grow
Source code, mainly to open up new bands
First record libmalloc source code malloc analysis ideas, need time to study the source code, later to complement perfect!!
Refer to the link
- The Beauty of iOS Advanced (vi) — Malloc analytics