2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 Implementation of the weak / associative references for non-GC mode.
28 #include "objc-private.h"
29 #include <objc/message.h>
31 #include "DenseMapExtras.h"
33 // expanded policy bits.
36 OBJC_ASSOCIATION_SETTER_ASSIGN = 0,
37 OBJC_ASSOCIATION_SETTER_RETAIN = 1,
38 OBJC_ASSOCIATION_SETTER_COPY = 3, // NOTE: both bits are set, so we can simply test 1 bit in releaseValue below.
39 OBJC_ASSOCIATION_GETTER_READ = (0 << 8),
40 OBJC_ASSOCIATION_GETTER_RETAIN = (1 << 8),
41 OBJC_ASSOCIATION_GETTER_AUTORELEASE = (2 << 8),
42 OBJC_ASSOCIATION_SYSTEM_OBJECT = _OBJC_ASSOCIATION_SYSTEM_OBJECT, // 1 << 16
45 spinlock_t AssociationsManagerLock;
49 class ObjcAssociation {
53 ObjcAssociation(uintptr_t policy, id value) : _policy(policy), _value(value) {}
54 ObjcAssociation() : _policy(0), _value(nil) {}
55 ObjcAssociation(const ObjcAssociation &other) = default;
56 ObjcAssociation &operator=(const ObjcAssociation &other) = default;
57 ObjcAssociation(ObjcAssociation &&other) : ObjcAssociation() {
61 inline void swap(ObjcAssociation &other) {
62 std::swap(_policy, other._policy);
63 std::swap(_value, other._value);
66 inline uintptr_t policy() const { return _policy; }
67 inline id value() const { return _value; }
69 inline void acquireValue() {
71 switch (_policy & 0xFF) {
72 case OBJC_ASSOCIATION_SETTER_RETAIN:
73 _value = objc_retain(_value);
75 case OBJC_ASSOCIATION_SETTER_COPY:
76 _value = ((id(*)(id, SEL))objc_msgSend)(_value, @selector(copy));
82 inline void releaseHeldValue() {
83 if (_value && (_policy & OBJC_ASSOCIATION_SETTER_RETAIN)) {
88 inline void retainReturnedValue() {
89 if (_value && (_policy & OBJC_ASSOCIATION_GETTER_RETAIN)) {
94 inline id autoreleaseReturnedValue() {
95 if (slowpath(_value && (_policy & OBJC_ASSOCIATION_GETTER_AUTORELEASE))) {
96 return objc_autorelease(_value);
102 typedef DenseMap<const void *, ObjcAssociation> ObjectAssociationMap;
103 typedef DenseMap<DisguisedPtr<objc_object>, ObjectAssociationMap> AssociationsHashMap;
105 // class AssociationsManager manages a lock / hash table singleton pair.
106 // Allocating an instance acquires the lock
108 class AssociationsManager {
109 using Storage = ExplicitInitDenseMap<DisguisedPtr<objc_object>, ObjectAssociationMap>;
110 static Storage _mapStorage;
113 AssociationsManager() { AssociationsManagerLock.lock(); }
114 ~AssociationsManager() { AssociationsManagerLock.unlock(); }
116 AssociationsHashMap &get() {
117 return _mapStorage.get();
125 AssociationsManager::Storage AssociationsManager::_mapStorage;
129 using namespace objc;
132 _objc_associations_init()
134 AssociationsManager::init();
138 _object_get_associative_reference(id object, const void *key)
140 ObjcAssociation association{};
143 AssociationsManager manager;
144 AssociationsHashMap &associations(manager.get());
145 AssociationsHashMap::iterator i = associations.find((objc_object *)object);
146 if (i != associations.end()) {
147 ObjectAssociationMap &refs = i->second;
148 ObjectAssociationMap::iterator j = refs.find(key);
149 if (j != refs.end()) {
150 association = j->second;
151 association.retainReturnedValue();
156 return association.autoreleaseReturnedValue();
160 _object_set_associative_reference(id object, const void *key, id value, uintptr_t policy)
162 // This code used to work when nil was passed for object and key. Some code
163 // probably relies on that to not crash. Check and handle it explicitly.
164 // rdar://problem/44094390
165 if (!object && !value) return;
167 if (object->getIsa()->forbidsAssociatedObjects())
168 _objc_fatal("objc_setAssociatedObject called on instance (%p) of class %s which does not allow associated objects", object, object_getClassName(object));
170 DisguisedPtr<objc_object> disguised{(objc_object *)object};
171 ObjcAssociation association{policy, value};
173 // retain the new value (if any) outside the lock.
174 association.acquireValue();
176 bool isFirstAssociation = false;
178 AssociationsManager manager;
179 AssociationsHashMap &associations(manager.get());
182 auto refs_result = associations.try_emplace(disguised, ObjectAssociationMap{});
183 if (refs_result.second) {
184 /* it's the first association we make */
185 isFirstAssociation = true;
188 /* establish or replace the association */
189 auto &refs = refs_result.first->second;
190 auto result = refs.try_emplace(key, std::move(association));
191 if (!result.second) {
192 association.swap(result.first->second);
195 auto refs_it = associations.find(disguised);
196 if (refs_it != associations.end()) {
197 auto &refs = refs_it->second;
198 auto it = refs.find(key);
199 if (it != refs.end()) {
200 association.swap(it->second);
202 if (refs.size() == 0) {
203 associations.erase(refs_it);
211 // Call setHasAssociatedObjects outside the lock, since this
212 // will call the object's _noteAssociatedObjects method if it
213 // has one, and this may trigger +initialize which might do
214 // arbitrary stuff, including setting more associated objects.
215 if (isFirstAssociation)
216 object->setHasAssociatedObjects();
218 // release the old value (outside of the lock).
219 association.releaseHeldValue();
222 // Unlike setting/getting an associated reference,
223 // this function is performance sensitive because of
224 // raw isa objects (such as OS Objects) that can't track
225 // whether they have associated objects.
227 _object_remove_assocations(id object, bool deallocating)
229 ObjectAssociationMap refs{};
232 AssociationsManager manager;
233 AssociationsHashMap &associations(manager.get());
234 AssociationsHashMap::iterator i = associations.find((objc_object *)object);
235 if (i != associations.end()) {
236 refs.swap(i->second);
238 // If we are not deallocating, then SYSTEM_OBJECT associations are preserved.
239 bool didReInsert = false;
241 for (auto &ref: refs) {
242 if (ref.second.policy() & OBJC_ASSOCIATION_SYSTEM_OBJECT) {
243 i->second.insert(ref);
249 associations.erase(i);
253 // Associations to be released after the normal ones.
254 SmallVector<ObjcAssociation *, 4> laterRefs;
256 // release everything (outside of the lock).
257 for (auto &i: refs) {
258 if (i.second.policy() & OBJC_ASSOCIATION_SYSTEM_OBJECT) {
259 // If we are not deallocating, then RELEASE_LATER associations don't get released.
261 laterRefs.append(&i.second);
263 i.second.releaseHeldValue();
266 for (auto *later: laterRefs) {
267 later->releaseHeldValue();