Alloc bottom layer exploration – early

First of all have to say, it is the first time to explore the underlying code of apple, before all the oc development, is based on the function of the visible xcode that occasionally see the underlying code, also think big, and had as an IOS industry for 5 or 6 years, apparently couldn’t keep up with rhythm, learning is urgent.

Let’s do a simple test to verify the underlying pointer. The code is as follows:

    LGPerson *p1 = [LGPerson alloc];
    LGPerson *p2 = [p1 init];
    LGPerson *p3 = [p1 init];
    
    LGPerson *p4 = [LGPerson alloc];
    
    NSLog(@"%@-%p-%p",p1,p1,&p1);
    NSLog(@"%@-%p-%p",p2,p2,&p2);
    NSLog(@"%@-%p-%p",p3,p3,&p3);
    NSLog(@"%@-%p-%p",p4,p4,&p4);
Copy the code

The following output is displayed:

It can be seen from the above test:

  • p1,p2,p3three-objectThe object addresses are consistent.Pointer addresses are inconsistent
  • p4withp1,p2,p3Of these three objectsObject address and pointer address are inconsistent

Why is that? We can see that from the chart below

Conclusion:

  • allocHas the function of opening up memory, andinitNo new memory is created
  • Stack memory address opened fromhightolow, the heap’s memory is opened fromlowtohigh

Alloc underlying exploration – methods

Today, I learned several ways to view the underlying source code, which is a simple object’s alloc method source location view.

Method one, the symbol breakpoint location method

  1. Next sign breakpoint of alloc, but close first

  2. The symbol breakpoint is opened when the code alloc is executed

  3. If you continue, you will see the libobjc.a.dylib source library where the alloc method resides

Control +step into

  1. Add a breakpoint to the alloc code

  2. After executing the breakpoint, hold down CTRL and click Step into xcode to enter the thread. You can see that objC-alloc is executed

  3. The symbol breakpoint of objc-alloc below

  4. After continuing, you can see the libobjc.a.dylib source library where the alloc method resides

Method three, assembly view

  1. Open assembly (Debug->Debug WorkFlow -> Always show disasesmbly)

  2. At the point of the alloc code breakpoint, after the assembly, you can see the objC-alloc method execute, at this time copy the objC-alloc next symbol breakpoint

  3. If you continue, you will see the source code location

Alloc low-level exploration – source code

See the source code in the library, open source address: apple opensource.apple.com/tarballs/ library file libobjc. A. d. ylib, search objc can find the source code, there are multiple versions, to find the latest download check, the results are as follows:

According to the previous complaint, we can obtain a copy of Apple’s underlying OBJC source code, the specific source code and the source code can be compiled and configured to supplement later

Alloc low-level exploration – Alloc flow

Using the previous exploration method and the underlying code to track the alloc process, the alloC process can be known as follows:

According to the process follow-up, alloc is mainly used to open up memory space, and the core code is as follows:

CLS ->instanceSize: How much memory to set up

Calloc: How do you create space

Obj ->initInstanceIsa: associate class and open pointer address

Follow up the instanceSize method

Finally we execute the method ALIGN16. Align16 is actually an algorithm, a hexadecimal alignment algorithm: for example, the x value we pass in is 40

Size_t (15) is 15.

=> (x + size_t(15)) & ~size_t(15)

=> (40 + 15) & ~15

= > 55 & ~ 15

Binary representation of 15:0000 0000 1111

1111 1111 1111 0000

Binary representation of 55:0000 0000 0011 0111

Result: 0000 0000 0011 0000 => multiple of 16, that is, 16-byte alignment

In this case, the result is hexadecimal alignment.

So why hexadecimal alignment?

Answer: This is space for time ~

A byte is a unit of memory, and 1 memory is also called 1 byte. However, when the CPU reads memory, it does not read in bytes, but in blocks, so the size of blocks is the scale of memory access.Copy the code
If it's not aligned, the CPU will have to spend a lot of time trying to figure out how many bytes you want to read when you're trying to read a lot of memory. This will make the CPU inefficient. If you want the CPU to be efficient without reducing the number of bytes you need to read, you need to find a specification.Copy the code

So why 16-byte alignment instead of 8-byte alignment?

Apple uses 16-byte alignment because the first part of an OC object is called an ISA pointer, and it has to exist. Pointers are 8 bytes long. Even if you have no other attributes in your object, you must have an ISA, and that object has to take up at least 8 bytes. If the 8-bit bytes are aligned, the memory space of two consecutive memory objects will be completely next to each other and will be prone to chaos if both are objects with no attributes.Copy the code
With 16 bytes as a block, ensure that the CPU when reading, read by block can be more efficient, but also not easy to chaos.Copy the code