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);
45 #define likely(x) __builtin_expect(!!(x),1)
46 #define unlikely(x) __builtin_expect(!!(x),0)
48 #define likely(x) !!(x)
49 #define unlikely(x) !!(x)
54 #define va_copy(dest, src) __va_copy((dest), (src))
56 #define va_copy(dest, src) (dest) = (src)
60 #define CANARY 0x5A1106
64 /* A canary value used to determine whether a pointer is ralloc'd. */
67 struct ralloc_header *parent;
69 /* The first child (head of a linked list) */
70 struct ralloc_header *child;
72 /* Linked list of siblings */
73 struct ralloc_header *prev;
74 struct ralloc_header *next;
76 void (*destructor)(void *);
79 typedef struct ralloc_header ralloc_header;
81 static void unlink_block(ralloc_header *info);
82 static void unsafe_free(ralloc_header *info);
84 static ralloc_header *
85 get_header(const void *ptr)
87 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
88 sizeof(ralloc_header));
89 assert(info->canary == CANARY);
93 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
96 add_child(ralloc_header *parent, ralloc_header *info)
99 info->parent = parent;
100 info->next = parent->child;
101 parent->child = info;
103 if (info->next != NULL)
104 info->next->prev = info;
109 ralloc_context(const void *ctx)
111 return ralloc_size(ctx, 0);
115 ralloc_size(const void *ctx, size_t size)
117 void *block = calloc(1, size + sizeof(ralloc_header));
119 ralloc_header *info = (ralloc_header *) block;
120 ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
122 add_child(parent, info);
124 info->canary = CANARY;
126 return PTR_FROM_HEADER(info);
130 rzalloc_size(const void *ctx, size_t size)
132 void *ptr = ralloc_size(ctx, size);
133 if (likely(ptr != NULL))
134 memset(ptr, 0, size);
138 /* helper function - assumes ptr != NULL */
140 resize(void *ptr, size_t size)
142 ralloc_header *child, *old, *info;
144 old = get_header(ptr);
145 info = realloc(old, size + sizeof(ralloc_header));
150 /* Update parent and sibling's links to the reallocated node. */
151 if (info != old && info->parent != NULL) {
152 if (info->parent->child == old)
153 info->parent->child = info;
155 if (info->prev != NULL)
156 info->prev->next = info;
158 if (info->next != NULL)
159 info->next->prev = info;
162 /* Update child->parent links for all children */
163 for (child = info->child; child != NULL; child = child->next)
164 child->parent = info;
166 return PTR_FROM_HEADER(info);
170 reralloc_size(const void *ctx, void *ptr, size_t size)
172 if (unlikely(ptr == NULL))
173 return ralloc_size(ctx, size);
175 assert(ralloc_parent(ptr) == ctx);
176 return resize(ptr, size);
180 ralloc_array_size(const void *ctx, size_t size, unsigned count)
182 if (count > SIZE_MAX/size)
185 return ralloc_size(ctx, size * count);
189 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
191 if (count > SIZE_MAX/size)
194 return rzalloc_size(ctx, size * count);
198 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
200 if (count > SIZE_MAX/size)
203 return reralloc_size(ctx, ptr, size * count);
207 ralloc_free(void *ptr)
214 info = get_header(ptr);
220 unlink_block(ralloc_header *info)
222 /* Unlink from parent & siblings */
223 if (info->parent != NULL) {
224 if (info->parent->child == info)
225 info->parent->child = info->next;
227 if (info->prev != NULL)
228 info->prev->next = info->next;
230 if (info->next != NULL)
231 info->next->prev = info->prev;
239 unsafe_free(ralloc_header *info)
241 /* Recursively free any children...don't waste time unlinking them. */
243 while (info->child != NULL) {
245 info->child = temp->next;
249 /* Free the block itself. Call the destructor first, if any. */
250 if (info->destructor != NULL)
251 info->destructor(PTR_FROM_HEADER(info));
257 ralloc_steal(const void *new_ctx, void *ptr)
259 ralloc_header *info, *parent;
261 if (unlikely(ptr == NULL))
264 info = get_header(ptr);
265 parent = get_header(new_ctx);
269 add_child(parent, info);
273 ralloc_parent(const void *ptr)
277 if (unlikely(ptr == NULL))
280 info = get_header(ptr);
281 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
284 static void *autofree_context = NULL;
289 ralloc_free(autofree_context);
293 ralloc_autofree_context(void)
295 if (unlikely(autofree_context == NULL)) {
296 autofree_context = ralloc_context(NULL);
299 return autofree_context;
303 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
305 ralloc_header *info = get_header(ptr);
306 info->destructor = destructor;
310 ralloc_strdup(const void *ctx, const char *str)
315 if (unlikely(str == NULL))
319 ptr = ralloc_array(ctx, char, n + 1);
326 ralloc_strndup(const void *ctx, const char *str, size_t max)
331 if (unlikely(str == NULL))
338 ptr = ralloc_array(ctx, char, n + 1);
344 /* helper routine for strcat/strncat - n is the exact amount to copy */
346 cat(char **dest, const char *str, size_t n)
349 size_t existing_length;
350 assert(dest != NULL && *dest != NULL);
352 existing_length = strlen(*dest);
353 both = resize(*dest, existing_length + n + 1);
354 if (unlikely(both == NULL))
357 memcpy(both + existing_length, str, n);
358 both[existing_length + n] = '\0';
366 ralloc_strcat(char **dest, const char *str)
368 return cat(dest, str, strlen(str));
372 ralloc_strncat(char **dest, const char *str, size_t n)
374 /* Clamp n to the string length */
375 size_t str_length = strlen(str);
379 return cat(dest, str, n);
383 ralloc_asprintf(const void *ctx, const char *fmt, ...)
388 ptr = ralloc_vasprintf(ctx, fmt, args);
393 /* Return the length of the string that would be generated by a printf-style
394 * format and argument list, not including the \0 byte.
397 printf_length(const char *fmt, va_list untouched_args)
402 /* Make a copy of the va_list so the original caller can still use it */
404 va_copy(args, untouched_args);
407 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
408 * if the number of characters to write is greater than count.
410 size = _vscprintf(fmt, args);
413 size = vsnprintf(&junk, 1, fmt, args);
423 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
425 size_t size = printf_length(fmt, args) + 1;
427 char *ptr = ralloc_size(ctx, size);
429 vsnprintf(ptr, size, fmt, args);
435 ralloc_asprintf_append(char **str, const char *fmt, ...)
440 success = ralloc_vasprintf_append(str, fmt, args);
446 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
448 size_t existing_length;
450 existing_length = *str ? strlen(*str) : 0;
451 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
455 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
460 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
466 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
474 if (unlikely(*str == NULL)) {
475 // Assuming a NULL context is probably bad, but it's expected behavior.
476 *str = ralloc_vasprintf(NULL, fmt, args);
480 new_length = printf_length(fmt, args);
482 ptr = resize(*str, *start + new_length + 1);
483 if (unlikely(ptr == NULL))
486 vsnprintf(ptr + *start, new_length + 1, fmt, args);
488 *start += new_length;