data_private.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2011-2013 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. * IMPORTANT: This header file describes INTERNAL interfaces to libdispatch
  22. * which are subject to change in future releases of Mac OS X. Any applications
  23. * relying on these interfaces WILL break.
  24. */
  25. #ifndef __DISPATCH_DATA_PRIVATE__
  26. #define __DISPATCH_DATA_PRIVATE__
  27. #ifndef __DISPATCH_INDIRECT__
  28. #error "Please #include <dispatch/dispatch.h> instead of this file directly."
  29. #include <dispatch/base.h> // for HeaderDoc
  30. #endif
  31. DISPATCH_ASSUME_NONNULL_BEGIN
  32. __BEGIN_DECLS
  33. /*!
  34. * @const DISPATCH_DATA_DESTRUCTOR_NONE
  35. * @discussion The destructor for dispatch data objects that require no buffer
  36. * memory management. This can be used to allow a data object to efficiently
  37. * encapsulate buffers that should not be copied or freed by the system.
  38. */
  39. #define DISPATCH_DATA_DESTRUCTOR_NONE (_dispatch_data_destructor_none)
  40. API_AVAILABLE(macos(10.8), ios(6.0))
  41. DISPATCH_DATA_DESTRUCTOR_TYPE_DECL(none);
  42. /*!
  43. * @const DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE
  44. * @discussion The destructor for dispatch data objects that have been created
  45. * from buffers that require deallocation using vm_deallocate.
  46. */
  47. #define DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE \
  48. (_dispatch_data_destructor_vm_deallocate)
  49. API_AVAILABLE(macos(10.8), ios(6.0)) DISPATCH_LINUX_UNAVAILABLE()
  50. DISPATCH_DATA_DESTRUCTOR_TYPE_DECL(vm_deallocate);
  51. /*!
  52. * @function dispatch_data_create_f
  53. * Creates a dispatch data object from the given contiguous buffer of memory. If
  54. * a non-default destructor is provided, ownership of the buffer remains with
  55. * the caller (i.e. the bytes will not be copied). The last release of the data
  56. * object will result in the invocation of the specified destructor function on
  57. * specified queue to free the buffer (passed as the context parameter).
  58. *
  59. * If the DISPATCH_DATA_DESTRUCTOR_FREE destructor is provided the buffer will
  60. * be freed via free(3) and the queue argument ignored.
  61. *
  62. * If the DISPATCH_DATA_DESTRUCTOR_DEFAULT destructor is provided, data object
  63. * creation will copy the buffer into internal memory managed by the system.
  64. *
  65. * @param buffer A contiguous buffer of data.
  66. * @param size The size of the contiguous buffer of data.
  67. * @param queue The queue to which the destructor should be submitted.
  68. * @param destructor The destructor function responsible for freeing the
  69. * data buffer when it is no longer needed.
  70. * @result A newly created dispatch data object.
  71. */
  72. API_AVAILABLE(macos(10.9), ios(7.0))
  73. DISPATCH_EXPORT DISPATCH_RETURNS_RETAINED DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  74. dispatch_data_t
  75. dispatch_data_create_f(const void *buffer,
  76. size_t size,
  77. dispatch_queue_t _Nullable queue,
  78. dispatch_function_t _Nullable destructor);
  79. /*!
  80. * @function dispatch_data_create_alloc
  81. * Creates a dispatch data object representing a newly allocated memory region
  82. * of the given size. If a non-NULL reference to a pointer is provided, it is
  83. * filled with the location of the memory region.
  84. *
  85. * It is the responsibility of the application to ensure that the data object
  86. * becomes immutable (i.e. the returned memory region is not further modified)
  87. * once the dispatch data object is passed to other API.
  88. *
  89. * @param size The size of the required allocation.
  90. * @param buffer_ptr A pointer to a pointer variable to be filled with the
  91. * location of the newly allocated memory region, or NULL.
  92. * @result A newly created dispatch data object.
  93. */
  94. API_AVAILABLE(macos(10.9), ios(6.0))
  95. DISPATCH_EXPORT DISPATCH_RETURNS_RETAINED
  96. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  97. dispatch_data_t
  98. dispatch_data_create_alloc(size_t size, void *_Nullable *_Nullable buffer_ptr);
  99. /*!
  100. * @typedef dispatch_data_applier_function_t
  101. * A function to be invoked for every contiguous memory region in a data object.
  102. *
  103. * @param context Application-defined context parameter.
  104. * @param region A data object representing the current region.
  105. * @param offset The logical offset of the current region to the start
  106. * of the data object.
  107. * @param buffer The location of the memory for the current region.
  108. * @param size The size of the memory for the current region.
  109. * @result A Boolean indicating whether traversal should continue.
  110. */
  111. typedef bool (*dispatch_data_applier_function_t)(void *_Nullable context,
  112. dispatch_data_t region, size_t offset, const void *buffer, size_t size);
  113. /*!
  114. * @function dispatch_data_apply_f
  115. * Traverse the memory regions represented by the specified dispatch data object
  116. * in logical order and invoke the specified function once for every contiguous
  117. * memory region encountered.
  118. *
  119. * Each invocation of the function is passed a data object representing the
  120. * current region and its logical offset, along with the memory location and
  121. * extent of the region. These allow direct read access to the memory region,
  122. * but are only valid until the passed-in region object is released. Note that
  123. * the region object is released by the system when the function returns, it is
  124. * the responsibility of the application to retain it if the region object or
  125. * the associated memory location are needed after the function returns.
  126. *
  127. * @param data The data object to traverse.
  128. * @param context The application-defined context to pass to the function.
  129. * @param applier The function to be invoked for every contiguous memory
  130. * region in the data object.
  131. * @result A Boolean indicating whether traversal completed
  132. * successfully.
  133. */
  134. API_AVAILABLE(macos(10.9), ios(6.0))
  135. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  136. bool
  137. dispatch_data_apply_f(dispatch_data_t data, void *_Nullable context,
  138. dispatch_data_applier_function_t applier);
  139. #if TARGET_OS_MAC
  140. /*!
  141. * @function dispatch_data_make_memory_entry
  142. * Return a mach memory entry for the memory regions represented by the
  143. * specified dispatch data object.
  144. *
  145. * For data objects created with the DISPATCH_DATA_DESTRUCTOR_VM_DEALLOCATE
  146. * destructor, directly makes a memory entry from the represented region;
  147. * otherwise, makes a memory entry from newly allocated pages containing a copy
  148. * of the represented memory regions.
  149. *
  150. * @param data The data object to make a memory entry for.
  151. * @result A mach port for the newly made memory entry, or
  152. * MACH_PORT_NULL if an error occurred.
  153. */
  154. API_AVAILABLE(macos(10.9), ios(6.0))
  155. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
  156. mach_port_t
  157. dispatch_data_make_memory_entry(dispatch_data_t data);
  158. #endif
  159. /*!
  160. * @functiongroup Dispatch data transform SPI
  161. */
  162. /*!
  163. * @typedef dispatch_data_format_type_t
  164. *
  165. * @abstract
  166. * Data formats are used to specify the input and output types of data supplied
  167. * to dispatch_data_create_transform.
  168. */
  169. typedef const struct dispatch_data_format_type_s *dispatch_data_format_type_t;
  170. #define DISPATCH_DATA_FORMAT_TYPE_DECL(name) \
  171. DISPATCH_EXPORT const struct dispatch_data_format_type_s \
  172. _dispatch_data_format_type_##name
  173. /*!
  174. * @const DISPATCH_DATA_FORMAT_TYPE_NONE
  175. * @discussion A data format denoting that the given input or output format is,
  176. * or should be, comprised of raw data bytes with no given encoding.
  177. */
  178. #define DISPATCH_DATA_FORMAT_TYPE_NONE (&_dispatch_data_format_type_none)
  179. API_AVAILABLE(macos(10.8), ios(6.0))
  180. DISPATCH_DATA_FORMAT_TYPE_DECL(none);
  181. /*!
  182. * @const DISPATCH_DATA_FORMAT_TYPE_BASE32
  183. * @discussion A data format denoting that the given input or output format is,
  184. * or should be, encoded in Base32 (RFC 4648) format. On input, this format will
  185. * skip whitespace characters. Cannot be used in conjunction with UTF format
  186. * types.
  187. */
  188. #define DISPATCH_DATA_FORMAT_TYPE_BASE32 (&_dispatch_data_format_type_base32)
  189. API_AVAILABLE(macos(10.8), ios(6.0))
  190. DISPATCH_DATA_FORMAT_TYPE_DECL(base32);
  191. /*!
  192. * @const DISPATCH_DATA_FORMAT_TYPE_BASE32HEX
  193. * @discussion A data format denoting that the given input or output format is,
  194. * or should be, encoded in Base32Hex (RFC 4648) format. On input, this format
  195. * will skip whitespace characters. Cannot be used in conjunction with UTF
  196. * format types.
  197. */
  198. #define DISPATCH_DATA_FORMAT_TYPE_BASE32HEX \
  199. (&_dispatch_data_format_type_base32hex)
  200. API_AVAILABLE(macos(10.9), ios(7.0))
  201. DISPATCH_DATA_FORMAT_TYPE_DECL(base32hex);
  202. /*!
  203. * @const DISPATCH_DATA_FORMAT_TYPE_BASE64
  204. * @discussion A data format denoting that the given input or output format is,
  205. * or should be, encoded in Base64 (RFC 4648) format. On input, this format will
  206. * skip whitespace characters. Cannot be used in conjunction with UTF format
  207. * types.
  208. */
  209. #define DISPATCH_DATA_FORMAT_TYPE_BASE64 (&_dispatch_data_format_type_base64)
  210. API_AVAILABLE(macos(10.8), ios(6.0))
  211. DISPATCH_DATA_FORMAT_TYPE_DECL(base64);
  212. /*!
  213. * @const DISPATCH_DATA_FORMAT_TYPE_UTF8
  214. * @discussion A data format denoting that the given input or output format is,
  215. * or should be, encoded in UTF-8 format. Is only valid when used in conjunction
  216. * with other UTF format types.
  217. */
  218. #define DISPATCH_DATA_FORMAT_TYPE_UTF8 (&_dispatch_data_format_type_utf8)
  219. API_AVAILABLE(macos(10.8), ios(6.0))
  220. DISPATCH_DATA_FORMAT_TYPE_DECL(utf8);
  221. /*!
  222. * @const DISPATCH_DATA_FORMAT_TYPE_UTF16LE
  223. * @discussion A data format denoting that the given input or output format is,
  224. * or should be, encoded in UTF-16LE format. Is only valid when used in
  225. * conjunction with other UTF format types.
  226. */
  227. #define DISPATCH_DATA_FORMAT_TYPE_UTF16LE (&_dispatch_data_format_type_utf16le)
  228. API_AVAILABLE(macos(10.8), ios(6.0))
  229. DISPATCH_DATA_FORMAT_TYPE_DECL(utf16le);
  230. /*!
  231. * @const DISPATCH_DATA_FORMAT_TYPE_UTF16BE
  232. * @discussion A data format denoting that the given input or output format is,
  233. * or should be, encoded in UTF-16BE format. Is only valid when used in
  234. * conjunction with other UTF format types.
  235. */
  236. #define DISPATCH_DATA_FORMAT_TYPE_UTF16BE (&_dispatch_data_format_type_utf16be)
  237. API_AVAILABLE(macos(10.8), ios(6.0))
  238. DISPATCH_DATA_FORMAT_TYPE_DECL(utf16be);
  239. /*!
  240. * @const DISPATCH_DATA_FORMAT_TYPE_UTFANY
  241. * @discussion A data format denoting that dispatch_data_create_transform should
  242. * attempt to automatically detect the input type based on the presence of a
  243. * byte order mark character at the beginning of the data. In the absence of a
  244. * BOM, the data will be assumed to be in UTF-8 format. Only valid as an input
  245. * format.
  246. */
  247. #define DISPATCH_DATA_FORMAT_TYPE_UTF_ANY (&_dispatch_data_format_type_utf_any)
  248. API_AVAILABLE(macos(10.8), ios(6.0))
  249. DISPATCH_DATA_FORMAT_TYPE_DECL(utf_any);
  250. /*!
  251. * @function dispatch_data_create_transform
  252. * Returns a new dispatch data object after transforming the given data object
  253. * from the supplied format, into the given output format.
  254. *
  255. * @param data
  256. * The data object representing the region(s) of memory to transform.
  257. * @param input_type
  258. * Flags specifying the input format of the source dispatch_data_t
  259. *
  260. * @param output_type
  261. * Flags specifying the expected output format of the resulting transformation.
  262. *
  263. * @result
  264. * A newly created dispatch data object, dispatch_data_empty if no has been
  265. * produced, or NULL if an error occurred.
  266. */
  267. API_AVAILABLE(macos(10.8), ios(6.0))
  268. DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_RETURNS_RETAINED
  269. DISPATCH_WARN_RESULT DISPATCH_NOTHROW
  270. dispatch_data_t
  271. dispatch_data_create_with_transform(dispatch_data_t data,
  272. dispatch_data_format_type_t input_type,
  273. dispatch_data_format_type_t output_type);
  274. /*!
  275. * @function dispatch_data_get_flattened_bytes_4libxpc
  276. *
  277. * Similar to dispatch_data_create_map() but attaches it to the passed in
  278. * dispatch data.
  279. *
  280. * The returned mapping, if not NULL, has the size returned by
  281. * dispatch_data_get_size() for the specified object, and its lifetime is tied
  282. * to the one of the dispatch data itself.
  283. *
  284. * @discussion
  285. * This interface is reserved for XPC usage and is not considered stable ABI.
  286. *
  287. *
  288. * @result
  289. * A newly created linear mapping for this data object, may return NULL if
  290. * making the dispatch data contiguous failed to allocate memory.
  291. */
  292. API_AVAILABLE(macos(10.14), ios(12.0), tvos(12.0), watchos(5.0), bridgeos(4.0))
  293. const void *_Nullable
  294. dispatch_data_get_flattened_bytes_4libxpc(dispatch_data_t data);
  295. __END_DECLS
  296. DISPATCH_ASSUME_NONNULL_END
  297. #endif // __DISPATCH_DATA_PRIVATE__