A, alloc object pointer address and memory address analysis

In the source analysis of alloc, first through the example to view the difference between pointer address and memory address:

Print memory, memory address, and pointer address respectively. The result is as follows:

Results analysis:

  1. Alloc opens up memory space and creates objects, p1, P2, p3 pointing to the same memory space.LGPerson<0x280fd02c0>
  2. Init’s memory pointer is the same. No manipulation is done on the pointer to create space, so its contents and memory address are the same.
  3. Pointer addresses in init stack; Each pointer is 8 intervals apart

NSLog printing

%@ object %p pointer address %p -> p1 object memory address %p -> &p1 object pointer address

Two, alloc bottom explore the source code way

  1. Symbol breakpoint mode directly with flow -> source code: libobjc.a.dylib ‘objc_alloc:

    • If YOU add a breakpoint at LGPerson alloc, and you run the project, it will stop at LGPerson alloc

    • Hold down thecontrolKey, clickStep into leftkey

    • After clicking in, it displaysobjc_allocThe underlying function

    • Keep looking at the underlying source code structure and add oneobjc_allocSymbol breakpoint

    • Next, it showsobjc_allocSource code library methodlibobjc.A.dylib`objc_alloc:

  2. See the trace flow through assembly

    • inLGPerson allocAdd a breakpoint at, run the project, will stop atLGPerson allThe location of the

    • Xcode toolbar selectionDebug --> Debug Workflow --> Always Show Disassembly, select the current selection to go to assembly code

    • Hold down the Control key and click Step into ↓ to enter the Symbol Stub for: objc_alloc function

    • Hold down thecontrolKey, clickStep into leftKey, breakpoint atobjc_allocfunction

    • Summary: ① Xcode toolbar selectionDebug --> Debug Workflow --> Always Show Disassembly, ② add oneobjc_allocSymbol breakpoint, start project, execute to source librarylibobjc.A.dylib`objc_alloc

  3. Determine unknown by known sign breakpoint alloc: libobjc.a.dylib ‘+[NSObject alloc]:, used in conjunction with step 2

    • inLGPerson allocAdd a breakpoint at, run the project, will stop atLGPerson allocThe location of the

    • Add an alloc symbol breakpoint

    • Libobjc.a. dylib ‘+[NSObject alloc]:

Three, source code analysis alloc process

  • The preparatory work

Opensource.apple.com Source Browser: Opensource.apple.com/tarballs/ objc4-818 source: opensource.apple.com/tarballs/ob…

  1. Process analysis

    • [step 1] Source search alloc {method into the method of alloc (source analysis begins)

    • [step 2] Jump to _objc_rootAlloc method implementation

    • Step 3: Enter the callAlloc method

      At step 3, in the callAlloc method, verify whether _objc_rootAllocWithZone or objc_msgSend is executed, as shown above.

      Set symbol breakpoint debugging and find the _objc_rootAllocWithZone flow

      added

      #define fastPath (x) (__builtin_expect(bool(x), 1)) #define slowPath (x) (__builtin_expect(bool(x), 0))

      Fastpath: performs in-depth optimization of the executed processes, mainly for the released version, and improves the running speed slowPath: all execution processes need to be executed without optimization

  2. Alloc mainline flow

    Part of the alloc process trace has been completed, but what exactly alloc does is still incomplete??

    Open the source code, set the breakpoint set the breakpoint in the main function at LGPerson *p = [LGPerson alloc]; LGPerson *p = [LGPerson alloc]; Set breakpoints at alloc and _objc_rootAlloc as shown below:

    You do trace discovery, you call the alloc function, you go into the callAlloc function, you call objc_msgSend once, you send an Alloc message.

    Interpretation of execution process:

    1. Determine if there is a custom alloc/allocWithZone local implementation in the cache, obviously not in the first run class.

    2. What is CLS? The class? No! Typedef struct objc_class *Class; . So CLS is a pointer to a structure, and that structure is the Core Foundation layer class!

    3. Classes are initialized when the read_images method is executed, and instance objects are initialized when alloc is executed.

    4. The first execution ((id (*) (id, SEL) objc_msgSend) (CLS, @ the selector (alloc)); , does a slow method lookup, finds the Alloc method of the NSObject class, and puts the method in the method cache.

    5. So in addition to calling the alloc method the first time, the _objc_rootAllocWithZone method goes directly to object initialization.

Alloc core method

_class_createInstanceFromZone analysis

  • CLS ->instanceSize calculates the memory size

The compiler optimization is performed, and the cache.fastInstancesize method in the cache is executed to calculate the required memory size

  • calloc

Apply to the system to open up memory, return address pointer. The process allocates a memory temporarily, and the memory allocated after calloc is called is the memory address used to create the object.

In normal development, an object would normally be printed in a format like

(which is a pointer). Why not here?

  • Mainly becauseObjc addressNot yet with incomingclsMake associations,
  • And it confirms thatallocThe fundamental function ofCreate a memory
  • Obj ->initInstanceIsa: associate with ISA

    Associated with the corresponding class, the memory space to be opened up points to the class to be associated with! Obj has only one memory address before calling obj->initInstanceIsa, after which the object type is LGPerson.

added

Memory optimization

The class defined has no attributes and only inherits NSObject, so the actual size of this instance is 8 bytes,

static inline uint32_t word_align(uint32_t x) {

    return (x + WORD_MASK) & ~WORD_MASK;

}
Copy the code

1, 8 – > NSObject

2. At least 16

  • Usually memory is composed of a byte, CPU in the access to data, not byte as a unit of storage, but in the block as a unit of access, block size for memory access strength. Frequently accessing data with unaligned bytes greatly reduces CPU performance, so you can reduce CPU overhead by reducing the number of accesses

  • Of course, an object must have other attributes. If there is no attribute, 8 bytes will be reserved, that is, the 16-byte alignment. Otherwise, it means that the ISA of this object and the ISA of other objects are next to each other, which may cause access confusion

  • With 16-byte alignment, CPU reads are faster and access is safer without clutter

3, byte alignment :(x + WORD_MASK) & ~WORD_MASK;

(8 + 7) &~ 7 — > 15&~ 7 8 bytes are aligned with the integer 15:0000 1111! 7:1111 1000 7:0000 0111 15 & ~7:0000 1000 = 8

Why is 8 a multiple of space in exchange for time, the entire memory 8 bytes the most

  

OC object initialization analysis flowchart: