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 @property (readonly) NSUInteger length;
36 @property (readonly) const void *bytes NS_RETURNS_INNER_POINTER;
38 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
39 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm;
44 @interface DISPATCH_CLASS(data_empty) : DISPATCH_CLASS(data)
47 @implementation DISPATCH_CLASS(data)
49 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
50 return _dispatch_objc_alloc(self, sizeof(struct dispatch_data_s));
54 return [self initWithBytes:NULL length:0 copy:NO freeWhenDone:NO
58 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
59 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm {
60 dispatch_block_t destructor;
62 destructor = DISPATCH_DATA_DESTRUCTOR_DEFAULT;
63 } else if (freeBytes) {
65 destructor = DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
67 destructor = DISPATCH_DATA_DESTRUCTOR_FREE;
70 destructor = DISPATCH_DATA_DESTRUCTOR_NONE;
72 dispatch_data_init(self, bytes, length, destructor);
76 #define _dispatch_data_objc_dispose(selector) \
77 struct dispatch_data_s *dd = (void*)self; \
78 _dispatch_data_dispose(self); \
79 dispatch_queue_t tq = dd->do_targetq; \
80 dispatch_function_t func = dd->finalizer; \
81 void *ctxt = dd->ctxt; \
85 tq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);\
87 dispatch_async_f(tq, ctxt, func); \
90 _os_object_release_internal((_os_object_t)tq); \
94 _dispatch_data_objc_dispose(dealloc);
98 _dispatch_data_objc_dispose(finalize);
101 - (BOOL)_bytesAreVM {
102 struct dispatch_data_s *dd = (void*)self;
103 return dd->destructor == DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
106 - (void)_setContext:(void*)context {
107 struct dispatch_data_s *dd = (void*)self;
111 - (void*)_getContext {
112 struct dispatch_data_s *dd = (void*)self;
116 - (void)_setFinalizer:(dispatch_function_t)finalizer {
117 struct dispatch_data_s *dd = (void*)self;
118 dd->finalizer = finalizer;
121 - (void)_setTargetQueue:(dispatch_queue_t)queue {
122 struct dispatch_data_s *dd = (void*)self;
123 _os_object_retain_internal((_os_object_t)queue);
124 dispatch_queue_t prev;
125 prev = dispatch_atomic_xchg2o(dd, do_targetq, queue, release);
126 if (prev) _os_object_release_internal((_os_object_t)prev);
129 - (NSString *)debugDescription {
130 Class nsstring = objc_lookUpClass("NSString");
131 if (!nsstring) return nil;
133 _dispatch_data_debug(self, buf, sizeof(buf));
134 return [nsstring stringWithFormat:
135 [nsstring stringWithUTF8String:"<%s: %s>"],
136 class_getName([self class]), buf];
139 - (NSUInteger)length {
140 struct dispatch_data_s *dd = (void*)self;
144 - (const void *)bytes {
145 struct dispatch_data_s *dd = (void*)self;
146 return _dispatch_data_get_flattened_bytes(dd);
150 struct dispatch_data_s *dd = (void*)self;
151 return !dd->size || _dispatch_data_map_direct(dd, 0, NULL, NULL) != NULL;
156 @implementation DISPATCH_CLASS(data_empty)
158 // Force non-lazy class realization rdar://10640168
166 - (oneway void)release {
173 - (NSUInteger)retainCount {
177 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
178 return (id)&_dispatch_data_empty;
181 - (void)_setContext:(void*) DISPATCH_UNUSED context {
184 - (void*)_getContext {
188 - (void)_setFinalizer:(dispatch_function_t) DISPATCH_UNUSED finalizer {
191 - (void)_setTargetQueue:(dispatch_queue_t) DISPATCH_UNUSED queue {