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
;
42 #if OS_OBJECT_USE_OBJC
43 @interface
DISPATCH_CLASS(data
) : NSObject
<DISPATCH_CLASS(data
)>
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)
51 DISPATCH_CLASS_DECL(data
);
52 #define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
53 #define DISPATCH_DATA_EMPTY_CLASS DISPATCH_VTABLE(data)
56 struct dispatch_data_s
{
58 const void *do_vtable
;
59 dispatch_queue_t do_targetq
;
63 DISPATCH_STRUCT_HEADER(data
);
66 dispatch_block_t destructor
;
67 size_t size
, num_records
;
68 range_record records
[0];
71 DISPATCH_ALWAYS_INLINE
73 _dispatch_data_leaf(struct dispatch_data_s
*dd
)
75 return dd
->num_records
== 0;
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)
84 DISPATCH_ALWAYS_INLINE
86 _dispatch_data_num_records(struct dispatch_data_s
*dd
)
88 return dd
->num_records
?: 1;
91 typedef dispatch_data_t (*dispatch_transform_t
)(dispatch_data_t data
);
93 struct dispatch_data_format_type_s
{
97 dispatch_transform_t decode
;
98 dispatch_transform_t encode
;
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
);
106 _dispatch_data_get_flattened_bytes(struct dispatch_data_s
*dd
);
108 #if !defined(__cplusplus)
110 const dispatch_block_t _dispatch_data_destructor_inline
;
111 #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
= dispatch_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__