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
54 /* Align the header's size so that ralloc() allocations will return with the
55 * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on
56 * 64-bit), avoiding performance penalities on x86 and alignment faults on
62 #elif defined(__LP64__)
63 __attribute__((aligned(16)))
65 __attribute__((aligned(8)))
70 /* A canary value used to determine whether a pointer is ralloc'd. */
74 struct ralloc_header
*parent
;
76 /* The first child (head of a linked list) */
77 struct ralloc_header
*child
;
79 /* Linked list of siblings */
80 struct ralloc_header
*prev
;
81 struct ralloc_header
*next
;
83 void (*destructor
)(void *);
86 typedef struct ralloc_header ralloc_header
;
88 static void unlink_block(ralloc_header
*info
);
89 static void unsafe_free(ralloc_header
*info
);
91 static ralloc_header
*
92 get_header(const void *ptr
)
94 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
95 sizeof(ralloc_header
));
97 assert(info
->canary
== CANARY
);
102 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
105 add_child(ralloc_header
*parent
, ralloc_header
*info
)
107 if (parent
!= NULL
) {
108 info
->parent
= parent
;
109 info
->next
= parent
->child
;
110 parent
->child
= info
;
112 if (info
->next
!= NULL
)
113 info
->next
->prev
= info
;
118 ralloc_context(const void *ctx
)
120 return ralloc_size(ctx
, 0);
124 ralloc_size(const void *ctx
, size_t size
)
126 void *block
= malloc(size
+ sizeof(ralloc_header
));
128 ralloc_header
*parent
;
130 if (unlikely(block
== NULL
))
133 info
= (ralloc_header
*) block
;
134 /* measurements have shown that calloc is slower (because of
135 * the multiplication overflow checking?), so clear things
142 info
->destructor
= NULL
;
144 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
146 add_child(parent
, info
);
149 info
->canary
= CANARY
;
152 return PTR_FROM_HEADER(info
);
156 rzalloc_size(const void *ctx
, size_t size
)
158 void *ptr
= ralloc_size(ctx
, size
);
161 memset(ptr
, 0, size
);
166 /* helper function - assumes ptr != NULL */
168 resize(void *ptr
, size_t size
)
170 ralloc_header
*child
, *old
, *info
;
172 old
= get_header(ptr
);
173 info
= realloc(old
, size
+ sizeof(ralloc_header
));
178 /* Update parent and sibling's links to the reallocated node. */
179 if (info
!= old
&& info
->parent
!= NULL
) {
180 if (info
->parent
->child
== old
)
181 info
->parent
->child
= info
;
183 if (info
->prev
!= NULL
)
184 info
->prev
->next
= info
;
186 if (info
->next
!= NULL
)
187 info
->next
->prev
= info
;
190 /* Update child->parent links for all children */
191 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
192 child
->parent
= info
;
194 return PTR_FROM_HEADER(info
);
198 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
200 if (unlikely(ptr
== NULL
))
201 return ralloc_size(ctx
, size
);
203 assert(ralloc_parent(ptr
) == ctx
);
204 return resize(ptr
, size
);
208 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
210 if (count
> SIZE_MAX
/size
)
213 return ralloc_size(ctx
, size
* count
);
217 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
219 if (count
> SIZE_MAX
/size
)
222 return rzalloc_size(ctx
, size
* count
);
226 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
228 if (count
> SIZE_MAX
/size
)
231 return reralloc_size(ctx
, ptr
, size
* count
);
235 ralloc_free(void *ptr
)
242 info
= get_header(ptr
);
248 unlink_block(ralloc_header
*info
)
250 /* Unlink from parent & siblings */
251 if (info
->parent
!= NULL
) {
252 if (info
->parent
->child
== info
)
253 info
->parent
->child
= info
->next
;
255 if (info
->prev
!= NULL
)
256 info
->prev
->next
= info
->next
;
258 if (info
->next
!= NULL
)
259 info
->next
->prev
= info
->prev
;
267 unsafe_free(ralloc_header
*info
)
269 /* Recursively free any children...don't waste time unlinking them. */
271 while (info
->child
!= NULL
) {
273 info
->child
= temp
->next
;
277 /* Free the block itself. Call the destructor first, if any. */
278 if (info
->destructor
!= NULL
)
279 info
->destructor(PTR_FROM_HEADER(info
));
285 ralloc_steal(const void *new_ctx
, void *ptr
)
287 ralloc_header
*info
, *parent
;
289 if (unlikely(ptr
== NULL
))
292 info
= get_header(ptr
);
293 parent
= get_header(new_ctx
);
297 add_child(parent
, info
);
301 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
303 ralloc_header
*new_info
, *old_info
, *child
;
305 if (unlikely(old_ctx
== NULL
))
308 old_info
= get_header(old_ctx
);
309 new_info
= get_header(new_ctx
);
311 /* If there are no children, bail. */
312 if (unlikely(old_info
->child
== NULL
))
315 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
316 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
317 child
->parent
= new_info
;
320 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
321 child
->next
= new_info
->child
;
322 child
->parent
= new_info
;
323 new_info
->child
= old_info
->child
;
324 old_info
->child
= NULL
;
328 ralloc_parent(const void *ptr
)
332 if (unlikely(ptr
== NULL
))
335 info
= get_header(ptr
);
336 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
340 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
342 ralloc_header
*info
= get_header(ptr
);
343 info
->destructor
= destructor
;
347 ralloc_strdup(const void *ctx
, const char *str
)
352 if (unlikely(str
== NULL
))
356 ptr
= ralloc_array(ctx
, char, n
+ 1);
363 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
368 if (unlikely(str
== NULL
))
371 n
= strnlen(str
, max
);
372 ptr
= ralloc_array(ctx
, char, n
+ 1);
378 /* helper routine for strcat/strncat - n is the exact amount to copy */
380 cat(char **dest
, const char *str
, size_t n
)
383 size_t existing_length
;
384 assert(dest
!= NULL
&& *dest
!= NULL
);
386 existing_length
= strlen(*dest
);
387 both
= resize(*dest
, existing_length
+ n
+ 1);
388 if (unlikely(both
== NULL
))
391 memcpy(both
+ existing_length
, str
, n
);
392 both
[existing_length
+ n
] = '\0';
400 ralloc_strcat(char **dest
, const char *str
)
402 return cat(dest
, str
, strlen(str
));
406 ralloc_strncat(char **dest
, const char *str
, size_t n
)
408 /* Clamp n to the string length */
409 size_t str_length
= strlen(str
);
413 return cat(dest
, str
, n
);
417 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
422 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
427 /* Return the length of the string that would be generated by a printf-style
428 * format and argument list, not including the \0 byte.
431 printf_length(const char *fmt
, va_list untouched_args
)
436 /* Make a copy of the va_list so the original caller can still use it */
438 va_copy(args
, untouched_args
);
441 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
442 * if the number of characters to write is greater than count.
444 size
= _vscprintf(fmt
, args
);
447 size
= vsnprintf(&junk
, 1, fmt
, args
);
457 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
459 size_t size
= printf_length(fmt
, args
) + 1;
461 char *ptr
= ralloc_size(ctx
, size
);
463 vsnprintf(ptr
, size
, fmt
, args
);
469 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
474 success
= ralloc_vasprintf_append(str
, fmt
, args
);
480 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
482 size_t existing_length
;
484 existing_length
= *str
? strlen(*str
) : 0;
485 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
489 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
494 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
500 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
508 if (unlikely(*str
== NULL
)) {
509 // Assuming a NULL context is probably bad, but it's expected behavior.
510 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
511 *start
= strlen(*str
);
515 new_length
= printf_length(fmt
, args
);
517 ptr
= resize(*str
, *start
+ new_length
+ 1);
518 if (unlikely(ptr
== NULL
))
521 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
523 *start
+= new_length
;
527 /***************************************************************************
528 * Linear allocator for short-lived allocations.
529 ***************************************************************************
531 * The allocator consists of a parent node (2K buffer), which requires
532 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
533 * directly, because the parent doesn't track them. You have to release
534 * the parent node in order to release all its children.
536 * The allocator uses a fixed-sized buffer with a monotonically increasing
537 * offset after each allocation. If the buffer is all used, another buffer
538 * is allocated, sharing the same ralloc parent, so all buffers are at
539 * the same level in the ralloc hierarchy.
541 * The linear parent node is always the first buffer and keeps track of all
545 #define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1))
547 #define MIN_LINEAR_BUFSIZE 2048
548 #define SUBALLOC_ALIGNMENT sizeof(uintptr_t)
549 #define LMAGIC 0x87b9c7d3
551 struct linear_header
{
553 unsigned magic
; /* for debugging */
555 unsigned offset
; /* points to the first unused byte in the buffer */
556 unsigned size
; /* size of the buffer */
557 void *ralloc_parent
; /* new buffers will use this */
558 struct linear_header
*next
; /* next buffer if we have more */
559 struct linear_header
*latest
; /* the only buffer that has free space */
561 /* After this structure, the buffer begins.
562 * Each suballocation consists of linear_size_chunk as its header followed
563 * by the suballocation, so it goes:
565 * - linear_size_chunk
567 * - linear_size_chunk
571 * linear_size_chunk is only needed by linear_realloc.
575 struct linear_size_chunk
{
576 unsigned size
; /* for realloc */
580 typedef struct linear_header linear_header
;
581 typedef struct linear_size_chunk linear_size_chunk
;
583 #define LINEAR_PARENT_TO_HEADER(parent) \
585 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
587 /* Allocate the linear buffer with its header. */
588 static linear_header
*
589 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
593 min_size
+= sizeof(linear_size_chunk
);
595 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
596 min_size
= MIN_LINEAR_BUFSIZE
;
598 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
603 node
->magic
= LMAGIC
;
606 node
->size
= min_size
;
607 node
->ralloc_parent
= ralloc_ctx
;
614 linear_alloc_child(void *parent
, unsigned size
)
616 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
617 linear_header
*latest
= first
->latest
;
618 linear_header
*new_node
;
619 linear_size_chunk
*ptr
;
622 assert(first
->magic
== LMAGIC
);
623 assert(!latest
->next
);
625 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
626 full_size
= sizeof(linear_size_chunk
) + size
;
628 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
629 /* allocate a new node */
630 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
631 if (unlikely(!new_node
))
634 first
->latest
= new_node
;
635 latest
->latest
= new_node
;
636 latest
->next
= new_node
;
640 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
642 latest
->offset
+= full_size
;
647 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
651 if (unlikely(!ralloc_ctx
))
654 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
656 node
= create_linear_node(ralloc_ctx
, size
);
660 return linear_alloc_child((char*)node
+
661 sizeof(linear_header
) +
662 sizeof(linear_size_chunk
), size
);
666 linear_zalloc_child(void *parent
, unsigned size
)
668 void *ptr
= linear_alloc_child(parent
, size
);
671 memset(ptr
, 0, size
);
676 linear_zalloc_parent(void *parent
, unsigned size
)
678 void *ptr
= linear_alloc_parent(parent
, size
);
681 memset(ptr
, 0, size
);
686 linear_free_parent(void *ptr
)
693 node
= LINEAR_PARENT_TO_HEADER(ptr
);
694 assert(node
->magic
== LMAGIC
);
705 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
712 node
= LINEAR_PARENT_TO_HEADER(ptr
);
713 assert(node
->magic
== LMAGIC
);
716 ralloc_steal(new_ralloc_ctx
, node
);
717 node
->ralloc_parent
= new_ralloc_ctx
;
723 ralloc_parent_of_linear_parent(void *ptr
)
725 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
726 assert(node
->magic
== LMAGIC
);
727 return node
->ralloc_parent
;
731 linear_realloc(void *parent
, void *old
, unsigned new_size
)
733 unsigned old_size
= 0;
734 ralloc_header
*new_ptr
;
736 new_ptr
= linear_alloc_child(parent
, new_size
);
741 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
743 if (likely(new_ptr
&& old_size
))
744 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
749 /* All code below is pretty much copied from ralloc and only the alloc
750 * calls are different.
754 linear_strdup(void *parent
, const char *str
)
763 ptr
= linear_alloc_child(parent
, n
+ 1);
773 linear_asprintf(void *parent
, const char *fmt
, ...)
778 ptr
= linear_vasprintf(parent
, fmt
, args
);
784 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
786 unsigned size
= printf_length(fmt
, args
) + 1;
788 char *ptr
= linear_alloc_child(parent
, size
);
790 vsnprintf(ptr
, size
, fmt
, args
);
796 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
801 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
807 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
809 size_t existing_length
;
811 existing_length
= *str
? strlen(*str
) : 0;
812 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
816 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
817 const char *fmt
, ...)
822 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
828 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
829 const char *fmt
, va_list args
)
836 if (unlikely(*str
== NULL
)) {
837 *str
= linear_vasprintf(parent
, fmt
, args
);
838 *start
= strlen(*str
);
842 new_length
= printf_length(fmt
, args
);
844 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
845 if (unlikely(ptr
== NULL
))
848 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
850 *start
+= new_length
;
854 /* helper routine for strcat/strncat - n is the exact amount to copy */
856 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
859 unsigned existing_length
;
860 assert(dest
!= NULL
&& *dest
!= NULL
);
862 existing_length
= strlen(*dest
);
863 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
864 if (unlikely(both
== NULL
))
867 memcpy(both
+ existing_length
, str
, n
);
868 both
[existing_length
+ n
] = '\0';
875 linear_strcat(void *parent
, char **dest
, const char *str
)
877 return linear_cat(parent
, dest
, str
, strlen(str
));