alloc的底層調(diào)用
1. alloc
會調(diào)用_objc_rootAlloc
+ (id)alloc {
return _objc_rootAlloc(self);
}
2._objc_rootAlloc
會調(diào)用callAlloc(cls, false, true)
;
id _objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
3.預(yù)編譯階段,fastpath()
告訴系統(tǒng)大概率會執(zhí)行下面的過程尔苦,即:_objc_rootAllocWithZone(cls, nil)
static ALWAYS_INLINE id 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));
}
4.接下來執(zhí)行_class_createInstanceFromZone(cls, 0, nil,OBJECT_CONSTRUCT_CALL_BADALLOC)
id _objc_rootAllocWithZone(Class cls, malloc_zone_t *zone __unused)
{
// allocWithZone under __OBJC2__ ignores the zone parameter
return _class_createInstanceFromZone(cls, 0, nil,
OBJECT_CONSTRUCT_CALL_BADALLOC);
}
5._class_createInstanceFromZone
這里是具體干的事情
1.需要開辟多少空間
size = cls->instanceSize(extraBytes);
2.申請內(nèi)存空間obj = (id)calloc(1, size)
3.將這段內(nèi)存空間與isa關(guān)聯(lián)obj->initInstanceIsa(cls, hasCxxDtor);
4.返回指向該段內(nèi)存地址的指針if (fastpath(!hasCxxCtor)) {return obj;}
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;
// 1:要開辟多少內(nèi)存
size = cls->instanceSize(extraBytes);
if (outAllocatedSize) *outAllocatedSize = size;
id obj;
if (zone) {
obj = (id)malloc_zone_calloc((malloc_zone_t *)zone, 1, size);
} else {
// 2;怎么去申請內(nèi)存
obj = (id)calloc(1, size);
}
if (slowpath(!obj)) {
if (construct_flags & OBJECT_CONSTRUCT_CALL_BADALLOC) {
return _objc_callBadAllocHandler(cls);
}
return nil;
}
// 3: 將這段內(nèi)存空間與isa關(guān)聯(lián)
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);
}
// 4: 返回指向該段內(nèi)存地址的指針
if (fastpath(!hasCxxCtor)) {
return obj;
}
construct_flags |= OBJECT_CONSTRUCT_FREE_ONFAILURE;
return object_cxxConstructFromClass(obj, cls, construct_flags);
}
alloc流程圖
alloc流程圖
init底層調(diào)用
返回
self
+ (id)init {
return (id)self;
}
new的底層調(diào)用
1.
[callAlloc(self, false) init]
其實就是[[XXX alloc]init]
2. 但是一般開發(fā)中并不建議使用new,主要是因為有時會重寫init方法做一些自定義的操作,用new初始化無法走到重寫init的自定義的部分赡若,因為new是直接調(diào)用底層的C函數(shù)去實現(xiàn)的列赎。
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
面試題
1.分析以下代打印的結(jié)果
LGPerson *p1 = [LGPerson alloc];
LGPerson *p2 = [p1 init];
LGPerson *p3 = [p1 init];
LGNSLog(@"%@ - %p - %p",p1,p1,&p1);
NSLog(@"%@ - %p - %p",p2,p2,&p2);
NSLog(@"%@ - %p - %p",p3,p3,&p3);
打印的結(jié)果:
結(jié)果
分析:
1.首先p1=p2=p3 扣溺,是同一個對象骇窍,占據(jù)同一段內(nèi)存空間,所依打印出來的地址相同
2. &p1 &p2 &p3 但是指向同一段內(nèi)存空間的指針是不同的,所以 &p1 &p2 &p3不同
分析圖.png