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@
23 #if DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
25 #if _OS_OBJECT_OBJC_ARC
26 #error "Cannot build with ARC"
29 #include <Foundation/NSString.h>
31 // NOTE: this file must not contain any atomic operations
33 @interface DISPATCH_CLASS(data) () <DISPATCH_CLASS(data)>
34 @property (readonly,nonatomic) NSUInteger length;
35 @property (readonly,nonatomic) const void *bytes NS_RETURNS_INNER_POINTER;
37 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
38 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm;
43 @interface DISPATCH_CLASS(data_empty) : DISPATCH_CLASS(data)
46 @implementation DISPATCH_CLASS(data)
48 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
49 return _dispatch_objc_alloc(self, sizeof(struct dispatch_data_s));
53 return [self initWithBytes:NULL length:0 copy:NO freeWhenDone:NO
57 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
58 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm {
59 dispatch_block_t destructor;
61 destructor = DISPATCH_DATA_DESTRUCTOR_DEFAULT;
62 } else if (freeBytes) {
64 destructor = DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
66 destructor = DISPATCH_DATA_DESTRUCTOR_FREE;
69 destructor = DISPATCH_DATA_DESTRUCTOR_NONE;
71 _dispatch_data_init_with_bytes(self, bytes, length, destructor);
76 struct dispatch_data_s *dd = (void*)self;
77 _dispatch_data_dispose(self, NULL);
78 dispatch_queue_t tq = dd->do_targetq;
79 dispatch_function_t func = dd->finalizer;
80 void *ctxt = dd->ctxt;
84 tq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
86 dispatch_async_f(tq, ctxt, func);
89 _os_object_release_internal((_os_object_t)tq);
94 struct dispatch_data_s *dd = (void*)self;
95 return dd->destructor == DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
98 - (void)_setContext:(void*)context {
99 struct dispatch_data_s *dd = (void*)self;
103 - (void*)_getContext {
104 struct dispatch_data_s *dd = (void*)self;
108 - (void)_setFinalizer:(dispatch_function_t)finalizer {
109 struct dispatch_data_s *dd = (void*)self;
110 dd->finalizer = finalizer;
113 - (void)_setTargetQueue:(dispatch_queue_t)queue {
114 struct dispatch_data_s *dd = (void*)self;
115 return _dispatch_data_set_target_queue(dd, queue);
118 - (NSString *)debugDescription {
119 Class nsstring = objc_lookUpClass("NSString");
120 if (!nsstring) return nil;
122 _dispatch_data_debug(self, buf, sizeof(buf));
123 NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
124 if (!format) return nil;
125 return [nsstring stringWithFormat:format, class_getName([self class]), buf];
128 - (NSUInteger)length {
129 struct dispatch_data_s *dd = (void*)self;
133 - (const void *)bytes {
134 struct dispatch_data_s *dd = (void*)self;
135 return _dispatch_data_get_flattened_bytes(dd);
139 struct dispatch_data_s *dd = (void*)self;
140 return !dd->size || _dispatch_data_map_direct(dd, 0, NULL, NULL) != NULL;
154 @implementation DISPATCH_CLASS(data_empty)
156 // Force non-lazy class realization rdar://10640168
164 - (oneway void)release {
171 - (NSUInteger)retainCount {
175 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
176 return (id)&_dispatch_data_empty;
179 - (void)_setContext:(void*) DISPATCH_UNUSED context {
182 - (void*)_getContext {
186 - (void)_setFinalizer:(dispatch_function_t) DISPATCH_UNUSED finalizer {
189 - (void)_setTargetQueue:(dispatch_queue_t) DISPATCH_UNUSED queue {