]> git.saurik.com Git - apple/libdispatch.git/blob - dispatch/data.h
libdispatch-228.23.tar.gz
[apple/libdispatch.git] / dispatch / data.h
1 /*
2 * Copyright (c) 2009-2011 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 #ifndef __DISPATCH_DATA__
22 #define __DISPATCH_DATA__
23
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
27 #endif
28
29 __BEGIN_DECLS
30
31 /*! @header
32 * Dispatch data objects describe contiguous or sparse regions of memory that
33 * may be managed by the system or by the application.
34 * Dispatch data objects are immutable, any direct access to memory regions
35 * represented by dispatch objects must not modify that memory.
36 */
37
38 /*!
39 * @typedef dispatch_data_t
40 * A dispatch object representing memory regions.
41 */
42 DISPATCH_DECL(dispatch_data);
43
44 /*!
45 * @var dispatch_data_empty
46 * @discussion The singleton dispatch data object representing a zero-length
47 * memory region.
48 */
49 #define dispatch_data_empty \
50 DISPATCH_GLOBAL_OBJECT(dispatch_data_t, _dispatch_data_empty)
51 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
52 DISPATCH_EXPORT struct dispatch_data_s _dispatch_data_empty;
53
54 #ifdef __BLOCKS__
55
56 /*!
57 * @const DISPATCH_DATA_DESTRUCTOR_DEFAULT
58 * @discussion The default destructor for dispatch data objects.
59 * Used at data object creation to indicate that the supplied buffer should
60 * be copied into internal storage managed by the system.
61 */
62 #define DISPATCH_DATA_DESTRUCTOR_DEFAULT NULL
63
64 /*!
65 * @const DISPATCH_DATA_DESTRUCTOR_FREE
66 * @discussion The destructor for dispatch data objects created from a malloc'd
67 * buffer. Used at data object creation to indicate that the supplied buffer
68 * was allocated by the malloc() family and should be destroyed with free(3).
69 */
70 #define DISPATCH_DATA_DESTRUCTOR_FREE (_dispatch_data_destructor_free)
71 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
72 DISPATCH_EXPORT const dispatch_block_t _dispatch_data_destructor_free;
73
74 /*!
75 * @function dispatch_data_create
76 * Creates a dispatch data object from the given contiguous buffer of memory. If
77 * a non-default destructor is provided, ownership of the buffer remains with
78 * the caller (i.e. the bytes will not be copied). The last release of the data
79 * object will result in the invocation of the specified destructor on the
80 * specified queue to free the buffer.
81 *
82 * If the DISPATCH_DATA_DESTRUCTOR_FREE destructor is provided the buffer will
83 * be freed via free(3) and the queue argument ignored.
84 *
85 * If the DISPATCH_DATA_DESTRUCTOR_DEFAULT destructor is provided, data object
86 * creation will copy the buffer into internal memory managed by the system.
87 *
88 * @param buffer A contiguous buffer of data.
89 * @param size The size of the contiguous buffer of data.
90 * @param queue The queue to which the destructor should be submitted.
91 * @param destructor The destructor responsible for freeing the data when it
92 * is no longer needed.
93 * @result A newly created dispatch data object.
94 */
95 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
96 DISPATCH_EXPORT DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT DISPATCH_NOTHROW
97 dispatch_data_t
98 dispatch_data_create(const void *buffer,
99 size_t size,
100 dispatch_queue_t queue,
101 dispatch_block_t destructor);
102
103 /*!
104 * @function dispatch_data_get_size
105 * Returns the logical size of the memory region(s) represented by the specified
106 * dispatch data object.
107 *
108 * @param data The dispatch data object to query.
109 * @result The number of bytes represented by the data object.
110 */
111 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
112 DISPATCH_EXPORT DISPATCH_PURE DISPATCH_NONNULL1 DISPATCH_NOTHROW
113 size_t
114 dispatch_data_get_size(dispatch_data_t data);
115
116 /*!
117 * @function dispatch_data_create_map
118 * Maps the memory represented by the specified dispatch data object as a single
119 * contiguous memory region and returns a new data object representing it.
120 * If non-NULL references to a pointer and a size variable are provided, they
121 * are filled with the location and extent of that region. These allow direct
122 * read access to the represented memory, but are only valid until the returned
123 * object is released. Under ARC, if that object is held in a variable with
124 * automatic storage, care needs to be taken to ensure that it is not released
125 * by the compiler before memory access via the pointer has been completed.
126 *
127 * @param data The dispatch data object to map.
128 * @param buffer_ptr A pointer to a pointer variable to be filled with the
129 * location of the mapped contiguous memory region, or
130 * NULL.
131 * @param size_ptr A pointer to a size_t variable to be filled with the
132 * size of the mapped contiguous memory region, or NULL.
133 * @result A newly created dispatch data object.
134 */
135 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
136 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_RETURNS_RETAINED
137 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
138 dispatch_data_t
139 dispatch_data_create_map(dispatch_data_t data,
140 const void **buffer_ptr,
141 size_t *size_ptr);
142
143 /*!
144 * @function dispatch_data_create_concat
145 * Returns a new dispatch data object representing the concatenation of the
146 * specified data objects. Those objects may be released by the application
147 * after the call returns (however, the system might not deallocate the memory
148 * region(s) described by them until the newly created object has also been
149 * released).
150 *
151 * @param data1 The data object representing the region(s) of memory to place
152 * at the beginning of the newly created object.
153 * @param data2 The data object representing the region(s) of memory to place
154 * at the end of the newly created object.
155 * @result A newly created object representing the concatenation of the
156 * data1 and data2 objects.
157 */
158 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
159 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_RETURNS_RETAINED
160 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
161 dispatch_data_t
162 dispatch_data_create_concat(dispatch_data_t data1, dispatch_data_t data2);
163
164 /*!
165 * @function dispatch_data_create_subrange
166 * Returns a new dispatch data object representing a subrange of the specified
167 * data object, which may be released by the application after the call returns
168 * (however, the system might not deallocate the memory region(s) described by
169 * that object until the newly created object has also been released).
170 *
171 * @param data The data object representing the region(s) of memory to
172 * create a subrange of.
173 * @param offset The offset into the data object where the subrange
174 * starts.
175 * @param length The length of the range.
176 * @result A newly created object representing the specified
177 * subrange of the data object.
178 */
179 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
180 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_RETURNS_RETAINED
181 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
182 dispatch_data_t
183 dispatch_data_create_subrange(dispatch_data_t data,
184 size_t offset,
185 size_t length);
186
187 /*!
188 * @typedef dispatch_data_applier_t
189 * A block to be invoked for every contiguous memory region in a data object.
190 *
191 * @param region A data object representing the current region.
192 * @param offset The logical offset of the current region to the start
193 * of the data object.
194 * @param buffer The location of the memory for the current region.
195 * @param size The size of the memory for the current region.
196 * @result A Boolean indicating whether traversal should continue.
197 */
198 typedef bool (^dispatch_data_applier_t)(dispatch_data_t region,
199 size_t offset,
200 const void *buffer,
201 size_t size);
202
203 /*!
204 * @function dispatch_data_apply
205 * Traverse the memory regions represented by the specified dispatch data object
206 * in logical order and invoke the specified block once for every contiguous
207 * memory region encountered.
208 *
209 * Each invocation of the block is passed a data object representing the current
210 * region and its logical offset, along with the memory location and extent of
211 * the region. These allow direct read access to the memory region, but are only
212 * valid until the passed-in region object is released. Note that the region
213 * object is released by the system when the block returns, it is the
214 * responsibility of the application to retain it if the region object or the
215 * associated memory location are needed after the block returns.
216 *
217 * @param data The data object to traverse.
218 * @param applier The block to be invoked for every contiguous memory
219 * region in the data object.
220 * @result A Boolean indicating whether traversal completed
221 * successfully.
222 */
223 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
224 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
225 bool
226 dispatch_data_apply(dispatch_data_t data, dispatch_data_applier_t applier);
227
228 /*!
229 * @function dispatch_data_copy_region
230 * Finds the contiguous memory region containing the specified location among
231 * the regions represented by the specified object and returns a copy of the
232 * internal dispatch data object representing that region along with its logical
233 * offset in the specified object.
234 *
235 * @param data The dispatch data object to query.
236 * @param location The logical position in the data object to query.
237 * @param offset_ptr A pointer to a size_t variable to be filled with the
238 * logical offset of the returned region object to the
239 * start of the queried data object.
240 * @result A newly created dispatch data object.
241 */
242 __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_5_0)
243 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NONNULL3 DISPATCH_RETURNS_RETAINED
244 DISPATCH_WARN_RESULT DISPATCH_NOTHROW
245 dispatch_data_t
246 dispatch_data_copy_region(dispatch_data_t data,
247 size_t location,
248 size_t *offset_ptr);
249
250 #endif /* __BLOCKS__ */
251
252 __END_DECLS
253
254 #endif /* __DISPATCH_DATA__ */