应用程序全局有一个名为SideTables的hash表,当一个对象被引用的时候,会用该对象的内存地址计算出一个hash值,以该值为key在hash表中取出对应的SideTable,SideTable是一个结构体,包含一个自旋锁spinlock_t,一个引用计数表RefcountMap ,一个weak_table_t。
struct SideTable {
//锁
spinlock_t slock;
//强引用相关
RefcountMap refcnts;
//弱引用相关
weak_table_t weak_table;
...
}
refcnts有个名为buckets数组,在该数组里查找该对象的引用计数表,找到之后增加引用计数
id
objc_object::sidetable_retain()
{
#if SUPPORT_NONPOINTER_ISA
ASSERT(!isa.nonpointer);
#endif
SideTable& table = SideTables()[this];
table.lock();
size_t& refcntStorage = table.refcnts[this];
if (! (refcntStorage & SIDE_TABLE_RC_PINNED)) {
refcntStorage += SIDE_TABLE_RC_ONE;
}
table.unlock();
return (id)this;
}
网友评论