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