]> git.saurik.com Git - apple/libdispatch.git/blob - src/data_internal.h
libdispatch-913.30.4.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_with_bytes(dispatch_data_t data, const void *buffer,
104 size_t size, dispatch_block_t destructor);
105 void _dispatch_data_dispose(dispatch_data_t data, bool *allow_free);
106 void _dispatch_data_set_target_queue(struct dispatch_data_s *dd,
107 dispatch_queue_t tq);
108 size_t _dispatch_data_debug(dispatch_data_t data, char* buf, size_t bufsiz);
109 const void* _dispatch_data_get_flattened_bytes(struct dispatch_data_s *dd);
110
111 #if !defined(__cplusplus)
112 extern const dispatch_block_t _dispatch_data_destructor_inline;
113 #define DISPATCH_DATA_DESTRUCTOR_INLINE (_dispatch_data_destructor_inline)
114
115 /*
116 * the out parameters are about seeing "through" trivial subranges
117 * so for something like this: dd = { subrange [ dd1, offset1 ] },
118 * this will return { dd1, offset + offset1 }
119 *
120 * If the dispatch object isn't a trivial subrange, it returns { dd, offset }
121 */
122 DISPATCH_ALWAYS_INLINE
123 static inline const void*
124 _dispatch_data_map_direct(struct dispatch_data_s *dd, size_t offset,
125 struct dispatch_data_s **dd_out, size_t *from_out)
126 {
127 const void *buffer = NULL;
128
129 dispatch_assert(dd->size);
130 if (slowpath(!_dispatch_data_leaf(dd)) &&
131 _dispatch_data_num_records(dd) == 1) {
132 offset += dd->records[0].from;
133 dd = (struct dispatch_data_s *)dd->records[0].data_object;
134 }
135
136 if (fastpath(_dispatch_data_leaf(dd))) {
137 buffer = dd->buf + offset;
138 } else {
139 buffer = os_atomic_load((void **)&dd->buf, relaxed);
140 if (buffer) {
141 buffer += offset;
142 }
143 }
144 if (dd_out) *dd_out = dd;
145 if (from_out) *from_out = offset;
146 return buffer;
147 }
148
149 #endif // !defined(__cplusplus)
150
151 #endif // __DISPATCH_DATA_INTERNAL__