1. LLVM optimization

In the source code, the alloc class method is implemented as _objc_rootAlloc(self), but when it is executed, the system will execute one before _objc_rootAllocobjc_allocMethod, why is that?

In objc-runtime-new.mm, static size_t in _read_images Static void fixupMessageRef(message_ref_t * MSG) The reason is that Apple thinks some methods such as alloc are special because they do some operations on memory, so for security reasons, so before alloc, in LLVM compilation, we replace it with apple’s objc_alloc method, and after the call is completed, we go to the callAlloc method shown in the figure above

/************ * fixupMessageRef * Repairs an old vtable dispatch call site. Fix old Vtable scheduling call site. * vtable dispatch itself is not supported. Vtable scheduling itself is not supported. * * * * * * * * * * * * /Copy the code

2. Object memory

3. Internal alignment of the structure

Memory alignment principles

  1. Data member alignment rules:Struct or unionThe first data member is placed at offset 0 afterThe starting location of each data member store starts with an integer multiple of the size of the member or its children (such as arrays or structures)(If int is 4 bytes, start with an integer multiple of 4.)
  2. Struct as members: If a struct has some struct members, thenStructure members are stored from an integer multiple of the size of the largest element inside them(struct B: char, int, double) (struct B: char, int, double)
  3. The total sizeof a structure: the result of sizeof must be an integer multiple of the largest member within the structure

4, malloc

Let’s start by looking at a few methods to get the various memory sizes of a class

We getPointer to theThe size,The class instanceThe size andmalloc_size()Size, but where does malloc_size() come from? Let’s run the Malloc source code and write a simple internal analysis:

Print zone_calloc again to get the image abovenano_calloc, continue searching

The slot_bytes size is passed abovesegregated_size_to_fitMethod, jump to view:

  • Object memory alignment

So we know that the object will16 bytes aligned to multiples of 16

conclusion

  • The alloc underlying method is hooked and then redirected by the method

  • Member variables are 8 bytes memory aligned

  • Objects are 16-byte memory aligned (objects generally have member variables,NSObject:8 + member variables :n > 8, so 8-byte alignment is not appropriate, and space is safer)

  • Segregated_next_block: The whole process basically means that you iterate over and over looking for a space that can fit the desired size, and return the space address if found, or zero if not

  • Sizeof: an operator that takes the sizeof a type (int, size_t, structure, pointer variable, etc.). These values are converted to constants when the program is compiled and are retrieved directly at runtime

  • Class_getInstanceSize: is a function (called to create extra memory) that is retrieved at runtime and calculates the size of the class (at least as large as it needs to be)

    • The size of the memory required to create the object
    • Regardless of malloc functions, memory alignment is generally [8] aligned
    • #import <objc/runtime.h>
  • Malloc_size: heap space The size of memory allocated to an object

    • The malloc function on Mac and iOS always allocates a multiple of [16]