Alloc core operation

The core operations are located in calloc methods

CLS ->instanceSize: Calculates the required memory size

The execution process for calculating the size of memory to be opened is shown below

  • 1. Jump toinstanceSizeSource code implementation
Const size_t instanceSize (size_t extraBytes) {/ / fast calculation memory size if the compiler (fastpath (cache. HasFastInstanceSize (extraBytes))) { return cache.fastInstanceSize(extraBytes); Size_t size = alignedInstanceSize() + extraBytes; // CF requires all objects be at least 16 bytes. // If (size < 16) size = 16; // CF requires all objects be at least 16 bytes. return size; }Copy the code

With breakpoint debugging, the cache.fastInstancesize method is executed to quickly calculate the memory size

  • 2. Jump tofastInstanceSizeSource code implementation, through breakpoint debugging, will be executed toalign16
size_t fastInstanceSize(size_t extra) const { ASSERT(hasFastInstanceSize(extra)); //Gcc's built-in function __builtin_constant_p is used to determine if a value is compile-time. If EXP is constant, the function returns 1. Otherwise return 0 if (__builtin_constant_p(extra) && extra == 0) {return _flags & FAST_CACHE_ALLOC_MASK16; } else { size_t size = _flags & FAST_CACHE_ALLOC_MASK; // remove the FAST_CACHE_ALLOC_DELTA16 that was added // by setFastInstanceSize // Delete FAST_CACHE_ALLOC_DELTA16 8 bytes added by setFastInstanceSize return align16(size + extra-fast_cache_alloc_delta16); }}Copy the code
  • 3. Jump toalign16Source code implementation, this method is16-byte alignment algorithm
// 16-byte alignment algorithm static inline size_t align16(size_t x) {return (x + size_t(15)) & ~size_t(15); }Copy the code

Memory byte alignment principles

Before explaining why 16-byte alignment is needed, it is important to understand the principles of memory byte alignment

  • Data member alignment rules: Struct or union data member, the first data member is placed at offset 0, and the starting position of each data member is an integer multiple of the size of the member or its children (as long as the member has children, such as data, structure, etc.) (for example, int is 4 bytes in 32-bit machines, Start with an integer multiple of 4.
  • Data members are structuresIf a struct contains a number of struct members, the struct members are stored from an integer multiple of the size of the largest element in the struct (e.g. char, int, double).
  • Global alignment rules for structures: Total size of the structure, i.esizeofThe result must be an integer multiple of its internal larger members, and the insufficiency must be made up

Why is 16-byte alignment needed

Byte alignment is required for several reasons:

  • Memory is usually made up of bytes, and the CPU does not store data in bytes, but in bytesblockIs the unit access, and the block size is the memory access strength. Frequent access to byte unaligned data will greatly reduce CPU performance, so can passReduced access timestoReduce CPU overhead
  • 16-byte alignment is due to the first property in an objectisaAccount for8Of course, an object must have other attributes. If there is no attribute, 8 bytes will be reserved, that is, 16 bytes will be aligned. If there is no attribute, it means that the ISA of this object and the ISA of other objects are next to each other, which will easily cause access confusion
  • After 16 bytes are aligned, yesSpeed up CPU read speedAnd at the same time makeMore secure access, there will be no access chaos

Byte alignment – Summary

  • In the byte alignment algorithm, alignment is mainlyobjectAnd the essence of the object is a struct objc_objectThe structure of the body.
  • The structure of the bodyIn memoryContinuous deposit, so you can use this to perform strong rotation on the structure.
  • In the early days of Apple8Byte alignment,nowis16-byte alignment