关联对象

在分类中不能添加属性,但是却可以使用 关联对象的方式,给类添加变量。
主要重点是:

  1. 关联对象的实现方式。
  2. 关联对象散列表的存储和查找逻辑
  3. 关联对象的设置,以及retain/release

objc_setAssociatedObject

void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
{
    SetAssocHook.get()(object, key, value, policy);
}
//  静态的 SetAssocHook 中有一个成员 _base_objc_setAssociatedObject。 其get应该也是获取这个成员
static ChainedHookFunction SetAssocHook{_base_objc_setAssociatedObject};

template 
class ChainedHookFunction {
    std::atomic hook{nil};
public:
    ChainedHookFunction(Fn f) : hook{f} { };
    Fn get() {
        return hook.load(std::memory_order_acquire);
    }
    ...
};

static void _base_objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
{
  _object_set_associative_reference(object, key, value, policy);
}

下面就分析一下 具体的 _object_set_associative_reference 函数实现

_object_set_associative_reference

void
_object_set_associative_reference(id object, const void *key, id value, uintptr_t policy)
{
    // This code used to work when nil was passed for object and key. Some code
    // probably relies on that to not crash. Check and handle it explicitly.
    // rdar://problem/44094390
    if (!object && !value) return;

1. 定义两个结构体变量 disguised , association 然后根据value的类型进行处理
    DisguisedPtr disguised{(objc_object *)object};
    ObjcAssociation association{policy, value};

    // retain the new value (if any) outside the lock.
    // 如果是retain,则执行一下retain,如果是copy,就向value 发送一个copy 消息
    // 所以这里要和属性一样,要注意循环引用。
    association.acquireValue();
2. 创建AssociationsManager 对象。其中的静态成员 _mapStorage 使用来全局存储所有的关联对象数据。参考下面的具体的数据存储类型。
    {
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.get());
3. 如果 传入的value 有值。
1.   try_emplace 如果 objc 查找到对应的bucket(ObjectAssociationMap),会返回 second 为true,然后会执行 object->setHasAssociatedObjects(),设置标志位;。否则返回的second 为false (其中会在对应key的地方插入一个空的 bucket(ObjectAssociationMap),并且返回这个bucket的地址)
2.  在返回的bucket (ObjectAssociationMap 类型也是一个Densemap)中。然后将 key,value 插入到Densemap中(也是如果存在就插入,不足在就创建新的,然后在返回的为false 的情况下,在数据的指针地址修改数据)
        if (value) {
            // refs_result 是objc 对应的buckets 会自动创建
            auto refs_result = associations.try_emplace(disguised, ObjectAssociationMap{});
            if (refs_result.second) {
                /* it's the first association we make */
                // 第一次的创建会执行,设置标记位有关联值
                object->setHasAssociatedObjects();
            }

            /* establish or replace the association */
            // 取到 objc对应的 bucket的地址
            auto &refs = refs_result.first->second;
            // 找到存储 bucket 也是一个DenseMap,将key,association(包含value) 插入到bucket中
            auto result = refs.try_emplace(key, std::move(association));
            
            // 如果try_emplace没有查找到key,就创建新的bucket,然后返回false, 然后有新的bucket的地址。然后使用swap 修改这个地址的数据 
            if (!result.second) {
                association.swap(result.first->second);
            }
        } 
        
4. 如果传入的value 为 nil
1. 查找对应的bucket,  find 会执行 (LookupBucketFor(Val, TheBucket)) 
2. 如果找到了不是最后,就去执行associations.erase(refs_it); 
        else {
            // 根据objc 查找对应的 bucket(也是一个DenseMap)。find找不到,会返回end()
            auto refs_it = associations.find(disguised);
            if (refs_it != associations.end()) {
                // 取到bucket的地址,修改地址中的数据
                auto &refs = refs_it->second; // refs 是objc 对应的 bucket
                // 再去bucket 中查找key 对应的bucket。
                auto it = refs.find(key); //objc对应 bucket 也是一个densemap 然后根据key查找。找不到返回 end
                if (it != refs.end()) {
                    // 如果找到, refs要清楚 key对应的数据。删除内存
                    association.swap(it->second);
                    refs.erase(it);
                    if (refs.size() == 0) { // 如果objc对应的bucket中的数量是0,将这个bucket也清掉
                        associations.erase(refs_it);

                    }
                }
            }
        }
    }
    // release the old value (outside of the lock).
    // 对应上面 association.acquireValue(); ,这里release临时变量
    association.releaseHeldValue();
}

