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 @interface DISPATCH_CLASS(data) () <DISPATCH_CLASS(data)>
32 @property (readonly) NSUInteger length;
33 @property (readonly) const void *bytes NS_RETURNS_INNER_POINTER;
35 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
36 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm;
41 @interface DISPATCH_CLASS(data_empty) : DISPATCH_CLASS(data)
44 @implementation DISPATCH_CLASS(data)
46 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
47 return _dispatch_objc_alloc(self, sizeof(struct dispatch_data_s));
51 return [self initWithBytes:NULL length:0 copy:NO freeWhenDone:NO
55 - (id)initWithBytes:(void *)bytes length:(NSUInteger)length copy:(BOOL)copy
56 freeWhenDone:(BOOL)freeBytes bytesAreVM:(BOOL)vm {
57 dispatch_block_t destructor;
59 destructor = DISPATCH_DATA_DESTRUCTOR_DEFAULT;
60 } else if (freeBytes) {
62 destructor = DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
64 destructor = DISPATCH_DATA_DESTRUCTOR_FREE;
67 destructor = DISPATCH_DATA_DESTRUCTOR_NONE;
69 dispatch_data_init(self, bytes, length, destructor);
73 #define _dispatch_data_objc_dispose(selector) \
74 struct dispatch_data_s *dd = (void*)self; \
75 _dispatch_data_dispose(self); \
76 dispatch_queue_t tq = dd->do_targetq; \
77 dispatch_function_t func = dd->finalizer; \
78 void *ctxt = dd->ctxt; \
82 tq = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);\
84 dispatch_async_f(tq, ctxt, func); \
87 _os_object_release_internal((_os_object_t)tq); \
91 _dispatch_data_objc_dispose(dealloc);
95 struct dispatch_data_s *dd = (void*)self;
96 return dd->destructor == DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE;
99 - (void)_setContext:(void*)context {
100 struct dispatch_data_s *dd = (void*)self;
104 - (void*)_getContext {
105 struct dispatch_data_s *dd = (void*)self;
109 - (void)_setFinalizer:(dispatch_function_t)finalizer {
110 struct dispatch_data_s *dd = (void*)self;
111 dd->finalizer = finalizer;
114 - (void)_setTargetQueue:(dispatch_queue_t)queue {
115 struct dispatch_data_s *dd = (void*)self;
116 _os_object_retain_internal((_os_object_t)queue);
117 dispatch_queue_t prev;
118 prev = os_atomic_xchg2o(dd, do_targetq, queue, release);
119 if (prev) _os_object_release_internal((_os_object_t)prev);
122 - (NSString *)debugDescription {
123 Class nsstring = objc_lookUpClass("NSString");
124 if (!nsstring) return nil;
126 _dispatch_data_debug(self, buf, sizeof(buf));
127 return [nsstring stringWithFormat:
128 [nsstring stringWithUTF8String:"<%s: %s>"],
129 class_getName([self class]), buf];
132 - (NSUInteger)length {
133 struct dispatch_data_s *dd = (void*)self;
137 - (const void *)bytes {
138 struct dispatch_data_s *dd = (void*)self;
139 return _dispatch_data_get_flattened_bytes(dd);
143 struct dispatch_data_s *dd = (void*)self;
144 return !dd->size || _dispatch_data_map_direct(dd, 0, NULL, NULL) != NULL;
158 @implementation DISPATCH_CLASS(data_empty)
160 // Force non-lazy class realization rdar://10640168
168 - (oneway void)release {
175 - (NSUInteger)retainCount {
179 + (id)allocWithZone:(NSZone *) DISPATCH_UNUSED zone {
180 return (id)&_dispatch_data_empty;
183 - (void)_setContext:(void*) DISPATCH_UNUSED context {
186 - (void*)_getContext {
190 - (void)_setFinalizer:(dispatch_function_t) DISPATCH_UNUSED finalizer {
193 - (void)_setTargetQueue:(dispatch_queue_t) DISPATCH_UNUSED queue {