We all know that iOS uses alloc to create memory space, but we don’t know much about how it works. Let’s explore the underlying principles of alloc.

First create a project to explore the location of an OC object after alloc:

Address analysis is as follows:

Conclusion:

  • The object itself and its address are the same, but the address of its pointer is not

  • Alloc has the ability to open up a block of memory, init doesn’t have the ability to open up memory.

  • Stack memory is high to low address, heap memory is low to high address.

How do you look at the overall alloc process? If you want to do a good job, you must first sharpen its tools, first look at the three common ways of the underlying source code

Three ways to explore the underlying principles

So let’s start by putting a break point on alloc

1. Symbolic breakpoint

Once broken, hold down Control and click Step into to Step through the assembly.

Go to the underlying objc_alloc method

The lower notation breakpoint is objc_alloc

If you step through that, you’re going to go to the objc_alloc method.

2. Assembly

When you stop, look at its assembly code

You can see in assembly that the underlying alloc method is also calling objc_alloc

If you step through that, you’re going to go to the objc_alloc method.

3. Add a symbol breakpoint for alloc to locate the specific location

Step in, and you get to the _objc_rootAlloc method.

The alloc method will execute the _objc_rootAllocWithZone method. The alloc method will execute the _objc_rootAllocWithZone method.

Alloc source analysis

Specific source debugging and download, please see iOS source compilation debugging

  • The first step in everything is to make a break point

  • If I execute the code, it will execute toobjc_allocMethods * *

  • Enter thecallAllocMethod, this parameterallocWithZoneThe value offalse, so the last one will be executedobjc_msgSendMethods.

  • objc_msgSendThe parameters of the send method execution method arealloc, so enterallocMethods.

  • Go to the next step_objc_rootAllocmethods

  • Once again into thecallAllocMethod, this parameterallocWithZoneThe value is true

  • objc_msgSendThe parameters of the send method execution method areallocWithZone, perform the following operations:

  • Enter the_class_createInstanceFromZone:Method, which creates and returns objects through the three main methods.

Oc Object creation process

The oc object is created by three functions:

  1. CLS ->instanceSize() calculates memory size

  2. (id)calloc(1, size) Open memory

  3. Obj ->initInstanceIsa() associates the class with memory

  • 1. The firstinstanceSize()methods

Enter the instanceSize() method and step through the breakpoint

Enter the fastInstanceSize() method and step through the breakpoint

Enter the align16() method to calculate the space size by bitwise operation

In the case of no cache, the following function is executed and 8-byte alignment is performed

Finally, return to instanceSize() and the result is 16

  • 2. calloc()methods

The value returned by alloc does not execute the defined class.

  • 3. Associated objects

Execute the initInstanceIsa() method to initialize ISA

The ISA principles are not analyzed here

Assign a value to the defined object after executing the associated method.

So that’s how alloc creates an OC object.

Summary: The core purpose of alloc is to open up memory and associate classes with isa Pointers.

Alloc flowchart

Supplement: Why do I have to walk twice to create an objectcallAllocMethods?

Through THE LLVM analysis, Apple did the piling process:

  • The first time: when alloc is executed, the method mapping calls objc_alloc, which makes the pile operation (as the marker receiver), and then the first call callAlloc → objc_msgSend(alloc)

  • The second: I do alloc again, I do objc_alloc again, and there’s a mark, so instead of doing objc_alloc, I call my own alloc, _objc_rootAlloc→ callAlloc → objc_msgSend(allocWithZone)