]> git.saurik.com Git - apple/libdispatch.git/blob - src/data_internal.h
libdispatch-500.10.1.tar.gz
[apple/libdispatch.git] / src / data_internal.h
1 /*
2 * Copyright (c) 2009-2012 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 /*
22 * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
23 * which are subject to change in future releases of Mac OS X. Any applications
24 * relying on these interfaces WILL break.
25 */
26
27 #ifndef __DISPATCH_DATA_INTERNAL__
28 #define __DISPATCH_DATA_INTERNAL__
29
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
32 #include <dispatch/base.h> // for HeaderDoc
33 #endif
34
35 typedef struct range_record_s {
36 dispatch_data_t data_object;
37 size_t from;
38 size_t length;
39 } range_record;
40
41 #if USE_OBJC
42 #if OS_OBJECT_USE_OBJC
43 @interface DISPATCH_CLASS(data) : NSObject <DISPATCH_CLASS(data)>
44 @end
45 #endif
46 DISPATCH_OBJC_CLASS_DECL(data);
47 DISPATCH_OBJC_CLASS_DECL(data_empty);
48 #define DISPATCH_DATA_CLASS DISPATCH_OBJC_CLASS(data)
49 #define DISPATCH_DATA_EMPTY_CLASS DISPATCH_OBJC_CLASS(data_empty)
50 #else // USE_OBJC
51 DISPATCH_CLASS_DECL(data);
52 #define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
53 #define DISPATCH_DATA_EMPTY_CLASS DISPATCH_VTABLE(data)
54 #endif // USE_OBJC
55
56 struct dispatch_data_s {
57 #if USE_OBJC
58 const void *do_vtable;
59 dispatch_queue_t do_targetq;
60 void *ctxt;
61 void *finalizer;
62 #else // USE_OBJC
63 DISPATCH_STRUCT_HEADER(data);
64 #endif // USE_OBJC
65 const void *buf;
66 dispatch_block_t destructor;
67 size_t size, num_records;
68 range_record records[0];
69 };
70
71 DISPATCH_ALWAYS_INLINE
72 static inline bool
73 _dispatch_data_leaf(struct dispatch_data_s *dd)
74 {
75 return dd->num_records == 0;
76 }
77
78 /*
79 * This is about the number of records required to hold that dispatch data
80 * if it's not a leaf. Callers either want that value, or have to special
81 * case the case when the dispatch data *is* a leaf before (and that the actual
82 * embeded record count of that dispatch data is 0)
83 */
84 DISPATCH_ALWAYS_INLINE
85 static inline size_t
86 _dispatch_data_num_records(struct dispatch_data_s *dd)
87 {
88 return dd->num_records ?: 1;
89 }
90
91 typedef dispatch_data_t (*dispatch_transform_t)(dispatch_data_t data);
92
93 struct dispatch_data_format_type_s {
94 uint64_t type;
95 uint64_t input_mask;
96 uint64_t output_mask;
97 dispatch_transform_t decode;
98 dispatch_transform_t encode;
99 };
100
101 void dispatch_data_init(dispatch_data_t data, const void *buffer, size_t size,
102 dispatch_block_t destructor);
103 void _dispatch_data_dispose(dispatch_data_t data);
104 size_t _dispatch_data_debug(dispatch_data_t data, char* buf, size_t bufsiz);
105 const void*
106 _dispatch_data_get_flattened_bytes(struct dispatch_data_s *dd);
107
108 #if !defined(__cplusplus)
109 #if !__OBJC2__
110 const dispatch_block_t _dispatch_data_destructor_inline;
111 #define DISPATCH_DATA_DESTRUCTOR_INLINE (_dispatch_data_destructor_inline)
112 #endif // !__OBJC2__
113
114 /*
115 * the out parameters are about seeing "through" trivial subranges
116 * so for something like this: dd = { subrange [ dd1, offset1 ] },
117 * this will return { dd1, offset + offset1 }
118 *
119 * If the dispatch object isn't a trivial subrange, it returns { dd, offset }
120 */
121 DISPATCH_ALWAYS_INLINE
122 static inline const void*
123 _dispatch_data_map_direct(struct dispatch_data_s *dd, size_t offset,
124 struct dispatch_data_s **dd_out, size_t *from_out)
125 {
126 const void *buffer = NULL;
127
128 dispatch_assert(dd->size);
129 if (slowpath(!_dispatch_data_leaf(dd)) &&
130 _dispatch_data_num_records(dd) == 1) {
131 offset += dd->records[0].from;
132 dd = (struct dispatch_data_s *)dd->records[0].data_object;
133 }
134
135 if (fastpath(_dispatch_data_leaf(dd))) {
136 buffer = dd->buf + offset;
137 } else {
138 buffer = dispatch_atomic_load((void **)&dd->buf, relaxed);
139 if (buffer) {
140 buffer += offset;
141 }
142 }
143 if (dd_out) *dd_out = dd;
144 if (from_out) *from_out = offset;
145 return buffer;
146 }
147
148 #endif // !defined(__cplusplus)
149
150 #endif // __DISPATCH_DATA_INTERNAL__