由于Foundation框架并沒有公開,首先使用GNUstep說明
<pre>id obj =[ [NSObject alloc]init];</pre>
上述調(diào)用NSObject類的alloc類方法在NSObject.m的源代碼實(shí)現(xiàn)如下:
<pre>+(id) alloc { return [self allocwithzone:NSDefaultMallocZone()]; }</pre>
<pre>+(id)allocWithZone:(NSZone *)z { return NSALLocateObject (self, 0, z); }</pre>
通過allocwithzone方法調(diào)用NSAllocateObject函數(shù)分配了對(duì)象下面我們來看看NSALLocateObject函數(shù)
<pre>`struct obj_layout {
NSUInterger retained;
};
inline id
NSAllocateObject (Class aClass, NSUInteger extraBytes, NSZone* zone)
{
int size = 計(jì)算容納對(duì)象需要內(nèi)存大小;
id new = NSZoneMalloc(zone, size);
memset(new, 0, size);
new = (id)& ((struct obj_layout *)new)[1];
}
`</pre>
NSAllocateObject函數(shù)通過調(diào)用NSZoneMAlloc函數(shù)來分配存放對(duì)象所需要的內(nèi)存空間,之后將內(nèi)存空間置0,最后返回座位對(duì)象而使用的指針.
NSZone
這個(gè)類是為了防止內(nèi)存碎片化而引入的結(jié)構(gòu).對(duì)內(nèi)存分配的區(qū)域本身行進(jìn)多重畫管理,根據(jù)使用對(duì)象的目的,對(duì)象的大小而分配內(nèi)存,從而提供內(nèi)存管理的效率.但是ARC系統(tǒng)中,運(yùn)行時(shí)系統(tǒng)內(nèi)存管理已極具效率,使用區(qū)域管理內(nèi)存反而會(huì)引起內(nèi)存使用率底下的問題
以下是去掉NSZone后的代碼
<pre>`struct obj_layout {
NSUInterger retained;
};
+(id)alloc
{
int size = sizeof(struct obj_layout) +對(duì)象大小;
struct obj_layout *p= (struct obj_layout *)calloc(1, size);
return (id)(p +1);
}
`</pre>
alloc類方法用obj_layout中的retained整數(shù)來保存引用計(jì)數(shù),并將其寫入對(duì)象內(nèi)存頭部,該對(duì)象內(nèi)存塊全部置0后返回.
對(duì)象的引用計(jì)數(shù)可通過retainCount實(shí)例犯法取得
[obj retain]解析
<pre>`
-(id)reatin
{
NSIncrementExtreaRefCount (self);
return self;
}
inline void
NSIncrementExtreaRefCount(id anObject)
{
if (((struct obj_layout *)anObject)[-1].retained = UINT_MAX - 1 )
[NSExpression raise: NSInternalInconsistencyException format:@"NSIncrementExtreaRefCount()asked to increment to far");
((struct obj_layout *)anObject)[-1].reatined++;
}
`</pre>
以下為release實(shí)例方法的實(shí)現(xiàn)
<pre>`
-(void)release
{
if (NSDecrementExtraRefCountWasZero(self)) {
[self dealloc];
}
}
NSDecrementExtraRefCountWasZero(id anObject)
{
if (((struct obj_layout *)anObject)[-1].retained == 0)
{
return YES;
}else{
((struct obj_layout *)anObject)[-1].retained--;
return NO;
}
}`</pre>
<pre>`
-(void)dealloc
{
NSDeallocateObject(self);
}
NSDeallocateObject(id anObject)
{
struct obj_layout * o = &((struct obj_layout *)anObject) [-1];
free(0);
}`</pre>
以上就是alloc/retain/release/delloc在GNUstep中的實(shí)現(xiàn).
蘋果的實(shí)現(xiàn)
執(zhí)行順序如下
+alloc
+allocWithZone
class_createInstance
calloc
alloc類首先調(diào)用allocWithZone:類方法,這和GUNstep的實(shí)現(xiàn)相同,然后調(diào)用class_createINstance函數(shù)
函數(shù)源碼可以在objc-runtime-new.mm查看
retain/release實(shí)現(xiàn)方法和函數(shù)
-retainedCount
__CFDoExternRefOperation
CFBasicHashGetCountOfKey
-retain
__CFDoExternRefOperation
CFBasicHashAddValue
-release
__CFDoExternRefOperation
CFBasicHashRemoveValue
各個(gè)方法都調(diào)用了__CFDoExternRefOperation函數(shù),可以在Core Foundation框架源代碼中CFRuntime.c的____CFDoExternRefOperation函數(shù)中查看,下面是簡(jiǎn)化后的函數(shù)源代碼
<pre>`
int __CFDoExternRefOperation
{
CFBasicHashRef table = 取得對(duì)象對(duì)應(yīng)的散列表 (obj);
int count;
switch (op) {
case OPERATION_retainCount;
count = CFBasicHashGetCountOfKey(table, obj);
return count;
case OPERATION_retain;
CFBasicHashAddValue(table, obj);
case OPERATION_release;
CFBasicHashRemoveValue(table, obj);
}
}
`</pre>
GNUstep將引用計(jì)數(shù)保存在對(duì)象占用內(nèi)存塊頭部的變量中,而蘋果的實(shí)現(xiàn),則是保存在引用計(jì)數(shù)表的記錄中。通過內(nèi)存卡頭部管理引用計(jì)數(shù)的好處如下:
少量代碼即可完成
能夠統(tǒng)一管理引用計(jì)數(shù)用內(nèi)存塊與對(duì)象用內(nèi)存塊
通過引用計(jì)數(shù)表管理引用計(jì)數(shù)的好處如下:
對(duì)象用內(nèi)存塊的分配無需考慮內(nèi)存塊頭部
引用計(jì)數(shù)表各記錄中存有內(nèi)存塊地址,可從各個(gè)記錄中追溯到各對(duì)象的內(nèi)存塊
*** 關(guān)于蘋果的實(shí)現(xiàn)的第2條特性在調(diào)試時(shí)有著舉足輕重的作用:***
1.即使出現(xiàn)故障導(dǎo)致對(duì)象占用的內(nèi)存塊損壞,但只要引用計(jì)數(shù)表沒有被破壞,就能夠確認(rèn)各內(nèi)存塊的位置
2.在利用工具檢測(cè)內(nèi)存泄露時(shí),引用計(jì)數(shù)表的各記錄也有助于檢測(cè)各對(duì)象的持有者是否存在