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 */
39 #define likely(x) __builtin_expect(!!(x),1)
40 #define unlikely(x) __builtin_expect(!!(x),0)
42 #define likely(x) !!(x)
43 #define unlikely(x) !!(x)
48 #define va_copy(dest, src) __va_copy((dest), (src))
50 #define va_copy(dest, src) (dest) = (src)
54 #define CANARY 0x5A1106
58 /* 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
));
83 assert(info
->canary
== CANARY
);
87 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
90 add_child(ralloc_header
*parent
, ralloc_header
*info
)
93 info
->parent
= parent
;
94 info
->next
= parent
->child
;
97 if (info
->next
!= NULL
)
98 info
->next
->prev
= info
;
103 ralloc_context(const void *ctx
)
105 return ralloc_size(ctx
, 0);
109 ralloc_size(const void *ctx
, size_t size
)
111 void *block
= calloc(1, size
+ sizeof(ralloc_header
));
113 ralloc_header
*info
= (ralloc_header
*) block
;
114 ralloc_header
*parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
116 add_child(parent
, info
);
118 info
->canary
= CANARY
;
120 return PTR_FROM_HEADER(info
);
124 rzalloc_size(const void *ctx
, size_t size
)
126 void *ptr
= ralloc_size(ctx
, size
);
127 if (likely(ptr
!= NULL
))
128 memset(ptr
, 0, size
);
132 /* helper function - assumes ptr != NULL */
134 resize(void *ptr
, size_t size
)
136 ralloc_header
*child
, *old
, *info
;
138 old
= get_header(ptr
);
139 info
= realloc(old
, size
+ sizeof(ralloc_header
));
144 /* Update parent and sibling's links to the reallocated node. */
145 if (info
!= old
&& info
->parent
!= NULL
) {
146 if (info
->parent
->child
== old
)
147 info
->parent
->child
= info
;
149 if (info
->prev
!= NULL
)
150 info
->prev
->next
= info
;
152 if (info
->next
!= NULL
)
153 info
->next
->prev
= info
;
156 /* Update child->parent links for all children */
157 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
158 child
->parent
= info
;
160 return PTR_FROM_HEADER(info
);
164 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
166 if (unlikely(ptr
== NULL
))
167 return ralloc_size(ctx
, size
);
169 assert(ralloc_parent(ptr
) == ctx
);
170 return resize(ptr
, size
);
174 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
176 if (count
> SIZE_MAX
/size
)
179 return ralloc_size(ctx
, size
* count
);
183 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
185 if (count
> SIZE_MAX
/size
)
188 return rzalloc_size(ctx
, size
* count
);
192 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
194 if (count
> SIZE_MAX
/size
)
197 return reralloc_size(ctx
, ptr
, size
* count
);
201 ralloc_free(void *ptr
)
208 info
= get_header(ptr
);
214 unlink_block(ralloc_header
*info
)
216 /* Unlink from parent & siblings */
217 if (info
->parent
!= NULL
) {
218 if (info
->parent
->child
== info
)
219 info
->parent
->child
= info
->next
;
221 if (info
->prev
!= NULL
)
222 info
->prev
->next
= info
->next
;
224 if (info
->next
!= NULL
)
225 info
->next
->prev
= info
->prev
;
233 unsafe_free(ralloc_header
*info
)
235 /* Recursively free any children...don't waste time unlinking them. */
237 while (info
->child
!= NULL
) {
239 info
->child
= temp
->next
;
243 /* Free the block itself. Call the destructor first, if any. */
244 if (info
->destructor
!= NULL
)
245 info
->destructor(PTR_FROM_HEADER(info
));
251 ralloc_steal(const void *new_ctx
, void *ptr
)
253 ralloc_header
*info
, *parent
;
255 if (unlikely(ptr
== NULL
))
258 info
= get_header(ptr
);
259 parent
= get_header(new_ctx
);
263 add_child(parent
, info
);
267 ralloc_parent(const void *ptr
)
271 if (unlikely(ptr
== NULL
))
274 info
= get_header(ptr
);
275 return PTR_FROM_HEADER(info
->parent
);
278 static void *autofree_context
= NULL
;
283 ralloc_free(autofree_context
);
287 ralloc_autofree_context(void)
289 if (unlikely(autofree_context
== NULL
)) {
290 autofree_context
= ralloc_context(NULL
);
293 return autofree_context
;
297 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
299 ralloc_header
*info
= get_header(ptr
);
300 info
->destructor
= destructor
;
304 ralloc_strdup(const void *ctx
, const char *str
)
309 if (unlikely(str
== NULL
))
313 ptr
= ralloc_array(ctx
, char, n
+ 1);
320 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
325 if (unlikely(str
== NULL
))
332 ptr
= ralloc_array(ctx
, char, n
+ 1);
338 /* helper routine for strcat/strncat - n is the exact amount to copy */
340 cat(char **dest
, const char *str
, size_t n
)
343 size_t existing_length
;
344 assert(dest
!= NULL
&& *dest
!= NULL
);
346 existing_length
= strlen(*dest
);
347 both
= resize(*dest
, existing_length
+ n
+ 1);
348 if (unlikely(both
== NULL
))
351 memcpy(both
+ existing_length
, str
, n
);
352 both
[existing_length
+ n
] = '\0';
360 ralloc_strcat(char **dest
, const char *str
)
362 return cat(dest
, str
, strlen(str
));
366 ralloc_strncat(char **dest
, const char *str
, size_t n
)
368 /* Clamp n to the string length */
369 size_t str_length
= strlen(str
);
373 return cat(dest
, str
, n
);
377 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
382 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
387 /* Return the length of the string that would be generated by a printf-style
388 * format and argument list, not including the \0 byte.
391 printf_length(const char *fmt
, va_list untouched_args
)
396 /* Make a copy of the va_list so the original caller can still use it */
398 va_copy(args
, untouched_args
);
401 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
402 * if the number of characters to write is greater than count.
404 size
= _vscprintf(fmt
, args
);
407 size
= vsnprintf(&junk
, 1, fmt
, args
);
417 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
419 size_t size
= printf_length(fmt
, args
) + 1;
421 char *ptr
= ralloc_size(ctx
, size
);
423 vsnprintf(ptr
, size
, fmt
, args
);
429 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
434 success
= ralloc_vasprintf_append(str
, fmt
, args
);
440 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
442 size_t existing_length
, new_length
;
447 if (unlikely(*str
== NULL
)) {
448 // Assuming a NULL context is probably bad, but it's expected behavior.
449 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
453 existing_length
= strlen(*str
);
454 new_length
= printf_length(fmt
, args
);
456 ptr
= resize(*str
, existing_length
+ new_length
+ 1);
457 if (unlikely(ptr
== NULL
))
460 vsnprintf(ptr
+ existing_length
, new_length
+ 1, fmt
, args
);