数据存储的类型

AssociationsManager _mapStorage

AssociationsManager 中有一个全局的静态变量_mapStorage。类型是Storage,是一个 DenseMap的类型,是一个哈希表。对应的key: DisguisedPtr , value: ObjectAssociationMap

class AssociationsManager {
    using Storage = ExplicitInitDenseMap, ObjectAssociationMap>;
    static Storage _mapStorage;
    ...
}
ObjectAssociationMap

可以看到,ObjectAssociationMap也是一个DenseMap类型的 哈希表。对应的key: const void * , ObjcAssociation。

typedef DenseMap ObjectAssociationMap;
typedef DenseMap, ObjectAssociationMap> AssociationsHashMap;

class ObjcAssociation {
    uintptr_t _policy;
    id _value;
}

LookupBucketFor

  bool LookupBucketFor(const LookupKeyT &Val, BucketT *&FoundBucket) {
    const BucketT *ConstFoundBucket;
    // 去全局的表中查询,
    bool Result = const_cast(this)
      ->LookupBucketFor(Val, ConstFoundBucket);
    FoundBucket = const_cast(ConstFoundBucket);
    return Result;
  }

acquireValue

inline void acquireValue() {
        if (_value) {
            switch (_policy & 0xFF) {
            case OBJC_ASSOCIATION_SETTER_RETAIN:
                _value = objc_retain(_value);
                break;
            case OBJC_ASSOCIATION_SETTER_COPY:
                _value = ((id(*)(id, SEL))objc_msgSend)(_value, @selector(copy));
                break;
            }
        }
    }

ObjcAssociation

class ObjcAssociation {
    uintptr_t _policy;
    id _value;

相对应的其他

_object_get_associative_reference

这个函数会被 objc_getAssociatedObject 调用。 获取对象的关联对象数据

id _object_get_associative_reference(id object, const void *key)
{
    ObjcAssociation association{};

    {
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.get());
        AssociationsHashMap::iterator i = associations.find((objc_object *)object); // 先找第一层数据
        if (i != associations.end()) { // 如果有数据
            ObjectAssociationMap &refs = i->second;
            ObjectAssociationMap::iterator j = refs.find(key); // 再去着第二层的数据
            if (j != refs.end()) { // 如果有数据 
                association = j->second;
                association.retainReturnedValue(); 
            }
        }
    }
    return association.autoreleaseReturnedValue();
}

_object_remove_assocations

这个函数,全局搜索,再objc源码中,会被 objc_removeAssociatedObjects 和 objc_destructInstance 方法引用。当对象销毁时,获取找到,销毁关联的表,以及 根据是否retain 进行realse

// Unlike setting/getting an associated reference,
// this function is performance sensitive because of
// raw isa objects (such as OS Objects) that can't track
// whether they have associated objects.
void
_object_remove_assocations(id object)
{
    ObjectAssociationMap refs{};

    {
        AssociationsManager manager;
        AssociationsHashMap &associations(manager.get());
        AssociationsHashMap::iterator i = associations.find((objc_object *)object);
        if (i != associations.end()) {
            refs.swap(i->second);
            associations.erase(i);
        }
    }

    // release everything (outside of the lock).
    for (auto &i: refs) {
        i.second.releaseHeldValue();
    }
}

总结

  1. 关联对象存储的方式是 存储在 以DenseMap格式的全局对象里面。DenseMap是一个二次探查散列表表(hash值计算方式)。
  2. 关联对象的散列表有两层。第一层的DenseMap 存储着搜有 对象和 其对应数据的 DenseMap。
  3. 关联对象根据设置的策略会对 关联对象 进行retain/copy。 在被对象销毁时, 或者移除关联对象时,或对应是否执行 realse。

有一个不错的图片


image

参考:

  • https://blog.csdn.net/dashuniuniu/article/details/80043852
  • 关联对象

你可能感兴趣的:(关联对象)