萬物皆對象正压,那對象是怎么創(chuàng)建出來的呢猜年?帶著這個問題,我們首先得了解一個類
[[LGPerson alloc] init]
在創(chuàng)建的過程中alloc
做了些什么怜珍?init
做了些什么?alloc
是怎樣開辟內存的凤粗?
1.我們首先來看一個例子:
- 根據打印的信息酥泛,我們可以看出p1,p2,p3的指針地址是相同的,但是他們的內存地址卻是不同的,為什么是這樣呢柔袁?這就是接下來我們要討論的
alloc 和 init
到底做了些什么呆躲?
準備工作:
- 下載 objc4-781 源碼
- 編譯源碼,這期間會出現各類問題瘦馍,解決的方法請參考這篇文章objc4_debug
通過配置好的源碼歼秽,我們可以打斷點一步一步的往下走:
- 在
[LGPerson alloc]
方法打上斷點,會走到alloc
方法
- 在
+ (id)alloc {
return _objc_rootAlloc(self);
}
- 進入到
_objc_rootAlloc
方法
- 進入到
id
_objc_rootAlloc(Class cls)
{
return callAlloc(cls, false/*checkNil*/, true/*allocWithZone*/);
}
- 進入到
callAlloc
方法
- 進入到
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));
}
- 進入到
_objc_rootAllocWithZone
方法
- 進入到
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);
}
- 進入到
_class_createInstanceFromZone
方法, 這里面主要實現了 3個方法-
cls->instanceSize
計算出需要開辟的內存空間大小 -
calloc
向系統(tǒng)申請開辟內存情组,返回地址指針 -
obj->initInstanceIsa
將 cls類 與 obj指針(即isa)進行關聯(lián)
-
- 進入到
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 {
// alloc 開辟內存的地方
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);
}
- 我們可以通過斷點調試進入計算內存大小的方法
cls->instanceSize
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;
}
- 斷點會進入
cache.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);
}
}
- 斷點繼續(xù)下一步燥筷,會走到
align16
方法, 這個方法的作用是 16字節(jié)對齊算法。以前的都是8字節(jié)對齊院崇,由于內存的消耗越來越大了肆氓,8字節(jié)對齊已經不能滿足用戶需求了,所以現在都是16字節(jié)對齊底瓣。
static inline size_t align16(size_t x) {
return (x + size_t(15)) & ~size_t(15);
}
接下來我們分析init
方法做了什么
- 根據源碼我們可以進入到
init
的類方法實現,這里是一個工廠方法谢揪,返回一個強轉為id
類型的構造方法。
+ (id)init {
return (id)self;
}
- 通過 點擊
init
方法捐凭,我們會計入到init
的實例方法, 會返回_objc_rootInit
方法拨扶,跳轉進入到方法實現中,會返回obj
對象本身
- (id) init {
return _objc_rootInit(self);
}
id
_objc_rootInit(id obj)
{
return obj;
}
補充: 我們可以看一下 new
方法的源碼是怎樣實現的茁肠。
+ (id)new {
return [callAlloc(self, false/*checkNil*/) init];
}
- 通過源碼我們可以看出潘靖,調用
new
方法會返回callAlloc
方法調用init
方法闰挡。實際上就是調用了[alloc init]
方法。