前言
- 系統(tǒng)為我們創(chuàng)建了一個(gè)全局的weak_table,這個(gè)表里面有一個(gè)weak_entries這樣的一個(gè)一維數(shù)組,
- 這個(gè)weak_entries這個(gè)數(shù)組中的每個(gè)結(jié)構(gòu)體weak_entry_t,
- 其中referent為被弱引用的對(duì)象, 而referrers則是指向這個(gè)弱引用的的地址
使用
使用weak還是__weak底層都是調(diào)用storeWeak這個(gè)函數(shù),區(qū)別在于模板的第一個(gè)參數(shù)HaveOld,官方解釋如下
If HaveOld is true, the variable has an existing value
that needs to be cleaned up. This value might be nil.
重點(diǎn)函數(shù)
weak_register_no_lock函數(shù)
id weak_register_no_lock(weak_table_t *weak_table, id referent_id,
id *referrer_id, bool crashIfDeallocating)
{
// 被弱引用的對(duì)象
objc_object *referent = (objc_object *)referent_id;
// 指向弱引用對(duì)象的指針
objc_object **referrer = (objc_object **)referrer_id;
// now remember it and where it is being stored
weak_entry_t *entry;
if ((entry = weak_entry_for_referent(weak_table, referent))) {
append_referrer(entry, referrer);
}
else {
weak_entry_t new_entry(referent, referrer);
weak_grow_maybe(weak_table);
weak_entry_insert(weak_table, &new_entry);
}
// Do not set *referrer. objc_storeWeak() requires that the
// value not change.
return referent_id;
}
檢查weak_table中是否存在referent作為key的的weak_entry_t,如果存在,則插入一個(gè)新的指向這個(gè)弱引用對(duì)象的referrer地址,對(duì)應(yīng)的關(guān)系如下圖:
static void append_referrer(weak_entry_t *entry, objc_object **new_referrer)
{
if (! entry->out_of_line()) {
// Try to insert inline.
for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
if (entry->inline_referrers[i] == nil) {
entry->inline_referrers[i] = new_referrer;
return;
}
}
// Couldn't insert inline. Allocate out of line.
weak_referrer_t *new_referrers = (weak_referrer_t *)
calloc(WEAK_INLINE_COUNT, sizeof(weak_referrer_t));
// This constructed table is invalid, but grow_refs_and_insert
// will fix it and rehash it.
for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
new_referrers[i] = entry->inline_referrers[i];
}
entry->referrers = new_referrers;
entry->num_refs = WEAK_INLINE_COUNT;
entry->out_of_line_ness = REFERRERS_OUT_OF_LINE;
entry->mask = WEAK_INLINE_COUNT-1;
entry->max_hash_displacement = 0;
}
assert(entry->out_of_line());
if (entry->num_refs >= TABLE_SIZE(entry) * 3/4) {
return grow_refs_and_insert(entry, new_referrer);
}
size_t begin = w_hash_pointer(new_referrer) & (entry->mask);
size_t index = begin;
size_t hash_displacement = 0;
while (entry->referrers[index] != nil) {
hash_displacement++;
index = (index+1) & entry->mask;
if (index == begin) bad_weak_table(entry);
}
if (hash_displacement > entry->max_hash_displacement) {
entry->max_hash_displacement = hash_displacement;
}
weak_referrer_t &ref = entry->referrers[index];
ref = new_referrer;
entry->num_refs++;
}
沒有weak_entry_t存儲(chǔ)了referent的時(shí)候如何處理
weak_entry_t new_entry(referent, referrer);
weak_grow_maybe(weak_table);
weak_entry_insert(weak_table, &new_entry);
static void weak_grow_maybe(weak_table_t *weak_table)
{
size_t old_size = TABLE_SIZE(weak_table);
// Grow if at least 3/4 full.
if (weak_table->num_entries >= old_size * 3 / 4) {
weak_resize(weak_table, old_size ? old_size*2 : 64);
}
}
static void weak_resize(weak_table_t *weak_table, size_t new_size)
{
size_t old_size = TABLE_SIZE(weak_table);
weak_entry_t *old_entries = weak_table->weak_entries;
weak_entry_t *new_entries = (weak_entry_t *)
calloc(new_size, sizeof(weak_entry_t));
// 因?yàn)閙ask為2^n,所以-1,是的mask等于全1的二進(jìn)制
weak_table->mask = new_size - 1;
weak_table->weak_entries = new_entries;
weak_table->max_hash_displacement = 0;
weak_table->num_entries = 0; // restored by weak_entry_insert below
// 重新將老的數(shù)據(jù)插入到插入到新分配的空間中
if (old_entries) {
weak_entry_t *entry;
weak_entry_t *end = old_entries + old_size;
for (entry = old_entries; entry < end; entry++) {
if (entry->referent) {
weak_entry_insert(weak_table, entry);
}
}
free(old_entries);
}
}
當(dāng)weak_table的num_entries大于總量的3/4,其中這個(gè)總量存儲(chǔ)在weak_table的mask字段中,初始使用64,以后每次擴(kuò)容為上次大小的2倍.
接下來插入這個(gè)新的weak_entry_t
static void weak_entry_insert(weak_table_t *weak_table, weak_entry_t *new_entry)
{
weak_entry_t *weak_entries = weak_table->weak_entries;
assert(weak_entries != nil);
//
size_t begin = hash_pointer(new_entry->referent) & (weak_table->mask);
size_t index = begin;
size_t hash_displacement = 0;
while (weak_entries[index].referent != nil) {
index = (index+1) & weak_table->mask;
if (index == begin) bad_weak_table(weak_entries);
hash_displacement++;
}
// 這個(gè)index即為弱引用對(duì)象的地址,hash偏移后產(chǎn)生的
weak_entries[index] = *new_entry;
weak_table->num_entries++;
if (hash_displacement > weak_table->max_hash_displacement) {
weak_table->max_hash_displacement = hash_displacement;
}
}
weak_entry_insert
的算法算是__weak
實(shí)現(xiàn)的精華所在,如果直接使用弱引用對(duì)象的地址作為index,那么weak_entries
的大小就要alloc對(duì)應(yīng)系統(tǒng)位數(shù)的內(nèi)存大小,顯然不可能,這樣內(nèi)存空間將會(huì)全部被占用.因此出現(xiàn)了上面這個(gè)方法,根據(jù)存儲(chǔ)對(duì)象的數(shù)量,動(dòng)態(tài)申請(qǐng)內(nèi)存
,再根據(jù)引用對(duì)象的地址mask
后,一定是小于TABLE_SIZE
,但是可能有兩個(gè)不同的對(duì)象,結(jié)尾的地址是相同的,這個(gè)時(shí)候就需要特殊處理,每次index++,直到這個(gè)index對(duì)應(yīng)的位置沒有被使用.