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, findobj = (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 maincallocCreate a pointer

  • Enter the source code of Calloc, where the key code is1713 line malloc_zone_calloc
    • Among themdefault_zoneIs a default zone whose purpose is to boot the program into a create real zonezoneThe process of

  • Enter themalloc_zone_callocThe 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 enteringzone->allocSource code, found is acallocAt 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 realzone, i.e.,runtime_default_zonemethods
    • Use realzoneforcalloc

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 theruntime_default_zoneSource code implementation of

  • Enter theinline_malloc_default_zoneSource code implementation, through the viewmalloc_zonesThe value discovery of isNULLSo, we can get theta at this pointZone has not been assigned

Continue tracking source code

  • Go back todefault_zone_callocMethod, continue to execute, break inzone->callocIn this case, you can enter the source code implementation of Calloc through either of the above methodsnano_calloc

  • Enter thenano_callocMethod, where the key code is 878, where p is pointerPointer to theAs with the previous PTR, there are two main parts of logic
    • If I have less space to open upNANO_MAX_SIZE, then proceednanozone_tthemalloc
    • Otherwise, proceedhelper_zoneprocess

  • Enter the_nano_malloc_check_clearSource code, fold if else, look at the main process
    • Among themsegregated_next_blockThe pointer memory opening algorithm is to find the appropriate memory and return
    • slot_bytesIt’s the encryption algorithmsalt(The idea is to make encryption algorithms more secure, and it’s essentially a string of custom numbers.)

  • Enter thesegregated_size_to_fitEncryption 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_clearMethod, entersegregated_next_blockSource code, this method is mainlyGet memory pointer
    • But if it’s the first timesegregated_next_blockFunction, the band does not exist, the cache will not exist, so it will be calledsegregated_band_growTo break new groundband

  • Enter thesegregated_band_growSource 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