ralloc: Add new [v]asprintf_rewrite_tail functions.
[mesa.git] / src / glsl / ralloc.c
1 /*
2 * Copyright © 2010 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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
30
31 /* Android defines SIZE_MAX in limits.h, instead of the standard stdint.h */
32 #ifdef ANDROID
33 #include <limits.h>
34 #endif
35
36 #include "ralloc.h"
37
38 #ifdef __GNUC__
39 #define likely(x) __builtin_expect(!!(x),1)
40 #define unlikely(x) __builtin_expect(!!(x),0)
41 #else
42 #define likely(x) !!(x)
43 #define unlikely(x) !!(x)
44 #endif
45
46 #ifndef va_copy
47 #ifdef __va_copy
48 #define va_copy(dest, src) __va_copy((dest), (src))
49 #else
50 #define va_copy(dest, src) (dest) = (src)
51 #endif
52 #endif
53
54 #define CANARY 0x5A1106
55
56 struct ralloc_header
57 {
58 /* A canary value used to determine whether a pointer is ralloc'd. */
59 unsigned canary;
60
61 struct ralloc_header *parent;
62
63 /* The first child (head of a linked list) */
64 struct ralloc_header *child;
65
66 /* Linked list of siblings */
67 struct ralloc_header *prev;
68 struct ralloc_header *next;
69
70 void (*destructor)(void *);
71 };
72
73 typedef struct ralloc_header ralloc_header;
74
75 static void unlink_block(ralloc_header *info);
76 static void unsafe_free(ralloc_header *info);
77
78 static ralloc_header *
79 get_header(const void *ptr)
80 {
81 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
82 sizeof(ralloc_header));
83 assert(info->canary == CANARY);
84 return info;
85 }
86
87 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
88
89 static void
90 add_child(ralloc_header *parent, ralloc_header *info)
91 {
92 if (parent != NULL) {
93 info->parent = parent;
94 info->next = parent->child;
95 parent->child = info;
96
97 if (info->next != NULL)
98 info->next->prev = info;
99 }
100 }
101
102 void *
103 ralloc_context(const void *ctx)
104 {
105 return ralloc_size(ctx, 0);
106 }
107
108 void *
109 ralloc_size(const void *ctx, size_t size)
110 {
111 void *block = calloc(1, size + sizeof(ralloc_header));
112
113 ralloc_header *info = (ralloc_header *) block;
114 ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
115
116 add_child(parent, info);
117
118 info->canary = CANARY;
119
120 return PTR_FROM_HEADER(info);
121 }
122
123 void *
124 rzalloc_size(const void *ctx, size_t size)
125 {
126 void *ptr = ralloc_size(ctx, size);
127 if (likely(ptr != NULL))
128 memset(ptr, 0, size);
129 return ptr;
130 }
131
132 /* helper function - assumes ptr != NULL */
133 static void *
134 resize(void *ptr, size_t size)
135 {
136 ralloc_header *child, *old, *info;
137
138 old = get_header(ptr);
139 info = realloc(old, size + sizeof(ralloc_header));
140
141 if (info == NULL)
142 return NULL;
143
144 /* Update parent and sibling's links to the reallocated node. */
145 if (info != old && info->parent != NULL) {
146 if (info->parent->child == old)
147 info->parent->child = info;
148
149 if (info->prev != NULL)
150 info->prev->next = info;
151
152 if (info->next != NULL)
153 info->next->prev = info;
154 }
155
156 /* Update child->parent links for all children */
157 for (child = info->child; child != NULL; child = child->next)
158 child->parent = info;
159
160 return PTR_FROM_HEADER(info);
161 }
162
163 void *
164 reralloc_size(const void *ctx, void *ptr, size_t size)
165 {
166 if (unlikely(ptr == NULL))
167 return ralloc_size(ctx, size);
168
169 assert(ralloc_parent(ptr) == ctx);
170 return resize(ptr, size);
171 }
172
173 void *
174 ralloc_array_size(const void *ctx, size_t size, unsigned count)
175 {
176 if (count > SIZE_MAX/size)
177 return NULL;
178
179 return ralloc_size(ctx, size * count);
180 }
181
182 void *
183 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
184 {
185 if (count > SIZE_MAX/size)
186 return NULL;
187
188 return rzalloc_size(ctx, size * count);
189 }
190
191 void *
192 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
193 {
194 if (count > SIZE_MAX/size)
195 return NULL;
196
197 return reralloc_size(ctx, ptr, size * count);
198 }
199
200 void
201 ralloc_free(void *ptr)
202 {
203 ralloc_header *info;
204
205 if (ptr == NULL)
206 return;
207
208 info = get_header(ptr);
209 unlink_block(info);
210 unsafe_free(info);
211 }
212
213 static void
214 unlink_block(ralloc_header *info)
215 {
216 /* Unlink from parent & siblings */
217 if (info->parent != NULL) {
218 if (info->parent->child == info)
219 info->parent->child = info->next;
220
221 if (info->prev != NULL)
222 info->prev->next = info->next;
223
224 if (info->next != NULL)
225 info->next->prev = info->prev;
226 }
227 info->parent = NULL;
228 info->prev = NULL;
229 info->next = NULL;
230 }
231
232 static void
233 unsafe_free(ralloc_header *info)
234 {
235 /* Recursively free any children...don't waste time unlinking them. */
236 ralloc_header *temp;
237 while (info->child != NULL) {
238 temp = info->child;
239 info->child = temp->next;
240 unsafe_free(temp);
241 }
242
243 /* Free the block itself. Call the destructor first, if any. */
244 if (info->destructor != NULL)
245 info->destructor(PTR_FROM_HEADER(info));
246
247 free(info);
248 }
249
250 void
251 ralloc_steal(const void *new_ctx, void *ptr)
252 {
253 ralloc_header *info, *parent;
254
255 if (unlikely(ptr == NULL))
256 return;
257
258 info = get_header(ptr);
259 parent = get_header(new_ctx);
260
261 unlink_block(info);
262
263 add_child(parent, info);
264 }
265
266 void *
267 ralloc_parent(const void *ptr)
268 {
269 ralloc_header *info;
270
271 if (unlikely(ptr == NULL))
272 return NULL;
273
274 info = get_header(ptr);
275 return PTR_FROM_HEADER(info->parent);
276 }
277
278 static void *autofree_context = NULL;
279
280 static void
281 autofree(void)
282 {
283 ralloc_free(autofree_context);
284 }
285
286 void *
287 ralloc_autofree_context(void)
288 {
289 if (unlikely(autofree_context == NULL)) {
290 autofree_context = ralloc_context(NULL);
291 atexit(autofree);
292 }
293 return autofree_context;
294 }
295
296 void
297 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
298 {
299 ralloc_header *info = get_header(ptr);
300 info->destructor = destructor;
301 }
302
303 char *
304 ralloc_strdup(const void *ctx, const char *str)
305 {
306 size_t n;
307 char *ptr;
308
309 if (unlikely(str == NULL))
310 return NULL;
311
312 n = strlen(str);
313 ptr = ralloc_array(ctx, char, n + 1);
314 memcpy(ptr, str, n);
315 ptr[n] = '\0';
316 return ptr;
317 }
318
319 char *
320 ralloc_strndup(const void *ctx, const char *str, size_t max)
321 {
322 size_t n;
323 char *ptr;
324
325 if (unlikely(str == NULL))
326 return NULL;
327
328 n = strlen(str);
329 if (n > max)
330 n = max;
331
332 ptr = ralloc_array(ctx, char, n + 1);
333 memcpy(ptr, str, n);
334 ptr[n] = '\0';
335 return ptr;
336 }
337
338 /* helper routine for strcat/strncat - n is the exact amount to copy */
339 static bool
340 cat(char **dest, const char *str, size_t n)
341 {
342 char *both;
343 size_t existing_length;
344 assert(dest != NULL && *dest != NULL);
345
346 existing_length = strlen(*dest);
347 both = resize(*dest, existing_length + n + 1);
348 if (unlikely(both == NULL))
349 return false;
350
351 memcpy(both + existing_length, str, n);
352 both[existing_length + n] = '\0';
353
354 *dest = both;
355 return true;
356 }
357
358
359 bool
360 ralloc_strcat(char **dest, const char *str)
361 {
362 return cat(dest, str, strlen(str));
363 }
364
365 bool
366 ralloc_strncat(char **dest, const char *str, size_t n)
367 {
368 /* Clamp n to the string length */
369 size_t str_length = strlen(str);
370 if (str_length < n)
371 n = str_length;
372
373 return cat(dest, str, n);
374 }
375
376 char *
377 ralloc_asprintf(const void *ctx, const char *fmt, ...)
378 {
379 char *ptr;
380 va_list args;
381 va_start(args, fmt);
382 ptr = ralloc_vasprintf(ctx, fmt, args);
383 va_end(args);
384 return ptr;
385 }
386
387 /* Return the length of the string that would be generated by a printf-style
388 * format and argument list, not including the \0 byte.
389 */
390 static size_t
391 printf_length(const char *fmt, va_list untouched_args)
392 {
393 int size;
394 char junk;
395
396 /* Make a copy of the va_list so the original caller can still use it */
397 va_list args;
398 va_copy(args, untouched_args);
399
400 #ifdef _MSC_VER
401 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
402 * if the number of characters to write is greater than count.
403 */
404 size = _vscprintf(fmt, args);
405 (void)junk;
406 #else
407 size = vsnprintf(&junk, 1, fmt, args);
408 #endif
409 assert(size >= 0);
410
411 va_end(args);
412
413 return size;
414 }
415
416 char *
417 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
418 {
419 size_t size = printf_length(fmt, args) + 1;
420
421 char *ptr = ralloc_size(ctx, size);
422 if (ptr != NULL)
423 vsnprintf(ptr, size, fmt, args);
424
425 return ptr;
426 }
427
428 bool
429 ralloc_asprintf_append(char **str, const char *fmt, ...)
430 {
431 bool success;
432 va_list args;
433 va_start(args, fmt);
434 success = ralloc_vasprintf_append(str, fmt, args);
435 va_end(args);
436 return success;
437 }
438
439 bool
440 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
441 {
442 assert(str != NULL);
443 size_t existing_length = *str ? strlen(*str) : 0;
444 return ralloc_vasprintf_rewrite_tail(str, existing_length, fmt, args);
445 }
446
447 bool
448 ralloc_asprintf_rewrite_tail(char **str, size_t start, const char *fmt, ...)
449 {
450 bool success;
451 va_list args;
452 va_start(args, fmt);
453 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
454 va_end(args);
455 return success;
456 }
457
458 bool
459 ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
460 va_list args)
461 {
462 size_t new_length;
463 char *ptr;
464
465 assert(str != NULL);
466
467 if (unlikely(*str == NULL)) {
468 // Assuming a NULL context is probably bad, but it's expected behavior.
469 *str = ralloc_vasprintf(NULL, fmt, args);
470 return true;
471 }
472
473 new_length = printf_length(fmt, args);
474
475 ptr = resize(*str, start + new_length + 1);
476 if (unlikely(ptr == NULL))
477 return false;
478
479 vsnprintf(ptr + start, new_length + 1, fmt, args);
480 *str = ptr;
481 return true;
482 }