在NSObject类的alloc类方法上,执行所调用的方法和函数:
+alloc
+allocWithZone
class_createInstance
calloc
retainCount/retain/release实例方法又是怎样实现的呢?下面列出来:
-retainCount
__CFDoExternRefOperation
CFBasicHashGetCountOfKey
-retain
__CFDoExternRefOperation
CFBasicHashAddValue
-release
__CFDoExternRefOperation
CFBasicHashRemoveValue
(CFBasicHashRemoveValue返回0时,-release调用dealloc)
各个方法都通过一个调用了__CFDoExternRefOperation函数,调用了一系列名称相似的函数。如这些函数名的前缀“CF”所示,它们包含于Core Foundation框架源代码中,
即是CFRuntime中的__CFDoExternRefOperation函数,
int __CFDoExternRefOperation(uintptr_t op, id obj)
{
CFBasicHashRef table = 取得对象对应的散列表(obj);
int count;
switch(op)
{
case OPERATION_retainCount:
count = CFBasicHashGetCountOfKey(table, obj);
return count;
case OPERATION_retain:
CFBasicHashAddValue(table, obj);
return obj;
case OPERATION_release:
count = CFBasicHashRemoveValue(table, obj);
return 0 == count;
}
}
__CFDoExternRefOperation函数按retainCount/retain/release操作进行分发,调用不同的函数。NSObject类的retainCount/retain/release实例方法也许如下面
- (NSUInteger)retainCount
{
return (NSUInteger)__CFDoExternRefOperation(OPERATION_retainCount, self);
}
- (id)retain
{
return (id)__CFDoExternRefOperation(OPERATION_retain, self);
}
- (void)release
{
return __CFDoExternRefOperation(OPERATION_release, self);
}