util: remove unneeded Android ifdef from ralloc.c
[mesa.git] / src / util / ralloc.c
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
30
31 /* Some versions of MinGW are missing _vscprintf's declaration, although they
32 * still provide the symbol in the import library. */
33 #ifdef __MINGW32__
34 _CRTIMP int _vscprintf(const char *format, va_list argptr);
35 #endif
36
37 #include "ralloc.h"
38
39 #ifndef va_copy
40 #ifdef __va_copy
41 #define va_copy(dest, src) __va_copy((dest), (src))
42 #else
43 #define va_copy(dest, src) (dest) = (src)
44 #endif
45 #endif
46
47 #define CANARY 0x5A1106
48
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
52 * ARM.
53 */
54 struct
55 #ifdef _MSC_VER
56 __declspec(align(8))
57 #elif defined(__LP64__)
58 __attribute__((aligned(16)))
59 #else
60 __attribute__((aligned(8)))
61 #endif
62 ralloc_header
63 {
64 #ifdef DEBUG
65 /* A canary value used to determine whether a pointer is ralloc'd. */
66 unsigned canary;
67 #endif
68
69 struct ralloc_header *parent;
70
71 /* The first child (head of a linked list) */
72 struct ralloc_header *child;
73
74 /* Linked list of siblings */
75 struct ralloc_header *prev;
76 struct ralloc_header *next;
77
78 void (*destructor)(void *);
79 };
80
81 typedef struct ralloc_header ralloc_header;
82
83 static void unlink_block(ralloc_header *info);
84 static void unsafe_free(ralloc_header *info);
85
86 static ralloc_header *
87 get_header(const void *ptr)
88 {
89 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
90 sizeof(ralloc_header));
91 #ifdef DEBUG
92 assert(info->canary == CANARY);
93 #endif
94 return info;
95 }
96
97 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
98
99 static void
100 add_child(ralloc_header *parent, ralloc_header *info)
101 {
102 if (parent != NULL) {
103 info->parent = parent;
104 info->next = parent->child;
105 parent->child = info;
106
107 if (info->next != NULL)
108 info->next->prev = info;
109 }
110 }
111
112 void *
113 ralloc_context(const void *ctx)
114 {
115 return ralloc_size(ctx, 0);
116 }
117
118 void *
119 ralloc_size(const void *ctx, size_t size)
120 {
121 void *block = malloc(size + sizeof(ralloc_header));
122 ralloc_header *info;
123 ralloc_header *parent;
124
125 if (unlikely(block == NULL))
126 return NULL;
127
128 info = (ralloc_header *) block;
129 /* measurements have shown that calloc is slower (because of
130 * the multiplication overflow checking?), so clear things
131 * manually
132 */
133 info->parent = NULL;
134 info->child = NULL;
135 info->prev = NULL;
136 info->next = NULL;
137 info->destructor = NULL;
138
139 parent = ctx != NULL ? get_header(ctx) : NULL;
140
141 add_child(parent, info);
142
143 #ifdef DEBUG
144 info->canary = CANARY;
145 #endif
146
147 return PTR_FROM_HEADER(info);
148 }
149
150 void *
151 rzalloc_size(const void *ctx, size_t size)
152 {
153 void *ptr = ralloc_size(ctx, size);
154
155 if (likely(ptr))
156 memset(ptr, 0, size);
157
158 return ptr;
159 }
160
161 /* helper function - assumes ptr != NULL */
162 static void *
163 resize(void *ptr, size_t size)
164 {
165 ralloc_header *child, *old, *info;
166
167 old = get_header(ptr);
168 info = realloc(old, size + sizeof(ralloc_header));
169
170 if (info == NULL)
171 return NULL;
172
173 /* Update parent and sibling's links to the reallocated node. */
174 if (info != old && info->parent != NULL) {
175 if (info->parent->child == old)
176 info->parent->child = info;
177
178 if (info->prev != NULL)
179 info->prev->next = info;
180
181 if (info->next != NULL)
182 info->next->prev = info;
183 }
184
185 /* Update child->parent links for all children */
186 for (child = info->child; child != NULL; child = child->next)
187 child->parent = info;
188
189 return PTR_FROM_HEADER(info);
190 }
191
192 void *
193 reralloc_size(const void *ctx, void *ptr, size_t size)
194 {
195 if (unlikely(ptr == NULL))
196 return ralloc_size(ctx, size);
197
198 assert(ralloc_parent(ptr) == ctx);
199 return resize(ptr, size);
200 }
201
202 void *
203 ralloc_array_size(const void *ctx, size_t size, unsigned count)
204 {
205 if (count > SIZE_MAX/size)
206 return NULL;
207
208 return ralloc_size(ctx, size * count);
209 }
210
211 void *
212 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
213 {
214 if (count > SIZE_MAX/size)
215 return NULL;
216
217 return rzalloc_size(ctx, size * count);
218 }
219
220 void *
221 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
222 {
223 if (count > SIZE_MAX/size)
224 return NULL;
225
226 return reralloc_size(ctx, ptr, size * count);
227 }
228
229 void
230 ralloc_free(void *ptr)
231 {
232 ralloc_header *info;
233
234 if (ptr == NULL)
235 return;
236
237 info = get_header(ptr);
238 unlink_block(info);
239 unsafe_free(info);
240 }
241
242 static void
243 unlink_block(ralloc_header *info)
244 {
245 /* Unlink from parent & siblings */
246 if (info->parent != NULL) {
247 if (info->parent->child == info)
248 info->parent->child = info->next;
249
250 if (info->prev != NULL)
251 info->prev->next = info->next;
252
253 if (info->next != NULL)
254 info->next->prev = info->prev;
255 }
256 info->parent = NULL;
257 info->prev = NULL;
258 info->next = NULL;
259 }
260
261 static void
262 unsafe_free(ralloc_header *info)
263 {
264 /* Recursively free any children...don't waste time unlinking them. */
265 ralloc_header *temp;
266 while (info->child != NULL) {
267 temp = info->child;
268 info->child = temp->next;
269 unsafe_free(temp);
270 }
271
272 /* Free the block itself. Call the destructor first, if any. */
273 if (info->destructor != NULL)
274 info->destructor(PTR_FROM_HEADER(info));
275
276 free(info);
277 }
278
279 void
280 ralloc_steal(const void *new_ctx, void *ptr)
281 {
282 ralloc_header *info, *parent;
283
284 if (unlikely(ptr == NULL))
285 return;
286
287 info = get_header(ptr);
288 parent = get_header(new_ctx);
289
290 unlink_block(info);
291
292 add_child(parent, info);
293 }
294
295 void
296 ralloc_adopt(const void *new_ctx, void *old_ctx)
297 {
298 ralloc_header *new_info, *old_info, *child;
299
300 if (unlikely(old_ctx == NULL))
301 return;
302
303 old_info = get_header(old_ctx);
304 new_info = get_header(new_ctx);
305
306 /* If there are no children, bail. */
307 if (unlikely(old_info->child == NULL))
308 return;
309
310 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
311 for (child = old_info->child; child->next != NULL; child = child->next) {
312 child->parent = new_info;
313 }
314
315 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
316 child->next = new_info->child;
317 child->parent = new_info;
318 new_info->child = old_info->child;
319 old_info->child = NULL;
320 }
321
322 void *
323 ralloc_parent(const void *ptr)
324 {
325 ralloc_header *info;
326
327 if (unlikely(ptr == NULL))
328 return NULL;
329
330 info = get_header(ptr);
331 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
332 }
333
334 void
335 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
336 {
337 ralloc_header *info = get_header(ptr);
338 info->destructor = destructor;
339 }
340
341 char *
342 ralloc_strdup(const void *ctx, const char *str)
343 {
344 size_t n;
345 char *ptr;
346
347 if (unlikely(str == NULL))
348 return NULL;
349
350 n = strlen(str);
351 ptr = ralloc_array(ctx, char, n + 1);
352 memcpy(ptr, str, n);
353 ptr[n] = '\0';
354 return ptr;
355 }
356
357 char *
358 ralloc_strndup(const void *ctx, const char *str, size_t max)
359 {
360 size_t n;
361 char *ptr;
362
363 if (unlikely(str == NULL))
364 return NULL;
365
366 n = strnlen(str, max);
367 ptr = ralloc_array(ctx, char, n + 1);
368 memcpy(ptr, str, n);
369 ptr[n] = '\0';
370 return ptr;
371 }
372
373 /* helper routine for strcat/strncat - n is the exact amount to copy */
374 static bool
375 cat(char **dest, const char *str, size_t n)
376 {
377 char *both;
378 size_t existing_length;
379 assert(dest != NULL && *dest != NULL);
380
381 existing_length = strlen(*dest);
382 both = resize(*dest, existing_length + n + 1);
383 if (unlikely(both == NULL))
384 return false;
385
386 memcpy(both + existing_length, str, n);
387 both[existing_length + n] = '\0';
388
389 *dest = both;
390 return true;
391 }
392
393
394 bool
395 ralloc_strcat(char **dest, const char *str)
396 {
397 return cat(dest, str, strlen(str));
398 }
399
400 bool
401 ralloc_strncat(char **dest, const char *str, size_t n)
402 {
403 return cat(dest, str, strnlen(str, n));
404 }
405
406 char *
407 ralloc_asprintf(const void *ctx, const char *fmt, ...)
408 {
409 char *ptr;
410 va_list args;
411 va_start(args, fmt);
412 ptr = ralloc_vasprintf(ctx, fmt, args);
413 va_end(args);
414 return ptr;
415 }
416
417 /* Return the length of the string that would be generated by a printf-style
418 * format and argument list, not including the \0 byte.
419 */
420 static size_t
421 printf_length(const char *fmt, va_list untouched_args)
422 {
423 int size;
424 char junk;
425
426 /* Make a copy of the va_list so the original caller can still use it */
427 va_list args;
428 va_copy(args, untouched_args);
429
430 #ifdef _WIN32
431 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
432 * if the number of characters to write is greater than count.
433 */
434 size = _vscprintf(fmt, args);
435 (void)junk;
436 #else
437 size = vsnprintf(&junk, 1, fmt, args);
438 #endif
439 assert(size >= 0);
440
441 va_end(args);
442
443 return size;
444 }
445
446 char *
447 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
448 {
449 size_t size = printf_length(fmt, args) + 1;
450
451 char *ptr = ralloc_size(ctx, size);
452 if (ptr != NULL)
453 vsnprintf(ptr, size, fmt, args);
454
455 return ptr;
456 }
457
458 bool
459 ralloc_asprintf_append(char **str, const char *fmt, ...)
460 {
461 bool success;
462 va_list args;
463 va_start(args, fmt);
464 success = ralloc_vasprintf_append(str, fmt, args);
465 va_end(args);
466 return success;
467 }
468
469 bool
470 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
471 {
472 size_t existing_length;
473 assert(str != NULL);
474 existing_length = *str ? strlen(*str) : 0;
475 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
476 }
477
478 bool
479 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
480 {
481 bool success;
482 va_list args;
483 va_start(args, fmt);
484 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
485 va_end(args);
486 return success;
487 }
488
489 bool
490 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
491 va_list args)
492 {
493 size_t new_length;
494 char *ptr;
495
496 assert(str != NULL);
497
498 if (unlikely(*str == NULL)) {
499 // Assuming a NULL context is probably bad, but it's expected behavior.
500 *str = ralloc_vasprintf(NULL, fmt, args);
501 *start = strlen(*str);
502 return true;
503 }
504
505 new_length = printf_length(fmt, args);
506
507 ptr = resize(*str, *start + new_length + 1);
508 if (unlikely(ptr == NULL))
509 return false;
510
511 vsnprintf(ptr + *start, new_length + 1, fmt, args);
512 *str = ptr;
513 *start += new_length;
514 return true;
515 }
516
517 /***************************************************************************
518 * Linear allocator for short-lived allocations.
519 ***************************************************************************
520 *
521 * The allocator consists of a parent node (2K buffer), which requires
522 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
523 * directly, because the parent doesn't track them. You have to release
524 * the parent node in order to release all its children.
525 *
526 * The allocator uses a fixed-sized buffer with a monotonically increasing
527 * offset after each allocation. If the buffer is all used, another buffer
528 * is allocated, sharing the same ralloc parent, so all buffers are at
529 * the same level in the ralloc hierarchy.
530 *
531 * The linear parent node is always the first buffer and keeps track of all
532 * other buffers.
533 */
534
535 #define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1))
536
537 #define MIN_LINEAR_BUFSIZE 2048
538 #define SUBALLOC_ALIGNMENT sizeof(uintptr_t)
539 #define LMAGIC 0x87b9c7d3
540
541 struct linear_header {
542 #ifdef DEBUG
543 unsigned magic; /* for debugging */
544 #endif
545 unsigned offset; /* points to the first unused byte in the buffer */
546 unsigned size; /* size of the buffer */
547 void *ralloc_parent; /* new buffers will use this */
548 struct linear_header *next; /* next buffer if we have more */
549 struct linear_header *latest; /* the only buffer that has free space */
550
551 /* After this structure, the buffer begins.
552 * Each suballocation consists of linear_size_chunk as its header followed
553 * by the suballocation, so it goes:
554 *
555 * - linear_size_chunk
556 * - allocated space
557 * - linear_size_chunk
558 * - allocated space
559 * etc.
560 *
561 * linear_size_chunk is only needed by linear_realloc.
562 */
563 };
564
565 struct linear_size_chunk {
566 unsigned size; /* for realloc */
567 unsigned _padding;
568 };
569
570 typedef struct linear_header linear_header;
571 typedef struct linear_size_chunk linear_size_chunk;
572
573 #define LINEAR_PARENT_TO_HEADER(parent) \
574 (linear_header*) \
575 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
576
577 /* Allocate the linear buffer with its header. */
578 static linear_header *
579 create_linear_node(void *ralloc_ctx, unsigned min_size)
580 {
581 linear_header *node;
582
583 min_size += sizeof(linear_size_chunk);
584
585 if (likely(min_size < MIN_LINEAR_BUFSIZE))
586 min_size = MIN_LINEAR_BUFSIZE;
587
588 node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size);
589 if (unlikely(!node))
590 return NULL;
591
592 #ifdef DEBUG
593 node->magic = LMAGIC;
594 #endif
595 node->offset = 0;
596 node->size = min_size;
597 node->ralloc_parent = ralloc_ctx;
598 node->next = NULL;
599 node->latest = node;
600 return node;
601 }
602
603 void *
604 linear_alloc_child(void *parent, unsigned size)
605 {
606 linear_header *first = LINEAR_PARENT_TO_HEADER(parent);
607 linear_header *latest = first->latest;
608 linear_header *new_node;
609 linear_size_chunk *ptr;
610 unsigned full_size;
611
612 assert(first->magic == LMAGIC);
613 assert(!latest->next);
614
615 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
616 full_size = sizeof(linear_size_chunk) + size;
617
618 if (unlikely(latest->offset + full_size > latest->size)) {
619 /* allocate a new node */
620 new_node = create_linear_node(latest->ralloc_parent, size);
621 if (unlikely(!new_node))
622 return NULL;
623
624 first->latest = new_node;
625 latest->latest = new_node;
626 latest->next = new_node;
627 latest = new_node;
628 }
629
630 ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset);
631 ptr->size = size;
632 latest->offset += full_size;
633 return &ptr[1];
634 }
635
636 void *
637 linear_alloc_parent(void *ralloc_ctx, unsigned size)
638 {
639 linear_header *node;
640
641 if (unlikely(!ralloc_ctx))
642 return NULL;
643
644 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
645
646 node = create_linear_node(ralloc_ctx, size);
647 if (unlikely(!node))
648 return NULL;
649
650 return linear_alloc_child((char*)node +
651 sizeof(linear_header) +
652 sizeof(linear_size_chunk), size);
653 }
654
655 void *
656 linear_zalloc_child(void *parent, unsigned size)
657 {
658 void *ptr = linear_alloc_child(parent, size);
659
660 if (likely(ptr))
661 memset(ptr, 0, size);
662 return ptr;
663 }
664
665 void *
666 linear_zalloc_parent(void *parent, unsigned size)
667 {
668 void *ptr = linear_alloc_parent(parent, size);
669
670 if (likely(ptr))
671 memset(ptr, 0, size);
672 return ptr;
673 }
674
675 void
676 linear_free_parent(void *ptr)
677 {
678 linear_header *node;
679
680 if (unlikely(!ptr))
681 return;
682
683 node = LINEAR_PARENT_TO_HEADER(ptr);
684 assert(node->magic == LMAGIC);
685
686 while (node) {
687 void *ptr = node;
688
689 node = node->next;
690 ralloc_free(ptr);
691 }
692 }
693
694 void
695 ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
696 {
697 linear_header *node;
698
699 if (unlikely(!ptr))
700 return;
701
702 node = LINEAR_PARENT_TO_HEADER(ptr);
703 assert(node->magic == LMAGIC);
704
705 while (node) {
706 ralloc_steal(new_ralloc_ctx, node);
707 node->ralloc_parent = new_ralloc_ctx;
708 node = node->next;
709 }
710 }
711
712 void *
713 ralloc_parent_of_linear_parent(void *ptr)
714 {
715 linear_header *node = LINEAR_PARENT_TO_HEADER(ptr);
716 assert(node->magic == LMAGIC);
717 return node->ralloc_parent;
718 }
719
720 void *
721 linear_realloc(void *parent, void *old, unsigned new_size)
722 {
723 unsigned old_size = 0;
724 ralloc_header *new_ptr;
725
726 new_ptr = linear_alloc_child(parent, new_size);
727
728 if (unlikely(!old))
729 return new_ptr;
730
731 old_size = ((linear_size_chunk*)old)[-1].size;
732
733 if (likely(new_ptr && old_size))
734 memcpy(new_ptr, old, MIN2(old_size, new_size));
735
736 return new_ptr;
737 }
738
739 /* All code below is pretty much copied from ralloc and only the alloc
740 * calls are different.
741 */
742
743 char *
744 linear_strdup(void *parent, const char *str)
745 {
746 unsigned n;
747 char *ptr;
748
749 if (unlikely(!str))
750 return NULL;
751
752 n = strlen(str);
753 ptr = linear_alloc_child(parent, n + 1);
754 if (unlikely(!ptr))
755 return NULL;
756
757 memcpy(ptr, str, n);
758 ptr[n] = '\0';
759 return ptr;
760 }
761
762 char *
763 linear_asprintf(void *parent, const char *fmt, ...)
764 {
765 char *ptr;
766 va_list args;
767 va_start(args, fmt);
768 ptr = linear_vasprintf(parent, fmt, args);
769 va_end(args);
770 return ptr;
771 }
772
773 char *
774 linear_vasprintf(void *parent, const char *fmt, va_list args)
775 {
776 unsigned size = printf_length(fmt, args) + 1;
777
778 char *ptr = linear_alloc_child(parent, size);
779 if (ptr != NULL)
780 vsnprintf(ptr, size, fmt, args);
781
782 return ptr;
783 }
784
785 bool
786 linear_asprintf_append(void *parent, char **str, const char *fmt, ...)
787 {
788 bool success;
789 va_list args;
790 va_start(args, fmt);
791 success = linear_vasprintf_append(parent, str, fmt, args);
792 va_end(args);
793 return success;
794 }
795
796 bool
797 linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args)
798 {
799 size_t existing_length;
800 assert(str != NULL);
801 existing_length = *str ? strlen(*str) : 0;
802 return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args);
803 }
804
805 bool
806 linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
807 const char *fmt, ...)
808 {
809 bool success;
810 va_list args;
811 va_start(args, fmt);
812 success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args);
813 va_end(args);
814 return success;
815 }
816
817 bool
818 linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
819 const char *fmt, va_list args)
820 {
821 size_t new_length;
822 char *ptr;
823
824 assert(str != NULL);
825
826 if (unlikely(*str == NULL)) {
827 *str = linear_vasprintf(parent, fmt, args);
828 *start = strlen(*str);
829 return true;
830 }
831
832 new_length = printf_length(fmt, args);
833
834 ptr = linear_realloc(parent, *str, *start + new_length + 1);
835 if (unlikely(ptr == NULL))
836 return false;
837
838 vsnprintf(ptr + *start, new_length + 1, fmt, args);
839 *str = ptr;
840 *start += new_length;
841 return true;
842 }
843
844 /* helper routine for strcat/strncat - n is the exact amount to copy */
845 static bool
846 linear_cat(void *parent, char **dest, const char *str, unsigned n)
847 {
848 char *both;
849 unsigned existing_length;
850 assert(dest != NULL && *dest != NULL);
851
852 existing_length = strlen(*dest);
853 both = linear_realloc(parent, *dest, existing_length + n + 1);
854 if (unlikely(both == NULL))
855 return false;
856
857 memcpy(both + existing_length, str, n);
858 both[existing_length + n] = '\0';
859
860 *dest = both;
861 return true;
862 }
863
864 bool
865 linear_strcat(void *parent, char **dest, const char *str)
866 {
867 return linear_cat(parent, dest, str, strlen(str));
868 }