11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
|
* Public License, either version 2 or 3, which can be found in the file
* LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
* file.
*/
#include "config.h"
#ifdef OF_OBJFW_RUNTIME
#import "ObjFWRT.h"
#import "private.h"
# import "ObjFWRT.h"
# import "private.h"
#else
# import "OFObject.h"
# import "OFMapTable.h"
#endif
struct Association {
id object;
objc_associationPolicy policy;
};
#ifdef OF_OBJFW_RUNTIME
typedef struct objc_hashtable objc_hashtable;
#else
typedef OFMapTable objc_hashtable;
static const OFMapTableFunctions defaultFunctions = { NULL };
static objc_hashtable *
objc_hashtable_new(uint32_t (*hash)(const void *key),
bool (*equal)(const void *key1, const void *key2), uint32_t size)
{
return [[OFMapTable alloc] initWithKeyFunctions: defaultFunctions
objectFunctions: defaultFunctions];
}
static void
objc_hashtable_set(objc_hashtable *hashtable, const void *key,
const void *object)
{
return [hashtable setObject: (void *)object forKey: (void *)key];
}
static void *
objc_hashtable_get(objc_hashtable *hashtable, const void *key)
{
return [hashtable objectForKey: (void *)key];
}
static void
objc_hashtable_delete(objc_hashtable *hashtable, const void *key)
{
[hashtable removeObjectForKey: (void *)key];
}
static void
objc_hashtable_free(objc_hashtable *hashtable)
{
[hashtable release];
}
# define OBJC_ERROR(...) abort()
#endif
#ifdef OF_HAVE_THREADS
# define numSlots 8 /* needs to be a power of 2 */
# import "OFPlainMutex.h"
static OFSpinlock spinlocks[numSlots];
#else
# define numSlots 1
#endif
static struct objc_hashtable *hashtables[numSlots];
static objc_hashtable *hashtables[numSlots];
static OF_INLINE size_t
slotForObject(id object)
{
return ((size_t)((uintptr_t)object >> 4) & (numSlots - 1));
}
|
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
-
+
|
#ifdef OF_HAVE_THREADS
if (OFSpinlockLock(&spinlocks[slot]) != 0)
OBJC_ERROR("Failed to lock spinlock!");
@try {
#endif
struct objc_hashtable *objectHashtable;
objc_hashtable *objectHashtable;
struct Association *association;
objectHashtable = objc_hashtable_get(hashtables[slot], object);
if (objectHashtable == NULL) {
objectHashtable = objc_hashtable_new(hash, equal, 2);
objc_hashtable_set(hashtables[slot], object,
objectHashtable);
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
+
-
+
-
+
+
-
+
+
+
+
-
+
+
+
+
+
+
+
+
+
|
#endif
}
id
objc_getAssociatedObject(id object, const void *key)
{
size_t slot = slotForObject(object);
id ret;
#ifdef OF_HAVE_THREADS
if (OFSpinlockLock(&spinlocks[slot]) != 0)
OBJC_ERROR("Failed to lock spinlock!");
@try {
#endif
struct objc_hashtable *objectHashtable;
objc_hashtable *objectHashtable;
struct Association *association;
objectHashtable = objc_hashtable_get(hashtables[slot], object);
if (objectHashtable == NULL)
return nil;
association = objc_hashtable_get(objectHashtable, key);
if (association == NULL)
return nil;
switch (association->policy) {
case OBJC_ASSOCIATION_RETAIN:
case OBJC_ASSOCIATION_COPY:
return [[association->object retain] autorelease];
ret = [[association->object retain] autorelease];
break;
default:
return association->object;
ret = association->object;
break;
}
#ifdef OF_HAVE_THREADS
} @finally {
if (OFSpinlockUnlock(&spinlocks[slot]) != 0)
OBJC_ERROR("Failed to unlock spinlock!");
}
#endif
return ret;
}
void
objc_removeAssociatedObjects(id object)
{
size_t slot = slotForObject(object);
#ifdef OF_HAVE_THREADS
if (OFSpinlockLock(&spinlocks[slot]) != 0)
OBJC_ERROR("Failed to lock spinlock!");
@try {
#endif
struct objc_hashtable *objectHashtable;
objc_hashtable *objectHashtable;
objectHashtable = objc_hashtable_get(hashtables[slot], object);
if (objectHashtable == NULL)
return;
#ifdef OF_OBJFW_RUNTIME
for (uint32_t i = 0; i < objectHashtable->size; i++) {
struct Association *association;
if (objectHashtable->data[i] == NULL ||
objectHashtable->data[i] == &objc_deletedBucket)
continue;
association = (struct Association *)
objectHashtable->data[i]->object;
#else
OFMapTableEnumerator *enumerator =
[objectHashtable objectEnumerator];
void **iter;
while ((iter = [enumerator nextObject]) != NULL) {
struct Association *association = *iter;
#endif
switch (association->policy) {
case OBJC_ASSOCIATION_RETAIN:
case OBJC_ASSOCIATION_RETAIN_NONATOMIC:
case OBJC_ASSOCIATION_COPY:
case OBJC_ASSOCIATION_COPY_NONATOMIC:
[association->object release];
|