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 /* Some versions of MinGW are missing _vscprintf's declaration, although they
32 * still provide the symbol in the import library. */
34 _CRTIMP
int _vscprintf(const char *format
, va_list argptr
);
41 #define va_copy(dest, src) __va_copy((dest), (src))
43 #define va_copy(dest, src) (dest) = (src)
47 #define CANARY 0x5A1106
49 /* Align the header's size so that ralloc() allocations will return with the
50 * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on
51 * 64-bit), avoiding performance penalities on x86 and alignment faults on
61 #elif defined(__LP64__)
62 __attribute__((aligned(16)))
64 __attribute__((aligned(8)))
69 /* A canary value used to determine whether a pointer is ralloc'd. */
73 struct ralloc_header
*parent
;
75 /* The first child (head of a linked list) */
76 struct ralloc_header
*child
;
78 /* Linked list of siblings */
79 struct ralloc_header
*prev
;
80 struct ralloc_header
*next
;
82 void (*destructor
)(void *);
85 typedef struct ralloc_header ralloc_header
;
87 static void unlink_block(ralloc_header
*info
);
88 static void unsafe_free(ralloc_header
*info
);
90 static ralloc_header
*
91 get_header(const void *ptr
)
93 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
94 sizeof(ralloc_header
));
95 assert(info
->canary
== CANARY
);
99 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
102 add_child(ralloc_header
*parent
, ralloc_header
*info
)
104 if (parent
!= NULL
) {
105 info
->parent
= parent
;
106 info
->next
= parent
->child
;
107 parent
->child
= info
;
109 if (info
->next
!= NULL
)
110 info
->next
->prev
= info
;
115 ralloc_context(const void *ctx
)
117 return ralloc_size(ctx
, 0);
121 ralloc_size(const void *ctx
, size_t size
)
123 void *block
= malloc(size
+ sizeof(ralloc_header
));
125 ralloc_header
*parent
;
127 if (unlikely(block
== NULL
))
130 info
= (ralloc_header
*) block
;
131 /* measurements have shown that calloc is slower (because of
132 * the multiplication overflow checking?), so clear things
139 info
->destructor
= NULL
;
141 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
143 add_child(parent
, info
);
146 info
->canary
= CANARY
;
149 return PTR_FROM_HEADER(info
);
153 rzalloc_size(const void *ctx
, size_t size
)
155 void *ptr
= ralloc_size(ctx
, size
);
158 memset(ptr
, 0, size
);
163 /* helper function - assumes ptr != NULL */
165 resize(void *ptr
, size_t size
)
167 ralloc_header
*child
, *old
, *info
;
169 old
= get_header(ptr
);
170 info
= realloc(old
, size
+ sizeof(ralloc_header
));
175 /* Update parent and sibling's links to the reallocated node. */
176 if (info
!= old
&& info
->parent
!= NULL
) {
177 if (info
->parent
->child
== old
)
178 info
->parent
->child
= info
;
180 if (info
->prev
!= NULL
)
181 info
->prev
->next
= info
;
183 if (info
->next
!= NULL
)
184 info
->next
->prev
= info
;
187 /* Update child->parent links for all children */
188 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
189 child
->parent
= info
;
191 return PTR_FROM_HEADER(info
);
195 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
197 if (unlikely(ptr
== NULL
))
198 return ralloc_size(ctx
, size
);
200 assert(ralloc_parent(ptr
) == ctx
);
201 return resize(ptr
, size
);
205 rerzalloc_size(const void *ctx
, void *ptr
, size_t old_size
, size_t new_size
)
207 if (unlikely(ptr
== NULL
))
208 return rzalloc_size(ctx
, new_size
);
210 assert(ralloc_parent(ptr
) == ctx
);
211 ptr
= resize(ptr
, new_size
);
213 if (new_size
> old_size
)
214 memset((char *)ptr
+ old_size
, 0, new_size
- old_size
);
220 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
222 if (count
> SIZE_MAX
/size
)
225 return ralloc_size(ctx
, size
* count
);
229 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
231 if (count
> SIZE_MAX
/size
)
234 return rzalloc_size(ctx
, size
* count
);
238 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
240 if (count
> SIZE_MAX
/size
)
243 return reralloc_size(ctx
, ptr
, size
* count
);
247 rerzalloc_array_size(const void *ctx
, void *ptr
, size_t size
,
248 unsigned old_count
, unsigned new_count
)
250 if (new_count
> SIZE_MAX
/size
)
253 return rerzalloc_size(ctx
, ptr
, size
* old_count
, size
* new_count
);
257 ralloc_free(void *ptr
)
264 info
= get_header(ptr
);
270 unlink_block(ralloc_header
*info
)
272 /* Unlink from parent & siblings */
273 if (info
->parent
!= NULL
) {
274 if (info
->parent
->child
== info
)
275 info
->parent
->child
= info
->next
;
277 if (info
->prev
!= NULL
)
278 info
->prev
->next
= info
->next
;
280 if (info
->next
!= NULL
)
281 info
->next
->prev
= info
->prev
;
289 unsafe_free(ralloc_header
*info
)
291 /* Recursively free any children...don't waste time unlinking them. */
293 while (info
->child
!= NULL
) {
295 info
->child
= temp
->next
;
299 /* Free the block itself. Call the destructor first, if any. */
300 if (info
->destructor
!= NULL
)
301 info
->destructor(PTR_FROM_HEADER(info
));
307 ralloc_steal(const void *new_ctx
, void *ptr
)
309 ralloc_header
*info
, *parent
;
311 if (unlikely(ptr
== NULL
))
314 info
= get_header(ptr
);
315 parent
= new_ctx
? get_header(new_ctx
) : NULL
;
319 add_child(parent
, info
);
323 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
325 ralloc_header
*new_info
, *old_info
, *child
;
327 if (unlikely(old_ctx
== NULL
))
330 old_info
= get_header(old_ctx
);
331 new_info
= get_header(new_ctx
);
333 /* If there are no children, bail. */
334 if (unlikely(old_info
->child
== NULL
))
337 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
338 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
339 child
->parent
= new_info
;
341 child
->parent
= new_info
;
343 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
344 child
->next
= new_info
->child
;
346 child
->next
->prev
= child
;
347 new_info
->child
= old_info
->child
;
348 old_info
->child
= NULL
;
352 ralloc_parent(const void *ptr
)
356 if (unlikely(ptr
== NULL
))
359 info
= get_header(ptr
);
360 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
364 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
366 ralloc_header
*info
= get_header(ptr
);
367 info
->destructor
= destructor
;
371 ralloc_strdup(const void *ctx
, const char *str
)
376 if (unlikely(str
== NULL
))
380 ptr
= ralloc_array(ctx
, char, n
+ 1);
387 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
392 if (unlikely(str
== NULL
))
395 n
= strnlen(str
, max
);
396 ptr
= ralloc_array(ctx
, char, n
+ 1);
402 /* helper routine for strcat/strncat - n is the exact amount to copy */
404 cat(char **dest
, const char *str
, size_t n
)
407 size_t existing_length
;
408 assert(dest
!= NULL
&& *dest
!= NULL
);
410 existing_length
= strlen(*dest
);
411 both
= resize(*dest
, existing_length
+ n
+ 1);
412 if (unlikely(both
== NULL
))
415 memcpy(both
+ existing_length
, str
, n
);
416 both
[existing_length
+ n
] = '\0';
424 ralloc_strcat(char **dest
, const char *str
)
426 return cat(dest
, str
, strlen(str
));
430 ralloc_strncat(char **dest
, const char *str
, size_t n
)
432 return cat(dest
, str
, strnlen(str
, n
));
436 ralloc_str_append(char **dest
, const char *str
,
437 size_t existing_length
, size_t str_size
)
440 assert(dest
!= NULL
&& *dest
!= NULL
);
442 both
= resize(*dest
, existing_length
+ str_size
+ 1);
443 if (unlikely(both
== NULL
))
446 memcpy(both
+ existing_length
, str
, str_size
);
447 both
[existing_length
+ str_size
] = '\0';
455 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
460 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
465 /* Return the length of the string that would be generated by a printf-style
466 * format and argument list, not including the \0 byte.
469 printf_length(const char *fmt
, va_list untouched_args
)
474 /* Make a copy of the va_list so the original caller can still use it */
476 va_copy(args
, untouched_args
);
479 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
480 * if the number of characters to write is greater than count.
482 size
= _vscprintf(fmt
, args
);
485 size
= vsnprintf(&junk
, 1, fmt
, args
);
495 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
497 size_t size
= printf_length(fmt
, args
) + 1;
499 char *ptr
= ralloc_size(ctx
, size
);
501 vsnprintf(ptr
, size
, fmt
, args
);
507 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
512 success
= ralloc_vasprintf_append(str
, fmt
, args
);
518 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
520 size_t existing_length
;
522 existing_length
= *str
? strlen(*str
) : 0;
523 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
527 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
532 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
538 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
546 if (unlikely(*str
== NULL
)) {
547 // Assuming a NULL context is probably bad, but it's expected behavior.
548 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
549 *start
= strlen(*str
);
553 new_length
= printf_length(fmt
, args
);
555 ptr
= resize(*str
, *start
+ new_length
+ 1);
556 if (unlikely(ptr
== NULL
))
559 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
561 *start
+= new_length
;
565 /***************************************************************************
566 * Linear allocator for short-lived allocations.
567 ***************************************************************************
569 * The allocator consists of a parent node (2K buffer), which requires
570 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
571 * directly, because the parent doesn't track them. You have to release
572 * the parent node in order to release all its children.
574 * The allocator uses a fixed-sized buffer with a monotonically increasing
575 * offset after each allocation. If the buffer is all used, another buffer
576 * is allocated, sharing the same ralloc parent, so all buffers are at
577 * the same level in the ralloc hierarchy.
579 * The linear parent node is always the first buffer and keeps track of all
583 #define MIN_LINEAR_BUFSIZE 2048
584 #define SUBALLOC_ALIGNMENT 8
585 #define LMAGIC 0x87b9c7d3
590 #elif defined(__LP64__)
591 __attribute__((aligned(16)))
593 __attribute__((aligned(8)))
597 unsigned magic
; /* for debugging */
599 unsigned offset
; /* points to the first unused byte in the buffer */
600 unsigned size
; /* size of the buffer */
601 void *ralloc_parent
; /* new buffers will use this */
602 struct linear_header
*next
; /* next buffer if we have more */
603 struct linear_header
*latest
; /* the only buffer that has free space */
605 /* After this structure, the buffer begins.
606 * Each suballocation consists of linear_size_chunk as its header followed
607 * by the suballocation, so it goes:
609 * - linear_size_chunk
611 * - linear_size_chunk
615 * linear_size_chunk is only needed by linear_realloc.
619 struct linear_size_chunk
{
620 unsigned size
; /* for realloc */
624 typedef struct linear_header linear_header
;
625 typedef struct linear_size_chunk linear_size_chunk
;
627 #define LINEAR_PARENT_TO_HEADER(parent) \
629 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
631 /* Allocate the linear buffer with its header. */
632 static linear_header
*
633 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
637 min_size
+= sizeof(linear_size_chunk
);
639 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
640 min_size
= MIN_LINEAR_BUFSIZE
;
642 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
647 node
->magic
= LMAGIC
;
650 node
->size
= min_size
;
651 node
->ralloc_parent
= ralloc_ctx
;
658 linear_alloc_child(void *parent
, unsigned size
)
660 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
661 linear_header
*latest
= first
->latest
;
662 linear_header
*new_node
;
663 linear_size_chunk
*ptr
;
666 assert(first
->magic
== LMAGIC
);
667 assert(!latest
->next
);
669 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
670 full_size
= sizeof(linear_size_chunk
) + size
;
672 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
673 /* allocate a new node */
674 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
675 if (unlikely(!new_node
))
678 first
->latest
= new_node
;
679 latest
->latest
= new_node
;
680 latest
->next
= new_node
;
684 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
686 latest
->offset
+= full_size
;
688 assert((uintptr_t)&ptr
[1] % SUBALLOC_ALIGNMENT
== 0);
693 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
697 if (unlikely(!ralloc_ctx
))
700 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
702 node
= create_linear_node(ralloc_ctx
, size
);
706 return linear_alloc_child((char*)node
+
707 sizeof(linear_header
) +
708 sizeof(linear_size_chunk
), size
);
712 linear_zalloc_child(void *parent
, unsigned size
)
714 void *ptr
= linear_alloc_child(parent
, size
);
717 memset(ptr
, 0, size
);
722 linear_zalloc_parent(void *parent
, unsigned size
)
724 void *ptr
= linear_alloc_parent(parent
, size
);
727 memset(ptr
, 0, size
);
732 linear_free_parent(void *ptr
)
739 node
= LINEAR_PARENT_TO_HEADER(ptr
);
740 assert(node
->magic
== LMAGIC
);
751 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
758 node
= LINEAR_PARENT_TO_HEADER(ptr
);
759 assert(node
->magic
== LMAGIC
);
762 ralloc_steal(new_ralloc_ctx
, node
);
763 node
->ralloc_parent
= new_ralloc_ctx
;
769 ralloc_parent_of_linear_parent(void *ptr
)
771 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
772 assert(node
->magic
== LMAGIC
);
773 return node
->ralloc_parent
;
777 linear_realloc(void *parent
, void *old
, unsigned new_size
)
779 unsigned old_size
= 0;
780 ralloc_header
*new_ptr
;
782 new_ptr
= linear_alloc_child(parent
, new_size
);
787 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
789 if (likely(new_ptr
&& old_size
))
790 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
795 /* All code below is pretty much copied from ralloc and only the alloc
796 * calls are different.
800 linear_strdup(void *parent
, const char *str
)
809 ptr
= linear_alloc_child(parent
, n
+ 1);
819 linear_asprintf(void *parent
, const char *fmt
, ...)
824 ptr
= linear_vasprintf(parent
, fmt
, args
);
830 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
832 unsigned size
= printf_length(fmt
, args
) + 1;
834 char *ptr
= linear_alloc_child(parent
, size
);
836 vsnprintf(ptr
, size
, fmt
, args
);
842 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
847 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
853 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
855 size_t existing_length
;
857 existing_length
= *str
? strlen(*str
) : 0;
858 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
862 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
863 const char *fmt
, ...)
868 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
874 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
875 const char *fmt
, va_list args
)
882 if (unlikely(*str
== NULL
)) {
883 *str
= linear_vasprintf(parent
, fmt
, args
);
884 *start
= strlen(*str
);
888 new_length
= printf_length(fmt
, args
);
890 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
891 if (unlikely(ptr
== NULL
))
894 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
896 *start
+= new_length
;
900 /* helper routine for strcat/strncat - n is the exact amount to copy */
902 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
905 unsigned existing_length
;
906 assert(dest
!= NULL
&& *dest
!= NULL
);
908 existing_length
= strlen(*dest
);
909 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
910 if (unlikely(both
== NULL
))
913 memcpy(both
+ existing_length
, str
, n
);
914 both
[existing_length
+ n
] = '\0';
921 linear_strcat(void *parent
, char **dest
, const char *str
)
923 return linear_cat(parent
, dest
, str
, strlen(str
));