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
57 /* A canary value used to determine whether a pointer is ralloc'd. */
61 struct ralloc_header
*parent
;
63 /* The first child (head of a linked list) */
64 struct ralloc_header
*child
;
66 /* Linked list of siblings */
67 struct ralloc_header
*prev
;
68 struct ralloc_header
*next
;
70 void (*destructor
)(void *);
73 typedef struct ralloc_header ralloc_header
;
75 static void unlink_block(ralloc_header
*info
);
76 static void unsafe_free(ralloc_header
*info
);
78 static ralloc_header
*
79 get_header(const void *ptr
)
81 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
82 sizeof(ralloc_header
));
84 assert(info
->canary
== CANARY
);
89 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
92 add_child(ralloc_header
*parent
, ralloc_header
*info
)
95 info
->parent
= parent
;
96 info
->next
= parent
->child
;
99 if (info
->next
!= NULL
)
100 info
->next
->prev
= info
;
105 ralloc_context(const void *ctx
)
107 return ralloc_size(ctx
, 0);
111 ralloc_size(const void *ctx
, size_t size
)
113 /* ralloc_size was originally implemented using calloc, which meant some
114 * code accidentally relied on its zero filling behavior.
116 * TODO: Make ralloc_size not zero fill memory, and cleanup any code that
117 * should instead be using rzalloc.
119 return rzalloc_size(ctx
, size
);
123 rzalloc_size(const void *ctx
, size_t size
)
125 void *block
= malloc(size
+ sizeof(ralloc_header
));
127 ralloc_header
*parent
;
129 if (unlikely(block
== NULL
))
132 info
= (ralloc_header
*) block
;
133 /* measurements have shown that calloc is slower (because of
134 * the multiplication overflow checking?), so clear things
141 info
->destructor
= NULL
;
143 /* memset the allocation except for ralloc_header */
144 memset(&info
[1], 0, size
);
146 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
148 add_child(parent
, info
);
151 info
->canary
= CANARY
;
154 return PTR_FROM_HEADER(info
);
157 /* helper function - assumes ptr != NULL */
159 resize(void *ptr
, size_t size
)
161 ralloc_header
*child
, *old
, *info
;
163 old
= get_header(ptr
);
164 info
= realloc(old
, size
+ sizeof(ralloc_header
));
169 /* Update parent and sibling's links to the reallocated node. */
170 if (info
!= old
&& info
->parent
!= NULL
) {
171 if (info
->parent
->child
== old
)
172 info
->parent
->child
= info
;
174 if (info
->prev
!= NULL
)
175 info
->prev
->next
= info
;
177 if (info
->next
!= NULL
)
178 info
->next
->prev
= info
;
181 /* Update child->parent links for all children */
182 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
183 child
->parent
= info
;
185 return PTR_FROM_HEADER(info
);
189 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
191 if (unlikely(ptr
== NULL
))
192 return ralloc_size(ctx
, size
);
194 assert(ralloc_parent(ptr
) == ctx
);
195 return resize(ptr
, size
);
199 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
201 if (count
> SIZE_MAX
/size
)
204 return ralloc_size(ctx
, size
* count
);
208 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
210 if (count
> SIZE_MAX
/size
)
213 return rzalloc_size(ctx
, size
* count
);
217 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
219 if (count
> SIZE_MAX
/size
)
222 return reralloc_size(ctx
, ptr
, size
* count
);
226 ralloc_free(void *ptr
)
233 info
= get_header(ptr
);
239 unlink_block(ralloc_header
*info
)
241 /* Unlink from parent & siblings */
242 if (info
->parent
!= NULL
) {
243 if (info
->parent
->child
== info
)
244 info
->parent
->child
= info
->next
;
246 if (info
->prev
!= NULL
)
247 info
->prev
->next
= info
->next
;
249 if (info
->next
!= NULL
)
250 info
->next
->prev
= info
->prev
;
258 unsafe_free(ralloc_header
*info
)
260 /* Recursively free any children...don't waste time unlinking them. */
262 while (info
->child
!= NULL
) {
264 info
->child
= temp
->next
;
268 /* Free the block itself. Call the destructor first, if any. */
269 if (info
->destructor
!= NULL
)
270 info
->destructor(PTR_FROM_HEADER(info
));
276 ralloc_steal(const void *new_ctx
, void *ptr
)
278 ralloc_header
*info
, *parent
;
280 if (unlikely(ptr
== NULL
))
283 info
= get_header(ptr
);
284 parent
= get_header(new_ctx
);
288 add_child(parent
, info
);
292 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
294 ralloc_header
*new_info
, *old_info
, *child
;
296 if (unlikely(old_ctx
== NULL
))
299 old_info
= get_header(old_ctx
);
300 new_info
= get_header(new_ctx
);
302 /* If there are no children, bail. */
303 if (unlikely(old_info
->child
== NULL
))
306 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
307 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
308 child
->parent
= new_info
;
311 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
312 child
->next
= new_info
->child
;
313 child
->parent
= new_info
;
314 new_info
->child
= old_info
->child
;
315 old_info
->child
= NULL
;
319 ralloc_parent(const void *ptr
)
323 if (unlikely(ptr
== NULL
))
326 info
= get_header(ptr
);
327 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
330 static void *autofree_context
= NULL
;
335 ralloc_free(autofree_context
);
339 ralloc_autofree_context(void)
341 if (unlikely(autofree_context
== NULL
)) {
342 autofree_context
= ralloc_context(NULL
);
345 return autofree_context
;
349 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
351 ralloc_header
*info
= get_header(ptr
);
352 info
->destructor
= destructor
;
356 ralloc_strdup(const void *ctx
, const char *str
)
361 if (unlikely(str
== NULL
))
365 ptr
= ralloc_array(ctx
, char, n
+ 1);
372 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
377 if (unlikely(str
== NULL
))
380 n
= strnlen(str
, max
);
381 ptr
= ralloc_array(ctx
, char, n
+ 1);
387 /* helper routine for strcat/strncat - n is the exact amount to copy */
389 cat(char **dest
, const char *str
, size_t n
)
392 size_t existing_length
;
393 assert(dest
!= NULL
&& *dest
!= NULL
);
395 existing_length
= strlen(*dest
);
396 both
= resize(*dest
, existing_length
+ n
+ 1);
397 if (unlikely(both
== NULL
))
400 memcpy(both
+ existing_length
, str
, n
);
401 both
[existing_length
+ n
] = '\0';
409 ralloc_strcat(char **dest
, const char *str
)
411 return cat(dest
, str
, strlen(str
));
415 ralloc_strncat(char **dest
, const char *str
, size_t n
)
417 /* Clamp n to the string length */
418 size_t str_length
= strlen(str
);
422 return cat(dest
, str
, n
);
426 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
431 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
436 /* Return the length of the string that would be generated by a printf-style
437 * format and argument list, not including the \0 byte.
440 printf_length(const char *fmt
, va_list untouched_args
)
445 /* Make a copy of the va_list so the original caller can still use it */
447 va_copy(args
, untouched_args
);
450 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
451 * if the number of characters to write is greater than count.
453 size
= _vscprintf(fmt
, args
);
456 size
= vsnprintf(&junk
, 1, fmt
, args
);
466 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
468 size_t size
= printf_length(fmt
, args
) + 1;
470 char *ptr
= ralloc_size(ctx
, size
);
472 vsnprintf(ptr
, size
, fmt
, args
);
478 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
483 success
= ralloc_vasprintf_append(str
, fmt
, args
);
489 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
491 size_t existing_length
;
493 existing_length
= *str
? strlen(*str
) : 0;
494 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
498 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
503 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
509 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
517 if (unlikely(*str
== NULL
)) {
518 // Assuming a NULL context is probably bad, but it's expected behavior.
519 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
520 *start
= strlen(*str
);
524 new_length
= printf_length(fmt
, args
);
526 ptr
= resize(*str
, *start
+ new_length
+ 1);
527 if (unlikely(ptr
== NULL
))
530 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
532 *start
+= new_length
;