2 * Copyright (c) 2009-2012 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@
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.
27 #ifndef __DISPATCH_DATA_INTERNAL__
28 #define __DISPATCH_DATA_INTERNAL__
30 #ifndef __DISPATCH_INDIRECT__
31 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
32 #include <dispatch/base.h> // for HeaderDoc
35 typedef struct range_record_s
{
36 dispatch_data_t data_object
;
41 #if OS_OBJECT_HAVE_OBJC2
42 #define DISPATCH_DATA_IS_BRIDGED_TO_NSDATA 1
44 #define DISPATCH_DATA_IS_BRIDGED_TO_NSDATA 0
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)
54 DISPATCH_CLASS_DECL(data
);
55 #define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
56 #endif // DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
58 struct dispatch_data_s
{
59 #if DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
60 const void *do_vtable
;
61 dispatch_queue_t do_targetq
;
65 DISPATCH_OBJECT_HEADER(data
);
66 #endif // DISPATCH_DATA_IS_BRIDGED_TO_NSDATA
68 dispatch_block_t destructor
;
69 size_t size
, num_records
;
70 range_record records
[0];
73 DISPATCH_ALWAYS_INLINE
75 _dispatch_data_leaf(struct dispatch_data_s
*dd
)
77 return dd
->num_records
== 0;
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)
86 DISPATCH_ALWAYS_INLINE
88 _dispatch_data_num_records(struct dispatch_data_s
*dd
)
90 return dd
->num_records
?: 1;
93 typedef dispatch_data_t (*dispatch_transform_t
)(dispatch_data_t data
);
95 struct dispatch_data_format_type_s
{
99 dispatch_transform_t decode
;
100 dispatch_transform_t encode
;
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
);
108 _dispatch_data_get_flattened_bytes(struct dispatch_data_s
*dd
);
110 #if !defined(__cplusplus)
111 extern const dispatch_block_t _dispatch_data_destructor_inline
;
112 #define DISPATCH_DATA_DESTRUCTOR_INLINE (_dispatch_data_destructor_inline)
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 }
119 * If the dispatch object isn't a trivial subrange, it returns { dd, offset }
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
)
126 const void *buffer
= NULL
;
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
;
135 if (fastpath(_dispatch_data_leaf(dd
))) {
136 buffer
= dd
->buf
+ offset
;
138 buffer
= os_atomic_load((void **)&dd
->buf
, relaxed
);
143 if (dd_out
) *dd_out
= dd
;
144 if (from_out
) *from_out
= offset
;
148 #endif // !defined(__cplusplus)
150 #endif // __DISPATCH_DATA_INTERNAL__