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/objc.h>
25 #import "objc-config.h"
30 The weak table is a hash table governed by a single spin lock.
31 An allocated blob of memory, most often an object, but under GC any such allocation,
32 may have its address stored in a __weak marked storage location through use of
33 compiler generated write-barriers or hand coded uses of the register weak primitive.
34 Associated with the registration can be a callback block for the case when one of
35 the allocated chunks of memory is reclaimed.
36 The table is hashed on the address of the allocated memory. When __weak marked memory
37 changes its reference, we count on the fact that we can still see its previous reference.
39 So, in the hash table, indexed by the weakly referenced item, is a list of all locations
40 where this address is currently being stored.
42 For ARR, we also keep track of whether an arbitrary object is being deallocated by
43 briefly placing it in the table just prior to invoking dealloc, and removing it
44 via objc_clear_deallocating just prior to memory reclamation.
48 struct weak_referrer_t
{
49 id
*referrer
; // clear this address
51 typedef struct weak_referrer_t weak_referrer_t
;
53 struct weak_referrer_array_t
{
54 weak_referrer_t
*refs
;
57 size_t max_hash_displacement
;
59 typedef struct weak_referrer_array_t weak_referrer_array_t
;
63 weak_referrer_array_t referrers
;
65 typedef struct weak_entry_t weak_entry_t
;
70 struct weak_entry_t
*weak_entries
;
72 typedef struct weak_table_t weak_table_t
;
74 extern id
weak_register_no_lock(weak_table_t
*weak_table
, id referent
, id
*referrer
);
75 extern void weak_unregister_no_lock(weak_table_t
*weak_table
, id referent
, id
*referrer
);
77 extern id
arr_read_weak_reference(weak_table_t
*weak_table
, id
*referrer
);
78 extern void arr_clear_deallocating(weak_table_t
*weak_table
, id referent
);