2 * Copyright (c) 2011-2014 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
25 #if _OS_OBJECT_OBJC_ARC
26 #error "Cannot build with ARC"
28 #if defined(__OBJC_GC__)
29 #error Objective C GC isn't supported anymore
32 #if __has_include(<objc/objc-internal.h>)
33 #include <objc/objc-internal.h>
35 extern id _Nullable objc_retain(id _Nullable obj) __asm__("_objc_retain");
36 extern void objc_release(id _Nullable obj) __asm__("_objc_release");
37 extern void _objc_init(void);
38 extern void _objc_atfork_prepare(void);
39 extern void _objc_atfork_parent(void);
40 extern void _objc_atfork_child(void);
41 #endif // __has_include(<objc/objc-internal.h>)
42 #include <objc/objc-exception.h>
43 #include <Foundation/NSString.h>
45 // NOTE: this file must not contain any atomic operations
48 #pragma mark _os_object_t
51 _os_objc_alloc(Class cls, size_t size)
54 size -= sizeof(((struct _os_object_s *)NULL)->os_obj_isa);
55 while (!fastpath(obj = class_createInstance(cls, size))) {
56 _dispatch_temporary_resource_shortage();
62 _os_objc_destructInstance(id obj)
64 // noop if only Libystem is loaded
68 #if DISPATCH_COCOA_COMPAT
69 static bool _os_object_debug_missing_pools;
76 Block_callbacks_RR callbacks = {
77 sizeof(Block_callbacks_RR),
78 (void (*)(const void *))&objc_retain,
79 (void (*)(const void *))&objc_release,
80 (void (*)(const void *))&_os_objc_destructInstance
82 _Block_use_RR2(&callbacks);
83 #if DISPATCH_COCOA_COMPAT
84 const char *v = getenv("OBJC_DEBUG_MISSING_POOLS");
85 _os_object_debug_missing_pools = v && !strcmp(v, "YES");
90 _os_object_alloc_realized(const void *cls, size_t size)
92 dispatch_assert(size >= sizeof(struct _os_object_s));
93 return _os_objc_alloc(cls, size);
97 _os_object_alloc(const void *_cls, size_t size)
99 dispatch_assert(size >= sizeof(struct _os_object_s));
100 Class cls = _cls ? [(id)_cls class] : [OS_OBJECT_CLASS(object) class];
101 return _os_objc_alloc(cls, size);
105 _os_object_dealloc(_os_object_t obj)
111 _os_object_xref_dispose(_os_object_t obj)
113 struct _os_object_s *o = (struct _os_object_s *)obj;
114 _os_object_xrefcnt_dispose_barrier(o);
119 _os_object_dispose(_os_object_t obj)
121 struct _os_object_s *o = (struct _os_object_s *)obj;
122 _os_object_refcnt_dispose_barrier(o);
130 return objc_retain(obj);
135 os_release(void *obj)
137 return objc_release(obj);
141 _os_object_atfork_prepare(void)
143 return _objc_atfork_prepare();
147 _os_object_atfork_parent(void)
149 return _objc_atfork_parent();
153 _os_object_atfork_child(void)
155 return _objc_atfork_child();
159 #pragma mark _os_object
161 @implementation OS_OBJECT_CLASS(object)
162 DISPATCH_UNAVAILABLE_INIT()
165 return _os_object_retain(self);
168 -(oneway void)release {
169 return _os_object_release(self);
172 -(NSUInteger)retainCount {
173 return _os_object_retain_count(self);
176 -(BOOL)retainWeakReference {
177 return _os_object_retain_weak(self);
180 -(BOOL)allowsWeakReference {
181 return _os_object_allows_weak_reference(self);
184 - (void)_xref_dispose {
185 return _os_object_release_internal(self);
189 return _os_object_dealloc(self);
195 #pragma mark _dispatch_objc
196 #if OS_OBJECT_HAVE_OBJC2
199 _dispatch_objc_alloc(Class cls, size_t size)
201 return _os_objc_alloc(cls, size);
205 _dispatch_objc_retain(dispatch_object_t dou)
207 return (void)os_retain(dou);
211 _dispatch_objc_release(dispatch_object_t dou)
213 return os_release(dou);
217 _dispatch_objc_set_context(dispatch_object_t dou, void *context)
219 return [dou _setContext:context];
223 _dispatch_objc_get_context(dispatch_object_t dou)
225 return [dou _getContext];
229 _dispatch_objc_set_finalizer_f(dispatch_object_t dou,
230 dispatch_function_t finalizer)
232 return [dou _setFinalizer:finalizer];
236 _dispatch_objc_set_target_queue(dispatch_object_t dou, dispatch_queue_t queue)
238 return [dou _setTargetQueue:queue];
242 _dispatch_objc_suspend(dispatch_object_t dou)
244 return [dou _suspend];
248 _dispatch_objc_resume(dispatch_object_t dou)
250 return [dou _resume];
254 _dispatch_objc_activate(dispatch_object_t dou)
256 return [dou _activate];
260 _dispatch_objc_debug(dispatch_object_t dou, char* buf, size_t bufsiz)
262 NSUInteger offset = 0;
263 NSString *desc = [dou debugDescription];
264 [desc getBytes:buf maxLength:bufsiz-1 usedLength:&offset
265 encoding:NSUTF8StringEncoding options:(NSStringEncodingConversionOptions)0
266 range:NSMakeRange(0, [desc length]) remainingRange:NULL];
267 if (offset) buf[offset] = 0;
273 #pragma mark _dispatch_object
275 // Force non-lazy class realization rdar://10640168
276 #define DISPATCH_OBJC_LOAD() + (void)load {}
278 @implementation DISPATCH_CLASS(object)
279 DISPATCH_UNAVAILABLE_INIT()
282 return _dispatch_dispose(self); // calls _os_object_dealloc()
285 - (NSString *)debugDescription {
286 Class nsstring = objc_lookUpClass("NSString");
287 if (!nsstring) return nil;
289 struct dispatch_object_s *obj = (struct dispatch_object_s *)self;
290 if (dx_vtable(obj)->do_debug) {
291 dx_debug(obj, buf, sizeof(buf));
293 strlcpy(buf, dx_kind(obj), sizeof(buf));
295 NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
296 if (!format) return nil;
297 return [nsstring stringWithFormat:format, class_getName([self class]), buf];
300 - (void)dealloc DISPATCH_NORETURN {
301 DISPATCH_INTERNAL_CRASH(0, "Calling dealloc on a dispatch object");
302 [super dealloc]; // make clang happy
307 @implementation DISPATCH_CLASS(queue)
309 DISPATCH_UNAVAILABLE_INIT()
311 - (NSString *)description {
312 Class nsstring = objc_lookUpClass("NSString");
313 if (!nsstring) return nil;
314 NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
315 if (!format) return nil;
316 return [nsstring stringWithFormat:format, class_getName([self class]),
317 dispatch_queue_get_label(self), self];
320 - (void)_xref_dispose {
321 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
322 [super _xref_dispose];
327 @implementation DISPATCH_CLASS(source)
329 DISPATCH_UNAVAILABLE_INIT()
331 - (void)_xref_dispose {
332 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
333 _dispatch_source_xref_dispose(self);
334 [super _xref_dispose];
339 @implementation DISPATCH_CLASS(mach)
341 DISPATCH_UNAVAILABLE_INIT()
343 - (void)_xref_dispose {
344 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
345 _dispatch_mach_xref_dispose((struct dispatch_mach_s *)self);
346 [super _xref_dispose];
351 @implementation DISPATCH_CLASS(queue_runloop)
353 DISPATCH_UNAVAILABLE_INIT()
355 - (void)_xref_dispose {
356 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
357 _dispatch_runloop_queue_xref_dispose(self);
358 [super _xref_dispose];
363 #define DISPATCH_CLASS_IMPL(name) \
364 @implementation DISPATCH_CLASS(name) \
365 DISPATCH_OBJC_LOAD() \
366 DISPATCH_UNAVAILABLE_INIT() \
369 #if !DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
370 DISPATCH_CLASS_IMPL(data)
372 DISPATCH_CLASS_IMPL(semaphore)
373 DISPATCH_CLASS_IMPL(group)
374 DISPATCH_CLASS_IMPL(queue_serial)
375 DISPATCH_CLASS_IMPL(queue_concurrent)
376 DISPATCH_CLASS_IMPL(queue_main)
377 DISPATCH_CLASS_IMPL(queue_root)
378 DISPATCH_CLASS_IMPL(queue_mgr)
379 DISPATCH_CLASS_IMPL(queue_specific_queue)
380 DISPATCH_CLASS_IMPL(queue_attr)
381 DISPATCH_CLASS_IMPL(mach_msg)
382 DISPATCH_CLASS_IMPL(io)
383 DISPATCH_CLASS_IMPL(operation)
384 DISPATCH_CLASS_IMPL(disk)
386 @implementation OS_OBJECT_CLASS(voucher)
387 DISPATCH_UNAVAILABLE_INIT()
391 return (id)_voucher_retain_inline((struct voucher_s *)self);
394 -(oneway void)release {
395 return _voucher_release_inline((struct voucher_s *)self);
398 - (void)_xref_dispose {
399 return _voucher_xref_dispose(self); // calls _os_object_release_internal()
403 return _voucher_dispose(self); // calls _os_object_dealloc()
406 - (NSString *)debugDescription {
407 Class nsstring = objc_lookUpClass("NSString");
408 if (!nsstring) return nil;
410 _voucher_debug(self, buf, sizeof(buf));
411 NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
412 if (!format) return nil;
413 return [nsstring stringWithFormat:format, class_getName([self class]), buf];
418 #if VOUCHER_ENABLE_RECIPE_OBJECTS
419 @implementation OS_OBJECT_CLASS(voucher_recipe)
420 DISPATCH_UNAVAILABLE_INIT()
427 - (NSString *)debugDescription {
428 return nil; // TODO: voucher_recipe debugDescription
436 #pragma mark dispatch_last_resort_autorelease_pool
438 #if DISPATCH_COCOA_COMPAT
441 _dispatch_last_resort_autorelease_pool_push(dispatch_invoke_context_t dic)
443 if (!slowpath(_os_object_debug_missing_pools)) {
444 dic->dic_autorelease_pool = _dispatch_autorelease_pool_push();
449 _dispatch_last_resort_autorelease_pool_pop(dispatch_invoke_context_t dic)
451 if (!slowpath(_os_object_debug_missing_pools)) {
452 _dispatch_autorelease_pool_pop(dic->dic_autorelease_pool);
453 dic->dic_autorelease_pool = NULL;
457 #endif // DISPATCH_COCOA_COMPAT
460 #pragma mark dispatch_client_callout
462 // Abort on uncaught exceptions thrown from client callouts rdar://8577499
463 #if DISPATCH_USE_CLIENT_CALLOUT && !__USING_SJLJ_EXCEPTIONS__ && \
465 // On platforms with zero-cost exceptions, use a compiler-generated catch-all
466 // exception handler.
468 DISPATCH_NORETURN extern void objc_terminate(void);
470 #undef _dispatch_client_callout
472 _dispatch_client_callout(void *ctxt, dispatch_function_t f)
482 #undef _dispatch_client_callout2
484 _dispatch_client_callout2(void *ctxt, size_t i, void (*f)(void *, size_t))
495 #undef _dispatch_client_callout3
497 _dispatch_client_callout3(void *ctxt, dispatch_mach_reason_t reason,
498 dispatch_mach_msg_t dmsg, dispatch_mach_async_reply_callback_t f)
501 return f(ctxt, reason, dmsg);
508 #undef _dispatch_client_callout4
510 _dispatch_client_callout4(void *ctxt, dispatch_mach_reason_t reason,
511 dispatch_mach_msg_t dmsg, mach_error_t error,
512 dispatch_mach_handler_function_t f)
515 return f(ctxt, reason, dmsg, error);
523 #endif // DISPATCH_USE_CLIENT_CALLOUT