(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, 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 implementation of Calloc, where the key code is malloc_zone_calloc at line 1713

    • 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

- 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 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:

    • Hold down thecontrol + step intoAnd into thecallocSource code implementation of
    • And then through the LLDB commandp zone->callocdeFind the source code implementation, through printing thatzone->callocThe source code is implemented indefault_zone_callocMethod, and then search globallydefault_zone_callocMethod to find the 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 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 upNANO_MAX_SIZE, then proceednanozone_tthemalloc
    • Otherwise, proceedhelper_zoneprocess

  • Enter the _nano_malloc_check_clear source code, fold if else, see 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 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 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!!