util/ralloc: Remove double zero'ing of rzalloc buffers
[mesa.git] / src / util / 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 /* Some versions of MinGW are missing _vscprintf's declaration, although they
37 * still provide the symbol in the import library. */
38 #ifdef __MINGW32__
39 _CRTIMP int _vscprintf(const char *format, va_list argptr);
40 #endif
41
42 #include "ralloc.h"
43
44 #ifndef va_copy
45 #ifdef __va_copy
46 #define va_copy(dest, src) __va_copy((dest), (src))
47 #else
48 #define va_copy(dest, src) (dest) = (src)
49 #endif
50 #endif
51
52 #define CANARY 0x5A1106
53
54 struct ralloc_header
55 {
56 #ifdef DEBUG
57 /* A canary value used to determine whether a pointer is ralloc'd. */
58 unsigned canary;
59 #endif
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 #ifdef DEBUG
84 assert(info->canary == CANARY);
85 #endif
86 return info;
87 }
88
89 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
90
91 static void
92 add_child(ralloc_header *parent, ralloc_header *info)
93 {
94 if (parent != NULL) {
95 info->parent = parent;
96 info->next = parent->child;
97 parent->child = info;
98
99 if (info->next != NULL)
100 info->next->prev = info;
101 }
102 }
103
104 void *
105 ralloc_context(const void *ctx)
106 {
107 return ralloc_size(ctx, 0);
108 }
109
110 void *
111 ralloc_size(const void *ctx, size_t size)
112 {
113 /* ralloc_size was originally implemented using calloc, which meant some
114 * code accidentally relied on its zero filling behavior.
115 *
116 * TODO: Make ralloc_size not zero fill memory, and cleanup any code that
117 * should instead be using rzalloc.
118 */
119 return rzalloc_size(ctx, size);
120 }
121
122 void *
123 rzalloc_size(const void *ctx, size_t size)
124 {
125 void *block = calloc(1, size + sizeof(ralloc_header));
126 ralloc_header *info;
127 ralloc_header *parent;
128
129 if (unlikely(block == NULL))
130 return NULL;
131 info = (ralloc_header *) block;
132 parent = ctx != NULL ? get_header(ctx) : NULL;
133
134 add_child(parent, info);
135
136 #ifdef DEBUG
137 info->canary = CANARY;
138 #endif
139
140 return PTR_FROM_HEADER(info);
141 }
142
143 /* helper function - assumes ptr != NULL */
144 static void *
145 resize(void *ptr, size_t size)
146 {
147 ralloc_header *child, *old, *info;
148
149 old = get_header(ptr);
150 info = realloc(old, size + sizeof(ralloc_header));
151
152 if (info == NULL)
153 return NULL;
154
155 /* Update parent and sibling's links to the reallocated node. */
156 if (info != old && info->parent != NULL) {
157 if (info->parent->child == old)
158 info->parent->child = info;
159
160 if (info->prev != NULL)
161 info->prev->next = info;
162
163 if (info->next != NULL)
164 info->next->prev = info;
165 }
166
167 /* Update child->parent links for all children */
168 for (child = info->child; child != NULL; child = child->next)
169 child->parent = info;
170
171 return PTR_FROM_HEADER(info);
172 }
173
174 void *
175 reralloc_size(const void *ctx, void *ptr, size_t size)
176 {
177 if (unlikely(ptr == NULL))
178 return ralloc_size(ctx, size);
179
180 assert(ralloc_parent(ptr) == ctx);
181 return resize(ptr, size);
182 }
183
184 void *
185 ralloc_array_size(const void *ctx, size_t size, unsigned count)
186 {
187 if (count > SIZE_MAX/size)
188 return NULL;
189
190 return ralloc_size(ctx, size * count);
191 }
192
193 void *
194 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
195 {
196 if (count > SIZE_MAX/size)
197 return NULL;
198
199 return rzalloc_size(ctx, size * count);
200 }
201
202 void *
203 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
204 {
205 if (count > SIZE_MAX/size)
206 return NULL;
207
208 return reralloc_size(ctx, ptr, size * count);
209 }
210
211 void
212 ralloc_free(void *ptr)
213 {
214 ralloc_header *info;
215
216 if (ptr == NULL)
217 return;
218
219 info = get_header(ptr);
220 unlink_block(info);
221 unsafe_free(info);
222 }
223
224 static void
225 unlink_block(ralloc_header *info)
226 {
227 /* Unlink from parent & siblings */
228 if (info->parent != NULL) {
229 if (info->parent->child == info)
230 info->parent->child = info->next;
231
232 if (info->prev != NULL)
233 info->prev->next = info->next;
234
235 if (info->next != NULL)
236 info->next->prev = info->prev;
237 }
238 info->parent = NULL;
239 info->prev = NULL;
240 info->next = NULL;
241 }
242
243 static void
244 unsafe_free(ralloc_header *info)
245 {
246 /* Recursively free any children...don't waste time unlinking them. */
247 ralloc_header *temp;
248 while (info->child != NULL) {
249 temp = info->child;
250 info->child = temp->next;
251 unsafe_free(temp);
252 }
253
254 /* Free the block itself. Call the destructor first, if any. */
255 if (info->destructor != NULL)
256 info->destructor(PTR_FROM_HEADER(info));
257
258 free(info);
259 }
260
261 void
262 ralloc_steal(const void *new_ctx, void *ptr)
263 {
264 ralloc_header *info, *parent;
265
266 if (unlikely(ptr == NULL))
267 return;
268
269 info = get_header(ptr);
270 parent = get_header(new_ctx);
271
272 unlink_block(info);
273
274 add_child(parent, info);
275 }
276
277 void
278 ralloc_adopt(const void *new_ctx, void *old_ctx)
279 {
280 ralloc_header *new_info, *old_info, *child;
281
282 if (unlikely(old_ctx == NULL))
283 return;
284
285 old_info = get_header(old_ctx);
286 new_info = get_header(new_ctx);
287
288 /* If there are no children, bail. */
289 if (unlikely(old_info->child == NULL))
290 return;
291
292 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
293 for (child = old_info->child; child->next != NULL; child = child->next) {
294 child->parent = new_info;
295 }
296
297 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
298 child->next = new_info->child;
299 child->parent = new_info;
300 new_info->child = old_info->child;
301 old_info->child = NULL;
302 }
303
304 void *
305 ralloc_parent(const void *ptr)
306 {
307 ralloc_header *info;
308
309 if (unlikely(ptr == NULL))
310 return NULL;
311
312 info = get_header(ptr);
313 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
314 }
315
316 static void *autofree_context = NULL;
317
318 static void
319 autofree(void)
320 {
321 ralloc_free(autofree_context);
322 }
323
324 void *
325 ralloc_autofree_context(void)
326 {
327 if (unlikely(autofree_context == NULL)) {
328 autofree_context = ralloc_context(NULL);
329 atexit(autofree);
330 }
331 return autofree_context;
332 }
333
334 void
335 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
336 {
337 ralloc_header *info = get_header(ptr);
338 info->destructor = destructor;
339 }
340
341 char *
342 ralloc_strdup(const void *ctx, const char *str)
343 {
344 size_t n;
345 char *ptr;
346
347 if (unlikely(str == NULL))
348 return NULL;
349
350 n = strlen(str);
351 ptr = ralloc_array(ctx, char, n + 1);
352 memcpy(ptr, str, n);
353 ptr[n] = '\0';
354 return ptr;
355 }
356
357 char *
358 ralloc_strndup(const void *ctx, const char *str, size_t max)
359 {
360 size_t n;
361 char *ptr;
362
363 if (unlikely(str == NULL))
364 return NULL;
365
366 n = strnlen(str, max);
367 ptr = ralloc_array(ctx, char, n + 1);
368 memcpy(ptr, str, n);
369 ptr[n] = '\0';
370 return ptr;
371 }
372
373 /* helper routine for strcat/strncat - n is the exact amount to copy */
374 static bool
375 cat(char **dest, const char *str, size_t n)
376 {
377 char *both;
378 size_t existing_length;
379 assert(dest != NULL && *dest != NULL);
380
381 existing_length = strlen(*dest);
382 both = resize(*dest, existing_length + n + 1);
383 if (unlikely(both == NULL))
384 return false;
385
386 memcpy(both + existing_length, str, n);
387 both[existing_length + n] = '\0';
388
389 *dest = both;
390 return true;
391 }
392
393
394 bool
395 ralloc_strcat(char **dest, const char *str)
396 {
397 return cat(dest, str, strlen(str));
398 }
399
400 bool
401 ralloc_strncat(char **dest, const char *str, size_t n)
402 {
403 /* Clamp n to the string length */
404 size_t str_length = strlen(str);
405 if (str_length < n)
406 n = str_length;
407
408 return cat(dest, str, n);
409 }
410
411 char *
412 ralloc_asprintf(const void *ctx, const char *fmt, ...)
413 {
414 char *ptr;
415 va_list args;
416 va_start(args, fmt);
417 ptr = ralloc_vasprintf(ctx, fmt, args);
418 va_end(args);
419 return ptr;
420 }
421
422 /* Return the length of the string that would be generated by a printf-style
423 * format and argument list, not including the \0 byte.
424 */
425 static size_t
426 printf_length(const char *fmt, va_list untouched_args)
427 {
428 int size;
429 char junk;
430
431 /* Make a copy of the va_list so the original caller can still use it */
432 va_list args;
433 va_copy(args, untouched_args);
434
435 #ifdef _WIN32
436 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
437 * if the number of characters to write is greater than count.
438 */
439 size = _vscprintf(fmt, args);
440 (void)junk;
441 #else
442 size = vsnprintf(&junk, 1, fmt, args);
443 #endif
444 assert(size >= 0);
445
446 va_end(args);
447
448 return size;
449 }
450
451 char *
452 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
453 {
454 size_t size = printf_length(fmt, args) + 1;
455
456 char *ptr = ralloc_size(ctx, size);
457 if (ptr != NULL)
458 vsnprintf(ptr, size, fmt, args);
459
460 return ptr;
461 }
462
463 bool
464 ralloc_asprintf_append(char **str, const char *fmt, ...)
465 {
466 bool success;
467 va_list args;
468 va_start(args, fmt);
469 success = ralloc_vasprintf_append(str, fmt, args);
470 va_end(args);
471 return success;
472 }
473
474 bool
475 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
476 {
477 size_t existing_length;
478 assert(str != NULL);
479 existing_length = *str ? strlen(*str) : 0;
480 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
481 }
482
483 bool
484 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
485 {
486 bool success;
487 va_list args;
488 va_start(args, fmt);
489 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
490 va_end(args);
491 return success;
492 }
493
494 bool
495 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
496 va_list args)
497 {
498 size_t new_length;
499 char *ptr;
500
501 assert(str != NULL);
502
503 if (unlikely(*str == NULL)) {
504 // Assuming a NULL context is probably bad, but it's expected behavior.
505 *str = ralloc_vasprintf(NULL, fmt, args);
506 *start = strlen(*str);
507 return true;
508 }
509
510 new_length = printf_length(fmt, args);
511
512 ptr = resize(*str, *start + new_length + 1);
513 if (unlikely(ptr == NULL))
514 return false;
515
516 vsnprintf(ptr + *start, new_length + 1, fmt, args);
517 *str = ptr;
518 *start += new_length;
519 return true;
520 }