util: add blob_finish_get_buffer
[mesa.git] / src / util / blob.c
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 #include <string.h>
25
26 #include "main/macros.h"
27 #include "blob.h"
28
29 #ifdef HAVE_VALGRIND
30 #include <valgrind.h>
31 #include <memcheck.h>
32 #define VG(x) x
33 #else
34 #define VG(x)
35 #endif
36
37 #define BLOB_INITIAL_SIZE 4096
38
39 /* Ensure that \blob will be able to fit an additional object of size
40 * \additional. The growing (if any) will occur by doubling the existing
41 * allocation.
42 */
43 static bool
44 grow_to_fit(struct blob *blob, size_t additional)
45 {
46 size_t to_allocate;
47 uint8_t *new_data;
48
49 if (blob->out_of_memory)
50 return false;
51
52 if (blob->size + additional <= blob->allocated)
53 return true;
54
55 if (blob->fixed_allocation) {
56 blob->out_of_memory = true;
57 return false;
58 }
59
60 if (blob->allocated == 0)
61 to_allocate = BLOB_INITIAL_SIZE;
62 else
63 to_allocate = blob->allocated * 2;
64
65 to_allocate = MAX2(to_allocate, blob->allocated + additional);
66
67 new_data = realloc(blob->data, to_allocate);
68 if (new_data == NULL) {
69 blob->out_of_memory = true;
70 return false;
71 }
72
73 blob->data = new_data;
74 blob->allocated = to_allocate;
75
76 return true;
77 }
78
79 /* Align the blob->size so that reading or writing a value at (blob->data +
80 * blob->size) will result in an access aligned to a granularity of \alignment
81 * bytes.
82 *
83 * \return True unless allocation fails
84 */
85 static bool
86 align_blob(struct blob *blob, size_t alignment)
87 {
88 const size_t new_size = ALIGN(blob->size, alignment);
89
90 if (blob->size < new_size) {
91 if (!grow_to_fit(blob, new_size - blob->size))
92 return false;
93
94 if (blob->data)
95 memset(blob->data + blob->size, 0, new_size - blob->size);
96 blob->size = new_size;
97 }
98
99 return true;
100 }
101
102 static void
103 align_blob_reader(struct blob_reader *blob, size_t alignment)
104 {
105 blob->current = blob->data + ALIGN(blob->current - blob->data, alignment);
106 }
107
108 void
109 blob_init(struct blob *blob)
110 {
111 blob->data = NULL;
112 blob->allocated = 0;
113 blob->size = 0;
114 blob->fixed_allocation = false;
115 blob->out_of_memory = false;
116 }
117
118 void
119 blob_init_fixed(struct blob *blob, void *data, size_t size)
120 {
121 blob->data = data;
122 blob->allocated = size;
123 blob->size = 0;
124 blob->fixed_allocation = true;
125 blob->out_of_memory = false;
126 }
127
128 void
129 blob_finish_get_buffer(struct blob *blob, void **buffer, size_t *size)
130 {
131 *buffer = blob->data;
132 *size = blob->size;
133 blob->data = NULL;
134
135 /* Trim the buffer. */
136 *buffer = realloc(*buffer, *size);
137 }
138
139 bool
140 blob_overwrite_bytes(struct blob *blob,
141 size_t offset,
142 const void *bytes,
143 size_t to_write)
144 {
145 /* Detect an attempt to overwrite data out of bounds. */
146 if (offset + to_write < offset || blob->size < offset + to_write)
147 return false;
148
149 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
150
151 if (blob->data)
152 memcpy(blob->data + offset, bytes, to_write);
153
154 return true;
155 }
156
157 bool
158 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
159 {
160 if (! grow_to_fit(blob, to_write))
161 return false;
162
163 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
164
165 if (blob->data)
166 memcpy(blob->data + blob->size, bytes, to_write);
167 blob->size += to_write;
168
169 return true;
170 }
171
172 intptr_t
173 blob_reserve_bytes(struct blob *blob, size_t to_write)
174 {
175 intptr_t ret;
176
177 if (! grow_to_fit (blob, to_write))
178 return -1;
179
180 ret = blob->size;
181 blob->size += to_write;
182
183 return ret;
184 }
185
186 intptr_t
187 blob_reserve_uint32(struct blob *blob)
188 {
189 align_blob(blob, sizeof(uint32_t));
190 return blob_reserve_bytes(blob, sizeof(uint32_t));
191 }
192
193 intptr_t
194 blob_reserve_intptr(struct blob *blob)
195 {
196 align_blob(blob, sizeof(intptr_t));
197 return blob_reserve_bytes(blob, sizeof(intptr_t));
198 }
199
200 bool
201 blob_write_uint32(struct blob *blob, uint32_t value)
202 {
203 align_blob(blob, sizeof(value));
204
205 return blob_write_bytes(blob, &value, sizeof(value));
206 }
207
208 #define ASSERT_ALIGNED(_offset, _align) \
209 assert(ALIGN((_offset), (_align)) == (_offset))
210
211 bool
212 blob_overwrite_uint32 (struct blob *blob,
213 size_t offset,
214 uint32_t value)
215 {
216 ASSERT_ALIGNED(offset, sizeof(value));
217 return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
218 }
219
220 bool
221 blob_write_uint64(struct blob *blob, uint64_t value)
222 {
223 align_blob(blob, sizeof(value));
224
225 return blob_write_bytes(blob, &value, sizeof(value));
226 }
227
228 bool
229 blob_write_intptr(struct blob *blob, intptr_t value)
230 {
231 align_blob(blob, sizeof(value));
232
233 return blob_write_bytes(blob, &value, sizeof(value));
234 }
235
236 bool
237 blob_overwrite_intptr (struct blob *blob,
238 size_t offset,
239 intptr_t value)
240 {
241 ASSERT_ALIGNED(offset, sizeof(value));
242 return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
243 }
244
245 bool
246 blob_write_string(struct blob *blob, const char *str)
247 {
248 return blob_write_bytes(blob, str, strlen(str) + 1);
249 }
250
251 void
252 blob_reader_init(struct blob_reader *blob, const void *data, size_t size)
253 {
254 blob->data = data;
255 blob->end = blob->data + size;
256 blob->current = data;
257 blob->overrun = false;
258 }
259
260 /* Check that an object of size \size can be read from this blob.
261 *
262 * If not, set blob->overrun to indicate that we attempted to read too far.
263 */
264 static bool
265 ensure_can_read(struct blob_reader *blob, size_t size)
266 {
267 if (blob->overrun)
268 return false;
269
270 if (blob->current <= blob->end && blob->end - blob->current >= size)
271 return true;
272
273 blob->overrun = true;
274
275 return false;
276 }
277
278 const void *
279 blob_read_bytes(struct blob_reader *blob, size_t size)
280 {
281 const void *ret;
282
283 if (! ensure_can_read (blob, size))
284 return NULL;
285
286 ret = blob->current;
287
288 blob->current += size;
289
290 return ret;
291 }
292
293 void
294 blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size)
295 {
296 const void *bytes;
297
298 bytes = blob_read_bytes(blob, size);
299 if (bytes == NULL)
300 return;
301
302 memcpy(dest, bytes, size);
303 }
304
305 void
306 blob_skip_bytes(struct blob_reader *blob, size_t size)
307 {
308 if (ensure_can_read (blob, size))
309 blob->current += size;
310 }
311
312 /* These next three read functions have identical form. If we add any beyond
313 * these first three we should probably switch to generating these with a
314 * preprocessor macro.
315 */
316 uint32_t
317 blob_read_uint32(struct blob_reader *blob)
318 {
319 uint32_t ret;
320 int size = sizeof(ret);
321
322 align_blob_reader(blob, size);
323
324 if (! ensure_can_read(blob, size))
325 return 0;
326
327 ret = *((uint32_t*) blob->current);
328
329 blob->current += size;
330
331 return ret;
332 }
333
334 uint64_t
335 blob_read_uint64(struct blob_reader *blob)
336 {
337 uint64_t ret;
338 int size = sizeof(ret);
339
340 align_blob_reader(blob, size);
341
342 if (! ensure_can_read(blob, size))
343 return 0;
344
345 ret = *((uint64_t*) blob->current);
346
347 blob->current += size;
348
349 return ret;
350 }
351
352 intptr_t
353 blob_read_intptr(struct blob_reader *blob)
354 {
355 intptr_t ret;
356 int size = sizeof(ret);
357
358 align_blob_reader(blob, size);
359
360 if (! ensure_can_read(blob, size))
361 return 0;
362
363 ret = *((intptr_t *) blob->current);
364
365 blob->current += size;
366
367 return ret;
368 }
369
370 char *
371 blob_read_string(struct blob_reader *blob)
372 {
373 int size;
374 char *ret;
375 uint8_t *nul;
376
377 /* If we're already at the end, then this is an overrun. */
378 if (blob->current >= blob->end) {
379 blob->overrun = true;
380 return NULL;
381 }
382
383 /* Similarly, if there is no zero byte in the data remaining in this blob,
384 * we also consider that an overrun.
385 */
386 nul = memchr(blob->current, 0, blob->end - blob->current);
387
388 if (nul == NULL) {
389 blob->overrun = true;
390 return NULL;
391 }
392
393 size = nul - blob->current + 1;
394
395 assert(ensure_can_read(blob, size));
396
397 ret = (char *) blob->current;
398
399 blob->current += size;
400
401 return ret;
402 }