compiler: Move blob up a level
[mesa.git] / src / compiler / blob.h
1 /*
2 * Copyright © 2014 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #ifndef BLOB_H
25 #define BLOB_H
26
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /* The blob functions implement a simple, low-level API for serializing and
37 * deserializing.
38 *
39 * All objects written to a blob will be serialized directly, (without any
40 * additional meta-data to describe the data written). Therefore, it is the
41 * caller's responsibility to ensure that any data can be read later, (either
42 * by knowing exactly what data is expected, or by writing to the blob
43 * sufficient meta-data to describe what has been written).
44 *
45 * A blob is efficient in that it dynamically grows by doubling in size, so
46 * allocation costs are logarithmic.
47 */
48
49 struct blob {
50 /* The data actually written to the blob. */
51 uint8_t *data;
52
53 /** Number of bytes that have been allocated for \c data. */
54 size_t allocated;
55
56 /** The number of bytes that have actual data written to them. */
57 size_t size;
58
59 /**
60 * True if we've ever failed to realloc or if we go pas the end of a fixed
61 * allocation blob.
62 */
63 bool out_of_memory;
64 };
65
66 /* When done reading, the caller can ensure that everything was consumed by
67 * checking the following:
68 *
69 * 1. blob->current should be equal to blob->end, (if not, too little was
70 * read).
71 *
72 * 2. blob->overrun should be false, (otherwise, too much was read).
73 */
74 struct blob_reader {
75 uint8_t *data;
76 uint8_t *end;
77 uint8_t *current;
78 bool overrun;
79 };
80
81 /**
82 * Create a new, empty blob.
83 *
84 * \return The new blob, (or NULL in case of allocation failure).
85 */
86 struct blob *
87 blob_create(void);
88
89 /**
90 * Destroy a blob and free its memory.
91 */
92 static inline void
93 blob_destroy(struct blob *blob)
94 {
95 free(blob->data);
96 free(blob);
97 }
98
99 /**
100 * Add some unstructured, fixed-size data to a blob.
101 *
102 * \return True unless allocation failed.
103 */
104 bool
105 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
106
107 /**
108 * Reserve space in \blob for a number of bytes.
109 *
110 * Space will be allocated within the blob for these byes, but the bytes will
111 * be left uninitialized. The caller is expected to use the return value to
112 * write directly (and immediately) to these bytes.
113 *
114 * \note The return value is valid immediately upon return, but can be
115 * invalidated by any other call to a blob function. So the caller should call
116 * blob_reserve_byes immediately before writing through the returned pointer.
117 *
118 * This function is intended to be used when interfacing with an existing API
119 * that is not aware of the blob API, (so that blob_write_bytes cannot be
120 * called).
121 *
122 * \return A pointer to space allocated within \blob to which \to_write bytes
123 * can be written, (or NULL in case of any allocation error).
124 */
125 uint8_t *
126 blob_reserve_bytes(struct blob *blob, size_t to_write);
127
128 /**
129 * Overwrite some data previously written to the blob.
130 *
131 * Writes data to an existing portion of the blob at an offset of \offset.
132 * This data range must have previously been written to the blob by one of the
133 * blob_write_* calls.
134 *
135 * For example usage, see blob_overwrite_uint32
136 *
137 * \return True unless the requested offset or offset+to_write lie outside
138 * the current blob's size.
139 */
140 bool
141 blob_overwrite_bytes(struct blob *blob,
142 size_t offset,
143 const void *bytes,
144 size_t to_write);
145
146 /**
147 * Add a uint32_t to a blob.
148 *
149 * \note This function will only write to a uint32_t-aligned offset from the
150 * beginning of the blob's data, so some padding bytes may be added to the
151 * blob if this write follows some unaligned write (such as
152 * blob_write_string).
153 *
154 * \return True unless allocation failed.
155 */
156 bool
157 blob_write_uint32(struct blob *blob, uint32_t value);
158
159 /**
160 * Overwrite a uint32_t previously written to the blob.
161 *
162 * Writes a uint32_t value to an existing portion of the blob at an offset of
163 * \offset. This data range must have previously been written to the blob by
164 * one of the blob_write_* calls.
165 *
166 *
167 * The expected usage is something like the following pattern:
168 *
169 * size_t offset;
170 *
171 * offset = blob->size;
172 * blob_write_uint32 (blob, 0); // placeholder
173 * ... various blob write calls, writing N items ...
174 * blob_overwrite_uint32 (blob, offset, N);
175 *
176 * \return True unless the requested position or position+to_write lie outside
177 * the current blob's size.
178 */
179 bool
180 blob_overwrite_uint32(struct blob *blob,
181 size_t offset,
182 uint32_t value);
183
184 /**
185 * Add a uint64_t to a blob.
186 *
187 * \note This function will only write to a uint64_t-aligned offset from the
188 * beginning of the blob's data, so some padding bytes may be added to the
189 * blob if this write follows some unaligned write (such as
190 * blob_write_string).
191 *
192 * \return True unless allocation failed.
193 */
194 bool
195 blob_write_uint64(struct blob *blob, uint64_t value);
196
197 /**
198 * Add an intptr_t to a blob.
199 *
200 * \note This function will only write to an intptr_t-aligned offset from the
201 * beginning of the blob's data, so some padding bytes may be added to the
202 * blob if this write follows some unaligned write (such as
203 * blob_write_string).
204 *
205 * \return True unless allocation failed.
206 */
207 bool
208 blob_write_intptr(struct blob *blob, intptr_t value);
209
210 /**
211 * Add a NULL-terminated string to a blob, (including the NULL terminator).
212 *
213 * \return True unless allocation failed.
214 */
215 bool
216 blob_write_string(struct blob *blob, const char *str);
217
218 /**
219 * Start reading a blob, (initializing the contents of \blob for reading).
220 *
221 * After this call, the caller can use the various blob_read_* functions to
222 * read elements from the data array.
223 *
224 * For all of the blob_read_* functions, if there is insufficient data
225 * remaining, the functions will do nothing, (perhaps returning default values
226 * such as 0). The caller can detect this by noting that the blob_reader's
227 * current value is unchanged before and after the call.
228 */
229 void
230 blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size);
231
232 /**
233 * Read some unstructured, fixed-size data from the current location, (and
234 * update the current location to just past this data).
235 *
236 * \note The memory returned belongs to the data underlying the blob reader. The
237 * caller must copy the data in order to use it after the lifetime of the data
238 * underlying the blob reader.
239 *
240 * \return The bytes read (see note above about memory lifetime).
241 */
242 void *
243 blob_read_bytes(struct blob_reader *blob, size_t size);
244
245 /**
246 * Read some unstructured, fixed-size data from the current location, copying
247 * it to \dest (and update the current location to just past this data)
248 */
249 void
250 blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size);
251
252 /**
253 * Read a uint32_t from the current location, (and update the current location
254 * to just past this uint32_t).
255 *
256 * \note This function will only read from a uint32_t-aligned offset from the
257 * beginning of the blob's data, so some padding bytes may be skipped.
258 *
259 * \return The uint32_t read
260 */
261 uint32_t
262 blob_read_uint32(struct blob_reader *blob);
263
264 /**
265 * Read a uint64_t from the current location, (and update the current location
266 * to just past this uint64_t).
267 *
268 * \note This function will only read from a uint64_t-aligned offset from the
269 * beginning of the blob's data, so some padding bytes may be skipped.
270 *
271 * \return The uint64_t read
272 */
273 uint64_t
274 blob_read_uint64(struct blob_reader *blob);
275
276 /**
277 * Read an intptr_t value from the current location, (and update the
278 * current location to just past this intptr_t).
279 *
280 * \note This function will only read from an intptr_t-aligned offset from the
281 * beginning of the blob's data, so some padding bytes may be skipped.
282 *
283 * \return The intptr_t read
284 */
285 intptr_t
286 blob_read_intptr(struct blob_reader *blob);
287
288 /**
289 * Read a NULL-terminated string from the current location, (and update the
290 * current location to just past this string).
291 *
292 * \note The memory returned belongs to the data underlying the blob reader. The
293 * caller must copy the string in order to use the string after the lifetime
294 * of the data underlying the blob reader.
295 *
296 * \return The string read (see note above about memory lifetime). However, if
297 * there is no NULL byte remaining within the blob, this function returns
298 * NULL.
299 */
300 char *
301 blob_read_string(struct blob_reader *blob);
302
303 #ifdef __cplusplus
304 }
305 #endif
306
307 #endif /* BLOB_H */