-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharc.c
491 lines (414 loc) · 10.2 KB
/
arc.c
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
#include "os.h"
#include "kernobjc/types.h"
#include "types.h"
#include "kernobjc/arc.h"
#include "kernobjc/message.h"
#include "selector.h"
#include "message.h"
#include "class.h"
#include "associative.h"
#include "init.h"
#include "blocks.h"
/*
* Each autorelease pool ~ a page. We need the previous and top
* pointers, however, so the first two slots are taken.
*/
#define AUTORELEASE_POOL_SIZE ((PAGE_SIZE / sizeof(void*)) - 2)
/* Default hook for _objc_weak_load() */
static id
__objc_weak_load_default_hook(id object) { return object; }
/*
* Returns the object if it is not currently in the process of being
* deallocated. Returns nil otherwise.
*
* This hook must be set for weak references to work with automatic reference
* counting.
*/
id (*_objc_weak_load)(id object) = __objc_weak_load_default_hook;
struct objc_autorelease_pool {
struct objc_autorelease_pool *previous;
id *top;
id pool[AUTORELEASE_POOL_SIZE];
};
struct objc_arc_thread_data {
struct objc_autorelease_pool *pool;
};
struct object {
Class isa;
int retain_count;
};
static int objc_autorelease_object_count = 0;
static objc_tls_key objc_autorelease_pool_tls_key = 0;
/* Forward declarations of inline functions: */
static inline struct objc_autorelease_pool *
_objc_create_pool_if_necessary(struct objc_arc_thread_data *data);
static inline struct objc_arc_thread_data *
_objc_get_arc_thread_data(void){
struct objc_arc_thread_data *data = (struct objc_arc_thread_data*)
objc_get_tls_for_key(objc_autorelease_pool_tls_key);
if (data == NULL){
data = objc_zero_alloc(sizeof(struct objc_arc_thread_data),
M_AUTORELEASE_POOL_TYPE);
objc_set_tls_for_key(data, objc_autorelease_pool_tls_key);
}
return data;
}
static inline id
_objc_retain(id obj)
{
if (objc_object_is_small_object(obj)) {
return obj;
}
Class cls = obj->isa;
if ((Class)&_NSConcreteMallocBlock == cls ||
(Class)&_NSConcreteStackBlock == cls){
return _Block_copy(obj);
}
if (cls->flags.has_custom_arr) {
/* The class has custom ARR methods, send the message. */
return objc_send_retain_msg(obj);
}
/*
* The kernel objc run-time assumes
* that the objects actually have the retain count variable
* directly after the isa pointer.
*/
struct object *object = (struct object *)obj;
__sync_add_and_fetch(&(object->retain_count), 1);
return obj;
}
static inline void
_objc_release(id obj)
{
if (objc_object_is_small_object(obj)) {
return;
}
Class cls = obj->isa;
if (cls == &_NSConcreteMallocBlock){
_Block_release(obj);
return;
}
if ((cls == &_NSConcreteStackBlock) ||
(cls == &_NSConcreteGlobalBlock)){
return;
}
if (cls->flags.has_custom_arr) {
/* The class has custom ARR methods, send the message. */
objc_send_release_msg(obj);
return;
}
/*
* The kernel objc run-time assumes that the objects actually have the
* retain count variable directly after the isa pointer.
*/
struct object *object = (struct object *)obj;
if (__sync_sub_and_fetch(&(object->retain_count), 1) < 0) {
objc_delete_weak_refs(obj);
objc_send_dealloc_msg(obj);
}
}
static inline id
_objc_autorelease(id obj)
{
if (objc_object_is_small_object(obj)) {
return obj;
}
if (obj->isa->flags.has_custom_arr) {
/* Let the object's method handle it */
return objc_send_autorelease_msg(obj);
}
struct objc_arc_thread_data *data = _objc_get_arc_thread_data();
if (data != NULL){
/* Data == NULL generally only if allocation fails. */
struct objc_autorelease_pool *pool;
pool = _objc_create_pool_if_necessary(data);
++objc_autorelease_object_count;
*pool->top = obj;
++pool->top;
return obj;
}
/*
* Last resort - don't send the autorelease message since it can result in
* an inifinite loop.
*/
return obj;
}
/*
* Empties autorelease pools until it hits the stop pointer.
*/
static void
_objc_empty_pool_until(struct objc_arc_thread_data *data, id *stop)
{
struct objc_autorelease_pool *stop_pool = NULL;
if (stop != NULL) {
stop_pool = data->pool;
while (YES) {
if (stop_pool == NULL){
/*
* We haven't found the stop point in the pool
* list.
*/
return;
}
if ((stop >= stop_pool->pool) &&
(stop < &stop_pool->pool[AUTORELEASE_POOL_SIZE])) {
/* If the stop location is in this pool, stop */
break;
}
stop_pool = stop_pool->previous;
}
}
while (data->pool != stop_pool) {
/*
* We need to operate with (data->pool) and not just
* a cached ptr since the release may cause some
* additional autoreleases, which could cause
* a new pool to be installed.
*/
while (data->pool->top > data->pool->pool) {
--data->pool->top;
_objc_release(*data->pool->top);
--objc_autorelease_object_count;
}
/* Dispose of the pool itself */
struct objc_autorelease_pool *pool = data->pool;
data->pool = pool->previous;
objc_dealloc(pool, M_AUTORELEASE_POOL_TYPE);
}
/*
* We've reached a point where data->pool == stop_pool.
* Now, if there actually is a pool to be dealt with, we need
* to reach the stop point in that pool.
*/
if (data->pool != NULL){
while ((stop == NULL || (data->pool->top > stop)) &&
(data->pool->top > data->pool->pool)) {
--data->pool->top;
_objc_release(*data->pool->top);
--objc_autorelease_object_count;
}
}
}
/*
* Empties all the pools in the thread data supplied.
*/
static void
_objc_cleanup_pools(struct objc_arc_thread_data *data)
{
if (data->pool != NULL){
_objc_empty_pool_until(data, NULL);
objc_assert(data->pool == NULL,
"The pool should have been emptied!\n");
objc_dealloc(data, M_AUTORELEASE_POOL_TYPE);
}
}
static inline struct objc_autorelease_pool *
_objc_create_pool_if_necessary(struct objc_arc_thread_data *data)
{
struct objc_autorelease_pool *pool = data->pool;
if (pool == NULL || pool->top >= &pool->pool[AUTORELEASE_POOL_SIZE]){
/*
* Either there is no pool, or we've run out of space in
* the existing one.
*/
pool = objc_alloc_page(M_AUTORELEASE_POOL_TYPE);
pool->previous = data->pool;
pool->top = pool->pool;
data->pool = pool;
}
return pool;
}
#pragma mark -
#pragma mark Public functions
#pragma mark Autorelease Pool
void *
objc_autoreleasePoolPush(void)
{
struct objc_arc_thread_data *data = _objc_get_arc_thread_data();
if (data != NULL){
struct objc_autorelease_pool *pool;
pool = _objc_create_pool_if_necessary(data);
return pool->top;
}
objc_log("An issue occurred when pushing a pool"
" - thread data %p, data->pool %p\n",
data, data == NULL ? 0 : data->pool);
return NULL;
}
void
objc_autoreleasePoolPop(void *pool)
{
struct objc_arc_thread_data *data = _objc_get_arc_thread_data();
if (data != NULL && data->pool != NULL){
_objc_empty_pool_until(data, pool);
}else{
objc_log("An issue occurred when popping a pool %p"
" - thread data %p, data->pool %p\n",
pool, data, data == NULL ? 0 : data->pool);
}
}
#pragma mark -
#pragma mark Ref Count Management
id
objc_autorelease(id obj)
{
if (obj != nil){
obj = _objc_autorelease(obj);
}
return obj;
}
id
objc_retain(id obj)
{
if (obj != nil){
obj = _objc_retain(obj);
}
return obj;
}
id
objc_copy(id obj)
{
return objc_send_copy_msg(obj);
}
void
objc_release(id obj)
{
if (obj != nil){
_objc_release(obj);
}
}
id
objc_retainAutorelease(id obj)
{
return objc_autorelease(objc_retain(obj));
}
id
objc_storeStrong(id *addr, id value)
{
value = objc_retain(value);
objc_release(*addr);
*addr = value;
return value;
}
#pragma mark -
#pragma mark Weak Refs
/*
* We are using a special association type to implement weak refs.
* This association type zeroes the *addr on objc_remove_associated_objects().
*
* We need to store the same value for key and value in the associated object.
* To simply remove the weak ref, store nil into the value for key.
*/
objc_rw_lock objc_weak_refs_lock;
id
objc_storeWeak(id *addr, id obj)
{
OBJC_LOCK_FOR_SCOPE(&objc_weak_refs_lock);
id old = *addr;
if (obj == nil){
/* Remove the reference by setting it to nil */
objc_set_associated_object(old, addr, nil,
OBJC_ASSOCIATION_WEAK_REF);
*addr = nil;
return nil;
}
Class cl = objc_object_get_class_inline(obj);
if (&_NSConcreteGlobalBlock == cl){
// If this is a global block, it's never deallocated, so secretly make
// this a strong reference
// TODO: We probably also want to do the same for constant strings and
// classes.
*addr = obj;
return obj;
}
if (&_NSConcreteMallocBlock == cl){
obj = block_load_weak(obj);
}else if (cl->flags.has_custom_arr){
obj = _objc_weak_load(obj);
}else{
if (((struct object*)obj)->retain_count < 0){
obj = nil;
}
}
objc_set_associated_object(obj, addr, (id)addr,
OBJC_ASSOCIATION_WEAK_REF);
*addr = obj;
return obj;
}
void
objc_delete_weak_refs(id obj)
{
OBJC_LOCK_FOR_SCOPE(&objc_weak_refs_lock);
objc_remove_associated_weak_refs(obj);
}
id
objc_loadWeakRetained(id *addr)
{
OBJC_LOCK_FOR_SCOPE(&objc_weak_refs_lock);
id obj = *addr;
if (obj == nil){
return nil;
}
if (objc_object_is_small_object(obj)){
return obj;
}
// TODO malloc block
if (obj->isa->flags.has_custom_arr){
obj = _objc_weak_load(obj);
}else{
if (((struct object *)obj)->retain_count < 0){
return nil;
}
}
return objc_retain(obj);
}
id
objc_loadWeak(id *obj)
{
return objc_autorelease(objc_loadWeakRetained(obj));
}
void
objc_copyWeak(id *dest, id *src)
{
objc_release(objc_initWeak(dest, objc_loadWeakRetained(src)));
}
void
objc_moveWeak(id *dest, id *src)
{
OBJC_LOCK_FOR_SCOPE(&objc_weak_refs_lock);
id obj = *src;
*dest = *src;
*src = nil;
if (obj != nil){
/* Set nil for the old reference and add the new reference */
objc_set_associated_object(obj, src, nil,
OBJC_ASSOCIATION_WEAK_REF);
objc_set_associated_object(obj, dest, (id)dest,
OBJC_ASSOCIATION_WEAK_REF);
}
}
void
objc_destroyWeak(id *obj)
{
objc_storeWeak(obj, nil);
}
id objc_initWeak(id *object, id value)
{
*object = nil;
return objc_storeWeak(object, value);
}
#pragma mark -
#pragma mark Init Function
void
objc_arc_init(void)
{
objc_rw_lock_init(&objc_weak_refs_lock, "objc_weak_refs_lock");
objc_register_tls(&objc_autorelease_pool_tls_key,
(objc_tls_descructor)_objc_cleanup_pools);
}
void
objc_arc_destroy(void)
{
objc_rw_lock_destroy(&objc_weak_refs_lock);
objc_deregister_tls(objc_autorelease_pool_tls_key);
}