2 * Copyright © 2010 Intel Corporation
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:
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
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.
31 /* Android defines SIZE_MAX in limits.h, instead of the standard stdint.h */
36 /* Some versions of MinGW are missing _vscprintf's declaration, although they
37 * still provide the symbol in the import library. */
39 _CRTIMP int _vscprintf(const char *format, va_list argptr);
46 #define va_copy(dest, src) __va_copy((dest), (src))
48 #define va_copy(dest, src) (dest) = (src)
52 #define CANARY 0x5A1106
56 /* A canary value used to determine whether a pointer is ralloc'd. */
59 struct ralloc_header *parent;
61 /* The first child (head of a linked list) */
62 struct ralloc_header *child;
64 /* Linked list of siblings */
65 struct ralloc_header *prev;
66 struct ralloc_header *next;
68 void (*destructor)(void *);
71 typedef struct ralloc_header ralloc_header;
73 static void unlink_block(ralloc_header *info);
74 static void unsafe_free(ralloc_header *info);
76 static ralloc_header *
77 get_header(const void *ptr)
79 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
80 sizeof(ralloc_header));
81 assert(info->canary == CANARY);
85 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
88 add_child(ralloc_header *parent, ralloc_header *info)
91 info->parent = parent;
92 info->next = parent->child;
95 if (info->next != NULL)
96 info->next->prev = info;
101 ralloc_context(const void *ctx)
103 return ralloc_size(ctx, 0);
107 ralloc_size(const void *ctx, size_t size)
109 void *block = calloc(1, size + sizeof(ralloc_header));
111 ralloc_header *parent;
113 if (unlikely(block == NULL))
115 info = (ralloc_header *) block;
116 parent = ctx != NULL ? get_header(ctx) : NULL;
118 add_child(parent, info);
120 info->canary = CANARY;
122 return PTR_FROM_HEADER(info);
126 rzalloc_size(const void *ctx, size_t size)
128 void *ptr = ralloc_size(ctx, size);
129 if (likely(ptr != NULL))
130 memset(ptr, 0, size);
134 /* helper function - assumes ptr != NULL */
136 resize(void *ptr, size_t size)
138 ralloc_header *child, *old, *info;
140 old = get_header(ptr);
141 info = realloc(old, size + sizeof(ralloc_header));
146 /* Update parent and sibling's links to the reallocated node. */
147 if (info != old && info->parent != NULL) {
148 if (info->parent->child == old)
149 info->parent->child = info;
151 if (info->prev != NULL)
152 info->prev->next = info;
154 if (info->next != NULL)
155 info->next->prev = info;
158 /* Update child->parent links for all children */
159 for (child = info->child; child != NULL; child = child->next)
160 child->parent = info;
162 return PTR_FROM_HEADER(info);
166 reralloc_size(const void *ctx, void *ptr, size_t size)
168 if (unlikely(ptr == NULL))
169 return ralloc_size(ctx, size);
171 assert(ralloc_parent(ptr) == ctx);
172 return resize(ptr, size);
176 ralloc_array_size(const void *ctx, size_t size, unsigned count)
178 if (count > SIZE_MAX/size)
181 return ralloc_size(ctx, size * count);
185 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
187 if (count > SIZE_MAX/size)
190 return rzalloc_size(ctx, size * count);
194 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
196 if (count > SIZE_MAX/size)
199 return reralloc_size(ctx, ptr, size * count);
203 ralloc_free(void *ptr)
210 info = get_header(ptr);
216 unlink_block(ralloc_header *info)
218 /* Unlink from parent & siblings */
219 if (info->parent != NULL) {
220 if (info->parent->child == info)
221 info->parent->child = info->next;
223 if (info->prev != NULL)
224 info->prev->next = info->next;
226 if (info->next != NULL)
227 info->next->prev = info->prev;
235 unsafe_free(ralloc_header *info)
237 /* Recursively free any children...don't waste time unlinking them. */
239 while (info->child != NULL) {
241 info->child = temp->next;
245 /* Free the block itself. Call the destructor first, if any. */
246 if (info->destructor != NULL)
247 info->destructor(PTR_FROM_HEADER(info));
253 ralloc_steal(const void *new_ctx, void *ptr)
255 ralloc_header *info, *parent;
257 if (unlikely(ptr == NULL))
260 info = get_header(ptr);
261 parent = get_header(new_ctx);
265 add_child(parent, info);
269 ralloc_parent(const void *ptr)
273 if (unlikely(ptr == NULL))
276 info = get_header(ptr);
277 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
280 static void *autofree_context = NULL;
285 ralloc_free(autofree_context);
289 ralloc_autofree_context(void)
291 if (unlikely(autofree_context == NULL)) {
292 autofree_context = ralloc_context(NULL);
295 return autofree_context;
299 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
301 ralloc_header *info = get_header(ptr);
302 info->destructor = destructor;
306 ralloc_strdup(const void *ctx, const char *str)
311 if (unlikely(str == NULL))
315 ptr = ralloc_array(ctx, char, n + 1);
322 ralloc_strndup(const void *ctx, const char *str, size_t max)
327 if (unlikely(str == NULL))
334 ptr = ralloc_array(ctx, char, n + 1);
340 /* helper routine for strcat/strncat - n is the exact amount to copy */
342 cat(char **dest, const char *str, size_t n)
345 size_t existing_length;
346 assert(dest != NULL && *dest != NULL);
348 existing_length = strlen(*dest);
349 both = resize(*dest, existing_length + n + 1);
350 if (unlikely(both == NULL))
353 memcpy(both + existing_length, str, n);
354 both[existing_length + n] = '\0';
362 ralloc_strcat(char **dest, const char *str)
364 return cat(dest, str, strlen(str));
368 ralloc_strncat(char **dest, const char *str, size_t n)
370 /* Clamp n to the string length */
371 size_t str_length = strlen(str);
375 return cat(dest, str, n);
379 ralloc_asprintf(const void *ctx, const char *fmt, ...)
384 ptr = ralloc_vasprintf(ctx, fmt, args);
389 /* Return the length of the string that would be generated by a printf-style
390 * format and argument list, not including the \0 byte.
393 printf_length(const char *fmt, va_list untouched_args)
398 /* Make a copy of the va_list so the original caller can still use it */
400 va_copy(args, untouched_args);
403 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
404 * if the number of characters to write is greater than count.
406 size = _vscprintf(fmt, args);
409 size = vsnprintf(&junk, 1, fmt, args);
419 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
421 size_t size = printf_length(fmt, args) + 1;
423 char *ptr = ralloc_size(ctx, size);
425 vsnprintf(ptr, size, fmt, args);
431 ralloc_asprintf_append(char **str, const char *fmt, ...)
436 success = ralloc_vasprintf_append(str, fmt, args);
442 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
444 size_t existing_length;
446 existing_length = *str ? strlen(*str) : 0;
447 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
451 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
456 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
462 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
470 if (unlikely(*str == NULL)) {
471 // Assuming a NULL context is probably bad, but it's expected behavior.
472 *str = ralloc_vasprintf(NULL, fmt, args);
476 new_length = printf_length(fmt, args);
478 ptr = resize(*str, *start + new_length + 1);
479 if (unlikely(ptr == NULL))
482 vsnprintf(ptr + *start, new_length + 1, fmt, args);
484 *start += new_length;