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
57 #elif defined(__LP64__)
58 __attribute__((aligned(16)))
60 __attribute__((aligned(8)))
65 /* A canary value used to determine whether a pointer is ralloc'd. */
69 struct ralloc_header
*parent
;
71 /* The first child (head of a linked list) */
72 struct ralloc_header
*child
;
74 /* Linked list of siblings */
75 struct ralloc_header
*prev
;
76 struct ralloc_header
*next
;
78 void (*destructor
)(void *);
81 typedef struct ralloc_header ralloc_header
;
83 static void unlink_block(ralloc_header
*info
);
84 static void unsafe_free(ralloc_header
*info
);
86 static ralloc_header
*
87 get_header(const void *ptr
)
89 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
90 sizeof(ralloc_header
));
91 assert(info
->canary
== CANARY
);
95 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
98 add_child(ralloc_header
*parent
, ralloc_header
*info
)
100 if (parent
!= NULL
) {
101 info
->parent
= parent
;
102 info
->next
= parent
->child
;
103 parent
->child
= info
;
105 if (info
->next
!= NULL
)
106 info
->next
->prev
= info
;
111 ralloc_context(const void *ctx
)
113 return ralloc_size(ctx
, 0);
117 ralloc_size(const void *ctx
, size_t size
)
119 void *block
= malloc(size
+ sizeof(ralloc_header
));
121 ralloc_header
*parent
;
123 if (unlikely(block
== NULL
))
126 info
= (ralloc_header
*) block
;
127 /* measurements have shown that calloc is slower (because of
128 * the multiplication overflow checking?), so clear things
135 info
->destructor
= NULL
;
137 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
139 add_child(parent
, info
);
142 info
->canary
= CANARY
;
145 return PTR_FROM_HEADER(info
);
149 rzalloc_size(const void *ctx
, size_t size
)
151 void *ptr
= ralloc_size(ctx
, size
);
154 memset(ptr
, 0, size
);
159 /* helper function - assumes ptr != NULL */
161 resize(void *ptr
, size_t size
)
163 ralloc_header
*child
, *old
, *info
;
165 old
= get_header(ptr
);
166 info
= realloc(old
, size
+ sizeof(ralloc_header
));
171 /* Update parent and sibling's links to the reallocated node. */
172 if (info
!= old
&& info
->parent
!= NULL
) {
173 if (info
->parent
->child
== old
)
174 info
->parent
->child
= info
;
176 if (info
->prev
!= NULL
)
177 info
->prev
->next
= info
;
179 if (info
->next
!= NULL
)
180 info
->next
->prev
= info
;
183 /* Update child->parent links for all children */
184 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
185 child
->parent
= info
;
187 return PTR_FROM_HEADER(info
);
191 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
193 if (unlikely(ptr
== NULL
))
194 return ralloc_size(ctx
, size
);
196 assert(ralloc_parent(ptr
) == ctx
);
197 return resize(ptr
, size
);
201 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
203 if (count
> SIZE_MAX
/size
)
206 return ralloc_size(ctx
, size
* count
);
210 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
212 if (count
> SIZE_MAX
/size
)
215 return rzalloc_size(ctx
, size
* count
);
219 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
221 if (count
> SIZE_MAX
/size
)
224 return reralloc_size(ctx
, ptr
, size
* count
);
228 ralloc_free(void *ptr
)
235 info
= get_header(ptr
);
241 unlink_block(ralloc_header
*info
)
243 /* Unlink from parent & siblings */
244 if (info
->parent
!= NULL
) {
245 if (info
->parent
->child
== info
)
246 info
->parent
->child
= info
->next
;
248 if (info
->prev
!= NULL
)
249 info
->prev
->next
= info
->next
;
251 if (info
->next
!= NULL
)
252 info
->next
->prev
= info
->prev
;
260 unsafe_free(ralloc_header
*info
)
262 /* Recursively free any children...don't waste time unlinking them. */
264 while (info
->child
!= NULL
) {
266 info
->child
= temp
->next
;
270 /* Free the block itself. Call the destructor first, if any. */
271 if (info
->destructor
!= NULL
)
272 info
->destructor(PTR_FROM_HEADER(info
));
278 ralloc_steal(const void *new_ctx
, void *ptr
)
280 ralloc_header
*info
, *parent
;
282 if (unlikely(ptr
== NULL
))
285 info
= get_header(ptr
);
286 parent
= new_ctx
? get_header(new_ctx
) : NULL
;
290 add_child(parent
, info
);
294 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
296 ralloc_header
*new_info
, *old_info
, *child
;
298 if (unlikely(old_ctx
== NULL
))
301 old_info
= get_header(old_ctx
);
302 new_info
= get_header(new_ctx
);
304 /* If there are no children, bail. */
305 if (unlikely(old_info
->child
== NULL
))
308 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
309 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
310 child
->parent
= new_info
;
312 child
->parent
= new_info
;
314 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
315 child
->next
= new_info
->child
;
317 child
->next
->prev
= child
;
318 new_info
->child
= old_info
->child
;
319 old_info
->child
= NULL
;
323 ralloc_parent(const void *ptr
)
327 if (unlikely(ptr
== NULL
))
330 info
= get_header(ptr
);
331 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
335 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
337 ralloc_header
*info
= get_header(ptr
);
338 info
->destructor
= destructor
;
342 ralloc_strdup(const void *ctx
, const char *str
)
347 if (unlikely(str
== NULL
))
351 ptr
= ralloc_array(ctx
, char, n
+ 1);
358 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
363 if (unlikely(str
== NULL
))
366 n
= strnlen(str
, max
);
367 ptr
= ralloc_array(ctx
, char, n
+ 1);
373 /* helper routine for strcat/strncat - n is the exact amount to copy */
375 cat(char **dest
, const char *str
, size_t n
)
378 size_t existing_length
;
379 assert(dest
!= NULL
&& *dest
!= NULL
);
381 existing_length
= strlen(*dest
);
382 both
= resize(*dest
, existing_length
+ n
+ 1);
383 if (unlikely(both
== NULL
))
386 memcpy(both
+ existing_length
, str
, n
);
387 both
[existing_length
+ n
] = '\0';
395 ralloc_strcat(char **dest
, const char *str
)
397 return cat(dest
, str
, strlen(str
));
401 ralloc_strncat(char **dest
, const char *str
, size_t n
)
403 return cat(dest
, str
, strnlen(str
, n
));
407 ralloc_str_append(char **dest
, const char *str
,
408 size_t existing_length
, size_t str_size
)
411 assert(dest
!= NULL
&& *dest
!= NULL
);
413 both
= resize(*dest
, existing_length
+ str_size
+ 1);
414 if (unlikely(both
== NULL
))
417 memcpy(both
+ existing_length
, str
, str_size
);
418 both
[existing_length
+ str_size
] = '\0';
426 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
431 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
436 /* Return the length of the string that would be generated by a printf-style
437 * format and argument list, not including the \0 byte.
440 printf_length(const char *fmt
, va_list untouched_args
)
445 /* Make a copy of the va_list so the original caller can still use it */
447 va_copy(args
, untouched_args
);
450 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
451 * if the number of characters to write is greater than count.
453 size
= _vscprintf(fmt
, args
);
456 size
= vsnprintf(&junk
, 1, fmt
, args
);
466 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
468 size_t size
= printf_length(fmt
, args
) + 1;
470 char *ptr
= ralloc_size(ctx
, size
);
472 vsnprintf(ptr
, size
, fmt
, args
);
478 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
483 success
= ralloc_vasprintf_append(str
, fmt
, args
);
489 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
491 size_t existing_length
;
493 existing_length
= *str
? strlen(*str
) : 0;
494 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
498 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
503 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
509 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
517 if (unlikely(*str
== NULL
)) {
518 // Assuming a NULL context is probably bad, but it's expected behavior.
519 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
520 *start
= strlen(*str
);
524 new_length
= printf_length(fmt
, args
);
526 ptr
= resize(*str
, *start
+ new_length
+ 1);
527 if (unlikely(ptr
== NULL
))
530 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
532 *start
+= new_length
;
536 /***************************************************************************
537 * Linear allocator for short-lived allocations.
538 ***************************************************************************
540 * The allocator consists of a parent node (2K buffer), which requires
541 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
542 * directly, because the parent doesn't track them. You have to release
543 * the parent node in order to release all its children.
545 * The allocator uses a fixed-sized buffer with a monotonically increasing
546 * offset after each allocation. If the buffer is all used, another buffer
547 * is allocated, sharing the same ralloc parent, so all buffers are at
548 * the same level in the ralloc hierarchy.
550 * The linear parent node is always the first buffer and keeps track of all
554 #define MIN_LINEAR_BUFSIZE 2048
555 #define SUBALLOC_ALIGNMENT 8
556 #define LMAGIC 0x87b9c7d3
561 #elif defined(__LP64__)
562 __attribute__((aligned(16)))
564 __attribute__((aligned(8)))
568 unsigned magic
; /* for debugging */
570 unsigned offset
; /* points to the first unused byte in the buffer */
571 unsigned size
; /* size of the buffer */
572 void *ralloc_parent
; /* new buffers will use this */
573 struct linear_header
*next
; /* next buffer if we have more */
574 struct linear_header
*latest
; /* the only buffer that has free space */
576 /* After this structure, the buffer begins.
577 * Each suballocation consists of linear_size_chunk as its header followed
578 * by the suballocation, so it goes:
580 * - linear_size_chunk
582 * - linear_size_chunk
586 * linear_size_chunk is only needed by linear_realloc.
590 struct linear_size_chunk
{
591 unsigned size
; /* for realloc */
595 typedef struct linear_header linear_header
;
596 typedef struct linear_size_chunk linear_size_chunk
;
598 #define LINEAR_PARENT_TO_HEADER(parent) \
600 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
602 /* Allocate the linear buffer with its header. */
603 static linear_header
*
604 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
608 min_size
+= sizeof(linear_size_chunk
);
610 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
611 min_size
= MIN_LINEAR_BUFSIZE
;
613 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
618 node
->magic
= LMAGIC
;
621 node
->size
= min_size
;
622 node
->ralloc_parent
= ralloc_ctx
;
629 linear_alloc_child(void *parent
, unsigned size
)
631 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
632 linear_header
*latest
= first
->latest
;
633 linear_header
*new_node
;
634 linear_size_chunk
*ptr
;
637 assert(first
->magic
== LMAGIC
);
638 assert(!latest
->next
);
640 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
641 full_size
= sizeof(linear_size_chunk
) + size
;
643 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
644 /* allocate a new node */
645 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
646 if (unlikely(!new_node
))
649 first
->latest
= new_node
;
650 latest
->latest
= new_node
;
651 latest
->next
= new_node
;
655 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
657 latest
->offset
+= full_size
;
659 assert((uintptr_t)&ptr
[1] % SUBALLOC_ALIGNMENT
== 0);
664 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
668 if (unlikely(!ralloc_ctx
))
671 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
673 node
= create_linear_node(ralloc_ctx
, size
);
677 return linear_alloc_child((char*)node
+
678 sizeof(linear_header
) +
679 sizeof(linear_size_chunk
), size
);
683 linear_zalloc_child(void *parent
, unsigned size
)
685 void *ptr
= linear_alloc_child(parent
, size
);
688 memset(ptr
, 0, size
);
693 linear_zalloc_parent(void *parent
, unsigned size
)
695 void *ptr
= linear_alloc_parent(parent
, size
);
698 memset(ptr
, 0, size
);
703 linear_free_parent(void *ptr
)
710 node
= LINEAR_PARENT_TO_HEADER(ptr
);
711 assert(node
->magic
== LMAGIC
);
722 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
729 node
= LINEAR_PARENT_TO_HEADER(ptr
);
730 assert(node
->magic
== LMAGIC
);
733 ralloc_steal(new_ralloc_ctx
, node
);
734 node
->ralloc_parent
= new_ralloc_ctx
;
740 ralloc_parent_of_linear_parent(void *ptr
)
742 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
743 assert(node
->magic
== LMAGIC
);
744 return node
->ralloc_parent
;
748 linear_realloc(void *parent
, void *old
, unsigned new_size
)
750 unsigned old_size
= 0;
751 ralloc_header
*new_ptr
;
753 new_ptr
= linear_alloc_child(parent
, new_size
);
758 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
760 if (likely(new_ptr
&& old_size
))
761 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
766 /* All code below is pretty much copied from ralloc and only the alloc
767 * calls are different.
771 linear_strdup(void *parent
, const char *str
)
780 ptr
= linear_alloc_child(parent
, n
+ 1);
790 linear_asprintf(void *parent
, const char *fmt
, ...)
795 ptr
= linear_vasprintf(parent
, fmt
, args
);
801 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
803 unsigned size
= printf_length(fmt
, args
) + 1;
805 char *ptr
= linear_alloc_child(parent
, size
);
807 vsnprintf(ptr
, size
, fmt
, args
);
813 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
818 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
824 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
826 size_t existing_length
;
828 existing_length
= *str
? strlen(*str
) : 0;
829 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
833 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
834 const char *fmt
, ...)
839 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
845 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
846 const char *fmt
, va_list args
)
853 if (unlikely(*str
== NULL
)) {
854 *str
= linear_vasprintf(parent
, fmt
, args
);
855 *start
= strlen(*str
);
859 new_length
= printf_length(fmt
, args
);
861 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
862 if (unlikely(ptr
== NULL
))
865 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
867 *start
+= new_length
;
871 /* helper routine for strcat/strncat - n is the exact amount to copy */
873 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
876 unsigned existing_length
;
877 assert(dest
!= NULL
&& *dest
!= NULL
);
879 existing_length
= strlen(*dest
);
880 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
881 if (unlikely(both
== NULL
))
884 memcpy(both
+ existing_length
, str
, n
);
885 both
[existing_length
+ n
] = '\0';
892 linear_strcat(void *parent
, char **dest
, const char *str
)
894 return linear_cat(parent
, dest
, str
, strlen(str
));