compiler/blob: Add a concept of a fixed-allocation blob
[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 /** True if \c data a fixed allocation that we cannot resize
60 *
61 * \see blob_init_fixed
62 */
63 bool fixed_allocation;
64
65 /**
66 * True if we've ever failed to realloc or if we go pas the end of a fixed
67 * allocation blob.
68 */
69 bool out_of_memory;
70 };
71
72 /* When done reading, the caller can ensure that everything was consumed by
73 * checking the following:
74 *
75 * 1. blob->current should be equal to blob->end, (if not, too little was
76 * read).
77 *
78 * 2. blob->overrun should be false, (otherwise, too much was read).
79 */
80 struct blob_reader {
81 uint8_t *data;
82 uint8_t *end;
83 uint8_t *current;
84 bool overrun;
85 };
86
87 /**
88 * Init a new, empty blob.
89 */
90 void
91 blob_init(struct blob *blob);
92
93 /**
94 * Init a new, fixed-size blob.
95 *
96 * A fixed-size blob has a fixed block of data that will not be freed on
97 * blob_finish and will never be grown. If we hit the end, we simply start
98 * returning false from the write functions.
99 */
100 void
101 blob_init_fixed(struct blob *blob, void *data, size_t size);
102
103 /**
104 * Finish a blob and free its memory.
105 *
106 * If \blob was initialized with blob_init_fixed, the data pointer is
107 * considered to be owned by the user and will not be freed.
108 */
109 static inline void
110 blob_finish(struct blob *blob)
111 {
112 if (!blob->fixed_allocation)
113 free(blob->data);
114 }
115
116 /**
117 * Add some unstructured, fixed-size data to a blob.
118 *
119 * \return True unless allocation failed.
120 */
121 bool
122 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
123
124 /**
125 * Reserve space in \blob for a number of bytes.
126 *
127 * Space will be allocated within the blob for these byes, but the bytes will
128 * be left uninitialized. The caller is expected to use the return value to
129 * write directly (and immediately) to these bytes.
130 *
131 * \note The return value is valid immediately upon return, but can be
132 * invalidated by any other call to a blob function. So the caller should call
133 * blob_reserve_byes immediately before writing through the returned pointer.
134 *
135 * This function is intended to be used when interfacing with an existing API
136 * that is not aware of the blob API, (so that blob_write_bytes cannot be
137 * called).
138 *
139 * \return A pointer to space allocated within \blob to which \to_write bytes
140 * can be written, (or NULL in case of any allocation error).
141 */
142 uint8_t *
143 blob_reserve_bytes(struct blob *blob, size_t to_write);
144
145 /**
146 * Overwrite some data previously written to the blob.
147 *
148 * Writes data to an existing portion of the blob at an offset of \offset.
149 * This data range must have previously been written to the blob by one of the
150 * blob_write_* calls.
151 *
152 * For example usage, see blob_overwrite_uint32
153 *
154 * \return True unless the requested offset or offset+to_write lie outside
155 * the current blob's size.
156 */
157 bool
158 blob_overwrite_bytes(struct blob *blob,
159 size_t offset,
160 const void *bytes,
161 size_t to_write);
162
163 /**
164 * Add a uint32_t to a blob.
165 *
166 * \note This function will only write to a uint32_t-aligned offset from the
167 * beginning of the blob's data, so some padding bytes may be added to the
168 * blob if this write follows some unaligned write (such as
169 * blob_write_string).
170 *
171 * \return True unless allocation failed.
172 */
173 bool
174 blob_write_uint32(struct blob *blob, uint32_t value);
175
176 /**
177 * Overwrite a uint32_t previously written to the blob.
178 *
179 * Writes a uint32_t value to an existing portion of the blob at an offset of
180 * \offset. This data range must have previously been written to the blob by
181 * one of the blob_write_* calls.
182 *
183 *
184 * The expected usage is something like the following pattern:
185 *
186 * size_t offset;
187 *
188 * offset = blob->size;
189 * blob_write_uint32 (blob, 0); // placeholder
190 * ... various blob write calls, writing N items ...
191 * blob_overwrite_uint32 (blob, offset, N);
192 *
193 * \return True unless the requested position or position+to_write lie outside
194 * the current blob's size.
195 */
196 bool
197 blob_overwrite_uint32(struct blob *blob,
198 size_t offset,
199 uint32_t value);
200
201 /**
202 * Add a uint64_t to a blob.
203 *
204 * \note This function will only write to a uint64_t-aligned offset from the
205 * beginning of the blob's data, so some padding bytes may be added to the
206 * blob if this write follows some unaligned write (such as
207 * blob_write_string).
208 *
209 * \return True unless allocation failed.
210 */
211 bool
212 blob_write_uint64(struct blob *blob, uint64_t value);
213
214 /**
215 * Add an intptr_t to a blob.
216 *
217 * \note This function will only write to an intptr_t-aligned offset from the
218 * beginning of the blob's data, so some padding bytes may be added to the
219 * blob if this write follows some unaligned write (such as
220 * blob_write_string).
221 *
222 * \return True unless allocation failed.
223 */
224 bool
225 blob_write_intptr(struct blob *blob, intptr_t value);
226
227 /**
228 * Add a NULL-terminated string to a blob, (including the NULL terminator).
229 *
230 * \return True unless allocation failed.
231 */
232 bool
233 blob_write_string(struct blob *blob, const char *str);
234
235 /**
236 * Start reading a blob, (initializing the contents of \blob for reading).
237 *
238 * After this call, the caller can use the various blob_read_* functions to
239 * read elements from the data array.
240 *
241 * For all of the blob_read_* functions, if there is insufficient data
242 * remaining, the functions will do nothing, (perhaps returning default values
243 * such as 0). The caller can detect this by noting that the blob_reader's
244 * current value is unchanged before and after the call.
245 */
246 void
247 blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size);
248
249 /**
250 * Read some unstructured, fixed-size data from the current location, (and
251 * update the current location to just past this data).
252 *
253 * \note The memory returned belongs to the data underlying the blob reader. The
254 * caller must copy the data in order to use it after the lifetime of the data
255 * underlying the blob reader.
256 *
257 * \return The bytes read (see note above about memory lifetime).
258 */
259 void *
260 blob_read_bytes(struct blob_reader *blob, size_t size);
261
262 /**
263 * Read some unstructured, fixed-size data from the current location, copying
264 * it to \dest (and update the current location to just past this data)
265 */
266 void
267 blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size);
268
269 /**
270 * Read a uint32_t from the current location, (and update the current location
271 * to just past this uint32_t).
272 *
273 * \note This function will only read from a uint32_t-aligned offset from the
274 * beginning of the blob's data, so some padding bytes may be skipped.
275 *
276 * \return The uint32_t read
277 */
278 uint32_t
279 blob_read_uint32(struct blob_reader *blob);
280
281 /**
282 * Read a uint64_t from the current location, (and update the current location
283 * to just past this uint64_t).
284 *
285 * \note This function will only read from a uint64_t-aligned offset from the
286 * beginning of the blob's data, so some padding bytes may be skipped.
287 *
288 * \return The uint64_t read
289 */
290 uint64_t
291 blob_read_uint64(struct blob_reader *blob);
292
293 /**
294 * Read an intptr_t value from the current location, (and update the
295 * current location to just past this intptr_t).
296 *
297 * \note This function will only read from an intptr_t-aligned offset from the
298 * beginning of the blob's data, so some padding bytes may be skipped.
299 *
300 * \return The intptr_t read
301 */
302 intptr_t
303 blob_read_intptr(struct blob_reader *blob);
304
305 /**
306 * Read a NULL-terminated string from the current location, (and update the
307 * current location to just past this string).
308 *
309 * \note The memory returned belongs to the data underlying the blob reader. The
310 * caller must copy the string in order to use the string after the lifetime
311 * of the data underlying the blob reader.
312 *
313 * \return The string read (see note above about memory lifetime). However, if
314 * there is no NULL byte remaining within the blob, this function returns
315 * NULL.
316 */
317 char *
318 blob_read_string(struct blob_reader *blob);
319
320 #ifdef __cplusplus
321 }
322 #endif
323
324 #endif /* BLOB_H */