-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathobject.m
622 lines (514 loc) · 13.2 KB
/
object.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
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*
* Copyright (c) 2011-2014 Apple Inc. All rights reserved.
*
* @APPLE_APACHE_LICENSE_HEADER_START@
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @APPLE_APACHE_LICENSE_HEADER_END@
*/
#include "internal.h"
#if USE_OBJC
#if _OS_OBJECT_OBJC_ARC
#error "Cannot build with ARC"
#endif
#if defined(__OBJC_GC__)
#error Objective C GC isn't supported anymore
#endif
#if __has_include(<objc/objc-internal.h>)
#include <objc/objc-internal.h>
#else
extern id _Nullable objc_retain(id _Nullable obj) __asm__("_objc_retain");
extern void objc_release(id _Nullable obj) __asm__("_objc_release");
extern void _objc_init(void);
extern void _objc_atfork_prepare(void);
extern void _objc_atfork_parent(void);
extern void _objc_atfork_child(void);
#endif // __has_include(<objc/objc-internal.h>)
#include <objc/objc-exception.h>
#include <Foundation/NSString.h>
// NOTE: this file must not contain any atomic operations
#pragma mark -
#pragma mark _os_object_t
static inline id
_os_objc_alloc(Class cls, size_t size)
{
id obj;
size -= sizeof(((struct _os_object_s *)NULL)->os_obj_isa);
while (unlikely(!(obj = class_createInstance(cls, size)))) {
_dispatch_temporary_resource_shortage();
}
return obj;
}
static void*
_os_objc_destructInstance(id obj)
{
// noop if only Libystem is loaded
return obj;
}
#if DISPATCH_COCOA_COMPAT
static bool _os_object_debug_missing_pools;
#endif
void
_os_object_init(void)
{
_objc_init();
Block_callbacks_RR callbacks = {
sizeof(Block_callbacks_RR),
(void (*)(const void *))&objc_retain,
(void (*)(const void *))&objc_release,
(void (*)(const void *))&_os_objc_destructInstance
};
_Block_use_RR2(&callbacks);
#if DISPATCH_COCOA_COMPAT
const char *v = getenv("OBJC_DEBUG_MISSING_POOLS");
if (v) _os_object_debug_missing_pools = _dispatch_parse_bool(v);
v = getenv("DISPATCH_DEBUG_MISSING_POOLS");
if (v) _os_object_debug_missing_pools = _dispatch_parse_bool(v);
v = getenv("LIBDISPATCH_DEBUG_MISSING_POOLS");
if (v) _os_object_debug_missing_pools = _dispatch_parse_bool(v);
#endif
}
_os_object_t
_os_object_alloc_realized(const void *cls, size_t size)
{
dispatch_assert(size >= sizeof(struct _os_object_s));
return _os_objc_alloc(cls, size);
}
_os_object_t
_os_object_alloc(const void *_cls, size_t size)
{
dispatch_assert(size >= sizeof(struct _os_object_s));
Class cls = _cls ? [(id)_cls class] : [OS_OBJECT_CLASS(object) class];
return _os_objc_alloc(cls, size);
}
void
_os_object_dealloc(_os_object_t obj)
{
[obj dealloc];
}
void
_os_object_xref_dispose(_os_object_t obj)
{
struct _os_object_s *o = (struct _os_object_s *)obj;
_os_object_xrefcnt_dispose_barrier(o);
[obj _xref_dispose];
}
void
_os_object_dispose(_os_object_t obj)
{
struct _os_object_s *o = (struct _os_object_s *)obj;
_os_object_refcnt_dispose_barrier(o);
_os_object_dealloc(obj);
}
#undef os_retain
void*
os_retain(void *obj)
{
return objc_retain(obj);
}
#undef os_release
void
os_release(void *obj)
{
return objc_release(obj);
}
void
_os_object_atfork_prepare(void)
{
return _objc_atfork_prepare();
}
void
_os_object_atfork_parent(void)
{
return _objc_atfork_parent();
}
void
_os_object_atfork_child(void)
{
return _objc_atfork_child();
}
#pragma mark -
#pragma mark _os_object
@implementation OS_OBJECT_CLASS(object)
DISPATCH_UNAVAILABLE_INIT()
-(id)retain {
return _os_object_retain(self);
}
-(oneway void)release {
return _os_object_release_without_xref_dispose(self);
}
-(NSUInteger)retainCount {
return _os_object_retain_count(self);
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations"
-(BOOL)retainWeakReference {
return _os_object_retain_weak(self);
}
-(BOOL)allowsWeakReference {
return _os_object_allows_weak_reference(self);
}
#pragma clang diagnostic pop
- (void)_xref_dispose {
return _os_object_release_internal(self);
}
@end
#pragma mark -
#pragma mark _dispatch_objc
#if OS_OBJECT_HAVE_OBJC2
id
_dispatch_objc_alloc(Class cls, size_t size)
{
return _os_objc_alloc(cls, size);
}
void
_dispatch_objc_retain(dispatch_object_t dou)
{
return (void)os_retain(dou);
}
void
_dispatch_objc_release(dispatch_object_t dou)
{
return os_release(dou);
}
void
_dispatch_objc_set_context(dispatch_object_t dou, void *context)
{
return [dou _setContext:context];
}
void *
_dispatch_objc_get_context(dispatch_object_t dou)
{
return [dou _getContext];
}
void
_dispatch_objc_set_finalizer_f(dispatch_object_t dou,
dispatch_function_t finalizer)
{
return [dou _setFinalizer:finalizer];
}
void
_dispatch_objc_set_target_queue(dispatch_object_t dou, dispatch_queue_t queue)
{
return [dou _setTargetQueue:queue];
}
void
_dispatch_objc_suspend(dispatch_object_t dou)
{
return [dou _suspend];
}
void
_dispatch_objc_resume(dispatch_object_t dou)
{
return [dou _resume];
}
void
_dispatch_objc_activate(dispatch_object_t dou)
{
return [dou _activate];
}
size_t
_dispatch_objc_debug(dispatch_object_t dou, char* buf, size_t bufsiz)
{
NSUInteger offset = 0;
NSString *desc = [dou debugDescription];
[desc getBytes:buf maxLength:bufsiz-1 usedLength:&offset
encoding:NSUTF8StringEncoding options:(NSStringEncodingConversionOptions)0
range:NSMakeRange(0, [desc length]) remainingRange:NULL];
if (offset) buf[offset] = 0;
return offset;
}
#endif
#pragma mark -
#pragma mark _dispatch_object
@implementation DISPATCH_CLASS(object)
DISPATCH_UNAVAILABLE_INIT()
- (NSString *)debugDescription {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
char buf[2048];
struct dispatch_object_s *obj = (struct dispatch_object_s *)self;
if (dx_vtable(obj)->do_debug) {
dx_debug(obj, buf, sizeof(buf));
} else {
strlcpy(buf, object_getClassName(self), sizeof(buf));
}
NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
if (!format) return nil;
return [nsstring stringWithFormat:format, object_getClassName(self), buf];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)dealloc {
return _dispatch_dispose(self);
}
#pragma clang diagnostic pop
@end
OS_OBJECT_NONLAZY_CLASS
@implementation DISPATCH_CLASS(queue)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE()
- (NSString *)description {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
if (!format) return nil;
return [nsstring stringWithFormat:format, object_getClassName(self),
dispatch_queue_get_label(self), self];
}
- (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
[super _xref_dispose];
}
@end
OS_OBJECT_NONLAZY_CLASS
@implementation DISPATCH_CLASS(channel)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE()
- (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
_dispatch_channel_xref_dispose(self);
[super _xref_dispose];
}
@end
OS_OBJECT_NONLAZY_CLASS
@implementation DISPATCH_CLASS(source)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE()
- (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
_dispatch_source_xref_dispose(self);
[super _xref_dispose];
}
@end
OS_OBJECT_NONLAZY_CLASS
@implementation DISPATCH_CLASS(mach)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE()
- (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
_dispatch_mach_xref_dispose((struct dispatch_mach_s *)self);
[super _xref_dispose];
}
@end
OS_OBJECT_NONLAZY_CLASS
@implementation DISPATCH_CLASS(queue_runloop)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE()
- (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
_dispatch_runloop_queue_xref_dispose((dispatch_lane_t)self);
[super _xref_dispose];
}
@end
#define EMPTY_OS_OBJECT_CLASS_IMPL(name) \
OS_OBJECT_NONLAZY_CLASS \
@implementation name \
OS_OBJECT_NONLAZY_CLASS_LOAD \
DISPATCH_UNAVAILABLE_INIT() \
@end
#define DISPATCH_CLASS_IMPL(name) \
EMPTY_OS_OBJECT_CLASS_IMPL(DISPATCH_CLASS(name))
#if !DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
DISPATCH_CLASS_IMPL(data)
#endif
DISPATCH_CLASS_IMPL(semaphore)
DISPATCH_CLASS_IMPL(group)
DISPATCH_CLASS_IMPL(workloop)
DISPATCH_CLASS_IMPL(queue_serial)
DISPATCH_CLASS_IMPL(queue_concurrent)
DISPATCH_CLASS_IMPL(queue_main)
DISPATCH_CLASS_IMPL(queue_global)
#if DISPATCH_USE_PTHREAD_ROOT_QUEUES
DISPATCH_CLASS_IMPL(queue_pthread_root)
#endif
DISPATCH_CLASS_IMPL(queue_cooperative)
DISPATCH_CLASS_IMPL(queue_mgr)
DISPATCH_CLASS_IMPL(queue_attr)
DISPATCH_CLASS_IMPL(mach_msg)
DISPATCH_CLASS_IMPL(io)
DISPATCH_CLASS_IMPL(operation)
DISPATCH_CLASS_IMPL(disk)
#pragma mark os_workgroups
@implementation OS_OBJECT_CLASS(os_workgroup)
DISPATCH_UNAVAILABLE_INIT()
OS_OBJECT_USES_XREF_DISPOSE()
- (void)_xref_dispose {
_os_workgroup_xref_dispose(self);
[super _xref_dispose];
}
- (void) dealloc {
_os_workgroup_dispose(self);
[super dealloc];
}
- (NSString *) debugDescription {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
char buf[2048];
os_workgroup_t wg = (os_workgroup_t) self;
_os_workgroup_debug(wg, buf, sizeof(buf));
return [nsstring stringWithUTF8String:buf];
}
@end
@implementation OS_OBJECT_CLASS(os_workgroup_interval)
DISPATCH_UNAVAILABLE_INIT()
- (void) _xref_dispose {
_os_workgroup_interval_xref_dispose(self);
[super _xref_dispose];
}
- (void) dealloc {
_os_workgroup_interval_dispose(self);
[super dealloc];
}
@end
@implementation OS_OBJECT_CLASS(os_workgroup_parallel)
DISPATCH_UNAVAILABLE_INIT()
@end
#pragma mark eventlink
@implementation OS_OBJECT_CLASS(os_eventlink)
DISPATCH_UNAVAILABLE_INIT()
- (void) dealloc {
_os_eventlink_dispose(self);
[super dealloc];
}
@end
#pragma mark vouchers
OS_OBJECT_NONLAZY_CLASS
@implementation OS_OBJECT_CLASS(voucher)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
-(id)retain {
return (id)_voucher_retain_inline((struct voucher_s *)self);
}
-(oneway void)release {
return _voucher_release_inline((struct voucher_s *)self);
}
- (void)dealloc {
_voucher_dispose(self);
[super dealloc];
}
- (NSString *)debugDescription {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
char buf[2048];
_voucher_debug(self, buf, sizeof(buf));
NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
if (!format) return nil;
return [nsstring stringWithFormat:format, object_getClassName(self), buf];
}
@end
#if VOUCHER_ENABLE_RECIPE_OBJECTS
OS_OBJECT_NONLAZY_CLASS
@implementation OS_OBJECT_CLASS(voucher_recipe)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
- (NSString *)debugDescription {
return nil; // TODO: voucher_recipe debugDescription
}
@end
#endif
#pragma mark -
#pragma mark dispatch_last_resort_autorelease_pool
#if DISPATCH_COCOA_COMPAT
void
_dispatch_last_resort_autorelease_pool_push(dispatch_invoke_context_t dic)
{
if (likely(!_os_object_debug_missing_pools)) {
dic->dic_autorelease_pool = _dispatch_autorelease_pool_push();
}
}
void
_dispatch_last_resort_autorelease_pool_pop(dispatch_invoke_context_t dic)
{
if (likely(!_os_object_debug_missing_pools)) {
_dispatch_autorelease_pool_pop(dic->dic_autorelease_pool);
dic->dic_autorelease_pool = NULL;
}
}
#endif // DISPATCH_COCOA_COMPAT
#pragma mark -
#pragma mark dispatch_client_callout
// Abort on uncaught exceptions thrown from client callouts rdar://8577499
#if DISPATCH_USE_CLIENT_CALLOUT && !__USING_SJLJ_EXCEPTIONS__ && \
OS_OBJECT_HAVE_OBJC2
// On platforms with zero-cost exceptions, use a compiler-generated catch-all
// exception handler.
DISPATCH_NORETURN extern void objc_terminate(void);
#undef _dispatch_client_callout
void
_dispatch_client_callout(void *ctxt, dispatch_function_t f)
{
@try {
return f(ctxt);
}
@catch (...) {
objc_terminate();
}
}
#undef _dispatch_client_callout2
void
_dispatch_client_callout2(void *ctxt, size_t i, void (*f)(void *, size_t))
{
@try {
return f(ctxt, i);
}
@catch (...) {
objc_terminate();
}
}
#if HAVE_MACH
#undef _dispatch_client_callout3
void
_dispatch_client_callout3(void *ctxt, dispatch_mach_reason_t reason,
dispatch_mach_msg_t dmsg, dispatch_mach_async_reply_callback_t f)
{
@try {
return f(ctxt, reason, dmsg);
}
@catch (...) {
objc_terminate();
}
}
#undef _dispatch_client_callout4
void
_dispatch_client_callout4(void *ctxt, dispatch_mach_reason_t reason,
dispatch_mach_msg_t dmsg, mach_error_t error,
dispatch_mach_handler_function_t f)
{
@try {
return f(ctxt, reason, dmsg, error);
}
@catch (...) {
objc_terminate();
}
}
#endif // HAVE_MACH
#undef _dispatch_client_callout3_a
void
_dispatch_client_callout3_a(void *ctxt, size_t i, size_t w, dispatch_apply_attr_function_t f)
{
@try {
return f(ctxt, i, w);
}
@catch (...) {
objc_terminate();
}
}
#endif // DISPATCH_USE_CLIENT_CALLOUT
#endif // USE_OBJC