2 * Copyright (c) 2012-2013 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@
26 #error "Cannot build with legacy ObjC runtime"
28 #if _OS_OBJECT_OBJC_ARC
29 #error "Cannot build with ARC"
32 #include <Foundation/NSString.h>
34 @interface DISPATCH_CLASS(data) ()
35 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
36 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm;
40 @interface DISPATCH_CLASS(data_empty) : DISPATCH_CLASS(data)
43 @implementation DISPATCH_CLASS(data)
45 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
46 return _dispatch_objc_alloc(self, sizeof(struct dispatch_data_s));
50 return [self initWithBytes:NULL length:0 copy:NO freeWhenDone:NO
54 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
55 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm {
56 dispatch_block_t destructor;
58 destructor = DISPATCH_DATA_DESTRUCTOR_DEFAULT;
59 } else if (freeBytes) {
61 destructor = DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
63 destructor = DISPATCH_DATA_DESTRUCTOR_FREE;
66 destructor = DISPATCH_DATA_DESTRUCTOR_NONE;
68 dispatch_data_init(self, bytes, length, destructor);
72 #define _dispatch_data_objc_dispose(selector) \
73 struct dispatch_data_s *dd = (void*)self; \
74 _dispatch_data_dispose(self); \
75 dispatch_queue_t tq = dd->do_targetq; \
76 dispatch_function_t func = dd->finalizer; \
77 void *ctxt = dd->ctxt; \
81 tq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);\
83 dispatch_async_f(tq, ctxt, func); \
86 _dispatch_release(tq); \
90 _dispatch_data_objc_dispose(dealloc);
94 _dispatch_data_objc_dispose(finalize);
98 struct dispatch_data_s *dd = (void*)self;
99 return dd->destructor == DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
102 - (void)_setContext:(void*)context {
103 struct dispatch_data_s *dd = (void*)self;
107 - (void*)_getContext {
108 struct dispatch_data_s *dd = (void*)self;
112 - (void)_setFinalizer:(dispatch_function_t)finalizer {
113 struct dispatch_data_s *dd = (void*)self;
114 dd->finalizer = finalizer;
117 - (void)_setTargetQueue:(dispatch_queue_t)queue {
118 struct dispatch_data_s *dd = (void*)self;
119 _dispatch_retain(queue);
120 dispatch_queue_t prev;
121 prev = dispatch_atomic_xchg2o(dd, do_targetq, queue, release);
122 if (prev) _dispatch_release(prev);
125 - (NSString *)debugDescription {
126 Class nsstring = objc_lookUpClass("NSString");
127 if (!nsstring) return nil;
129 _dispatch_data_debug(self, buf, sizeof(buf));
130 return [nsstring stringWithFormat:
131 [nsstring stringWithUTF8String:"<%s: %s>"],
132 class_getName([self class]), buf];
137 @implementation DISPATCH_CLASS(data_empty)
139 // Force non-lazy class realization rdar://10640168
147 - (oneway void)release {
154 - (NSUInteger)retainCount {
158 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
159 return (id)&_dispatch_data_empty;
162 - (void)_setContext:(void*) DISPATCH_UNUSED context {
165 - (void*)_getContext {
169 - (void)_setFinalizer:(dispatch_function_t) DISPATCH_UNUSED finalizer {
172 - (void)_setTargetQueue:(dispatch_queue_t) DISPATCH_UNUSED queue {