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.
34 #define likely(x) __builtin_expect(!!(x),1)
35 #define unlikely(x) __builtin_expect(!!(x),0)
37 #define likely(x) !!(x)
38 #define unlikely(x) !!(x)
41 #define CANARY 0x5A1106
45 /* A canary value used to determine whether a pointer is ralloc'd. */
48 struct ralloc_header
*parent
;
50 /* The first child (head of a linked list) */
51 struct ralloc_header
*child
;
53 /* Linked list of siblings */
54 struct ralloc_header
*prev
;
55 struct ralloc_header
*next
;
57 void (*destructor
)(void *);
60 typedef struct ralloc_header ralloc_header
;
62 static void unlink_block(ralloc_header
*info
);
63 static void unsafe_free(ralloc_header
*info
);
65 static ralloc_header
*
66 get_header(const void *ptr
)
68 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
69 sizeof(ralloc_header
));
70 assert(info
->canary
== CANARY
);
74 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
77 add_child(ralloc_header
*parent
, ralloc_header
*info
)
80 info
->parent
= parent
;
81 info
->next
= parent
->child
;
84 if (info
->next
!= NULL
)
85 info
->next
->prev
= info
;
90 ralloc_context(const void *ctx
)
92 return ralloc_size(ctx
, 0);
96 ralloc_size(const void *ctx
, size_t size
)
98 void *block
= calloc(1, size
+ sizeof(ralloc_header
));
100 ralloc_header
*info
= (ralloc_header
*) block
;
101 ralloc_header
*parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
103 add_child(parent
, info
);
105 info
->canary
= CANARY
;
107 return PTR_FROM_HEADER(info
);
111 rzalloc_size(const void *ctx
, size_t size
)
113 void *ptr
= ralloc_size(ctx
, size
);
114 if (likely(ptr
!= NULL
))
115 memset(ptr
, 0, size
);
119 /* helper function - assumes ptr != NULL */
121 resize(void *ptr
, size_t size
)
123 ralloc_header
*child
, *old
, *info
;
125 old
= get_header(ptr
);
126 info
= realloc(old
, size
+ sizeof(ralloc_header
));
131 /* Update parent and sibling's links to the reallocated node. */
132 if (info
!= old
&& info
->parent
!= NULL
) {
133 if (info
->parent
->child
== old
)
134 info
->parent
->child
= info
;
136 if (info
->prev
!= NULL
)
137 info
->prev
->next
= info
;
139 if (info
->next
!= NULL
)
140 info
->next
->prev
= info
;
143 /* Update child->parent links for all children */
144 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
145 child
->parent
= info
;
147 return PTR_FROM_HEADER(info
);
151 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
153 if (unlikely(ptr
== NULL
))
154 return ralloc_size(ctx
, size
);
156 assert(ralloc_parent(ptr
) == ctx
);
157 return resize(ptr
, size
);
161 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
163 if (count
> SIZE_MAX
/size
)
166 return ralloc_size(ctx
, size
* count
);
170 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
172 if (count
> SIZE_MAX
/size
)
175 return rzalloc_size(ctx
, size
* count
);
179 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
181 if (count
> SIZE_MAX
/size
)
184 return reralloc_size(ctx
, ptr
, size
* count
);
188 ralloc_free(void *ptr
)
195 info
= get_header(ptr
);
201 unlink_block(ralloc_header
*info
)
203 /* Unlink from parent & siblings */
204 if (info
->parent
!= NULL
) {
205 if (info
->parent
->child
== info
)
206 info
->parent
->child
= info
->next
;
208 if (info
->prev
!= NULL
)
209 info
->prev
->next
= info
->next
;
211 if (info
->next
!= NULL
)
212 info
->next
->prev
= info
->prev
;
220 unsafe_free(ralloc_header
*info
)
222 /* Recursively free any children...don't waste time unlinking them. */
224 while (info
->child
!= NULL
) {
226 info
->child
= temp
->next
;
230 /* Free the block itself. Call the destructor first, if any. */
231 if (info
->destructor
!= NULL
)
232 info
->destructor(PTR_FROM_HEADER(info
));
238 ralloc_steal(const void *new_ctx
, void *ptr
)
240 ralloc_header
*info
, *parent
;
242 if (unlikely(ptr
== NULL
))
245 info
= get_header(ptr
);
246 parent
= get_header(new_ctx
);
250 add_child(parent
, info
);
254 ralloc_parent(const void *ptr
)
258 if (unlikely(ptr
== NULL
))
261 info
= get_header(ptr
);
262 return PTR_FROM_HEADER(info
->parent
);
265 static void *autofree_context
= NULL
;
270 ralloc_free(autofree_context
);
274 ralloc_autofree_context(void)
276 if (unlikely(autofree_context
== NULL
)) {
277 autofree_context
= ralloc_context(NULL
);
280 return autofree_context
;
284 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
286 ralloc_header
*info
= get_header(ptr
);
287 info
->destructor
= destructor
;
291 ralloc_strdup(const void *ctx
, const char *str
)
296 if (unlikely(str
== NULL
))
300 ptr
= ralloc_array(ctx
, char, n
+ 1);
307 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
312 if (unlikely(str
== NULL
))
319 ptr
= ralloc_array(ctx
, char, n
+ 1);
325 /* helper routine for strcat/strncat - n is the exact amount to copy */
327 cat(char **dest
, const char *str
, size_t n
)
330 size_t existing_length
;
331 assert(dest
!= NULL
&& *dest
!= NULL
);
333 existing_length
= strlen(*dest
);
334 both
= resize(*dest
, existing_length
+ n
+ 1);
335 if (unlikely(both
== NULL
))
338 memcpy(both
+ existing_length
, str
, n
);
339 both
[existing_length
+ n
] = '\0';
347 ralloc_strcat(char **dest
, const char *str
)
349 return cat(dest
, str
, strlen(str
));
353 ralloc_strncat(char **dest
, const char *str
, size_t n
)
355 /* Clamp n to the string length */
356 size_t str_length
= strlen(str
);
360 return cat(dest
, str
, n
);
364 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
369 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
374 /* Return the length of the string that would be generated by a printf-style
375 * format and argument list, not including the \0 byte.
378 printf_length(const char *fmt
, va_list untouched_args
)
383 /* Make a copy of the va_list so the original caller can still use it */
385 va_copy(args
, untouched_args
);
387 size
= vsnprintf(&junk
, 1, fmt
, args
);
394 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
396 size_t size
= printf_length(fmt
, args
) + 1;
398 char *ptr
= ralloc_size(ctx
, size
);
400 vsnprintf(ptr
, size
, fmt
, args
);
406 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
411 success
= ralloc_vasprintf_append(str
, fmt
, args
);
417 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
419 size_t existing_length
, new_length
;
424 if (unlikely(*str
== NULL
)) {
425 // Assuming a NULL context is probably bad, but it's expected behavior.
426 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
430 existing_length
= strlen(*str
);
431 new_length
= printf_length(fmt
, args
);
433 ptr
= resize(*str
, existing_length
+ new_length
+ 1);
434 if (unlikely(ptr
== NULL
))
437 vsnprintf(ptr
+ existing_length
, new_length
+ 1, fmt
, args
);