The code for associated objects is in objc-references.mm in the Objective-C runtime.
If I understand it correctly, there is one global hash map (static AssociationsHashMap *_map
in class AssociationsManager
) that maps the address of an object ("disguised" as uintptr_t
) to an ObjectAssociationMap
.
ObjectAssociationMap
stores all associations for one particular object and is created
when
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy)
is called the first time for an object.
ObjectAssociationMap
is a hash map that maps the key
to value
and policy
.
When an object is deallocated, _object_remove_assocations()
removes all associations
and releases the values if necessary.