]> git.saurik.com Git - apple/libdispatch.git/blob - src/object.m
libdispatch-703.30.5.tar.gz
[apple/libdispatch.git] / src / object.m
1 /*
2 * Copyright (c) 2011-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
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.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21 #include "internal.h"
22
23 #if USE_OBJC
24
25 #if _OS_OBJECT_OBJC_ARC
26 #error "Cannot build with ARC"
27 #endif
28 #if defined(__OBJC_GC__)
29 #error Objective C GC isn't supported anymore
30 #endif
31
32 #include <objc/objc-internal.h>
33 #include <objc/objc-exception.h>
34 #include <Foundation/NSString.h>
35
36 #pragma mark -
37 #pragma mark _os_object_t
38
39 static inline id
40 _os_objc_alloc(Class cls, size_t size)
41 {
42 id obj;
43 size -= sizeof(((struct _os_object_s *)NULL)->os_obj_isa);
44 while (!fastpath(obj = class_createInstance(cls, size))) {
45 _dispatch_temporary_resource_shortage();
46 }
47 return obj;
48 }
49
50 static void*
51 _os_objc_destructInstance(id obj)
52 {
53 // noop if only Libystem is loaded
54 return obj;
55 }
56
57 #if DISPATCH_COCOA_COMPAT
58 static bool _os_object_debug_missing_pools;
59 #endif
60
61 void
62 _os_object_init(void)
63 {
64 _objc_init();
65 Block_callbacks_RR callbacks = {
66 sizeof(Block_callbacks_RR),
67 (void (*)(const void *))&objc_retain,
68 (void (*)(const void *))&objc_release,
69 (void (*)(const void *))&_os_objc_destructInstance
70 };
71 _Block_use_RR2(&callbacks);
72 #if DISPATCH_COCOA_COMPAT
73 const char *v = getenv("OBJC_DEBUG_MISSING_POOLS");
74 _os_object_debug_missing_pools = v && !strcmp(v, "YES");
75 #endif
76 }
77
78 _os_object_t
79 _os_object_alloc_realized(const void *cls, size_t size)
80 {
81 dispatch_assert(size >= sizeof(struct _os_object_s));
82 return _os_objc_alloc(cls, size);
83 }
84
85 _os_object_t
86 _os_object_alloc(const void *_cls, size_t size)
87 {
88 dispatch_assert(size >= sizeof(struct _os_object_s));
89 Class cls = _cls ? [(id)_cls class] : [OS_OBJECT_CLASS(object) class];
90 return _os_objc_alloc(cls, size);
91 }
92
93 void
94 _os_object_dealloc(_os_object_t obj)
95 {
96 [obj dealloc];
97 }
98
99 void
100 _os_object_xref_dispose(_os_object_t obj)
101 {
102 struct _os_object_s *o = (struct _os_object_s *)obj;
103 _os_object_xrefcnt_dispose_barrier(o);
104 [obj _xref_dispose];
105 }
106
107 void
108 _os_object_dispose(_os_object_t obj)
109 {
110 struct _os_object_s *o = (struct _os_object_s *)obj;
111 _os_object_refcnt_dispose_barrier(o);
112 [obj _dispose];
113 }
114
115 #undef os_retain
116 void*
117 os_retain(void *obj)
118 {
119 return objc_retain(obj);
120 }
121
122 #undef os_release
123 void
124 os_release(void *obj)
125 {
126 return objc_release(obj);
127 }
128
129 #pragma mark -
130 #pragma mark _os_object
131
132 @implementation OS_OBJECT_CLASS(object)
133 DISPATCH_UNAVAILABLE_INIT()
134
135 -(id)retain {
136 return _os_object_retain(self);
137 }
138
139 -(oneway void)release {
140 return _os_object_release(self);
141 }
142
143 -(NSUInteger)retainCount {
144 return _os_object_retain_count(self);
145 }
146
147 -(BOOL)retainWeakReference {
148 return _os_object_retain_weak(self);
149 }
150
151 -(BOOL)allowsWeakReference {
152 return _os_object_allows_weak_reference(self);
153 }
154
155 - (void)_xref_dispose {
156 return _os_object_release_internal(self);
157 }
158
159 - (void)_dispose {
160 return _os_object_dealloc(self);
161 }
162
163 @end
164
165 #pragma mark -
166 #pragma mark _dispatch_objc
167 #if OS_OBJECT_HAVE_OBJC2
168
169 id
170 _dispatch_objc_alloc(Class cls, size_t size)
171 {
172 return _os_objc_alloc(cls, size);
173 }
174
175 void
176 _dispatch_objc_retain(dispatch_object_t dou)
177 {
178 return (void)os_retain(dou);
179 }
180
181 void
182 _dispatch_objc_release(dispatch_object_t dou)
183 {
184 return os_release(dou);
185 }
186
187 void
188 _dispatch_objc_set_context(dispatch_object_t dou, void *context)
189 {
190 return [dou _setContext:context];
191 }
192
193 void *
194 _dispatch_objc_get_context(dispatch_object_t dou)
195 {
196 return [dou _getContext];
197 }
198
199 void
200 _dispatch_objc_set_finalizer_f(dispatch_object_t dou,
201 dispatch_function_t finalizer)
202 {
203 return [dou _setFinalizer:finalizer];
204 }
205
206 void
207 _dispatch_objc_set_target_queue(dispatch_object_t dou, dispatch_queue_t queue)
208 {
209 return [dou _setTargetQueue:queue];
210 }
211
212 void
213 _dispatch_objc_suspend(dispatch_object_t dou)
214 {
215 return [dou _suspend];
216 }
217
218 void
219 _dispatch_objc_resume(dispatch_object_t dou)
220 {
221 return [dou _resume];
222 }
223
224 void
225 _dispatch_objc_activate(dispatch_object_t dou)
226 {
227 return [dou _activate];
228 }
229
230 size_t
231 _dispatch_objc_debug(dispatch_object_t dou, char* buf, size_t bufsiz)
232 {
233 NSUInteger offset = 0;
234 NSString *desc = [dou debugDescription];
235 [desc getBytes:buf maxLength:bufsiz-1 usedLength:&offset
236 encoding:NSUTF8StringEncoding options:0
237 range:NSMakeRange(0, [desc length]) remainingRange:NULL];
238 if (offset) buf[offset] = 0;
239 return offset;
240 }
241
242 #endif
243 #pragma mark -
244 #pragma mark _dispatch_object
245
246 // Force non-lazy class realization rdar://10640168
247 #define DISPATCH_OBJC_LOAD() + (void)load {}
248
249 @implementation DISPATCH_CLASS(object)
250 DISPATCH_UNAVAILABLE_INIT()
251
252 - (void)_dispose {
253 return _dispatch_dispose(self); // calls _os_object_dealloc()
254 }
255
256 - (NSString *)debugDescription {
257 Class nsstring = objc_lookUpClass("NSString");
258 if (!nsstring) return nil;
259 char buf[2048];
260 struct dispatch_object_s *obj = (struct dispatch_object_s *)self;
261 if (dx_vtable(obj)->do_debug) {
262 dx_debug(obj, buf, sizeof(buf));
263 } else {
264 strlcpy(buf, dx_kind(obj), sizeof(buf));
265 }
266 return [nsstring stringWithFormat:
267 [nsstring stringWithUTF8String:"<%s: %s>"],
268 class_getName([self class]), buf];
269 }
270
271 @end
272
273 @implementation DISPATCH_CLASS(queue)
274 DISPATCH_OBJC_LOAD()
275 DISPATCH_UNAVAILABLE_INIT()
276
277 - (NSString *)description {
278 Class nsstring = objc_lookUpClass("NSString");
279 if (!nsstring) return nil;
280 return [nsstring stringWithFormat:
281 [nsstring stringWithUTF8String:"<%s: %s[%p]>"],
282 class_getName([self class]), dispatch_queue_get_label(self), self];
283 }
284
285 - (void)_xref_dispose {
286 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
287 [super _xref_dispose];
288 }
289
290 @end
291
292 @implementation DISPATCH_CLASS(source)
293 DISPATCH_OBJC_LOAD()
294 DISPATCH_UNAVAILABLE_INIT()
295
296 - (void)_xref_dispose {
297 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
298 _dispatch_source_xref_dispose(self);
299 [super _xref_dispose];
300 }
301
302 @end
303
304 @implementation DISPATCH_CLASS(mach)
305 DISPATCH_OBJC_LOAD()
306 DISPATCH_UNAVAILABLE_INIT()
307
308 - (void)_xref_dispose {
309 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
310 [super _xref_dispose];
311 }
312
313 @end
314
315 @implementation DISPATCH_CLASS(queue_runloop)
316 DISPATCH_OBJC_LOAD()
317 DISPATCH_UNAVAILABLE_INIT()
318
319 - (void)_xref_dispose {
320 _dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
321 _dispatch_runloop_queue_xref_dispose(self);
322 [super _xref_dispose];
323 }
324
325 @end
326
327 #define DISPATCH_CLASS_IMPL(name) \
328 @implementation DISPATCH_CLASS(name) \
329 DISPATCH_OBJC_LOAD() \
330 DISPATCH_UNAVAILABLE_INIT() \
331 @end
332
333 #if !DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
334 DISPATCH_CLASS_IMPL(data)
335 #endif
336 DISPATCH_CLASS_IMPL(semaphore)
337 DISPATCH_CLASS_IMPL(group)
338 DISPATCH_CLASS_IMPL(queue_serial)
339 DISPATCH_CLASS_IMPL(queue_concurrent)
340 DISPATCH_CLASS_IMPL(queue_main)
341 DISPATCH_CLASS_IMPL(queue_root)
342 DISPATCH_CLASS_IMPL(queue_mgr)
343 DISPATCH_CLASS_IMPL(queue_specific_queue)
344 DISPATCH_CLASS_IMPL(queue_attr)
345 DISPATCH_CLASS_IMPL(mach_msg)
346 DISPATCH_CLASS_IMPL(io)
347 DISPATCH_CLASS_IMPL(operation)
348 DISPATCH_CLASS_IMPL(disk)
349
350 @implementation OS_OBJECT_CLASS(voucher)
351 DISPATCH_UNAVAILABLE_INIT()
352 DISPATCH_OBJC_LOAD()
353
354 - (void)_xref_dispose {
355 return _voucher_xref_dispose(self); // calls _os_object_release_internal()
356 }
357
358 - (void)_dispose {
359 return _voucher_dispose(self); // calls _os_object_dealloc()
360 }
361
362 - (NSString *)debugDescription {
363 Class nsstring = objc_lookUpClass("NSString");
364 if (!nsstring) return nil;
365 char buf[2048];
366 _voucher_debug(self, buf, sizeof(buf));
367 return [nsstring stringWithFormat:
368 [nsstring stringWithUTF8String:"<%s: %s>"],
369 class_getName([self class]), buf];
370 }
371
372 @end
373
374 #if VOUCHER_ENABLE_RECIPE_OBJECTS
375 @implementation OS_OBJECT_CLASS(voucher_recipe)
376 DISPATCH_UNAVAILABLE_INIT()
377 DISPATCH_OBJC_LOAD()
378
379 - (void)_dispose {
380
381 }
382
383 - (NSString *)debugDescription {
384 return nil; // TODO: voucher_recipe debugDescription
385 }
386
387 @end
388 #endif
389
390
391 #pragma mark -
392 #pragma mark dispatch_last_resort_autorelease_pool
393
394 #if DISPATCH_COCOA_COMPAT
395
396 void *
397 _dispatch_last_resort_autorelease_pool_push(void)
398 {
399 if (!slowpath(_os_object_debug_missing_pools)) {
400 return _dispatch_autorelease_pool_push();
401 }
402 return NULL;
403 }
404
405 void
406 _dispatch_last_resort_autorelease_pool_pop(void *context)
407 {
408 if (!slowpath(_os_object_debug_missing_pools)) {
409 return _dispatch_autorelease_pool_pop(context);
410 }
411 }
412
413 #endif // DISPATCH_COCOA_COMPAT
414
415 #pragma mark -
416 #pragma mark dispatch_client_callout
417
418 // Abort on uncaught exceptions thrown from client callouts rdar://8577499
419 #if DISPATCH_USE_CLIENT_CALLOUT && !__USING_SJLJ_EXCEPTIONS__ && \
420 OS_OBJECT_HAVE_OBJC2
421 // On platforms with zero-cost exceptions, use a compiler-generated catch-all
422 // exception handler.
423
424 DISPATCH_NORETURN extern void objc_terminate(void);
425
426 #undef _dispatch_client_callout
427 void
428 _dispatch_client_callout(void *ctxt, dispatch_function_t f)
429 {
430 @try {
431 return f(ctxt);
432 }
433 @catch (...) {
434 objc_terminate();
435 }
436 }
437
438 #undef _dispatch_client_callout2
439 void
440 _dispatch_client_callout2(void *ctxt, size_t i, void (*f)(void *, size_t))
441 {
442 @try {
443 return f(ctxt, i);
444 }
445 @catch (...) {
446 objc_terminate();
447 }
448 }
449
450 #if HAVE_MACH
451 #undef _dispatch_client_callout4
452 void
453 _dispatch_client_callout4(void *ctxt, dispatch_mach_reason_t reason,
454 dispatch_mach_msg_t dmsg, mach_error_t error,
455 dispatch_mach_handler_function_t f)
456 {
457 @try {
458 return f(ctxt, reason, dmsg, error);
459 }
460 @catch (...) {
461 objc_terminate();
462 }
463 }
464 #endif // HAVE_MACH
465
466 #endif // DISPATCH_USE_CLIENT_CALLOUT
467
468 #endif // USE_OBJC