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
63 __attribute__((aligned
))
68 /* A canary value used to determine whether a pointer is ralloc'd. */
72 struct ralloc_header
*parent
;
74 /* The first child (head of a linked list) */
75 struct ralloc_header
*child
;
77 /* Linked list of siblings */
78 struct ralloc_header
*prev
;
79 struct ralloc_header
*next
;
81 void (*destructor
)(void *);
84 typedef struct ralloc_header ralloc_header
;
86 static void unlink_block(ralloc_header
*info
);
87 static void unsafe_free(ralloc_header
*info
);
89 static ralloc_header
*
90 get_header(const void *ptr
)
92 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
93 sizeof(ralloc_header
));
95 assert(info
->canary
== CANARY
);
100 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
103 add_child(ralloc_header
*parent
, ralloc_header
*info
)
105 if (parent
!= NULL
) {
106 info
->parent
= parent
;
107 info
->next
= parent
->child
;
108 parent
->child
= info
;
110 if (info
->next
!= NULL
)
111 info
->next
->prev
= info
;
116 ralloc_context(const void *ctx
)
118 return ralloc_size(ctx
, 0);
122 ralloc_size(const void *ctx
, size_t size
)
124 void *block
= malloc(size
+ sizeof(ralloc_header
));
126 ralloc_header
*parent
;
128 if (unlikely(block
== NULL
))
131 info
= (ralloc_header
*) block
;
132 /* measurements have shown that calloc is slower (because of
133 * the multiplication overflow checking?), so clear things
140 info
->destructor
= NULL
;
142 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
144 add_child(parent
, info
);
147 info
->canary
= CANARY
;
150 return PTR_FROM_HEADER(info
);
154 rzalloc_size(const void *ctx
, size_t size
)
156 void *ptr
= ralloc_size(ctx
, size
);
159 memset(ptr
, 0, size
);
164 /* helper function - assumes ptr != NULL */
166 resize(void *ptr
, size_t size
)
168 ralloc_header
*child
, *old
, *info
;
170 old
= get_header(ptr
);
171 info
= realloc(old
, size
+ sizeof(ralloc_header
));
176 /* Update parent and sibling's links to the reallocated node. */
177 if (info
!= old
&& info
->parent
!= NULL
) {
178 if (info
->parent
->child
== old
)
179 info
->parent
->child
= info
;
181 if (info
->prev
!= NULL
)
182 info
->prev
->next
= info
;
184 if (info
->next
!= NULL
)
185 info
->next
->prev
= info
;
188 /* Update child->parent links for all children */
189 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
190 child
->parent
= info
;
192 return PTR_FROM_HEADER(info
);
196 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
198 if (unlikely(ptr
== NULL
))
199 return ralloc_size(ctx
, size
);
201 assert(ralloc_parent(ptr
) == ctx
);
202 return resize(ptr
, size
);
206 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
208 if (count
> SIZE_MAX
/size
)
211 return ralloc_size(ctx
, size
* count
);
215 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
217 if (count
> SIZE_MAX
/size
)
220 return rzalloc_size(ctx
, size
* count
);
224 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
226 if (count
> SIZE_MAX
/size
)
229 return reralloc_size(ctx
, ptr
, size
* count
);
233 ralloc_free(void *ptr
)
240 info
= get_header(ptr
);
246 unlink_block(ralloc_header
*info
)
248 /* Unlink from parent & siblings */
249 if (info
->parent
!= NULL
) {
250 if (info
->parent
->child
== info
)
251 info
->parent
->child
= info
->next
;
253 if (info
->prev
!= NULL
)
254 info
->prev
->next
= info
->next
;
256 if (info
->next
!= NULL
)
257 info
->next
->prev
= info
->prev
;
265 unsafe_free(ralloc_header
*info
)
267 /* Recursively free any children...don't waste time unlinking them. */
269 while (info
->child
!= NULL
) {
271 info
->child
= temp
->next
;
275 /* Free the block itself. Call the destructor first, if any. */
276 if (info
->destructor
!= NULL
)
277 info
->destructor(PTR_FROM_HEADER(info
));
283 ralloc_steal(const void *new_ctx
, void *ptr
)
285 ralloc_header
*info
, *parent
;
287 if (unlikely(ptr
== NULL
))
290 info
= get_header(ptr
);
291 parent
= get_header(new_ctx
);
295 add_child(parent
, info
);
299 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
301 ralloc_header
*new_info
, *old_info
, *child
;
303 if (unlikely(old_ctx
== NULL
))
306 old_info
= get_header(old_ctx
);
307 new_info
= get_header(new_ctx
);
309 /* If there are no children, bail. */
310 if (unlikely(old_info
->child
== NULL
))
313 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
314 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
315 child
->parent
= new_info
;
318 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
319 child
->next
= new_info
->child
;
320 child
->parent
= new_info
;
321 new_info
->child
= old_info
->child
;
322 old_info
->child
= NULL
;
326 ralloc_parent(const void *ptr
)
330 if (unlikely(ptr
== NULL
))
333 info
= get_header(ptr
);
334 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
338 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
340 ralloc_header
*info
= get_header(ptr
);
341 info
->destructor
= destructor
;
345 ralloc_strdup(const void *ctx
, const char *str
)
350 if (unlikely(str
== NULL
))
354 ptr
= ralloc_array(ctx
, char, n
+ 1);
361 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
366 if (unlikely(str
== NULL
))
369 n
= strnlen(str
, max
);
370 ptr
= ralloc_array(ctx
, char, n
+ 1);
376 /* helper routine for strcat/strncat - n is the exact amount to copy */
378 cat(char **dest
, const char *str
, size_t n
)
381 size_t existing_length
;
382 assert(dest
!= NULL
&& *dest
!= NULL
);
384 existing_length
= strlen(*dest
);
385 both
= resize(*dest
, existing_length
+ n
+ 1);
386 if (unlikely(both
== NULL
))
389 memcpy(both
+ existing_length
, str
, n
);
390 both
[existing_length
+ n
] = '\0';
398 ralloc_strcat(char **dest
, const char *str
)
400 return cat(dest
, str
, strlen(str
));
404 ralloc_strncat(char **dest
, const char *str
, size_t n
)
406 /* Clamp n to the string length */
407 size_t str_length
= strlen(str
);
411 return cat(dest
, str
, n
);
415 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
420 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
425 /* Return the length of the string that would be generated by a printf-style
426 * format and argument list, not including the \0 byte.
429 printf_length(const char *fmt
, va_list untouched_args
)
434 /* Make a copy of the va_list so the original caller can still use it */
436 va_copy(args
, untouched_args
);
439 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
440 * if the number of characters to write is greater than count.
442 size
= _vscprintf(fmt
, args
);
445 size
= vsnprintf(&junk
, 1, fmt
, args
);
455 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
457 size_t size
= printf_length(fmt
, args
) + 1;
459 char *ptr
= ralloc_size(ctx
, size
);
461 vsnprintf(ptr
, size
, fmt
, args
);
467 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
472 success
= ralloc_vasprintf_append(str
, fmt
, args
);
478 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
480 size_t existing_length
;
482 existing_length
= *str
? strlen(*str
) : 0;
483 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
487 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
492 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
498 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
506 if (unlikely(*str
== NULL
)) {
507 // Assuming a NULL context is probably bad, but it's expected behavior.
508 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
509 *start
= strlen(*str
);
513 new_length
= printf_length(fmt
, args
);
515 ptr
= resize(*str
, *start
+ new_length
+ 1);
516 if (unlikely(ptr
== NULL
))
519 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
521 *start
+= new_length
;
525 /***************************************************************************
526 * Linear allocator for short-lived allocations.
527 ***************************************************************************
529 * The allocator consists of a parent node (2K buffer), which requires
530 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
531 * directly, because the parent doesn't track them. You have to release
532 * the parent node in order to release all its children.
534 * The allocator uses a fixed-sized buffer with a monotonically increasing
535 * offset after each allocation. If the buffer is all used, another buffer
536 * is allocated, sharing the same ralloc parent, so all buffers are at
537 * the same level in the ralloc hierarchy.
539 * The linear parent node is always the first buffer and keeps track of all
543 #define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1))
545 #define MIN_LINEAR_BUFSIZE 2048
546 #define SUBALLOC_ALIGNMENT sizeof(uintptr_t)
547 #define LMAGIC 0x87b9c7d3
549 struct linear_header
{
551 unsigned magic
; /* for debugging */
553 unsigned offset
; /* points to the first unused byte in the buffer */
554 unsigned size
; /* size of the buffer */
555 void *ralloc_parent
; /* new buffers will use this */
556 struct linear_header
*next
; /* next buffer if we have more */
557 struct linear_header
*latest
; /* the only buffer that has free space */
559 /* After this structure, the buffer begins.
560 * Each suballocation consists of linear_size_chunk as its header followed
561 * by the suballocation, so it goes:
563 * - linear_size_chunk
565 * - linear_size_chunk
569 * linear_size_chunk is only needed by linear_realloc.
573 struct linear_size_chunk
{
574 unsigned size
; /* for realloc */
578 typedef struct linear_header linear_header
;
579 typedef struct linear_size_chunk linear_size_chunk
;
581 #define LINEAR_PARENT_TO_HEADER(parent) \
583 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
585 /* Allocate the linear buffer with its header. */
586 static linear_header
*
587 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
591 min_size
+= sizeof(linear_size_chunk
);
593 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
594 min_size
= MIN_LINEAR_BUFSIZE
;
596 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
601 node
->magic
= LMAGIC
;
604 node
->size
= min_size
;
605 node
->ralloc_parent
= ralloc_ctx
;
612 linear_alloc_child(void *parent
, unsigned size
)
614 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
615 linear_header
*latest
= first
->latest
;
616 linear_header
*new_node
;
617 linear_size_chunk
*ptr
;
620 assert(first
->magic
== LMAGIC
);
621 assert(!latest
->next
);
623 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
624 full_size
= sizeof(linear_size_chunk
) + size
;
626 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
627 /* allocate a new node */
628 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
629 if (unlikely(!new_node
))
632 first
->latest
= new_node
;
633 latest
->latest
= new_node
;
634 latest
->next
= new_node
;
638 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
640 latest
->offset
+= full_size
;
645 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
649 if (unlikely(!ralloc_ctx
))
652 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
654 node
= create_linear_node(ralloc_ctx
, size
);
658 return linear_alloc_child((char*)node
+
659 sizeof(linear_header
) +
660 sizeof(linear_size_chunk
), size
);
664 linear_zalloc_child(void *parent
, unsigned size
)
666 void *ptr
= linear_alloc_child(parent
, size
);
669 memset(ptr
, 0, size
);
674 linear_zalloc_parent(void *parent
, unsigned size
)
676 void *ptr
= linear_alloc_parent(parent
, size
);
679 memset(ptr
, 0, size
);
684 linear_free_parent(void *ptr
)
691 node
= LINEAR_PARENT_TO_HEADER(ptr
);
692 assert(node
->magic
== LMAGIC
);
703 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
710 node
= LINEAR_PARENT_TO_HEADER(ptr
);
711 assert(node
->magic
== LMAGIC
);
714 ralloc_steal(new_ralloc_ctx
, node
);
715 node
->ralloc_parent
= new_ralloc_ctx
;
721 ralloc_parent_of_linear_parent(void *ptr
)
723 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
724 assert(node
->magic
== LMAGIC
);
725 return node
->ralloc_parent
;
729 linear_realloc(void *parent
, void *old
, unsigned new_size
)
731 unsigned old_size
= 0;
732 ralloc_header
*new_ptr
;
734 new_ptr
= linear_alloc_child(parent
, new_size
);
739 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
741 if (likely(new_ptr
&& old_size
))
742 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
747 /* All code below is pretty much copied from ralloc and only the alloc
748 * calls are different.
752 linear_strdup(void *parent
, const char *str
)
761 ptr
= linear_alloc_child(parent
, n
+ 1);
771 linear_asprintf(void *parent
, const char *fmt
, ...)
776 ptr
= linear_vasprintf(parent
, fmt
, args
);
782 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
784 unsigned size
= printf_length(fmt
, args
) + 1;
786 char *ptr
= linear_alloc_child(parent
, size
);
788 vsnprintf(ptr
, size
, fmt
, args
);
794 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
799 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
805 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
807 size_t existing_length
;
809 existing_length
= *str
? strlen(*str
) : 0;
810 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
814 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
815 const char *fmt
, ...)
820 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
826 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
827 const char *fmt
, va_list args
)
834 if (unlikely(*str
== NULL
)) {
835 *str
= linear_vasprintf(parent
, fmt
, args
);
836 *start
= strlen(*str
);
840 new_length
= printf_length(fmt
, args
);
842 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
843 if (unlikely(ptr
== NULL
))
846 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
848 *start
+= new_length
;
852 /* helper routine for strcat/strncat - n is the exact amount to copy */
854 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
857 unsigned existing_length
;
858 assert(dest
!= NULL
&& *dest
!= NULL
);
860 existing_length
= strlen(*dest
);
861 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
862 if (unlikely(both
== NULL
))
865 memcpy(both
+ existing_length
, str
, n
);
866 both
[existing_length
+ n
] = '\0';
873 linear_strcat(void *parent
, char **dest
, const char *str
)
875 return linear_cat(parent
, dest
, str
, strlen(str
));