-
Notifications
You must be signed in to change notification settings - Fork 0
/
associate.m
458 lines (424 loc) · 11 KB
/
associate.m
1
2
3
4
5
6
7
8
9
10
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "objc/runtime.h"
#include "objc/objc-arc.h"
#include "nsobject.h"
#include "spinlock.h"
#include "class.h"
#include "dtable.h"
#include "selector.h"
#include "lock.h"
#include "gc_ops.h"
/**
* A single associative reference. Contains the key, value, and association
* policy.
*/
struct reference
{
/**
* The key used for identifying this object. Opaque pointer, should be set
* to 0 when this slot is unused.
*/
void *key;
/**
* The associated object. Note, if the policy is assign then this may be
* some other type of pointer...
*/
void *object;
/**
* Association policy.
*/
uintptr_t policy;
};
#define REFERENCE_LIST_SIZE 10
/**
* Linked list of references associated with an object. We assume that there
* won't be very many, so we don't bother with a proper hash table, and just
* iterate over a list.
*/
struct reference_list
{
/**
* Next group of references. This is only ever used if we have more than
* 10 references associated with an object, which seems highly unlikely.
*/
struct reference_list *next;
/**
* Mutex. Only set for the first reference list in a chain. Used for
* @syncronize().
*/
mutex_t lock;
/**
* Garbage collection type. This stores the location of all of the
* instance variables in the object that may contain pointers.
*/
void *gc_type;
/**
* Array of references.
*/
struct reference list[REFERENCE_LIST_SIZE];
};
enum
{
OBJC_ASSOCIATION_ATOMIC = 0x300,
};
static BOOL isAtomic(uintptr_t policy)
{
return (policy & OBJC_ASSOCIATION_ATOMIC) == OBJC_ASSOCIATION_ATOMIC;
}
static struct reference* findReference(struct reference_list *list, void *key)
{
if (NULL == list) { return NULL; }
for (int i=0 ; i<REFERENCE_LIST_SIZE ; i++)
{
if (list->list[i].key == key)
{
return &list->list[i];
}
}
return NULL;
}
static void cleanupReferenceList(struct reference_list *list)
{
if (NULL == list) { return; }
cleanupReferenceList(list->next);
for (int i=0 ; i<REFERENCE_LIST_SIZE ; i++)
{
struct reference *r = &list->list[i];
if (0 != r->key)
{
r->key = 0;
if (OBJC_ASSOCIATION_ASSIGN != r->policy)
{
// Full barrier - ensure that we've zero'd the key before doing
// this!
__sync_synchronize();
objc_release(r->object);
}
r->object = 0;
r->policy = 0;
}
}
}
static void freeReferenceList(struct reference_list *l)
{
if (NULL == l) { return; }
freeReferenceList(l->next);
gc->free(l);
}
static void setReference(struct reference_list *list,
void *key,
void *obj,
uintptr_t policy)
{
switch (policy)
{
// Ignore any unknown association policies
default: return;
case OBJC_ASSOCIATION_COPY_NONATOMIC:
case OBJC_ASSOCIATION_COPY:
obj = [(id)obj copy];
break;
case OBJC_ASSOCIATION_RETAIN_NONATOMIC:
case OBJC_ASSOCIATION_RETAIN:
obj = objc_retain(obj);
case OBJC_ASSOCIATION_ASSIGN:
break;
}
// While inserting into the list, we need to lock it temporarily.
volatile int *lock = lock_for_pointer(list);
lock_spinlock(lock);
struct reference *r = findReference(list, key);
// If there's an existing reference, then we can update it, otherwise we
// have to install a new one
if (NULL == r)
{
// Search for an unused slot
r = findReference(list, 0);
if (NULL == r)
{
struct reference_list *l = list;
while (NULL != l->next) { l = l->next; }
l->next = gc->malloc(sizeof(struct reference_list));
r = &l->next->list[0];
}
r->key = key;
}
unlock_spinlock(lock);
// Now we only need to lock if the old or new property is atomic
BOOL needLock = isAtomic(r->policy) || isAtomic(policy);
if (needLock)
{
lock = lock_for_pointer(r);
lock_spinlock(lock);
}
uintptr_t oldPolicy = r->policy;
id old = r->object;
r->policy = policy;
r->object = obj;
if (OBJC_ASSOCIATION_ASSIGN != oldPolicy)
{
objc_release(old);
}
if (needLock)
{
unlock_spinlock(lock);
}
}
static void deallocHiddenClass(id obj, SEL _cmd);
static inline Class findHiddenClass(id obj)
{
Class cls = obj->isa;
while (Nil != cls &&
!objc_test_class_flag(cls, objc_class_flag_assoc_class))
{
cls = class_getSuperclass(cls);
}
return cls;
}
static Class allocateHiddenClass(Class superclass)
{
Class newClass =
calloc(1, sizeof(struct objc_class) + sizeof(struct reference_list));
if (Nil == newClass) { return Nil; }
// Set up the new class
newClass->isa = superclass->isa;
newClass->name = superclass->name;
// Uncomment this for debugging: it makes it easier to track which hidden
// class is which
// static int count;
//asprintf(&newClass->name, "%s%d", superclass->name, count++);
newClass->info = objc_class_flag_resolved |
objc_class_flag_class | objc_class_flag_user_created |
objc_class_flag_new_abi | objc_class_flag_hidden_class |
objc_class_flag_assoc_class;
newClass->super_class = superclass;
newClass->dtable = uninstalled_dtable;
newClass->instance_size = superclass->instance_size;
LOCK_RUNTIME_FOR_SCOPE();
newClass->sibling_class = superclass->subclass_list;
superclass->subclass_list = newClass;
return newClass;
}
static inline Class initHiddenClassForObject(id obj)
{
Class hiddenClass = allocateHiddenClass(obj->isa);
assert(!class_isMetaClass(obj->isa));
static SEL cxx_destruct;
if (NULL == cxx_destruct)
{
cxx_destruct = sel_registerName(".cxx_destruct");
}
const char *types = sizeof(void*) == 4 ? "v8@0:4" : "v16@0:8";
class_addMethod(hiddenClass, cxx_destruct,
(IMP)deallocHiddenClass, types);
obj->isa = hiddenClass;
return hiddenClass;
}
static void deallocHiddenClass(id obj, SEL _cmd)
{
LOCK_RUNTIME_FOR_SCOPE();
Class hiddenClass = findHiddenClass(obj);
// After calling [super dealloc], the object will no longer exist.
// Free the hidden class.
struct reference_list *list = object_getIndexedIvars(hiddenClass);
DESTROY_LOCK(&list->lock);
cleanupReferenceList(list);
freeReferenceList(list->next);
free_dtable(hiddenClass->dtable);
// We shouldn't have any subclasses left at this point
assert(hiddenClass->subclass_list == 0);
// Remove the class from the subclass list of its superclass
Class sub = hiddenClass->super_class->subclass_list;
if (sub == hiddenClass)
{
hiddenClass->super_class->subclass_list = hiddenClass->sibling_class;
}
else
{
while (sub != NULL)
{
if ((Class)sub->sibling_class == hiddenClass)
{
sub->sibling_class = hiddenClass->sibling_class;
break;
}
sub = sub->sibling_class;
}
}
// Free the class
free(hiddenClass);
}
static struct reference_list* referenceListForObject(id object, BOOL create)
{
if (class_isMetaClass(object->isa))
{
Class cls = (Class)object;
if ((NULL == cls->extra_data) && create)
{
volatile int *lock = lock_for_pointer(cls);
struct reference_list *list = gc->malloc(sizeof(struct reference_list));
lock_spinlock(lock);
if (NULL == cls->extra_data)
{
INIT_LOCK(list->lock);
cls->extra_data = list;
unlock_spinlock(lock);
}
else
{
unlock_spinlock(lock);
gc->free(list);
}
}
return cls->extra_data;
}
Class hiddenClass = findHiddenClass(object);
if ((NULL == hiddenClass) && create)
{
volatile int *lock = lock_for_pointer(object);
lock_spinlock(lock);
hiddenClass = findHiddenClass(object);
if (NULL == hiddenClass)
{
hiddenClass = initHiddenClassForObject(object);
struct reference_list *list = object_getIndexedIvars(hiddenClass);
INIT_LOCK(list->lock);
}
unlock_spinlock(lock);
}
return hiddenClass ? object_getIndexedIvars(hiddenClass) : NULL;
}
void objc_setAssociatedObject(id object,
void *key,
id value,
objc_AssociationPolicy policy)
{
if (isSmallObject(object)) { return; }
struct reference_list *list = referenceListForObject(object, YES);
setReference(list, key, value, policy);
}
id objc_getAssociatedObject(id object, void *key)
{
if (isSmallObject(object)) { return nil; }
struct reference_list *list = referenceListForObject(object, NO);
if (NULL == list) { return nil; }
struct reference *r = findReference(list, key);
if (NULL != r)
{
return r->object;
}
if (class_isMetaClass(object->isa))
{
return nil;
}
Class cls = object->isa;
while (Nil != cls)
{
while (Nil != cls &&
!objc_test_class_flag(cls, objc_class_flag_assoc_class))
{
cls = class_getSuperclass(cls);
}
if (Nil != cls)
{
struct reference_list *next_list = object_getIndexedIvars(cls);
if (list != next_list)
{
list = next_list;
struct reference *r = findReference(list, key);
if (NULL != r)
{
return r->object;
}
}
cls = class_getSuperclass(cls);
}
}
return nil;
}
void objc_removeAssociatedObjects(id object)
{
if (isSmallObject(object)) { return; }
cleanupReferenceList(referenceListForObject(object, NO));
}
PRIVATE void *gc_typeForClass(Class cls)
{
struct reference_list *list = referenceListForObject(cls, YES);
return list->gc_type;
}
PRIVATE void gc_setTypeForClass(Class cls, void *type)
{
struct reference_list *list = referenceListForObject(cls, YES);
list->gc_type = type;
}
int objc_sync_enter(id object)
{
if ((object == 0) || isSmallObject(object)) { return 0; }
struct reference_list *list = referenceListForObject(object, YES);
LOCK(&list->lock);
return 0;
}
int objc_sync_exit(id object)
{
if ((object == 0) || isSmallObject(object)) { return 0; }
struct reference_list *list = referenceListForObject(object, NO);
if (NULL != list)
{
UNLOCK(&list->lock);
return 0;
}
return 1;
}
static Class hiddenClassForObject(id object)
{
if (isSmallObject(object)) { return nil; }
if (class_isMetaClass(object->isa))
{
return object->isa;
}
Class hiddenClass = findHiddenClass(object);
if (NULL == hiddenClass)
{
volatile int *lock = lock_for_pointer(object);
lock_spinlock(lock);
hiddenClass = findHiddenClass(object);
if (NULL == hiddenClass)
{
hiddenClass = initHiddenClassForObject(object);
struct reference_list *list = object_getIndexedIvars(hiddenClass);
INIT_LOCK(list->lock);
}
unlock_spinlock(lock);
}
return hiddenClass;
}
BOOL object_addMethod_np(id object, SEL name, IMP imp, const char *types)
{
return class_addMethod(hiddenClassForObject(object), name, imp, types);
}
IMP object_replaceMethod_np(id object, SEL name, IMP imp, const char *types)
{
return class_replaceMethod(hiddenClassForObject(object), name, imp, types);
}
static char prototypeKey;
id object_clone_np(id object)
{
if (isSmallObject(object)) { return object; }
// Make sure that the prototype has a hidden class, so that methods added
// to it will appear in the clone.
referenceListForObject(object, YES);
id new = class_createInstance(object->isa, 0);
Class hiddenClass = initHiddenClassForObject(new);
struct reference_list *list = object_getIndexedIvars(hiddenClass);
INIT_LOCK(list->lock);
objc_setAssociatedObject(new, &prototypeKey, object,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return new;
}
id object_getPrototype_np(id object)
{
return objc_getAssociatedObject(object, &prototypeKey);
}