vulkan: add vk_x11_strict_image_count option
[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 #ifndef NDEBUG
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 assert(info->canary == CANARY);
92 return info;
93 }
94
95 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
96
97 static void
98 add_child(ralloc_header *parent, ralloc_header *info)
99 {
100 if (parent != NULL) {
101 info->parent = parent;
102 info->next = parent->child;
103 parent->child = info;
104
105 if (info->next != NULL)
106 info->next->prev = info;
107 }
108 }
109
110 void *
111 ralloc_context(const void *ctx)
112 {
113 return ralloc_size(ctx, 0);
114 }
115
116 void *
117 ralloc_size(const void *ctx, size_t size)
118 {
119 void *block = malloc(size + sizeof(ralloc_header));
120 ralloc_header *info;
121 ralloc_header *parent;
122
123 if (unlikely(block == NULL))
124 return NULL;
125
126 info = (ralloc_header *) block;
127 /* measurements have shown that calloc is slower (because of
128 * the multiplication overflow checking?), so clear things
129 * manually
130 */
131 info->parent = NULL;
132 info->child = NULL;
133 info->prev = NULL;
134 info->next = NULL;
135 info->destructor = NULL;
136
137 parent = ctx != NULL ? get_header(ctx) : NULL;
138
139 add_child(parent, info);
140
141 #ifndef NDEBUG
142 info->canary = CANARY;
143 #endif
144
145 return PTR_FROM_HEADER(info);
146 }
147
148 void *
149 rzalloc_size(const void *ctx, size_t size)
150 {
151 void *ptr = ralloc_size(ctx, size);
152
153 if (likely(ptr))
154 memset(ptr, 0, size);
155
156 return ptr;
157 }
158
159 /* helper function - assumes ptr != NULL */
160 static void *
161 resize(void *ptr, size_t size)
162 {
163 ralloc_header *child, *old, *info;
164
165 old = get_header(ptr);
166 info = realloc(old, size + sizeof(ralloc_header));
167
168 if (info == NULL)
169 return NULL;
170
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;
175
176 if (info->prev != NULL)
177 info->prev->next = info;
178
179 if (info->next != NULL)
180 info->next->prev = info;
181 }
182
183 /* Update child->parent links for all children */
184 for (child = info->child; child != NULL; child = child->next)
185 child->parent = info;
186
187 return PTR_FROM_HEADER(info);
188 }
189
190 void *
191 reralloc_size(const void *ctx, void *ptr, size_t size)
192 {
193 if (unlikely(ptr == NULL))
194 return ralloc_size(ctx, size);
195
196 assert(ralloc_parent(ptr) == ctx);
197 return resize(ptr, size);
198 }
199
200 void *
201 rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size)
202 {
203 if (unlikely(ptr == NULL))
204 return rzalloc_size(ctx, new_size);
205
206 assert(ralloc_parent(ptr) == ctx);
207 ptr = resize(ptr, new_size);
208
209 if (new_size > old_size)
210 memset((char *)ptr + old_size, 0, new_size - old_size);
211
212 return ptr;
213 }
214
215 void *
216 ralloc_array_size(const void *ctx, size_t size, unsigned count)
217 {
218 if (count > SIZE_MAX/size)
219 return NULL;
220
221 return ralloc_size(ctx, size * count);
222 }
223
224 void *
225 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
226 {
227 if (count > SIZE_MAX/size)
228 return NULL;
229
230 return rzalloc_size(ctx, size * count);
231 }
232
233 void *
234 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
235 {
236 if (count > SIZE_MAX/size)
237 return NULL;
238
239 return reralloc_size(ctx, ptr, size * count);
240 }
241
242 void *
243 rerzalloc_array_size(const void *ctx, void *ptr, size_t size,
244 unsigned old_count, unsigned new_count)
245 {
246 if (new_count > SIZE_MAX/size)
247 return NULL;
248
249 return rerzalloc_size(ctx, ptr, size * old_count, size * new_count);
250 }
251
252 void
253 ralloc_free(void *ptr)
254 {
255 ralloc_header *info;
256
257 if (ptr == NULL)
258 return;
259
260 info = get_header(ptr);
261 unlink_block(info);
262 unsafe_free(info);
263 }
264
265 static void
266 unlink_block(ralloc_header *info)
267 {
268 /* Unlink from parent & siblings */
269 if (info->parent != NULL) {
270 if (info->parent->child == info)
271 info->parent->child = info->next;
272
273 if (info->prev != NULL)
274 info->prev->next = info->next;
275
276 if (info->next != NULL)
277 info->next->prev = info->prev;
278 }
279 info->parent = NULL;
280 info->prev = NULL;
281 info->next = NULL;
282 }
283
284 static void
285 unsafe_free(ralloc_header *info)
286 {
287 /* Recursively free any children...don't waste time unlinking them. */
288 ralloc_header *temp;
289 while (info->child != NULL) {
290 temp = info->child;
291 info->child = temp->next;
292 unsafe_free(temp);
293 }
294
295 /* Free the block itself. Call the destructor first, if any. */
296 if (info->destructor != NULL)
297 info->destructor(PTR_FROM_HEADER(info));
298
299 free(info);
300 }
301
302 void
303 ralloc_steal(const void *new_ctx, void *ptr)
304 {
305 ralloc_header *info, *parent;
306
307 if (unlikely(ptr == NULL))
308 return;
309
310 info = get_header(ptr);
311 parent = new_ctx ? get_header(new_ctx) : NULL;
312
313 unlink_block(info);
314
315 add_child(parent, info);
316 }
317
318 void
319 ralloc_adopt(const void *new_ctx, void *old_ctx)
320 {
321 ralloc_header *new_info, *old_info, *child;
322
323 if (unlikely(old_ctx == NULL))
324 return;
325
326 old_info = get_header(old_ctx);
327 new_info = get_header(new_ctx);
328
329 /* If there are no children, bail. */
330 if (unlikely(old_info->child == NULL))
331 return;
332
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;
336 }
337 child->parent = new_info;
338
339 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
340 child->next = new_info->child;
341 if (child->next)
342 child->next->prev = child;
343 new_info->child = old_info->child;
344 old_info->child = NULL;
345 }
346
347 void *
348 ralloc_parent(const void *ptr)
349 {
350 ralloc_header *info;
351
352 if (unlikely(ptr == NULL))
353 return NULL;
354
355 info = get_header(ptr);
356 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
357 }
358
359 void
360 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
361 {
362 ralloc_header *info = get_header(ptr);
363 info->destructor = destructor;
364 }
365
366 char *
367 ralloc_strdup(const void *ctx, const char *str)
368 {
369 size_t n;
370 char *ptr;
371
372 if (unlikely(str == NULL))
373 return NULL;
374
375 n = strlen(str);
376 ptr = ralloc_array(ctx, char, n + 1);
377 memcpy(ptr, str, n);
378 ptr[n] = '\0';
379 return ptr;
380 }
381
382 char *
383 ralloc_strndup(const void *ctx, const char *str, size_t max)
384 {
385 size_t n;
386 char *ptr;
387
388 if (unlikely(str == NULL))
389 return NULL;
390
391 n = strnlen(str, max);
392 ptr = ralloc_array(ctx, char, n + 1);
393 memcpy(ptr, str, n);
394 ptr[n] = '\0';
395 return ptr;
396 }
397
398 /* helper routine for strcat/strncat - n is the exact amount to copy */
399 static bool
400 cat(char **dest, const char *str, size_t n)
401 {
402 char *both;
403 size_t existing_length;
404 assert(dest != NULL && *dest != NULL);
405
406 existing_length = strlen(*dest);
407 both = resize(*dest, existing_length + n + 1);
408 if (unlikely(both == NULL))
409 return false;
410
411 memcpy(both + existing_length, str, n);
412 both[existing_length + n] = '\0';
413
414 *dest = both;
415 return true;
416 }
417
418
419 bool
420 ralloc_strcat(char **dest, const char *str)
421 {
422 return cat(dest, str, strlen(str));
423 }
424
425 bool
426 ralloc_strncat(char **dest, const char *str, size_t n)
427 {
428 return cat(dest, str, strnlen(str, n));
429 }
430
431 bool
432 ralloc_str_append(char **dest, const char *str,
433 size_t existing_length, size_t str_size)
434 {
435 char *both;
436 assert(dest != NULL && *dest != NULL);
437
438 both = resize(*dest, existing_length + str_size + 1);
439 if (unlikely(both == NULL))
440 return false;
441
442 memcpy(both + existing_length, str, str_size);
443 both[existing_length + str_size] = '\0';
444
445 *dest = both;
446
447 return true;
448 }
449
450 char *
451 ralloc_asprintf(const void *ctx, const char *fmt, ...)
452 {
453 char *ptr;
454 va_list args;
455 va_start(args, fmt);
456 ptr = ralloc_vasprintf(ctx, fmt, args);
457 va_end(args);
458 return ptr;
459 }
460
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.
463 */
464 static size_t
465 printf_length(const char *fmt, va_list untouched_args)
466 {
467 int size;
468 char junk;
469
470 /* Make a copy of the va_list so the original caller can still use it */
471 va_list args;
472 va_copy(args, untouched_args);
473
474 #ifdef _WIN32
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.
477 */
478 size = _vscprintf(fmt, args);
479 (void)junk;
480 #else
481 size = vsnprintf(&junk, 1, fmt, args);
482 #endif
483 assert(size >= 0);
484
485 va_end(args);
486
487 return size;
488 }
489
490 char *
491 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
492 {
493 size_t size = printf_length(fmt, args) + 1;
494
495 char *ptr = ralloc_size(ctx, size);
496 if (ptr != NULL)
497 vsnprintf(ptr, size, fmt, args);
498
499 return ptr;
500 }
501
502 bool
503 ralloc_asprintf_append(char **str, const char *fmt, ...)
504 {
505 bool success;
506 va_list args;
507 va_start(args, fmt);
508 success = ralloc_vasprintf_append(str, fmt, args);
509 va_end(args);
510 return success;
511 }
512
513 bool
514 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
515 {
516 size_t existing_length;
517 assert(str != NULL);
518 existing_length = *str ? strlen(*str) : 0;
519 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
520 }
521
522 bool
523 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
524 {
525 bool success;
526 va_list args;
527 va_start(args, fmt);
528 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
529 va_end(args);
530 return success;
531 }
532
533 bool
534 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
535 va_list args)
536 {
537 size_t new_length;
538 char *ptr;
539
540 assert(str != NULL);
541
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);
546 return true;
547 }
548
549 new_length = printf_length(fmt, args);
550
551 ptr = resize(*str, *start + new_length + 1);
552 if (unlikely(ptr == NULL))
553 return false;
554
555 vsnprintf(ptr + *start, new_length + 1, fmt, args);
556 *str = ptr;
557 *start += new_length;
558 return true;
559 }
560
561 /***************************************************************************
562 * Linear allocator for short-lived allocations.
563 ***************************************************************************
564 *
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.
569 *
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.
574 *
575 * The linear parent node is always the first buffer and keeps track of all
576 * other buffers.
577 */
578
579 #define MIN_LINEAR_BUFSIZE 2048
580 #define SUBALLOC_ALIGNMENT 8
581 #define LMAGIC 0x87b9c7d3
582
583 struct
584 #ifdef _MSC_VER
585 __declspec(align(8))
586 #elif defined(__LP64__)
587 __attribute__((aligned(16)))
588 #else
589 __attribute__((aligned(8)))
590 #endif
591 linear_header {
592 #ifndef NDEBUG
593 unsigned magic; /* for debugging */
594 #endif
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 */
600
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:
604 *
605 * - linear_size_chunk
606 * - allocated space
607 * - linear_size_chunk
608 * - allocated space
609 * etc.
610 *
611 * linear_size_chunk is only needed by linear_realloc.
612 */
613 };
614
615 struct linear_size_chunk {
616 unsigned size; /* for realloc */
617 unsigned _padding;
618 };
619
620 typedef struct linear_header linear_header;
621 typedef struct linear_size_chunk linear_size_chunk;
622
623 #define LINEAR_PARENT_TO_HEADER(parent) \
624 (linear_header*) \
625 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
626
627 /* Allocate the linear buffer with its header. */
628 static linear_header *
629 create_linear_node(void *ralloc_ctx, unsigned min_size)
630 {
631 linear_header *node;
632
633 min_size += sizeof(linear_size_chunk);
634
635 if (likely(min_size < MIN_LINEAR_BUFSIZE))
636 min_size = MIN_LINEAR_BUFSIZE;
637
638 node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size);
639 if (unlikely(!node))
640 return NULL;
641
642 #ifndef NDEBUG
643 node->magic = LMAGIC;
644 #endif
645 node->offset = 0;
646 node->size = min_size;
647 node->ralloc_parent = ralloc_ctx;
648 node->next = NULL;
649 node->latest = node;
650 return node;
651 }
652
653 void *
654 linear_alloc_child(void *parent, unsigned size)
655 {
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;
660 unsigned full_size;
661
662 assert(first->magic == LMAGIC);
663 assert(!latest->next);
664
665 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
666 full_size = sizeof(linear_size_chunk) + size;
667
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))
672 return NULL;
673
674 first->latest = new_node;
675 latest->latest = new_node;
676 latest->next = new_node;
677 latest = new_node;
678 }
679
680 ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset);
681 ptr->size = size;
682 latest->offset += full_size;
683
684 assert((uintptr_t)&ptr[1] % SUBALLOC_ALIGNMENT == 0);
685 return &ptr[1];
686 }
687
688 void *
689 linear_alloc_parent(void *ralloc_ctx, unsigned size)
690 {
691 linear_header *node;
692
693 if (unlikely(!ralloc_ctx))
694 return NULL;
695
696 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
697
698 node = create_linear_node(ralloc_ctx, size);
699 if (unlikely(!node))
700 return NULL;
701
702 return linear_alloc_child((char*)node +
703 sizeof(linear_header) +
704 sizeof(linear_size_chunk), size);
705 }
706
707 void *
708 linear_zalloc_child(void *parent, unsigned size)
709 {
710 void *ptr = linear_alloc_child(parent, size);
711
712 if (likely(ptr))
713 memset(ptr, 0, size);
714 return ptr;
715 }
716
717 void *
718 linear_zalloc_parent(void *parent, unsigned size)
719 {
720 void *ptr = linear_alloc_parent(parent, size);
721
722 if (likely(ptr))
723 memset(ptr, 0, size);
724 return ptr;
725 }
726
727 void
728 linear_free_parent(void *ptr)
729 {
730 linear_header *node;
731
732 if (unlikely(!ptr))
733 return;
734
735 node = LINEAR_PARENT_TO_HEADER(ptr);
736 assert(node->magic == LMAGIC);
737
738 while (node) {
739 void *ptr = node;
740
741 node = node->next;
742 ralloc_free(ptr);
743 }
744 }
745
746 void
747 ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
748 {
749 linear_header *node;
750
751 if (unlikely(!ptr))
752 return;
753
754 node = LINEAR_PARENT_TO_HEADER(ptr);
755 assert(node->magic == LMAGIC);
756
757 while (node) {
758 ralloc_steal(new_ralloc_ctx, node);
759 node->ralloc_parent = new_ralloc_ctx;
760 node = node->next;
761 }
762 }
763
764 void *
765 ralloc_parent_of_linear_parent(void *ptr)
766 {
767 linear_header *node = LINEAR_PARENT_TO_HEADER(ptr);
768 assert(node->magic == LMAGIC);
769 return node->ralloc_parent;
770 }
771
772 void *
773 linear_realloc(void *parent, void *old, unsigned new_size)
774 {
775 unsigned old_size = 0;
776 ralloc_header *new_ptr;
777
778 new_ptr = linear_alloc_child(parent, new_size);
779
780 if (unlikely(!old))
781 return new_ptr;
782
783 old_size = ((linear_size_chunk*)old)[-1].size;
784
785 if (likely(new_ptr && old_size))
786 memcpy(new_ptr, old, MIN2(old_size, new_size));
787
788 return new_ptr;
789 }
790
791 /* All code below is pretty much copied from ralloc and only the alloc
792 * calls are different.
793 */
794
795 char *
796 linear_strdup(void *parent, const char *str)
797 {
798 unsigned n;
799 char *ptr;
800
801 if (unlikely(!str))
802 return NULL;
803
804 n = strlen(str);
805 ptr = linear_alloc_child(parent, n + 1);
806 if (unlikely(!ptr))
807 return NULL;
808
809 memcpy(ptr, str, n);
810 ptr[n] = '\0';
811 return ptr;
812 }
813
814 char *
815 linear_asprintf(void *parent, const char *fmt, ...)
816 {
817 char *ptr;
818 va_list args;
819 va_start(args, fmt);
820 ptr = linear_vasprintf(parent, fmt, args);
821 va_end(args);
822 return ptr;
823 }
824
825 char *
826 linear_vasprintf(void *parent, const char *fmt, va_list args)
827 {
828 unsigned size = printf_length(fmt, args) + 1;
829
830 char *ptr = linear_alloc_child(parent, size);
831 if (ptr != NULL)
832 vsnprintf(ptr, size, fmt, args);
833
834 return ptr;
835 }
836
837 bool
838 linear_asprintf_append(void *parent, char **str, const char *fmt, ...)
839 {
840 bool success;
841 va_list args;
842 va_start(args, fmt);
843 success = linear_vasprintf_append(parent, str, fmt, args);
844 va_end(args);
845 return success;
846 }
847
848 bool
849 linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args)
850 {
851 size_t existing_length;
852 assert(str != NULL);
853 existing_length = *str ? strlen(*str) : 0;
854 return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args);
855 }
856
857 bool
858 linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
859 const char *fmt, ...)
860 {
861 bool success;
862 va_list args;
863 va_start(args, fmt);
864 success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args);
865 va_end(args);
866 return success;
867 }
868
869 bool
870 linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
871 const char *fmt, va_list args)
872 {
873 size_t new_length;
874 char *ptr;
875
876 assert(str != NULL);
877
878 if (unlikely(*str == NULL)) {
879 *str = linear_vasprintf(parent, fmt, args);
880 *start = strlen(*str);
881 return true;
882 }
883
884 new_length = printf_length(fmt, args);
885
886 ptr = linear_realloc(parent, *str, *start + new_length + 1);
887 if (unlikely(ptr == NULL))
888 return false;
889
890 vsnprintf(ptr + *start, new_length + 1, fmt, args);
891 *str = ptr;
892 *start += new_length;
893 return true;
894 }
895
896 /* helper routine for strcat/strncat - n is the exact amount to copy */
897 static bool
898 linear_cat(void *parent, char **dest, const char *str, unsigned n)
899 {
900 char *both;
901 unsigned existing_length;
902 assert(dest != NULL && *dest != NULL);
903
904 existing_length = strlen(*dest);
905 both = linear_realloc(parent, *dest, existing_length + n + 1);
906 if (unlikely(both == NULL))
907 return false;
908
909 memcpy(both + existing_length, str, n);
910 both[existing_length + n] = '\0';
911
912 *dest = both;
913 return true;
914 }
915
916 bool
917 linear_strcat(void *parent, char **dest, const char *str)
918 {
919 return linear_cat(parent, dest, str, strlen(str));
920 }