compiler: Move blob up a level
[mesa.git] / src / compiler / 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->allocated == 0)
56 to_allocate = BLOB_INITIAL_SIZE;
57 else
58 to_allocate = blob->allocated * 2;
59
60 to_allocate = MAX2(to_allocate, blob->allocated + additional);
61
62 new_data = realloc(blob->data, to_allocate);
63 if (new_data == NULL) {
64 blob->out_of_memory = true;
65 return false;
66 }
67
68 blob->data = new_data;
69 blob->allocated = to_allocate;
70
71 return true;
72 }
73
74 /* Align the blob->size so that reading or writing a value at (blob->data +
75 * blob->size) will result in an access aligned to a granularity of \alignment
76 * bytes.
77 *
78 * \return True unless allocation fails
79 */
80 static bool
81 align_blob(struct blob *blob, size_t alignment)
82 {
83 const size_t new_size = ALIGN(blob->size, alignment);
84
85 if (blob->size < new_size) {
86 if (!grow_to_fit(blob, new_size - blob->size))
87 return false;
88
89 memset(blob->data + blob->size, 0, new_size - blob->size);
90 blob->size = new_size;
91 }
92
93 return true;
94 }
95
96 static void
97 align_blob_reader(struct blob_reader *blob, size_t alignment)
98 {
99 blob->current = blob->data + ALIGN(blob->current - blob->data, alignment);
100 }
101
102 struct blob *
103 blob_create()
104 {
105 struct blob *blob = (struct blob *) malloc(sizeof(struct blob));
106 if (blob == NULL)
107 return NULL;
108
109 blob->data = NULL;
110 blob->allocated = 0;
111 blob->size = 0;
112 blob->out_of_memory = false;
113
114 return blob;
115 }
116
117 bool
118 blob_overwrite_bytes(struct blob *blob,
119 size_t offset,
120 const void *bytes,
121 size_t to_write)
122 {
123 /* Detect an attempt to overwrite data out of bounds. */
124 if (blob->size < offset + to_write)
125 return false;
126
127 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
128
129 memcpy(blob->data + offset, bytes, to_write);
130
131 return true;
132 }
133
134 bool
135 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
136 {
137 if (! grow_to_fit(blob, to_write))
138 return false;
139
140 VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
141
142 memcpy(blob->data + blob->size, bytes, to_write);
143 blob->size += to_write;
144
145 return true;
146 }
147
148 uint8_t *
149 blob_reserve_bytes(struct blob *blob, size_t to_write)
150 {
151 uint8_t *ret;
152
153 if (! grow_to_fit (blob, to_write))
154 return NULL;
155
156 ret = blob->data + blob->size;
157 blob->size += to_write;
158
159 return ret;
160 }
161
162 bool
163 blob_write_uint32(struct blob *blob, uint32_t value)
164 {
165 align_blob(blob, sizeof(value));
166
167 return blob_write_bytes(blob, &value, sizeof(value));
168 }
169
170 bool
171 blob_overwrite_uint32 (struct blob *blob,
172 size_t offset,
173 uint32_t value)
174 {
175 return blob_overwrite_bytes(blob, offset, &value, sizeof(value));
176 }
177
178 bool
179 blob_write_uint64(struct blob *blob, uint64_t value)
180 {
181 align_blob(blob, sizeof(value));
182
183 return blob_write_bytes(blob, &value, sizeof(value));
184 }
185
186 bool
187 blob_write_intptr(struct blob *blob, intptr_t value)
188 {
189 align_blob(blob, sizeof(value));
190
191 return blob_write_bytes(blob, &value, sizeof(value));
192 }
193
194 bool
195 blob_write_string(struct blob *blob, const char *str)
196 {
197 return blob_write_bytes(blob, str, strlen(str) + 1);
198 }
199
200 void
201 blob_reader_init(struct blob_reader *blob, uint8_t *data, size_t size)
202 {
203 blob->data = data;
204 blob->end = data + size;
205 blob->current = data;
206 blob->overrun = false;
207 }
208
209 /* Check that an object of size \size can be read from this blob.
210 *
211 * If not, set blob->overrun to indicate that we attempted to read too far.
212 */
213 static bool
214 ensure_can_read(struct blob_reader *blob, size_t size)
215 {
216 if (blob->overrun)
217 return false;
218
219 if (blob->current < blob->end && blob->end - blob->current >= size)
220 return true;
221
222 blob->overrun = true;
223
224 return false;
225 }
226
227 void *
228 blob_read_bytes(struct blob_reader *blob, size_t size)
229 {
230 void *ret;
231
232 if (! ensure_can_read (blob, size))
233 return NULL;
234
235 ret = blob->current;
236
237 blob->current += size;
238
239 return ret;
240 }
241
242 void
243 blob_copy_bytes(struct blob_reader *blob, uint8_t *dest, size_t size)
244 {
245 uint8_t *bytes;
246
247 bytes = blob_read_bytes(blob, size);
248 if (bytes == NULL)
249 return;
250
251 memcpy(dest, bytes, size);
252 }
253
254 /* These next three read functions have identical form. If we add any beyond
255 * these first three we should probably switch to generating these with a
256 * preprocessor macro.
257 */
258 uint32_t
259 blob_read_uint32(struct blob_reader *blob)
260 {
261 uint32_t ret;
262 int size = sizeof(ret);
263
264 align_blob_reader(blob, size);
265
266 if (! ensure_can_read(blob, size))
267 return 0;
268
269 ret = *((uint32_t*) blob->current);
270
271 blob->current += size;
272
273 return ret;
274 }
275
276 uint64_t
277 blob_read_uint64(struct blob_reader *blob)
278 {
279 uint64_t ret;
280 int size = sizeof(ret);
281
282 align_blob_reader(blob, size);
283
284 if (! ensure_can_read(blob, size))
285 return 0;
286
287 ret = *((uint64_t*) blob->current);
288
289 blob->current += size;
290
291 return ret;
292 }
293
294 intptr_t
295 blob_read_intptr(struct blob_reader *blob)
296 {
297 intptr_t ret;
298 int size = sizeof(ret);
299
300 align_blob_reader(blob, size);
301
302 if (! ensure_can_read(blob, size))
303 return 0;
304
305 ret = *((intptr_t *) blob->current);
306
307 blob->current += size;
308
309 return ret;
310 }
311
312 char *
313 blob_read_string(struct blob_reader *blob)
314 {
315 int size;
316 char *ret;
317 uint8_t *nul;
318
319 /* If we're already at the end, then this is an overrun. */
320 if (blob->current >= blob->end) {
321 blob->overrun = true;
322 return NULL;
323 }
324
325 /* Similarly, if there is no zero byte in the data remaining in this blob,
326 * we also consider that an overrun.
327 */
328 nul = memchr(blob->current, 0, blob->end - blob->current);
329
330 if (nul == NULL) {
331 blob->overrun = true;
332 return NULL;
333 }
334
335 size = nul - blob->current + 1;
336
337 assert(ensure_can_read(blob, size));
338
339 ret = (char *) blob->current;
340
341 blob->current += size;
342
343 return ret;
344 }