-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassociative.c
More file actions
602 lines (506 loc) · 14 KB
/
associative.c
File metadata and controls
602 lines (506 loc) · 14 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#include "os.h"
#include "kernobjc/runtime.h"
#include "types.h"
#include "associative.h"
#include "class_extra.h"
#include "class.h"
#include "sarray2.h"
#include "dtable.h"
#include "spinlock.h"
#include "selector.h"
#include "utils.h"
#include "init.h"
/* Needed for lock name allocations. */
#define POOL_TYPE char
#define POOL_NAME objc_associative
#define POOL_MALLOC_TYPE M_FAKE_CLASS_TYPE
#define POOL_FREE_FORM_SIZE 1
#include "pool.h"
#define REF_CNT 10
struct reference {
void *key;
void *value;
objc_AssociationPolicy policy;
};
struct reference_list {
struct reference_list *next;
objc_rw_lock lock;
struct reference refs[REF_CNT];
};
/*
* Associated objects install a fake class for each object that uses them.
*
* The class isn't an actual class, but a fake class.
*/
struct objc_assoc_fake_class {
OBJC_CLASS_COMMON_FIELDS;
/* Reference list follows. */
struct reference_list list;
};
/*
* Looks for the first fake class in the class hierarchy.
*/
static Class
_objc_find_class_for_object(id object)
{
Class cl = object->isa;
objc_debug_log("Looking for a class for object (%p), obj->isa = %p\n", object, cl);
while (cl != Nil && !cl->flags.fake) {
objc_debug_log("Getting a superclass of %p[%s%s]\n", cl, class_getName(cl), cl->flags.meta ? "::meta" : "");
cl = class_getSuperclass(cl);
}
return cl;
}
static void
_objc_associated_object_cxx_destruct(id self, SEL _cmd)
{
struct objc_assoc_fake_class *cl;
cl = (struct objc_assoc_fake_class *)_objc_find_class_for_object(self);
objc_remove_associated_objects(self);
free_dtable((dtable_t*)&cl->dtable);
objc_dealloc(cl, M_FAKE_CLASS_TYPE);
}
/*
* Returns a unique name for a lock based on the class name and object pointer.
*/
static inline char *
_objc_unique_lock_name_for_object(id object, Class cl)
{
/*
* The lock names need to be unique, so we're actually allocating the
* name. We have a common prefix, that is suffixed by the object's
* class' name and a hex of the obj pointer.
*/
const char *name_prefix = "objc_assoc_fake_class_lock_";
const char *class_name = object_getClassName(object);
/* We need a format 0x1234 - 0x == 2 + sizeof(void*)*2 */
unsigned pointer_str_len = ((sizeof(void*) * 2) + 2);
char pointer_str[pointer_str_len];
/* Interestingly, the preprocessor uses some builtin snprintf, which
* segfaults, which might be a temporary Clang bug, though...
*/
int(*fn)(char*,size_t,const char*,...) = objc_format_string;
fn(pointer_str, pointer_str_len - 1, "%p", object);
pointer_str[pointer_str_len] = '\0'; /* NULL termination */
/* Now concat it */
unsigned int name_prefix_len = objc_strlen(name_prefix);
unsigned int class_name_len = objc_strlen(class_name);
unsigned int lock_name_len = name_prefix_len + class_name_len
+ pointer_str_len;
char *lock_name = objc_associative_pool_alloc(lock_name_len);
objc_copy_memory(lock_name, name_prefix, name_prefix_len);
objc_copy_memory(lock_name + name_prefix_len, class_name,
class_name_len);
objc_copy_memory(lock_name + name_prefix_len + class_name_len,
pointer_str, pointer_str_len + 1); /* +1 for NULL-term */
return lock_name;
}
/*
* Looks for the fake class in the class hierarchy. If not found
* and create == YES, allocates it and installs the isa pointer.
*/
static Class
_objc_class_for_object(id object, BOOL create)
{
Class superclass = object->isa;
struct objc_assoc_fake_class *cl;
cl = (struct objc_assoc_fake_class *)_objc_find_class_for_object(object);
if (cl == NULL && create){
volatile int *spin_lock = lock_for_pointer(object);
lock_spinlock(spin_lock);
cl = (struct objc_assoc_fake_class*)_objc_find_class_for_object(object);
if (cl != NULL){
/* Someone was faster. */
return (Class)cl;
}
cl = objc_zero_alloc(sizeof(struct objc_assoc_fake_class),
M_FAKE_CLASS_TYPE);
cl->isa = superclass->isa;
cl->super_class = superclass;
cl->dtable = uninstalled_dtable;
cl->flags.fake = YES;
cl->flags.resolved = YES;
char *lock_name = _objc_unique_lock_name_for_object(object, superclass);
objc_debug_log("Created lock name: %s\n", lock_name);
objc_rw_lock_init(&cl->list.lock,
lock_name);
object->isa = (Class)cl;
unlock_spinlock(spin_lock);
objc_debug_log("Created fake class (%s[%p]) for object %p\n",
class_getName(superclass),
cl,
object);
class_addMethod((Class)cl,
objc_cxx_destruct_selector,
(IMP)_objc_associated_object_cxx_destruct,
sel_getTypes(objc_cxx_destruct_selector));
}
return (Class)cl;
}
/*
* Returns a reference to the objc_object_ref_list struct for
* that particular object, or NULL if none exists and create == NULL.
*/
static struct reference_list *
_objc_ref_list_for_object(id object, BOOL create)
{
if (object->isa->flags.meta){
/*
* It's a class - install the associated object directly onto
* the metaclass.
*/
Class cl = (Class)object;
void **extra_space;
extra_space = objc_class_extra_with_identifier(cl,
OBJC_ASSOCIATED_OBJECTS_IDENTIFIER);
if (*extra_space == NULL && create){
objc_debug_log("Creating ref list on class %s\n", class_getName(cl));
struct reference_list *list;
list = objc_zero_alloc(sizeof(struct reference_list),
M_REFLIST_TYPE);
/*
* The lock names need to be unique, so we're actually allocating the
* name. We have a common prefix, that is suffixed by the object's
* class' name and a hex of the obj pointer.
*/
objc_rw_lock_init(&list->lock,
_objc_unique_lock_name_for_object(object, cl));
*extra_space = list;
}
return *extra_space;
}
/*
* It's not a class, create a fake class and update the isa pointer
* (if create == YES)
*/
Class cl = _objc_class_for_object(object, create);
if (cl != Nil){
return &((struct objc_assoc_fake_class*)cl)->list;
}
return NULL;
}
/*
* Returns YES when the policy is one of the atomic ones.
*/
static inline BOOL
_objc_is_policy_atomic(objc_AssociationPolicy policy)
{
static objc_AssociationPolicy const OBJC_ASSOCIATION_ATOMIC = 0x300;
return (policy & OBJC_ASSOCIATION_ATOMIC) == OBJC_ASSOCIATION_ATOMIC;
}
/*
* Disposes of the object according to the policy. This pretty much means
* releasing the object unless the policy was assign.
*/
static inline void
_objc_dispose_of_object_according_to_policy(id object,
objc_AssociationPolicy policy)
{
if (policy == OBJC_ASSOCIATION_WEAK_REF){
void **address = (void**)object;
if (address != NULL){
*(void**)address = NULL;
}
}else if (policy != OBJC_ASSOCIATION_ASSIGN){
objc_release(object);
}
}
static inline struct reference *
_objc_find_key_reference_in_list(struct reference_list *list, void *key)
{
while (list != NULL){
for (int i = 0; i < REF_CNT; ++i){
if (list->refs[i].key == key){
return &list->refs[i];
}
}
list = list->next;
}
return NULL;
}
static inline struct reference *
_objc_find_free_reference_in_list(struct reference_list *list, BOOL create)
{
while (list != NULL){
for (int i = 0; i < REF_CNT; ++i){
if (list->refs[i].key == NULL){
return &list->refs[i];
}
}
/*
* Haven't found any free ref field and we're at the end of the
* linked list - if creation is allowed, allocate.
*/
if (list->next == NULL && create){
list->next = objc_zero_alloc(sizeof(struct reference_list),
M_REFLIST_TYPE);
return &list->next->refs[0];
}
list = list->next;
}
return NULL;
}
static void
_objc_remove_associative_list(struct reference_list *prev,
struct reference_list *list,
volatile int *lock,
BOOL free){
if (list->next != NULL){
_objc_remove_associative_list(list, list->next, lock, free);
}
/*
* Need to lock it for the pointer reassignement.
*/
if (prev != NULL){
lock_spinlock(lock);
prev->next = list->next;
unlock_spinlock(lock);
}
for (int i = 0; i < REF_CNT; ++i){
struct reference *ref = &list->refs[i];
if (ref->key != NULL){
_objc_dispose_of_object_according_to_policy(ref->value,
ref->policy);
}
ref->key = NULL;
ref->value = NULL;
ref->policy = 0;
}
if (free){
objc_dealloc(list, M_REFLIST_TYPE);
}
}
static inline void
_objc_remove_associative_lists_for_object(id object)
{
volatile int *spin_lock = lock_for_pointer(object);
if (object->isa->flags.meta){
void **extra_space;
extra_space = objc_class_extra_with_identifier((Class)object,
OBJC_ASSOCIATED_OBJECTS_IDENTIFIER);
if (*extra_space == NULL){
return;
}
struct reference_list *list = *extra_space;
if (list->next != NULL){
_objc_remove_associative_list(list,
list->next,
spin_lock,
NO);
}
objc_rw_lock_destroy(&list->lock);
_objc_remove_associative_list(NULL,
list,
spin_lock,
YES);
*extra_space = NULL;
}else{
struct objc_assoc_fake_class *cl;
cl = (struct objc_assoc_fake_class*)
_objc_class_for_object(object, NO);
if (cl == NULL){
return;
}
if (cl->list.next != NULL){
_objc_remove_associative_list(&cl->list, cl->list.next,
spin_lock, YES);
}
objc_rw_lock_destroy(&cl->list.lock);
_objc_remove_associative_list(NULL, &cl->list,
spin_lock, NO);
}
}
#pragma mark -
#pragma mark Public Functions
id
objc_get_associated_object(id object, void *key)
{
if (object == nil || key == NULL){
return nil;
}
if (objc_object_is_small_object(object)){
return nil;
}
id result = nil;
struct reference_list *list = _objc_ref_list_for_object(object, NO);
if (list == NULL){
return nil;
}
objc_rw_lock_rlock(&list->lock);
struct reference *ref = _objc_find_key_reference_in_list(list, key);
objc_rw_lock_unlock(&list->lock);
if (ref != NULL){
switch (ref->policy) {
case OBJC_ASSOCIATION_ASSIGN:
result = ref->value;
break;
case OBJC_ASSOCIATION_RETAIN_NONATOMIC:
result = objc_retain(ref->value);
break;
case OBJC_ASSOCIATION_COPY_NONATOMIC:
result = objc_copy(ref->value);
break;
case OBJC_ASSOCIATION_COPY:
/* This is atomic, need to lock the WLOCK */
objc_rw_lock_wlock(&list->lock);
result = objc_copy(ref->value);
objc_rw_lock_unlock(&list->lock);
break;
case OBJC_ASSOCIATION_RETAIN:
/* This is atomic, need to lock the WLOCK */
objc_rw_lock_wlock(&list->lock);
result = objc_retain(ref->value);
objc_rw_lock_unlock(&list->lock);
break;
default:
break;
}
}
return result;
}
void
objc_set_associated_object(id object, void *key, id value,
objc_AssociationPolicy policy)
{
if (object == nil || key == NULL){
return;
}
if (objc_object_is_small_object(object)){
return;
}
struct reference_list *list = _objc_ref_list_for_object(object, YES);
/* WLOCK because we may be actually modifying the linked list */
objc_rw_lock_wlock(&list->lock);
struct reference *ref = _objc_find_key_reference_in_list(list, key);
if (ref == NULL){
/* No reference, perhaps need to allocate some */
ref = _objc_find_free_reference_in_list(list, YES);
objc_assert(ref != NULL,
"_objc_reference_create_for_object hasn't"
" created reference list for object %p!\n", object);
}
/*
* See whether either policy is atomic and if not, we can unlock the
* lock. Otherwise, keep it locked and unlock it after all the
* modification of ref.
*/
BOOL unlocked = NO;
BOOL either_policy_atomic = _objc_is_policy_atomic(policy)
|| _objc_is_policy_atomic(ref->policy);
if (!either_policy_atomic){
unlocked = YES;
objc_rw_lock_unlock(&list->lock);
}
switch (policy) {
case OBJC_ASSOCIATION_RETAIN_NONATOMIC:
case OBJC_ASSOCIATION_RETAIN:
value = objc_retain(value);
break;
case OBJC_ASSOCIATION_COPY_NONATOMIC:
case OBJC_ASSOCIATION_COPY:
value = objc_copy(value);
break;
default:
break;
}
objc_AssociationPolicy old_policy = ref->policy;
id old_value = ref->value;
ref->key = key;
ref->policy = policy;
ref->value = value;
_objc_dispose_of_object_according_to_policy(old_value, old_policy);
/*
* If either of the policies was atomic, the lock is still locked, need
* to unlock it.
*/
if (either_policy_atomic){
unlocked = YES;
objc_rw_lock_unlock(&list->lock);
}
objc_assert(unlocked,
"The lock associated with object %p wasn't unloacked!\n", object);
}
int
objc_sync_enter(id obj)
{
if (obj == nil || objc_object_is_small_object(obj)){
return 0;
}
struct reference_list *list = _objc_ref_list_for_object(obj, YES);
objc_rw_lock_wlock(&list->lock);
return 0;
}
int
objc_sync_exit(id obj)
{
if (obj == nil || objc_object_is_small_object(obj)){
return 0;
}
struct reference_list *list = _objc_ref_list_for_object(obj, NO);
if (list == NULL){
objc_abort("Unlocking an object that obviously wasn't locked.\n");
return 0;
}
return objc_rw_lock_unlock(&list->lock);
}
void
objc_remove_associated_objects(id object)
{
if (object == nil){
return;
}
if (objc_object_is_small_object(object)){
return;
}
_objc_remove_associative_lists_for_object(object);
}
void
objc_remove_associated_weak_refs(id object)
{
if (objc_object_is_small_object(object)){
/* Small objects do not have associated objects */
return;
}
/*
* This function is quite similar to the objc_remove_associated_objects,
* but doesn't actually free any objects, or any of the
* objc_object_ref_list structures.
*/
struct reference_list *list = _objc_ref_list_for_object(object, NO);
if (list == NULL){
/* Nothing to do */
return;
}
/*
* Go through the lists and remove all weak refs.
*/
objc_rw_lock *lock = &list->lock;
objc_rw_lock_wlock(lock);
while (list != NULL) {
for (int i = 0; i < REF_CNT; ++i){
struct reference *ref = &list->refs[i];
if (ref->policy == OBJC_ASSOCIATION_WEAK_REF){
if (ref->value != NULL){
*(void**)ref->value = NULL;
}
ref->value = nil;
ref->key = NULL;
ref->policy = 0;
}
}
list = list->next;
}
objc_rw_lock_unlock(lock);
}
PRIVATE void
objc_associated_objects_init(void)
{
/*
* No-op at this moment.
*/
}
PRIVATE void
objc_associated_objects_destroy(void)
{
objc_associative_pool_free();
}