weak_table

前言

  • 系统为我们创建了一个全局的weak_table,这个表里面有一个weak_entries这样的一个一维数组,
  • 这个weak_entries这个数组中的每个结构体weak_entry_t,
  • 其中referent为被弱引用的对象, 而referrers则是指向这个弱引用的的地址

使用

使用weak还是__weak底层都是调用storeWeak这个函数,区别在于模板的第一个参数HaveOld,官方解释如下

If HaveOld is true, the variable has an existing value 
that needs to be cleaned up. This value might be nil.

重点函数

weak_register_no_lock函数

id weak_register_no_lock(weak_table_t *weak_table, id referent_id, 
                      id *referrer_id, bool crashIfDeallocating)
{
   // 被弱引用的对象
    objc_object *referent = (objc_object *)referent_id;
   // 指向弱引用对象的指针
    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,如果存在,则插入一个新的指向这个弱引用对象的referrer地址,对应的关系如下图:

referrer 指向
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存储了referent的时候如何处理

        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));
   // 因为mask为2^n,所以-1,是的mask等于全1的二进制
    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
    
   // 重新将老的数据插入到插入到新分配的空间中
    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);
    }
}

当weak_table的num_entries大于总量的3/4,其中这个总量存储在weak_table的mask字段中,初始使用64,以后每次扩容为上次大小的2倍.
接下来插入这个新的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++;
    }
    
   // 这个index即为弱引用对象的地址,hash偏移后产生的
    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实现的精华所在,如果直接使用弱引用对象的地址作为index,那么weak_entries的大小就要alloc对应系统位数的内存大小,显然不可能,这样内存空间将会全部被占用.因此出现了上面这个方法,根据存储对象的数量,动态申请内存,再根据引用对象的地址mask后,一定是小于TABLE_SIZE,但是可能有两个不同的对象,结尾的地址是相同的,这个时候就需要特殊处理,每次index++,直到这个index对应的位置没有被使用.

参考:
iOS __weak的底层实现

你可能感兴趣的:(weak_table)