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
57 /* A canary value used to determine whether a pointer is ralloc'd. */
61 struct ralloc_header
*parent
;
63 /* The first child (head of a linked list) */
64 struct ralloc_header
*child
;
66 /* Linked list of siblings */
67 struct ralloc_header
*prev
;
68 struct ralloc_header
*next
;
70 void (*destructor
)(void *);
73 typedef struct ralloc_header ralloc_header
;
75 static void unlink_block(ralloc_header
*info
);
76 static void unsafe_free(ralloc_header
*info
);
78 static ralloc_header
*
79 get_header(const void *ptr
)
81 ralloc_header
*info
= (ralloc_header
*) (((char *) ptr
) -
82 sizeof(ralloc_header
));
84 assert(info
->canary
== CANARY
);
89 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
92 add_child(ralloc_header
*parent
, ralloc_header
*info
)
95 info
->parent
= parent
;
96 info
->next
= parent
->child
;
99 if (info
->next
!= NULL
)
100 info
->next
->prev
= info
;
105 ralloc_context(const void *ctx
)
107 return ralloc_size(ctx
, 0);
111 ralloc_size(const void *ctx
, size_t size
)
113 void *block
= malloc(size
+ sizeof(ralloc_header
));
115 ralloc_header
*parent
;
117 if (unlikely(block
== NULL
))
120 info
= (ralloc_header
*) block
;
121 /* measurements have shown that calloc is slower (because of
122 * the multiplication overflow checking?), so clear things
129 info
->destructor
= NULL
;
131 parent
= ctx
!= NULL
? get_header(ctx
) : NULL
;
133 add_child(parent
, info
);
136 info
->canary
= CANARY
;
139 return PTR_FROM_HEADER(info
);
143 rzalloc_size(const void *ctx
, size_t size
)
145 void *ptr
= ralloc_size(ctx
, size
);
148 memset(ptr
, 0, size
);
153 /* helper function - assumes ptr != NULL */
155 resize(void *ptr
, size_t size
)
157 ralloc_header
*child
, *old
, *info
;
159 old
= get_header(ptr
);
160 info
= realloc(old
, size
+ sizeof(ralloc_header
));
165 /* Update parent and sibling's links to the reallocated node. */
166 if (info
!= old
&& info
->parent
!= NULL
) {
167 if (info
->parent
->child
== old
)
168 info
->parent
->child
= info
;
170 if (info
->prev
!= NULL
)
171 info
->prev
->next
= info
;
173 if (info
->next
!= NULL
)
174 info
->next
->prev
= info
;
177 /* Update child->parent links for all children */
178 for (child
= info
->child
; child
!= NULL
; child
= child
->next
)
179 child
->parent
= info
;
181 return PTR_FROM_HEADER(info
);
185 reralloc_size(const void *ctx
, void *ptr
, size_t size
)
187 if (unlikely(ptr
== NULL
))
188 return ralloc_size(ctx
, size
);
190 assert(ralloc_parent(ptr
) == ctx
);
191 return resize(ptr
, size
);
195 ralloc_array_size(const void *ctx
, size_t size
, unsigned count
)
197 if (count
> SIZE_MAX
/size
)
200 return ralloc_size(ctx
, size
* count
);
204 rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
)
206 if (count
> SIZE_MAX
/size
)
209 return rzalloc_size(ctx
, size
* count
);
213 reralloc_array_size(const void *ctx
, void *ptr
, size_t size
, unsigned count
)
215 if (count
> SIZE_MAX
/size
)
218 return reralloc_size(ctx
, ptr
, size
* count
);
222 ralloc_free(void *ptr
)
229 info
= get_header(ptr
);
235 unlink_block(ralloc_header
*info
)
237 /* Unlink from parent & siblings */
238 if (info
->parent
!= NULL
) {
239 if (info
->parent
->child
== info
)
240 info
->parent
->child
= info
->next
;
242 if (info
->prev
!= NULL
)
243 info
->prev
->next
= info
->next
;
245 if (info
->next
!= NULL
)
246 info
->next
->prev
= info
->prev
;
254 unsafe_free(ralloc_header
*info
)
256 /* Recursively free any children...don't waste time unlinking them. */
258 while (info
->child
!= NULL
) {
260 info
->child
= temp
->next
;
264 /* Free the block itself. Call the destructor first, if any. */
265 if (info
->destructor
!= NULL
)
266 info
->destructor(PTR_FROM_HEADER(info
));
272 ralloc_steal(const void *new_ctx
, void *ptr
)
274 ralloc_header
*info
, *parent
;
276 if (unlikely(ptr
== NULL
))
279 info
= get_header(ptr
);
280 parent
= get_header(new_ctx
);
284 add_child(parent
, info
);
288 ralloc_adopt(const void *new_ctx
, void *old_ctx
)
290 ralloc_header
*new_info
, *old_info
, *child
;
292 if (unlikely(old_ctx
== NULL
))
295 old_info
= get_header(old_ctx
);
296 new_info
= get_header(new_ctx
);
298 /* If there are no children, bail. */
299 if (unlikely(old_info
->child
== NULL
))
302 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
303 for (child
= old_info
->child
; child
->next
!= NULL
; child
= child
->next
) {
304 child
->parent
= new_info
;
307 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
308 child
->next
= new_info
->child
;
309 child
->parent
= new_info
;
310 new_info
->child
= old_info
->child
;
311 old_info
->child
= NULL
;
315 ralloc_parent(const void *ptr
)
319 if (unlikely(ptr
== NULL
))
322 info
= get_header(ptr
);
323 return info
->parent
? PTR_FROM_HEADER(info
->parent
) : NULL
;
326 static void *autofree_context
= NULL
;
331 ralloc_free(autofree_context
);
335 ralloc_autofree_context(void)
337 if (unlikely(autofree_context
== NULL
)) {
338 autofree_context
= ralloc_context(NULL
);
341 return autofree_context
;
345 ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *))
347 ralloc_header
*info
= get_header(ptr
);
348 info
->destructor
= destructor
;
352 ralloc_strdup(const void *ctx
, const char *str
)
357 if (unlikely(str
== NULL
))
361 ptr
= ralloc_array(ctx
, char, n
+ 1);
368 ralloc_strndup(const void *ctx
, const char *str
, size_t max
)
373 if (unlikely(str
== NULL
))
376 n
= strnlen(str
, max
);
377 ptr
= ralloc_array(ctx
, char, n
+ 1);
383 /* helper routine for strcat/strncat - n is the exact amount to copy */
385 cat(char **dest
, const char *str
, size_t n
)
388 size_t existing_length
;
389 assert(dest
!= NULL
&& *dest
!= NULL
);
391 existing_length
= strlen(*dest
);
392 both
= resize(*dest
, existing_length
+ n
+ 1);
393 if (unlikely(both
== NULL
))
396 memcpy(both
+ existing_length
, str
, n
);
397 both
[existing_length
+ n
] = '\0';
405 ralloc_strcat(char **dest
, const char *str
)
407 return cat(dest
, str
, strlen(str
));
411 ralloc_strncat(char **dest
, const char *str
, size_t n
)
413 /* Clamp n to the string length */
414 size_t str_length
= strlen(str
);
418 return cat(dest
, str
, n
);
422 ralloc_asprintf(const void *ctx
, const char *fmt
, ...)
427 ptr
= ralloc_vasprintf(ctx
, fmt
, args
);
432 /* Return the length of the string that would be generated by a printf-style
433 * format and argument list, not including the \0 byte.
436 printf_length(const char *fmt
, va_list untouched_args
)
441 /* Make a copy of the va_list so the original caller can still use it */
443 va_copy(args
, untouched_args
);
446 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
447 * if the number of characters to write is greater than count.
449 size
= _vscprintf(fmt
, args
);
452 size
= vsnprintf(&junk
, 1, fmt
, args
);
462 ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
)
464 size_t size
= printf_length(fmt
, args
) + 1;
466 char *ptr
= ralloc_size(ctx
, size
);
468 vsnprintf(ptr
, size
, fmt
, args
);
474 ralloc_asprintf_append(char **str
, const char *fmt
, ...)
479 success
= ralloc_vasprintf_append(str
, fmt
, args
);
485 ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
)
487 size_t existing_length
;
489 existing_length
= *str
? strlen(*str
) : 0;
490 return ralloc_vasprintf_rewrite_tail(str
, &existing_length
, fmt
, args
);
494 ralloc_asprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
, ...)
499 success
= ralloc_vasprintf_rewrite_tail(str
, start
, fmt
, args
);
505 ralloc_vasprintf_rewrite_tail(char **str
, size_t *start
, const char *fmt
,
513 if (unlikely(*str
== NULL
)) {
514 // Assuming a NULL context is probably bad, but it's expected behavior.
515 *str
= ralloc_vasprintf(NULL
, fmt
, args
);
516 *start
= strlen(*str
);
520 new_length
= printf_length(fmt
, args
);
522 ptr
= resize(*str
, *start
+ new_length
+ 1);
523 if (unlikely(ptr
== NULL
))
526 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
528 *start
+= new_length
;
532 /***************************************************************************
533 * Linear allocator for short-lived allocations.
534 ***************************************************************************
536 * The allocator consists of a parent node (2K buffer), which requires
537 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
538 * directly, because the parent doesn't track them. You have to release
539 * the parent node in order to release all its children.
541 * The allocator uses a fixed-sized buffer with a monotonically increasing
542 * offset after each allocation. If the buffer is all used, another buffer
543 * is allocated, sharing the same ralloc parent, so all buffers are at
544 * the same level in the ralloc hierarchy.
546 * The linear parent node is always the first buffer and keeps track of all
550 #define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1))
552 #define MIN_LINEAR_BUFSIZE 2048
553 #define SUBALLOC_ALIGNMENT sizeof(uintptr_t)
554 #define LMAGIC 0x87b9c7d3
556 struct linear_header
{
558 unsigned magic
; /* for debugging */
560 unsigned offset
; /* points to the first unused byte in the buffer */
561 unsigned size
; /* size of the buffer */
562 void *ralloc_parent
; /* new buffers will use this */
563 struct linear_header
*next
; /* next buffer if we have more */
564 struct linear_header
*latest
; /* the only buffer that has free space */
566 /* After this structure, the buffer begins.
567 * Each suballocation consists of linear_size_chunk as its header followed
568 * by the suballocation, so it goes:
570 * - linear_size_chunk
572 * - linear_size_chunk
576 * linear_size_chunk is only needed by linear_realloc.
580 struct linear_size_chunk
{
581 unsigned size
; /* for realloc */
585 typedef struct linear_header linear_header
;
586 typedef struct linear_size_chunk linear_size_chunk
;
588 #define LINEAR_PARENT_TO_HEADER(parent) \
590 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
592 /* Allocate the linear buffer with its header. */
593 static linear_header
*
594 create_linear_node(void *ralloc_ctx
, unsigned min_size
)
598 min_size
+= sizeof(linear_size_chunk
);
600 if (likely(min_size
< MIN_LINEAR_BUFSIZE
))
601 min_size
= MIN_LINEAR_BUFSIZE
;
603 node
= ralloc_size(ralloc_ctx
, sizeof(linear_header
) + min_size
);
608 node
->magic
= LMAGIC
;
611 node
->size
= min_size
;
612 node
->ralloc_parent
= ralloc_ctx
;
619 linear_alloc_child(void *parent
, unsigned size
)
621 linear_header
*first
= LINEAR_PARENT_TO_HEADER(parent
);
622 linear_header
*latest
= first
->latest
;
623 linear_header
*new_node
;
624 linear_size_chunk
*ptr
;
627 assert(first
->magic
== LMAGIC
);
628 assert(!latest
->next
);
630 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
631 full_size
= sizeof(linear_size_chunk
) + size
;
633 if (unlikely(latest
->offset
+ full_size
> latest
->size
)) {
634 /* allocate a new node */
635 new_node
= create_linear_node(latest
->ralloc_parent
, size
);
636 if (unlikely(!new_node
))
639 first
->latest
= new_node
;
640 latest
->latest
= new_node
;
641 latest
->next
= new_node
;
645 ptr
= (linear_size_chunk
*)((char*)&latest
[1] + latest
->offset
);
647 latest
->offset
+= full_size
;
652 linear_alloc_parent(void *ralloc_ctx
, unsigned size
)
656 if (unlikely(!ralloc_ctx
))
659 size
= ALIGN_POT(size
, SUBALLOC_ALIGNMENT
);
661 node
= create_linear_node(ralloc_ctx
, size
);
665 return linear_alloc_child((char*)node
+
666 sizeof(linear_header
) +
667 sizeof(linear_size_chunk
), size
);
671 linear_zalloc_child(void *parent
, unsigned size
)
673 void *ptr
= linear_alloc_child(parent
, size
);
676 memset(ptr
, 0, size
);
681 linear_zalloc_parent(void *parent
, unsigned size
)
683 void *ptr
= linear_alloc_parent(parent
, size
);
686 memset(ptr
, 0, size
);
691 linear_free_parent(void *ptr
)
698 node
= LINEAR_PARENT_TO_HEADER(ptr
);
699 assert(node
->magic
== LMAGIC
);
710 ralloc_steal_linear_parent(void *new_ralloc_ctx
, void *ptr
)
717 node
= LINEAR_PARENT_TO_HEADER(ptr
);
718 assert(node
->magic
== LMAGIC
);
721 ralloc_steal(new_ralloc_ctx
, node
);
722 node
->ralloc_parent
= new_ralloc_ctx
;
728 ralloc_parent_of_linear_parent(void *ptr
)
730 linear_header
*node
= LINEAR_PARENT_TO_HEADER(ptr
);
731 assert(node
->magic
== LMAGIC
);
732 return node
->ralloc_parent
;
736 linear_realloc(void *parent
, void *old
, unsigned new_size
)
738 unsigned old_size
= 0;
739 ralloc_header
*new_ptr
;
741 new_ptr
= linear_alloc_child(parent
, new_size
);
746 old_size
= ((linear_size_chunk
*)old
)[-1].size
;
748 if (likely(new_ptr
&& old_size
))
749 memcpy(new_ptr
, old
, MIN2(old_size
, new_size
));
754 /* All code below is pretty much copied from ralloc and only the alloc
755 * calls are different.
759 linear_strdup(void *parent
, const char *str
)
768 ptr
= linear_alloc_child(parent
, n
+ 1);
778 linear_asprintf(void *parent
, const char *fmt
, ...)
783 ptr
= linear_vasprintf(parent
, fmt
, args
);
789 linear_vasprintf(void *parent
, const char *fmt
, va_list args
)
791 unsigned size
= printf_length(fmt
, args
) + 1;
793 char *ptr
= linear_alloc_child(parent
, size
);
795 vsnprintf(ptr
, size
, fmt
, args
);
801 linear_asprintf_append(void *parent
, char **str
, const char *fmt
, ...)
806 success
= linear_vasprintf_append(parent
, str
, fmt
, args
);
812 linear_vasprintf_append(void *parent
, char **str
, const char *fmt
, va_list args
)
814 size_t existing_length
;
816 existing_length
= *str
? strlen(*str
) : 0;
817 return linear_vasprintf_rewrite_tail(parent
, str
, &existing_length
, fmt
, args
);
821 linear_asprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
822 const char *fmt
, ...)
827 success
= linear_vasprintf_rewrite_tail(parent
, str
, start
, fmt
, args
);
833 linear_vasprintf_rewrite_tail(void *parent
, char **str
, size_t *start
,
834 const char *fmt
, va_list args
)
841 if (unlikely(*str
== NULL
)) {
842 *str
= linear_vasprintf(parent
, fmt
, args
);
843 *start
= strlen(*str
);
847 new_length
= printf_length(fmt
, args
);
849 ptr
= linear_realloc(parent
, *str
, *start
+ new_length
+ 1);
850 if (unlikely(ptr
== NULL
))
853 vsnprintf(ptr
+ *start
, new_length
+ 1, fmt
, args
);
855 *start
+= new_length
;
859 /* helper routine for strcat/strncat - n is the exact amount to copy */
861 linear_cat(void *parent
, char **dest
, const char *str
, unsigned n
)
864 unsigned existing_length
;
865 assert(dest
!= NULL
&& *dest
!= NULL
);
867 existing_length
= strlen(*dest
);
868 both
= linear_realloc(parent
, *dest
, existing_length
+ n
+ 1);
869 if (unlikely(both
== NULL
))
872 memcpy(both
+ existing_length
, str
, n
);
873 both
[existing_length
+ n
] = '\0';
880 linear_strcat(void *parent
, char **dest
, const char *str
)
882 return linear_cat(parent
, dest
, str
, strlen(str
));