ios weak实现原理用的表伪代码

1.伪代码模拟weak用到的主要表

struct weak_referrer_t{
     int weakPtr;//瞎写的不做参考
};

//hash表
struct weak_entry_t{
    int index;
    //存储弱引用该对象的指针的指针的数组
    weak_referrer_t inline_referrers[3];
    id referent;//传入的对象
    int count;
};

//weak_table——hash表
struct weak_table_t{
    //hash数组,用来存储弱引用对象的相关信息weak_entry_t
    weak_entry_t weak_entries[3];
};
struct SideTable {
    //weak_table——hash表
    weak_table_t weak_table;
    
};

//全局的hash表
struct SideTables {
    //全局的hash数组
    SideTable SideTable[3];
    
};

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        struct weak_entry_t weak_entry_t1 = {1,[NSObject alloc]};
        struct weak_entry_t weak_entry_t2 = {2,[NSObject alloc]};
        struct weak_entry_t weak_entry_t3 = {3,[NSObject alloc]};
        
        struct weak_table_t weak_table_t;
        weak_table_t.weak_entries[0] = weak_entry_t1;
        weak_table_t.weak_entries[1] = weak_entry_t2;
        weak_table_t.weak_entries[2] = weak_entry_t3;
        
        struct SideTable SideTable;
        SideTable.weak_table = weak_table_t;
      
       };

参照链接:https://blog.csdn.net/u013378438/article/details/82790332

你可能感兴趣的:(iOS)