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 void *block
= calloc(1, size
+ sizeof(ralloc_header
));
115 ralloc_header
*parent
;
117 if (unlikely(block
== NULL
))
119 info
= (ralloc_header
*) block
;
120 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
122 add_child(parent
, info
);
125 info
->canary
= CANARY
;
128 return PTR_FROM_HEADER(info
);
132 rzalloc_size(const void *ctx
, size_t size
)
134 void *ptr
= ralloc_size(ctx
, size
);
135 if (likely(ptr
!= NULL
))
136 memset(ptr
, 0, size
);
140 /* helper function - assumes ptr != NULL */
142 resize(void *ptr
, size_t size
)
144 ralloc_header
*child
, *old
, *info
;
146 old
= get_header(ptr
);
147 info
= realloc(old
, size
+ sizeof(ralloc_header
));
152 /* Update parent and sibling's links to the reallocated node. */
153 if (info
!= old
&& info
->parent
!= NULL
) {
154 if (info
->parent
->child
== old
)
155 info
->parent
->child
= info
;
157 if (info
->prev
!= NULL
)
158 info
->prev
->next
= info
;
160 if (info
->next
!= NULL
)
161 info
->next
->prev
= info
;
164 /* Update child->parent links for all children */
165 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
166 child
->parent
= info
;
168 return PTR_FROM_HEADER(info
);
172 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
174 if (unlikely(ptr
== NULL
))
175 return ralloc_size(ctx
, size
);
177 assert(ralloc_parent(ptr
) == ctx
);
178 return resize(ptr
, size
);
182 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
184 if (count
> SIZE_MAX
/size
)
187 return ralloc_size(ctx
, size
* count
);
191 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
193 if (count
> SIZE_MAX
/size
)
196 return rzalloc_size(ctx
, size
* count
);
200 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
202 if (count
> SIZE_MAX
/size
)
205 return reralloc_size(ctx
, ptr
, size
* count
);
209 ralloc_free(void *ptr
)
216 info
= get_header(ptr
);
222 unlink_block(ralloc_header
*info
)
224 /* Unlink from parent & siblings */
225 if (info
->parent
!= NULL
) {
226 if (info
->parent
->child
== info
)
227 info
->parent
->child
= info
->next
;
229 if (info
->prev
!= NULL
)
230 info
->prev
->next
= info
->next
;
232 if (info
->next
!= NULL
)
233 info
->next
->prev
= info
->prev
;
241 unsafe_free(ralloc_header
*info
)
243 /* Recursively free any children...don't waste time unlinking them. */
245 while (info
->child
!= NULL
) {
247 info
->child
= temp
->next
;
251 /* Free the block itself. Call the destructor first, if any. */
252 if (info
->destructor
!= NULL
)
253 info
->destructor(PTR_FROM_HEADER(info
));
259 ralloc_steal(const void *new_ctx
, void *ptr
)
261 ralloc_header
*info
, *parent
;
263 if (unlikely(ptr
== NULL
))
266 info
= get_header(ptr
);
267 parent
= get_header(new_ctx
);
271 add_child(parent
, info
);
275 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
277 ralloc_header
*new_info
, *old_info
, *child
;
279 if (unlikely(old_ctx
== NULL
))
282 old_info
= get_header(old_ctx
);
283 new_info
= get_header(new_ctx
);
285 /* If there are no children, bail. */
286 if (unlikely(old_info
->child
== NULL
))
289 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
290 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
291 child
->parent
= new_info
;
294 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
295 child
->next
= new_info
->child
;
296 new_info
->child
= old_info
->child
;
297 old_info
->child
= NULL
;
301 ralloc_parent(const void *ptr
)
305 if (unlikely(ptr
== NULL
))
308 info
= get_header(ptr
);
309 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
312 static void *autofree_context
= NULL
;
317 ralloc_free(autofree_context
);
321 ralloc_autofree_context(void)
323 if (unlikely(autofree_context
== NULL
)) {
324 autofree_context
= ralloc_context(NULL
);
327 return autofree_context
;
331 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
333 ralloc_header
*info
= get_header(ptr
);
334 info
->destructor
= destructor
;
338 ralloc_strdup(const void *ctx
, const char *str
)
343 if (unlikely(str
== NULL
))
347 ptr
= ralloc_array(ctx
, char, n
+ 1);
354 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
359 if (unlikely(str
== NULL
))
362 n
= strnlen(str
, max
);
363 ptr
= ralloc_array(ctx
, char, n
+ 1);
369 /* helper routine for strcat/strncat - n is the exact amount to copy */
371 cat(char **dest
, const char *str
, size_t n
)
374 size_t existing_length
;
375 assert(dest
!= NULL
&& *dest
!= NULL
);
377 existing_length
= strlen(*dest
);
378 both
= resize(*dest
, existing_length
+ n
+ 1);
379 if (unlikely(both
== NULL
))
382 memcpy(both
+ existing_length
, str
, n
);
383 both
[existing_length
+ n
] = '\0';
391 ralloc_strcat(char **dest
, const char *str
)
393 return cat(dest
, str
, strlen(str
));
397 ralloc_strncat(char **dest
, const char *str
, size_t n
)
399 /* Clamp n to the string length */
400 size_t str_length
= strlen(str
);
404 return cat(dest
, str
, n
);
408 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
413 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
418 /* Return the length of the string that would be generated by a printf-style
419 * format and argument list, not including the \0 byte.
422 printf_length(const char *fmt
, va_list untouched_args
)
427 /* Make a copy of the va_list so the original caller can still use it */
429 va_copy(args
, untouched_args
);
432 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
433 * if the number of characters to write is greater than count.
435 size
= _vscprintf(fmt
, args
);
438 size
= vsnprintf(&junk
, 1, fmt
, args
);
448 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
450 size_t size
= printf_length(fmt
, args
) + 1;
452 char *ptr
= ralloc_size(ctx
, size
);
454 vsnprintf(ptr
, size
, fmt
, args
);
460 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
465 success
= ralloc_vasprintf_append(str
, fmt
, args
);
471 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
473 size_t existing_length
;
475 existing_length
= *str
? strlen(*str
) : 0;
476 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
480 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
485 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
491 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
499 if (unlikely(*str
== NULL
)) {
500 // Assuming a NULL context is probably bad, but it's expected behavior.
501 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
502 *start
= strlen(*str
);
506 new_length
= printf_length(fmt
, args
);
508 ptr
= resize(*str
, *start
+ new_length
+ 1);
509 if (unlikely(ptr
== NULL
))
512 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
514 *start
+= new_length
;