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 rerzalloc_size(const void *ctx
, void *ptr
, size_t old_size
, size_t new_size
)
203 if (unlikely(ptr
== NULL
))
204 return rzalloc_size(ctx
, new_size
);
206 assert(ralloc_parent(ptr
) == ctx
);
207 ptr
= resize(ptr
, new_size
);
209 if (new_size
> old_size
)
210 memset((char *)ptr
+ old_size
, 0, new_size
- old_size
);
216 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
218 if (count
> SIZE_MAX
/size
)
221 return ralloc_size(ctx
, size
* count
);
225 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
227 if (count
> SIZE_MAX
/size
)
230 return rzalloc_size(ctx
, size
* count
);
234 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
236 if (count
> SIZE_MAX
/size
)
239 return reralloc_size(ctx
, ptr
, size
* count
);
243 rerzalloc_array_size(const void *ctx
, void *ptr
, size_t size
,
244 unsigned old_count
, unsigned new_count
)
246 if (new_count
> SIZE_MAX
/size
)
249 return rerzalloc_size(ctx
, ptr
, size
* old_count
, size
* new_count
);
253 ralloc_free(void *ptr
)
260 info
= get_header(ptr
);
266 unlink_block(ralloc_header
*info
)
268 /* Unlink from parent & siblings */
269 if (info
->parent
!= NULL
) {
270 if (info
->parent
->child
== info
)
271 info
->parent
->child
= info
->next
;
273 if (info
->prev
!= NULL
)
274 info
->prev
->next
= info
->next
;
276 if (info
->next
!= NULL
)
277 info
->next
->prev
= info
->prev
;
285 unsafe_free(ralloc_header
*info
)
287 /* Recursively free any children...don't waste time unlinking them. */
289 while (info
->child
!= NULL
) {
291 info
->child
= temp
->next
;
295 /* Free the block itself. Call the destructor first, if any. */
296 if (info
->destructor
!= NULL
)
297 info
->destructor(PTR_FROM_HEADER(info
));
303 ralloc_steal(const void *new_ctx
, void *ptr
)
305 ralloc_header
*info
, *parent
;
307 if (unlikely(ptr
== NULL
))
310 info
= get_header(ptr
);
311 parent
= new_ctx
? get_header(new_ctx
) : NULL
;
315 add_child(parent
, info
);
319 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
321 ralloc_header
*new_info
, *old_info
, *child
;
323 if (unlikely(old_ctx
== NULL
))
326 old_info
= get_header(old_ctx
);
327 new_info
= get_header(new_ctx
);
329 /* If there are no children, bail. */
330 if (unlikely(old_info
->child
== NULL
))
333 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
334 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
335 child
->parent
= new_info
;
337 child
->parent
= new_info
;
339 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
340 child
->next
= new_info
->child
;
342 child
->next
->prev
= child
;
343 new_info
->child
= old_info
->child
;
344 old_info
->child
= NULL
;
348 ralloc_parent(const void *ptr
)
352 if (unlikely(ptr
== NULL
))
355 info
= get_header(ptr
);
356 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
360 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
362 ralloc_header
*info
= get_header(ptr
);
363 info
->destructor
= destructor
;
367 ralloc_strdup(const void *ctx
, const char *str
)
372 if (unlikely(str
== NULL
))
376 ptr
= ralloc_array(ctx
, char, n
+ 1);
383 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
388 if (unlikely(str
== NULL
))
391 n
= strnlen(str
, max
);
392 ptr
= ralloc_array(ctx
, char, n
+ 1);
398 /* helper routine for strcat/strncat - n is the exact amount to copy */
400 cat(char **dest
, const char *str
, size_t n
)
403 size_t existing_length
;
404 assert(dest
!= NULL
&& *dest
!= NULL
);
406 existing_length
= strlen(*dest
);
407 both
= resize(*dest
, existing_length
+ n
+ 1);
408 if (unlikely(both
== NULL
))
411 memcpy(both
+ existing_length
, str
, n
);
412 both
[existing_length
+ n
] = '\0';
420 ralloc_strcat(char **dest
, const char *str
)
422 return cat(dest
, str
, strlen(str
));
426 ralloc_strncat(char **dest
, const char *str
, size_t n
)
428 return cat(dest
, str
, strnlen(str
, n
));
432 ralloc_str_append(char **dest
, const char *str
,
433 size_t existing_length
, size_t str_size
)
436 assert(dest
!= NULL
&& *dest
!= NULL
);
438 both
= resize(*dest
, existing_length
+ str_size
+ 1);
439 if (unlikely(both
== NULL
))
442 memcpy(both
+ existing_length
, str
, str_size
);
443 both
[existing_length
+ str_size
] = '\0';
451 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
456 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
461 /* Return the length of the string that would be generated by a printf-style
462 * format and argument list, not including the \0 byte.
465 printf_length(const char *fmt
, va_list untouched_args
)
470 /* Make a copy of the va_list so the original caller can still use it */
472 va_copy(args
, untouched_args
);
475 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
476 * if the number of characters to write is greater than count.
478 size
= _vscprintf(fmt
, args
);
481 size
= vsnprintf(&junk
, 1, fmt
, args
);
491 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
493 size_t size
= printf_length(fmt
, args
) + 1;
495 char *ptr
= ralloc_size(ctx
, size
);
497 vsnprintf(ptr
, size
, fmt
, args
);
503 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
508 success
= ralloc_vasprintf_append(str
, fmt
, args
);
514 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
516 size_t existing_length
;
518 existing_length
= *str
? strlen(*str
) : 0;
519 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
523 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
528 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
534 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
542 if (unlikely(*str
== NULL
)) {
543 // Assuming a NULL context is probably bad, but it's expected behavior.
544 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
545 *start
= strlen(*str
);
549 new_length
= printf_length(fmt
, args
);
551 ptr
= resize(*str
, *start
+ new_length
+ 1);
552 if (unlikely(ptr
== NULL
))
555 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
557 *start
+= new_length
;
561 /***************************************************************************
562 * Linear allocator for short-lived allocations.
563 ***************************************************************************
565 * The allocator consists of a parent node (2K buffer), which requires
566 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
567 * directly, because the parent doesn't track them. You have to release
568 * the parent node in order to release all its children.
570 * The allocator uses a fixed-sized buffer with a monotonically increasing
571 * offset after each allocation. If the buffer is all used, another buffer
572 * is allocated, sharing the same ralloc parent, so all buffers are at
573 * the same level in the ralloc hierarchy.
575 * The linear parent node is always the first buffer and keeps track of all
579 #define MIN_LINEAR_BUFSIZE 2048
580 #define SUBALLOC_ALIGNMENT 8
581 #define LMAGIC 0x87b9c7d3
586 #elif defined(__LP64__)
587 __attribute__((aligned(16)))
589 __attribute__((aligned(8)))
593 unsigned magic
; /* for debugging */
595 unsigned offset
; /* points to the first unused byte in the buffer */
596 unsigned size
; /* size of the buffer */
597 void *ralloc_parent
; /* new buffers will use this */
598 struct linear_header
*next
; /* next buffer if we have more */
599 struct linear_header
*latest
; /* the only buffer that has free space */
601 /* After this structure, the buffer begins.
602 * Each suballocation consists of linear_size_chunk as its header followed
603 * by the suballocation, so it goes:
605 * - linear_size_chunk
607 * - linear_size_chunk
611 * linear_size_chunk is only needed by linear_realloc.
615 struct linear_size_chunk
{
616 unsigned size
; /* for realloc */
620 typedef struct linear_header linear_header
;
621 typedef struct linear_size_chunk linear_size_chunk
;
623 #define LINEAR_PARENT_TO_HEADER(parent) \
625 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
627 /* Allocate the linear buffer with its header. */
628 static linear_header
*
629 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
633 min_size
+= sizeof(linear_size_chunk
);
635 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
636 min_size
= MIN_LINEAR_BUFSIZE
;
638 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
643 node
->magic
= LMAGIC
;
646 node
->size
= min_size
;
647 node
->ralloc_parent
= ralloc_ctx
;
654 linear_alloc_child(void *parent
, unsigned size
)
656 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
657 linear_header
*latest
= first
->latest
;
658 linear_header
*new_node
;
659 linear_size_chunk
*ptr
;
662 assert(first
->magic
== LMAGIC
);
663 assert(!latest
->next
);
665 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
666 full_size
= sizeof(linear_size_chunk
) + size
;
668 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
669 /* allocate a new node */
670 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
671 if (unlikely(!new_node
))
674 first
->latest
= new_node
;
675 latest
->latest
= new_node
;
676 latest
->next
= new_node
;
680 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
682 latest
->offset
+= full_size
;
684 assert((uintptr_t)&ptr
[1] % SUBALLOC_ALIGNMENT
== 0);
689 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
693 if (unlikely(!ralloc_ctx
))
696 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
698 node
= create_linear_node(ralloc_ctx
, size
);
702 return linear_alloc_child((char*)node
+
703 sizeof(linear_header
) +
704 sizeof(linear_size_chunk
), size
);
708 linear_zalloc_child(void *parent
, unsigned size
)
710 void *ptr
= linear_alloc_child(parent
, size
);
713 memset(ptr
, 0, size
);
718 linear_zalloc_parent(void *parent
, unsigned size
)
720 void *ptr
= linear_alloc_parent(parent
, size
);
723 memset(ptr
, 0, size
);
728 linear_free_parent(void *ptr
)
735 node
= LINEAR_PARENT_TO_HEADER(ptr
);
736 assert(node
->magic
== LMAGIC
);
747 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
754 node
= LINEAR_PARENT_TO_HEADER(ptr
);
755 assert(node
->magic
== LMAGIC
);
758 ralloc_steal(new_ralloc_ctx
, node
);
759 node
->ralloc_parent
= new_ralloc_ctx
;
765 ralloc_parent_of_linear_parent(void *ptr
)
767 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
768 assert(node
->magic
== LMAGIC
);
769 return node
->ralloc_parent
;
773 linear_realloc(void *parent
, void *old
, unsigned new_size
)
775 unsigned old_size
= 0;
776 ralloc_header
*new_ptr
;
778 new_ptr
= linear_alloc_child(parent
, new_size
);
783 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
785 if (likely(new_ptr
&& old_size
))
786 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
791 /* All code below is pretty much copied from ralloc and only the alloc
792 * calls are different.
796 linear_strdup(void *parent
, const char *str
)
805 ptr
= linear_alloc_child(parent
, n
+ 1);
815 linear_asprintf(void *parent
, const char *fmt
, ...)
820 ptr
= linear_vasprintf(parent
, fmt
, args
);
826 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
828 unsigned size
= printf_length(fmt
, args
) + 1;
830 char *ptr
= linear_alloc_child(parent
, size
);
832 vsnprintf(ptr
, size
, fmt
, args
);
838 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
843 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
849 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
851 size_t existing_length
;
853 existing_length
= *str
? strlen(*str
) : 0;
854 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
858 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
859 const char *fmt
, ...)
864 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
870 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
871 const char *fmt
, va_list args
)
878 if (unlikely(*str
== NULL
)) {
879 *str
= linear_vasprintf(parent
, fmt
, args
);
880 *start
= strlen(*str
);
884 new_length
= printf_length(fmt
, args
);
886 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
887 if (unlikely(ptr
== NULL
))
890 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
892 *start
+= new_length
;
896 /* helper routine for strcat/strncat - n is the exact amount to copy */
898 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
901 unsigned existing_length
;
902 assert(dest
!= NULL
&& *dest
!= NULL
);
904 existing_length
= strlen(*dest
);
905 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
906 if (unlikely(both
== NULL
))
909 memcpy(both
+ existing_length
, str
, n
);
910 both
[existing_length
+ n
] = '\0';
917 linear_strcat(void *parent
, char **dest
, const char *str
)
919 return linear_cat(parent
, dest
, str
, strlen(str
));