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#if defined(__LP64__) && !defined(DISPATCH_DATA_USE_LEAF_MEMBER) && !USE_OBJC
36// explicit leaf member is free on 64bit due to padding
37#define DISPATCH_DATA_USE_LEAF_MEMBER 1
38#endif
39
40typedef struct range_record_s {
41	dispatch_data_t data_object;
42	size_t from;
43	size_t length;
44} range_record;
45
46#if USE_OBJC
47#if OS_OBJECT_USE_OBJC
48@interface DISPATCH_CLASS(data) : NSObject <DISPATCH_CLASS(data)>
49@end
50#endif
51DISPATCH_OBJC_CLASS_DECL(data);
52DISPATCH_OBJC_CLASS_DECL(data_empty);
53#define DISPATCH_DATA_CLASS DISPATCH_OBJC_CLASS(data)
54#define DISPATCH_DATA_EMPTY_CLASS DISPATCH_OBJC_CLASS(data_empty)
55#else // USE_OBJC
56DISPATCH_CLASS_DECL(data);
57#define DISPATCH_DATA_CLASS DISPATCH_VTABLE(data)
58#define DISPATCH_DATA_EMPTY_CLASS DISPATCH_VTABLE(data)
59#endif // USE_OBJC
60
61struct dispatch_data_s {
62#if USE_OBJC
63	const void *do_vtable;
64	dispatch_queue_t do_targetq;
65	void *ctxt;
66	void *finalizer;
67#else // USE_OBJC
68	DISPATCH_STRUCT_HEADER(data);
69#endif // USE_OBJC
70#if DISPATCH_DATA_USE_LEAF_MEMBER
71	bool leaf;
72#endif
73	dispatch_block_t destructor;
74	size_t size, num_records;
75	union {
76		const void* buf;
77		range_record records[0];
78	};
79};
80
81#if DISPATCH_DATA_USE_LEAF_MEMBER
82#define _dispatch_data_leaf(d) ((d)->leaf)
83#define _dispatch_data_num_records(d) ((d)->num_records)
84#else
85#define _dispatch_data_leaf(d) ((d)->num_records ? 0 : ((d)->size ? 1 : 0))
86#define _dispatch_data_num_records(d) \
87		(_dispatch_data_leaf(d) ? 1 : (d)->num_records)
88#endif // DISPATCH_DATA_USE_LEAF_MEMBER
89
90typedef dispatch_data_t (*dispatch_transform_t)(dispatch_data_t data);
91
92struct dispatch_data_format_type_s {
93	uint64_t type;
94	uint64_t input_mask;
95	uint64_t output_mask;
96	dispatch_transform_t decode;
97	dispatch_transform_t encode;
98};
99
100void dispatch_data_init(dispatch_data_t data, const void *buffer, size_t size,
101		dispatch_block_t destructor);
102void _dispatch_data_dispose(dispatch_data_t data);
103size_t _dispatch_data_debug(dispatch_data_t data, char* buf, size_t bufsiz);
104const dispatch_block_t _dispatch_data_destructor_inline;
105#define DISPATCH_DATA_DESTRUCTOR_INLINE (_dispatch_data_destructor_inline)
106
107#if !__OBJC2__
108
109static inline const void*
110_dispatch_data_map_direct(dispatch_data_t dd)
111{
112	size_t offset = 0;
113	if (slowpath(!dd->size)) {
114		return NULL;
115	}
116	if (slowpath(!_dispatch_data_leaf(dd)) &&
117			_dispatch_data_num_records(dd) == 1 &&
118			_dispatch_data_leaf(dd->records[0].data_object)) {
119		offset = dd->records[0].from;
120		dd = dd->records[0].data_object;
121	}
122	return fastpath(_dispatch_data_leaf(dd)) ? (dd->buf + offset) : NULL;
123}
124
125#endif // !__OBJC2__
126
127#endif // __DISPATCH_DATA_INTERNAL__
128