2 * Copyright (c) 2010-2011 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 #include "objc-private.h"
26 #include "objc-weak.h"
30 #include <sys/types.h>
31 #include <libkern/OSAtomic.h>
33 #define TABLE_SIZE(entry) (entry->mask ? entry->mask + 1 : 0)
35 static void append_referrer(weak_entry_t *entry, objc_object **new_referrer);
38 void objc_weak_error(void)
41 static void bad_weak_table(weak_entry_t *entries)
43 _objc_fatal("bad weak table at %p. This may be a runtime bug or a "
44 "memory error somewhere else.", entries);
48 * Unique hash function for object pointers only.
50 * @param key The object pointer
52 * @return Size unrestricted hash of pointer.
54 static inline uintptr_t hash_pointer(objc_object *key) {
55 return ptr_hash((uintptr_t)key);
59 * Unique hash function for weak object pointers only.
61 * @param key The weak object pointer.
63 * @return Size unrestricted hash of pointer.
65 static inline uintptr_t w_hash_pointer(objc_object **key) {
66 return ptr_hash((uintptr_t)key);
70 * Grow the entry's hash table of referrers. Rehashes each
73 * @param entry Weak pointer hash set for a particular object.
75 __attribute__((noinline, used))
76 static void grow_refs_and_insert(weak_entry_t *entry,
77 objc_object **new_referrer)
79 ASSERT(entry->out_of_line());
81 size_t old_size = TABLE_SIZE(entry);
82 size_t new_size = old_size ? old_size * 2 : 8;
84 size_t num_refs = entry->num_refs;
85 weak_referrer_t *old_refs = entry->referrers;
86 entry->mask = new_size - 1;
88 entry->referrers = (weak_referrer_t *)
89 calloc(TABLE_SIZE(entry), sizeof(weak_referrer_t));
91 entry->max_hash_displacement = 0;
93 for (size_t i = 0; i < old_size && num_refs > 0; i++) {
94 if (old_refs[i] != nil) {
95 append_referrer(entry, old_refs[i]);
100 append_referrer(entry, new_referrer);
101 if (old_refs) free(old_refs);
105 * Add the given referrer to set of weak pointers in this entry.
106 * Does not perform duplicate checking (b/c weak pointers are never
107 * added to a set twice).
109 * @param entry The entry holding the set of weak pointers.
110 * @param new_referrer The new weak pointer to be added.
112 static void append_referrer(weak_entry_t *entry, objc_object **new_referrer)
114 if (! entry->out_of_line()) {
115 // Try to insert inline.
116 for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
117 if (entry->inline_referrers[i] == nil) {
118 entry->inline_referrers[i] = new_referrer;
123 // Couldn't insert inline. Allocate out of line.
124 weak_referrer_t *new_referrers = (weak_referrer_t *)
125 calloc(WEAK_INLINE_COUNT, sizeof(weak_referrer_t));
126 // This constructed table is invalid, but grow_refs_and_insert
127 // will fix it and rehash it.
128 for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
129 new_referrers[i] = entry->inline_referrers[i];
131 entry->referrers = new_referrers;
132 entry->num_refs = WEAK_INLINE_COUNT;
133 entry->out_of_line_ness = REFERRERS_OUT_OF_LINE;
134 entry->mask = WEAK_INLINE_COUNT-1;
135 entry->max_hash_displacement = 0;
138 ASSERT(entry->out_of_line());
140 if (entry->num_refs >= TABLE_SIZE(entry) * 3/4) {
141 return grow_refs_and_insert(entry, new_referrer);
143 size_t begin = w_hash_pointer(new_referrer) & (entry->mask);
144 size_t index = begin;
145 size_t hash_displacement = 0;
146 while (entry->referrers[index] != nil) {
148 index = (index+1) & entry->mask;
149 if (index == begin) bad_weak_table(entry);
151 if (hash_displacement > entry->max_hash_displacement) {
152 entry->max_hash_displacement = hash_displacement;
154 weak_referrer_t &ref = entry->referrers[index];
160 * Remove old_referrer from set of referrers, if it's present.
161 * Does not remove duplicates, because duplicates should not exist.
163 * @todo this is slow if old_referrer is not present. Is this ever the case?
165 * @param entry The entry holding the referrers.
166 * @param old_referrer The referrer to remove.
168 static void remove_referrer(weak_entry_t *entry, objc_object **old_referrer)
170 if (! entry->out_of_line()) {
171 for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
172 if (entry->inline_referrers[i] == old_referrer) {
173 entry->inline_referrers[i] = nil;
177 _objc_inform("Attempted to unregister unknown __weak variable "
178 "at %p. This is probably incorrect use of "
179 "objc_storeWeak() and objc_loadWeak(). "
180 "Break on objc_weak_error to debug.\n",
186 size_t begin = w_hash_pointer(old_referrer) & (entry->mask);
187 size_t index = begin;
188 size_t hash_displacement = 0;
189 while (entry->referrers[index] != old_referrer) {
190 index = (index+1) & entry->mask;
191 if (index == begin) bad_weak_table(entry);
193 if (hash_displacement > entry->max_hash_displacement) {
194 _objc_inform("Attempted to unregister unknown __weak variable "
195 "at %p. This is probably incorrect use of "
196 "objc_storeWeak() and objc_loadWeak(). "
197 "Break on objc_weak_error to debug.\n",
203 entry->referrers[index] = nil;
208 * Add new_entry to the object's table of weak references.
209 * Does not check whether the referent is already in the table.
211 static void weak_entry_insert(weak_table_t *weak_table, weak_entry_t *new_entry)
213 weak_entry_t *weak_entries = weak_table->weak_entries;
214 ASSERT(weak_entries != nil);
216 size_t begin = hash_pointer(new_entry->referent) & (weak_table->mask);
217 size_t index = begin;
218 size_t hash_displacement = 0;
219 while (weak_entries[index].referent != nil) {
220 index = (index+1) & weak_table->mask;
221 if (index == begin) bad_weak_table(weak_entries);
225 weak_entries[index] = *new_entry;
226 weak_table->num_entries++;
228 if (hash_displacement > weak_table->max_hash_displacement) {
229 weak_table->max_hash_displacement = hash_displacement;
234 static void weak_resize(weak_table_t *weak_table, size_t new_size)
236 size_t old_size = TABLE_SIZE(weak_table);
238 weak_entry_t *old_entries = weak_table->weak_entries;
239 weak_entry_t *new_entries = (weak_entry_t *)
240 calloc(new_size, sizeof(weak_entry_t));
242 weak_table->mask = new_size - 1;
243 weak_table->weak_entries = new_entries;
244 weak_table->max_hash_displacement = 0;
245 weak_table->num_entries = 0; // restored by weak_entry_insert below
249 weak_entry_t *end = old_entries + old_size;
250 for (entry = old_entries; entry < end; entry++) {
251 if (entry->referent) {
252 weak_entry_insert(weak_table, entry);
259 // Grow the given zone's table of weak references if it is full.
260 static void weak_grow_maybe(weak_table_t *weak_table)
262 size_t old_size = TABLE_SIZE(weak_table);
264 // Grow if at least 3/4 full.
265 if (weak_table->num_entries >= old_size * 3 / 4) {
266 weak_resize(weak_table, old_size ? old_size*2 : 64);
270 // Shrink the table if it is mostly empty.
271 static void weak_compact_maybe(weak_table_t *weak_table)
273 size_t old_size = TABLE_SIZE(weak_table);
275 // Shrink if larger than 1024 buckets and at most 1/16 full.
276 if (old_size >= 1024 && old_size / 16 >= weak_table->num_entries) {
277 weak_resize(weak_table, old_size / 8);
278 // leaves new table no more than 1/2 full
284 * Remove entry from the zone's table of weak references.
286 static void weak_entry_remove(weak_table_t *weak_table, weak_entry_t *entry)
289 if (entry->out_of_line()) free(entry->referrers);
290 bzero(entry, sizeof(*entry));
292 weak_table->num_entries--;
294 weak_compact_maybe(weak_table);
299 * Return the weak reference table entry for the given referent.
300 * If there is no entry for referent, return NULL.
304 * @param referent The object. Must not be nil.
306 * @return The table of weak referrers to this object.
308 static weak_entry_t *
309 weak_entry_for_referent(weak_table_t *weak_table, objc_object *referent)
313 weak_entry_t *weak_entries = weak_table->weak_entries;
315 if (!weak_entries) return nil;
317 size_t begin = hash_pointer(referent) & weak_table->mask;
318 size_t index = begin;
319 size_t hash_displacement = 0;
320 while (weak_table->weak_entries[index].referent != referent) {
321 index = (index+1) & weak_table->mask;
322 if (index == begin) bad_weak_table(weak_table->weak_entries);
324 if (hash_displacement > weak_table->max_hash_displacement) {
329 return &weak_table->weak_entries[index];
333 * Unregister an already-registered weak reference.
334 * This is used when referrer's storage is about to go away, but referent
335 * isn't dead yet. (Otherwise, zeroing referrer later would be a
336 * bad memory access.)
337 * Does nothing if referent/referrer is not a currently active weak reference.
338 * Does not zero referrer.
340 * FIXME currently requires old referent value to be passed in (lame)
341 * FIXME unregistration should be automatic if referrer is collected
343 * @param weak_table The global weak table.
344 * @param referent The object.
345 * @param referrer The weak reference.
348 weak_unregister_no_lock(weak_table_t *weak_table, id referent_id,
351 objc_object *referent = (objc_object *)referent_id;
352 objc_object **referrer = (objc_object **)referrer_id;
356 if (!referent) return;
358 if ((entry = weak_entry_for_referent(weak_table, referent))) {
359 remove_referrer(entry, referrer);
361 if (entry->out_of_line() && entry->num_refs != 0) {
365 for (size_t i = 0; i < WEAK_INLINE_COUNT; i++) {
366 if (entry->inline_referrers[i]) {
374 weak_entry_remove(weak_table, entry);
378 // Do not set *referrer = nil. objc_storeWeak() requires that the
383 * Registers a new (object, weak pointer) pair. Creates a new weak
384 * object entry if it does not exist.
386 * @param weak_table The global weak table.
387 * @param referent The object pointed to by the weak reference.
388 * @param referrer The weak pointer address.
391 weak_register_no_lock(weak_table_t *weak_table, id referent_id,
392 id *referrer_id, WeakRegisterDeallocatingOptions deallocatingOptions)
394 objc_object *referent = (objc_object *)referent_id;
395 objc_object **referrer = (objc_object **)referrer_id;
397 if (referent->isTaggedPointerOrNil()) return referent_id;
399 // ensure that the referenced object is viable
400 if (deallocatingOptions == ReturnNilIfDeallocating ||
401 deallocatingOptions == CrashIfDeallocating) {
403 if (!referent->ISA()->hasCustomRR()) {
404 deallocating = referent->rootIsDeallocating();
407 // Use lookUpImpOrForward so we can avoid the assert in
408 // class_getInstanceMethod, since we intentionally make this
409 // callout with the lock held.
410 auto allowsWeakReference = (BOOL(*)(objc_object *, SEL))
411 lookUpImpOrForwardTryCache((id)referent, @selector(allowsWeakReference),
413 if ((IMP)allowsWeakReference == _objc_msgForward) {
417 ! (*allowsWeakReference)(referent, @selector(allowsWeakReference));
421 if (deallocatingOptions == CrashIfDeallocating) {
422 _objc_fatal("Cannot form weak reference to instance (%p) of "
423 "class %s. It is possible that this object was "
424 "over-released, or is in the process of deallocation.",
425 (void*)referent, object_getClassName((id)referent));
432 // now remember it and where it is being stored
434 if ((entry = weak_entry_for_referent(weak_table, referent))) {
435 append_referrer(entry, referrer);
438 weak_entry_t new_entry(referent, referrer);
439 weak_grow_maybe(weak_table);
440 weak_entry_insert(weak_table, &new_entry);
443 // Do not set *referrer. objc_storeWeak() requires that the
452 weak_is_registered_no_lock(weak_table_t *weak_table, id referent_id)
454 return weak_entry_for_referent(weak_table, (objc_object *)referent_id);
460 * Called by dealloc; nils out all weak pointers that point to the
461 * provided object so that they can no longer be used.
464 * @param referent The object being deallocated.
467 weak_clear_no_lock(weak_table_t *weak_table, id referent_id)
469 objc_object *referent = (objc_object *)referent_id;
471 weak_entry_t *entry = weak_entry_for_referent(weak_table, referent);
473 /// XXX shouldn't happen, but does with mismatched CF/objc
474 //printf("XXX no entry for clear deallocating %p\n", referent);
478 // zero out references
479 weak_referrer_t *referrers;
482 if (entry->out_of_line()) {
483 referrers = entry->referrers;
484 count = TABLE_SIZE(entry);
487 referrers = entry->inline_referrers;
488 count = WEAK_INLINE_COUNT;
491 for (size_t i = 0; i < count; ++i) {
492 objc_object **referrer = referrers[i];
494 if (*referrer == referent) {
497 else if (*referrer) {
498 _objc_inform("__weak variable at %p holds %p instead of %p. "
499 "This is probably incorrect use of "
500 "objc_storeWeak() and objc_loadWeak(). "
501 "Break on objc_weak_error to debug.\n",
502 referrer, (void*)*referrer, (void*)referent);
508 weak_entry_remove(weak_table, entry);