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