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)
43 #define va_copy(dest, src) __va_copy((dest), (src))
45 #define va_copy(dest, src) (dest) = (src)
49 #define CANARY 0x5A1106
53 /* A canary value used to determine whether a pointer is ralloc'd. */
56 struct ralloc_header
*parent
;
58 /* The first child (head of a linked list) */
59 struct ralloc_header
*child
;
61 /* Linked list of siblings */
62 struct ralloc_header
*prev
;
63 struct ralloc_header
*next
;
65 void (*destructor
)(void *);
68 typedef struct ralloc_header ralloc_header
;
70 static void unlink_block(ralloc_header
*info
);
71 static void unsafe_free(ralloc_header
*info
);
73 static ralloc_header
*
74 get_header(const void *ptr
)
76 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
77 sizeof(ralloc_header
));
78 assert(info
->canary
== CANARY
);
82 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
85 add_child(ralloc_header
*parent
, ralloc_header
*info
)
88 info
->parent
= parent
;
89 info
->next
= parent
->child
;
92 if (info
->next
!= NULL
)
93 info
->next
->prev
= info
;
98 ralloc_context(const void *ctx
)
100 return ralloc_size(ctx
, 0);
104 ralloc_size(const void *ctx
, size_t size
)
106 void *block
= calloc(1, size
+ sizeof(ralloc_header
));
108 ralloc_header
*info
= (ralloc_header
*) block
;
109 ralloc_header
*parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
111 add_child(parent
, info
);
113 info
->canary
= CANARY
;
115 return PTR_FROM_HEADER(info
);
119 rzalloc_size(const void *ctx
, size_t size
)
121 void *ptr
= ralloc_size(ctx
, size
);
122 if (likely(ptr
!= NULL
))
123 memset(ptr
, 0, size
);
127 /* helper function - assumes ptr != NULL */
129 resize(void *ptr
, size_t size
)
131 ralloc_header
*child
, *old
, *info
;
133 old
= get_header(ptr
);
134 info
= realloc(old
, size
+ sizeof(ralloc_header
));
139 /* Update parent and sibling's links to the reallocated node. */
140 if (info
!= old
&& info
->parent
!= NULL
) {
141 if (info
->parent
->child
== old
)
142 info
->parent
->child
= info
;
144 if (info
->prev
!= NULL
)
145 info
->prev
->next
= info
;
147 if (info
->next
!= NULL
)
148 info
->next
->prev
= info
;
151 /* Update child->parent links for all children */
152 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
153 child
->parent
= info
;
155 return PTR_FROM_HEADER(info
);
159 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
161 if (unlikely(ptr
== NULL
))
162 return ralloc_size(ctx
, size
);
164 assert(ralloc_parent(ptr
) == ctx
);
165 return resize(ptr
, size
);
169 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
171 if (count
> SIZE_MAX
/size
)
174 return ralloc_size(ctx
, size
* count
);
178 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
180 if (count
> SIZE_MAX
/size
)
183 return rzalloc_size(ctx
, size
* count
);
187 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
189 if (count
> SIZE_MAX
/size
)
192 return reralloc_size(ctx
, ptr
, size
* count
);
196 ralloc_free(void *ptr
)
203 info
= get_header(ptr
);
209 unlink_block(ralloc_header
*info
)
211 /* Unlink from parent & siblings */
212 if (info
->parent
!= NULL
) {
213 if (info
->parent
->child
== info
)
214 info
->parent
->child
= info
->next
;
216 if (info
->prev
!= NULL
)
217 info
->prev
->next
= info
->next
;
219 if (info
->next
!= NULL
)
220 info
->next
->prev
= info
->prev
;
228 unsafe_free(ralloc_header
*info
)
230 /* Recursively free any children...don't waste time unlinking them. */
232 while (info
->child
!= NULL
) {
234 info
->child
= temp
->next
;
238 /* Free the block itself. Call the destructor first, if any. */
239 if (info
->destructor
!= NULL
)
240 info
->destructor(PTR_FROM_HEADER(info
));
246 ralloc_steal(const void *new_ctx
, void *ptr
)
248 ralloc_header
*info
, *parent
;
250 if (unlikely(ptr
== NULL
))
253 info
= get_header(ptr
);
254 parent
= get_header(new_ctx
);
258 add_child(parent
, info
);
262 ralloc_parent(const void *ptr
)
266 if (unlikely(ptr
== NULL
))
269 info
= get_header(ptr
);
270 return PTR_FROM_HEADER(info
->parent
);
273 static void *autofree_context
= NULL
;
278 ralloc_free(autofree_context
);
282 ralloc_autofree_context(void)
284 if (unlikely(autofree_context
== NULL
)) {
285 autofree_context
= ralloc_context(NULL
);
288 return autofree_context
;
292 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
294 ralloc_header
*info
= get_header(ptr
);
295 info
->destructor
= destructor
;
299 ralloc_strdup(const void *ctx
, const char *str
)
304 if (unlikely(str
== NULL
))
308 ptr
= ralloc_array(ctx
, char, n
+ 1);
315 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
320 if (unlikely(str
== NULL
))
327 ptr
= ralloc_array(ctx
, char, n
+ 1);
333 /* helper routine for strcat/strncat - n is the exact amount to copy */
335 cat(char **dest
, const char *str
, size_t n
)
338 size_t existing_length
;
339 assert(dest
!= NULL
&& *dest
!= NULL
);
341 existing_length
= strlen(*dest
);
342 both
= resize(*dest
, existing_length
+ n
+ 1);
343 if (unlikely(both
== NULL
))
346 memcpy(both
+ existing_length
, str
, n
);
347 both
[existing_length
+ n
] = '\0';
355 ralloc_strcat(char **dest
, const char *str
)
357 return cat(dest
, str
, strlen(str
));
361 ralloc_strncat(char **dest
, const char *str
, size_t n
)
363 /* Clamp n to the string length */
364 size_t str_length
= strlen(str
);
368 return cat(dest
, str
, n
);
372 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
377 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
382 /* Return the length of the string that would be generated by a printf-style
383 * format and argument list, not including the \0 byte.
386 printf_length(const char *fmt
, va_list untouched_args
)
391 /* Make a copy of the va_list so the original caller can still use it */
393 va_copy(args
, untouched_args
);
396 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
397 * if the number of characters to write is greater than count.
399 size
= _vscprintf(fmt
, args
);
402 size
= vsnprintf(&junk
, 1, fmt
, args
);
412 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
414 size_t size
= printf_length(fmt
, args
) + 1;
416 char *ptr
= ralloc_size(ctx
, size
);
418 vsnprintf(ptr
, size
, fmt
, args
);
424 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
429 success
= ralloc_vasprintf_append(str
, fmt
, args
);
435 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
437 size_t existing_length
, new_length
;
442 if (unlikely(*str
== NULL
)) {
443 // Assuming a NULL context is probably bad, but it's expected behavior.
444 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
448 existing_length
= strlen(*str
);
449 new_length
= printf_length(fmt
, args
);
451 ptr
= resize(*str
, existing_length
+ new_length
+ 1);
452 if (unlikely(ptr
== NULL
))
455 vsnprintf(ptr
+ existing_length
, new_length
+ 1, fmt
, args
);