一,实际的使用
NBPerson * person1 = [NBPerson alloc];
NBPerson * person2 = [person1 init];
NBPerson * person3 = [person1 init];
NBPerson * person4 = [NBPerson alloc];
NSLog(@"%@---%p--%p",person1,person1,&person1);
NSLog(@"%@---%p--%p",person2,person2,&person2);
NSLog(@"%@---%p--%p",person3,person3,&person3);
NSLog(@"%@---%p--%p",person4,person4,&person4);
//输出结果
**2021-07-17 20:56:11.522447+0800 alloc_探索[953:28200] <NBPerson: 0x600002e44410>---0x600002e44410--0x7ffee8177158**
**2021-07-17 20:56:11.522676+0800 alloc_探索[953:28200] <NBPerson: 0x600002e44410>---0x600002e44410--0x7ffee8177150**
**2021-07-17 20:56:11.522915+0800 alloc_探索[953:28200] <NBPerson: 0x600002e44410>---0x600002e44410--0x7ffee8177148**
**2021-07-17 20:56:11.523478+0800 alloc_探索[953:28200] <NBPerson: 0x600002e44420>---0x600002e44420--0x7ffee8177140**
观察
- person1,person2,person3内存地址一样 person4不一样。
- person1,person2,person3,person4,指针地址都不一样
- person1,person2,person3内存地址比 person4地址小
- person1,person2,person3,person4指针地址,越来越小
图
graph TD
&person1-0x7ffee8177158 --> alloc1-0x600002e44410
&person2-0x7ffee8177150 --> alloc1-0x600002e44410
&person3-0x7ffee8177148 --> alloc1-0x600002e44410
&person4-0x7ffee8177140 --> alloc2-0x600002e44420
结论
- alloc开辟实际内存(堆),init没有开辟内存
- 堆地址由低到高,栈地址由高到低
二,探索底层必须要会
- 断点调试
- 汇编探索 Xcode->Debug->Debug Workflow->Always Show Disassembly
三,源码探索
- 源码objc-818.2
- 打开源码
- 开始探索
- 全局搜索alloc { 找到代码
+ ( id )alloc {
return _objc_rootAlloc(self);
}
- _objc_rootAlloc
**id**
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
- callAlloc
callAlloc(Class cls, **bool** checkNil, **bool** allocWithZone=**false**)
{
#if __OBJC2__
**if** (slowpath(checkNil && !cls)) **return** **nil**;
**if** (fastpath(!cls->ISA()->hasCustomAWZ())) {
**return** _objc_rootAllocWithZone(cls, **nil**);
}
#endif
\
// No shortcuts available.
**if** (allocWithZone) {
**return** ((**id**(*)(**id**, **SEL**, **struct** _NSZone *))objc_msgSend)(cls, **@selector**(allocWithZone:), **nil**);
}
**return** ((**id**(*)(**id**, **SEL**))objc_msgSend)(cls, **@selector**(alloc));
}
- _objc_rootAllocWithZone
_objc_rootAllocWithZone(Class cls, malloc_zone_t *zone)
{
**id** obj;
\
**if** (fastpath(!zone)) {
obj = class_createInstance(cls, 0);
} **else** {
obj = class_createInstanceFromZone(cls, 0, zone);
}
\
**if** (slowpath(!obj)) obj = _objc_callBadAllocHandler(cls);
**return** obj;
}
- ob'j'c-runtime-new.mm
- _class_createInstanceFromZone
**static** ALWAYS_INLINE **id**
_class_createInstanceFromZone(Class cls, size_t extraBytes, **void** *zone,
**int** construct_flags = OBJECT_CONSTRUCT_NONE,
**bool** cxxConstruct = **true**,
size_t *outAllocatedSize = **nil**)
{
ASSERT(cls->isRealized());
\
// Read class's info bits all at once for performance
**bool** hasCxxCtor = cxxConstruct && cls->hasCxxCtor();
**bool** hasCxxDtor = cls->hasCxxDtor();
**bool** fast = cls->canAllocNonpointer();
size_t size;
\
size = cls->instanceSize(extraBytes);
**if** (outAllocatedSize) *outAllocatedSize = size;
\
**id** obj;
**if** (zone) {
obj = (**id**)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
} **else** {
obj = (**id**)calloc(1, size);
}
**if** (slowpath(!obj)) {
**if** (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
**return** _objc_callBadAllocHandler(cls);
}
**return** **nil**;
}
\
**if** (!zone && fast) {
obj->initInstanceIsa(cls, hasCxxDtor);
} **else** {
// Use raw pointer isa on the assumption that they might be
// doing something weird with the zone or RR.
obj->initIsa(cls);
}
\
**if** (fastpath(!hasCxxCtor)) {
**return** obj;
}
\
construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
**return** object_cxxConstructFromClass(obj, cls, construct_flags);
}
- instanceSize 计算内存大小
**inline** size_t instanceSize(size_t extraBytes) **const** {
**if** (fastpath(cache.hasFastInstanceSize(extraBytes))) {
**return** cache.fastInstanceSize(extraBytes);
}
\
size_t size = alignedInstanceSize() + extraBytes;
// CF requires all objects be at least 16 bytes.
**if** (size < 16) size = 16;
**return** size;
}
- fastInstanceSize
size_t fastInstanceSize(size_t extra) **const**
{
ASSERT(hasFastInstanceSize(extra));
\
**if** (__builtin_constant_p(extra) && extra == 0) {
**return** _flags & FAST_CACHE_ALLOC_MASK16;
} else {
size_t size = _flags & FAST_CACHE_ALLOC_MASK;
// remove the FAST_CACHE_ALLOC_DELTA16 that was added
// by setFastInstanceSize
return align16(size + extra - FAST_CACHE_ALLOC_DELTA16);
}
}
- align16 16字节对齐
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
-
calloc 开辟内存返回地址指针
-
初始化指针,关联类
inline void
objc_object::initInstanceIsa(Class cls, bool hasCxxDtor)
{
ASSERT(!cls->instancesRequireRawIsa());
ASSERT(hasCxxDtor == cls->hasCxxDtor());
initIsa(cls, true, hasCxxDtor);
}
- init探究
- (id)init {
return _objc_rootInit(self);
}
- init返回自身 主要用来子类重写
id
_objc_rootInit(id obj)
{
// In practice, it will be hard to rely on this function.
// Many classes do not properly chain -init calls.
return obj;
}
- new探究 alloc+init
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
四,流程图
graph TD
alloc --> _objc_rootAlloc -->callAlloc --> _objc_rootAllocWithZone --> _class_createInstanceFromZone --> instanceSize
_class_createInstanceFromZone --> calloc
_class_createInstanceFromZone --> initInstanceIsa
callAlloc --> _objc_msgSend