]> git.saurik.com Git - apple/libdispatch.git/blob - src/data_internal.h
libdispatch-703.50.37.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 OS_OBJECT_HAVE_OBJC2
42 #define DISPATCH_DATA_IS_BRIDGED_TO_NSDATA 1
43 #else
44 #define DISPATCH_DATA_IS_BRIDGED_TO_NSDATA 0
45 #endif
46
47 #if DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
48 DISPATCH_OBJC_CLASS_DECL(data);
49 DISPATCH_OBJC_CLASS_DECL(data_empty);
50 _OS_OBJECT_DECL_PROTOCOL(dispatch_data, dispatch_object);
51 #define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
52 #define DISPATCH_DATA_EMPTY_CLASS DISPATCH_VTABLE(data_empty)
53 #else
54 DISPATCH_CLASS_DECL(data);
55 #define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
56 #endif // DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
57
58 struct dispatch_data_s {
59 #if DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
60 const void *do_vtable;
61 dispatch_queue_t do_targetq;
62 void *ctxt;
63 void *finalizer;
64 #else
65 DISPATCH_OBJECT_HEADER(data);
66 #endif // DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
67 const void *buf;
68 dispatch_block_t destructor;
69 size_t size, num_records;
70 range_record records[0];
71 };
72
73 DISPATCH_ALWAYS_INLINE
74 static inline bool
75 _dispatch_data_leaf(struct dispatch_data_s *dd)
76 {
77 return dd->num_records == 0;
78 }
79
80 /*
81 * This is about the number of records required to hold that dispatch data
82 * if it's not a leaf. Callers either want that value, or have to special
83 * case the case when the dispatch data *is* a leaf before (and that the actual
84 * embedded record count of that dispatch data is 0)
85 */
86 DISPATCH_ALWAYS_INLINE
87 static inline size_t
88 _dispatch_data_num_records(struct dispatch_data_s *dd)
89 {
90 return dd->num_records ?: 1;
91 }
92
93 typedef dispatch_data_t (*dispatch_transform_t)(dispatch_data_t data);
94
95 struct dispatch_data_format_type_s {
96 uint64_t type;
97 uint64_t input_mask;
98 uint64_t output_mask;
99 dispatch_transform_t decode;
100 dispatch_transform_t encode;
101 };
102
103 void dispatch_data_init(dispatch_data_t data, const void *buffer, size_t size,
104 dispatch_block_t destructor);
105 void _dispatch_data_dispose(dispatch_data_t data);
106 size_t _dispatch_data_debug(dispatch_data_t data, char* buf, size_t bufsiz);
107 const void*
108 _dispatch_data_get_flattened_bytes(struct dispatch_data_s *dd);
109
110 #if !defined(__cplusplus)
111 extern const dispatch_block_t _dispatch_data_destructor_inline;
112 #define DISPATCH_DATA_DESTRUCTOR_INLINE (_dispatch_data_destructor_inline)
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 = os_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__