(Summary of articles on underlying principles of iOS)
(iOS)
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 implementation of Calloc, where the key code is malloc_zone_calloc at line 1713
- 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
- Where 'zone->calloc' passes in the zone as' default_zone 'from the previous step - the' purpose 'of this key code is to' request a pointer and return the pointer address 'copy codeCopy 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:
- Hold down the
control
+step into
And into thecalloc
Source code implementation of - And then through the LLDB command
p zone->callocde
Find the source code implementation, through printing thatzone->calloc
The source code is implemented indefault_zone_calloc
Method, and then search globallydefault_zone_calloc
Method to find the implementation
- Hold down the
-
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 pointer and just like PTR, there are two 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, see 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 codeCopy 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 the _nano_malloc_check_clear method and enter the segregated_next_block source code. This method is mainly about getting a pointer to memory
- 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!!