“This is the first day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

preface

  • At present, I plan to go deep into the bottom of OC and Swift, hoping to improve myself and share some gains.

This main note: Explore Alloc&init by looking at objC’s source code

  • Here to introduce you to a daniu has configured the source code GitHub, we can directly download the use.

1. After downloading, after some configuration, the real machine compiles, and then comes to the following debugging interface

Note: allocwithZone is basically discarded and useless; In general, alloc and allocwithZone work the same way. Both alloc and allocwithZone can be used to create instances. In fact, alloc calls allocwithZone by default.

2. Best Companies rank: 61_class_createInstanceFromZoneThere are three important steps in the (class creates instance from zone) method

Note:

  • A. It can be found that alloc hasApplying for Memory Space,Create an object,And give Pointers the ability to address;
  • B. Obtain the memory size of the instance:cls->instanceSize()
  • C. Allocate memory space according to the size of the memory, so that the instance points to the start memory address:calloc
  • d. Associated with the isaInstance isa points to the class:obj->initInstanceIsa(cls, hasCxxDtor)
  • E:word_alignByte alignment(x + WORD_MASK) & ~WORD_MASKThis algorithm is designed to makeisaThe memory space required by the object is a multiple of 8 -8 byte aligned,mallocSizeThe memory size allocated by the system is 16 bytes aligned to compute attribute methods etc. Function:Memory alignment, which makes CPU reading easier, trades space for time

3. Summarize all alloc processes (from Baidu) :

4. Explore init, new

id _objc_rootInit(id obj)
{
    return obj;
}
Copy the code

That’s obj from alloc.

- (id)init {
    return _objc_rootInit(self);
}
Copy the code

Init doesn’t do anything, generally in this case it’s factory override method, custom, that’s how we write code all the time

- (instancetype)init{ self = [super init]; if (self) { [self doSomething]; } return self; Self = [super init] = if (self) = if (self) = if (self) = if (self) = if (self) = if (self) = if (self) = if (self Do a safe handle or the subclass implementation is meaninglessCopy the code

New is a combination of callAlloc and init

+ (id)new {
    return [callAlloc(self, false/*checkNil*/) init];
}
Copy the code

5. Development:

  • Compilation optimization: fastpath and slowpath can be used to determine whether compilation optimization is needed or not. In release mode, compilation optimization will be performed and fastpath judgment will be performed. Functions: reduce compilation time, link time, running time and idle time of optimization;

    Reprint: iOS looks at project compile times and optimizes compilation speed methods

  • Print statement records: Po and p: p stands for “expression” — prints the object pointer; Po is “expression-o” — prints the object itself; X /4gx object — output four hexadecimal 8-byte address Spaces (x for hexadecimal, 4 for four, and g for 8-byte units, equivalent to x/4xg objects); X object — represents the memory address of the object printed in hexadecimal (x represents hexadecimal);

6. Conclusion:

Alloc&init is a method that shows how memory is created by using the _class_createInstanceFromZone method. How memory is allocated, current class and pointer address relationship. Let’s dig deeper, exploring the details of how memory is allocated and how attributes in objects affect memory allocation.