util/ralloc: add ralloc_str_append() helper
[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 bool
407 ralloc_str_append(char **dest, const char *str,
408 size_t existing_length, size_t str_size)
409 {
410 char *both;
411 assert(dest != NULL && *dest != NULL);
412
413 both = resize(*dest, existing_length + str_size + 1);
414 if (unlikely(both == NULL))
415 return false;
416
417 memcpy(both + existing_length, str, str_size);
418 both[existing_length + str_size] = '\0';
419
420 *dest = both;
421
422 return true;
423 }
424
425 char *
426 ralloc_asprintf(const void *ctx, const char *fmt, ...)
427 {
428 char *ptr;
429 va_list args;
430 va_start(args, fmt);
431 ptr = ralloc_vasprintf(ctx, fmt, args);
432 va_end(args);
433 return ptr;
434 }
435
436 /* Return the length of the string that would be generated by a printf-style
437 * format and argument list, not including the \0 byte.
438 */
439 static size_t
440 printf_length(const char *fmt, va_list untouched_args)
441 {
442 int size;
443 char junk;
444
445 /* Make a copy of the va_list so the original caller can still use it */
446 va_list args;
447 va_copy(args, untouched_args);
448
449 #ifdef _WIN32
450 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
451 * if the number of characters to write is greater than count.
452 */
453 size = _vscprintf(fmt, args);
454 (void)junk;
455 #else
456 size = vsnprintf(&junk, 1, fmt, args);
457 #endif
458 assert(size >= 0);
459
460 va_end(args);
461
462 return size;
463 }
464
465 char *
466 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
467 {
468 size_t size = printf_length(fmt, args) + 1;
469
470 char *ptr = ralloc_size(ctx, size);
471 if (ptr != NULL)
472 vsnprintf(ptr, size, fmt, args);
473
474 return ptr;
475 }
476
477 bool
478 ralloc_asprintf_append(char **str, const char *fmt, ...)
479 {
480 bool success;
481 va_list args;
482 va_start(args, fmt);
483 success = ralloc_vasprintf_append(str, fmt, args);
484 va_end(args);
485 return success;
486 }
487
488 bool
489 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
490 {
491 size_t existing_length;
492 assert(str != NULL);
493 existing_length = *str ? strlen(*str) : 0;
494 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
495 }
496
497 bool
498 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
499 {
500 bool success;
501 va_list args;
502 va_start(args, fmt);
503 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
504 va_end(args);
505 return success;
506 }
507
508 bool
509 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
510 va_list args)
511 {
512 size_t new_length;
513 char *ptr;
514
515 assert(str != NULL);
516
517 if (unlikely(*str == NULL)) {
518 // Assuming a NULL context is probably bad, but it's expected behavior.
519 *str = ralloc_vasprintf(NULL, fmt, args);
520 *start = strlen(*str);
521 return true;
522 }
523
524 new_length = printf_length(fmt, args);
525
526 ptr = resize(*str, *start + new_length + 1);
527 if (unlikely(ptr == NULL))
528 return false;
529
530 vsnprintf(ptr + *start, new_length + 1, fmt, args);
531 *str = ptr;
532 *start += new_length;
533 return true;
534 }
535
536 /***************************************************************************
537 * Linear allocator for short-lived allocations.
538 ***************************************************************************
539 *
540 * The allocator consists of a parent node (2K buffer), which requires
541 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
542 * directly, because the parent doesn't track them. You have to release
543 * the parent node in order to release all its children.
544 *
545 * The allocator uses a fixed-sized buffer with a monotonically increasing
546 * offset after each allocation. If the buffer is all used, another buffer
547 * is allocated, sharing the same ralloc parent, so all buffers are at
548 * the same level in the ralloc hierarchy.
549 *
550 * The linear parent node is always the first buffer and keeps track of all
551 * other buffers.
552 */
553
554 #define ALIGN_POT(x, y) (((x) + (y) - 1) & ~((y) - 1))
555
556 #define MIN_LINEAR_BUFSIZE 2048
557 #define SUBALLOC_ALIGNMENT sizeof(uintptr_t)
558 #define LMAGIC 0x87b9c7d3
559
560 struct linear_header {
561 #ifdef DEBUG
562 unsigned magic; /* for debugging */
563 #endif
564 unsigned offset; /* points to the first unused byte in the buffer */
565 unsigned size; /* size of the buffer */
566 void *ralloc_parent; /* new buffers will use this */
567 struct linear_header *next; /* next buffer if we have more */
568 struct linear_header *latest; /* the only buffer that has free space */
569
570 /* After this structure, the buffer begins.
571 * Each suballocation consists of linear_size_chunk as its header followed
572 * by the suballocation, so it goes:
573 *
574 * - linear_size_chunk
575 * - allocated space
576 * - linear_size_chunk
577 * - allocated space
578 * etc.
579 *
580 * linear_size_chunk is only needed by linear_realloc.
581 */
582 };
583
584 struct linear_size_chunk {
585 unsigned size; /* for realloc */
586 unsigned _padding;
587 };
588
589 typedef struct linear_header linear_header;
590 typedef struct linear_size_chunk linear_size_chunk;
591
592 #define LINEAR_PARENT_TO_HEADER(parent) \
593 (linear_header*) \
594 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
595
596 /* Allocate the linear buffer with its header. */
597 static linear_header *
598 create_linear_node(void *ralloc_ctx, unsigned min_size)
599 {
600 linear_header *node;
601
602 min_size += sizeof(linear_size_chunk);
603
604 if (likely(min_size < MIN_LINEAR_BUFSIZE))
605 min_size = MIN_LINEAR_BUFSIZE;
606
607 node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size);
608 if (unlikely(!node))
609 return NULL;
610
611 #ifdef DEBUG
612 node->magic = LMAGIC;
613 #endif
614 node->offset = 0;
615 node->size = min_size;
616 node->ralloc_parent = ralloc_ctx;
617 node->next = NULL;
618 node->latest = node;
619 return node;
620 }
621
622 void *
623 linear_alloc_child(void *parent, unsigned size)
624 {
625 linear_header *first = LINEAR_PARENT_TO_HEADER(parent);
626 linear_header *latest = first->latest;
627 linear_header *new_node;
628 linear_size_chunk *ptr;
629 unsigned full_size;
630
631 assert(first->magic == LMAGIC);
632 assert(!latest->next);
633
634 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
635 full_size = sizeof(linear_size_chunk) + size;
636
637 if (unlikely(latest->offset + full_size > latest->size)) {
638 /* allocate a new node */
639 new_node = create_linear_node(latest->ralloc_parent, size);
640 if (unlikely(!new_node))
641 return NULL;
642
643 first->latest = new_node;
644 latest->latest = new_node;
645 latest->next = new_node;
646 latest = new_node;
647 }
648
649 ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset);
650 ptr->size = size;
651 latest->offset += full_size;
652 return &ptr[1];
653 }
654
655 void *
656 linear_alloc_parent(void *ralloc_ctx, unsigned size)
657 {
658 linear_header *node;
659
660 if (unlikely(!ralloc_ctx))
661 return NULL;
662
663 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
664
665 node = create_linear_node(ralloc_ctx, size);
666 if (unlikely(!node))
667 return NULL;
668
669 return linear_alloc_child((char*)node +
670 sizeof(linear_header) +
671 sizeof(linear_size_chunk), size);
672 }
673
674 void *
675 linear_zalloc_child(void *parent, unsigned size)
676 {
677 void *ptr = linear_alloc_child(parent, size);
678
679 if (likely(ptr))
680 memset(ptr, 0, size);
681 return ptr;
682 }
683
684 void *
685 linear_zalloc_parent(void *parent, unsigned size)
686 {
687 void *ptr = linear_alloc_parent(parent, size);
688
689 if (likely(ptr))
690 memset(ptr, 0, size);
691 return ptr;
692 }
693
694 void
695 linear_free_parent(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 void *ptr = node;
707
708 node = node->next;
709 ralloc_free(ptr);
710 }
711 }
712
713 void
714 ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
715 {
716 linear_header *node;
717
718 if (unlikely(!ptr))
719 return;
720
721 node = LINEAR_PARENT_TO_HEADER(ptr);
722 assert(node->magic == LMAGIC);
723
724 while (node) {
725 ralloc_steal(new_ralloc_ctx, node);
726 node->ralloc_parent = new_ralloc_ctx;
727 node = node->next;
728 }
729 }
730
731 void *
732 ralloc_parent_of_linear_parent(void *ptr)
733 {
734 linear_header *node = LINEAR_PARENT_TO_HEADER(ptr);
735 assert(node->magic == LMAGIC);
736 return node->ralloc_parent;
737 }
738
739 void *
740 linear_realloc(void *parent, void *old, unsigned new_size)
741 {
742 unsigned old_size = 0;
743 ralloc_header *new_ptr;
744
745 new_ptr = linear_alloc_child(parent, new_size);
746
747 if (unlikely(!old))
748 return new_ptr;
749
750 old_size = ((linear_size_chunk*)old)[-1].size;
751
752 if (likely(new_ptr && old_size))
753 memcpy(new_ptr, old, MIN2(old_size, new_size));
754
755 return new_ptr;
756 }
757
758 /* All code below is pretty much copied from ralloc and only the alloc
759 * calls are different.
760 */
761
762 char *
763 linear_strdup(void *parent, const char *str)
764 {
765 unsigned n;
766 char *ptr;
767
768 if (unlikely(!str))
769 return NULL;
770
771 n = strlen(str);
772 ptr = linear_alloc_child(parent, n + 1);
773 if (unlikely(!ptr))
774 return NULL;
775
776 memcpy(ptr, str, n);
777 ptr[n] = '\0';
778 return ptr;
779 }
780
781 char *
782 linear_asprintf(void *parent, const char *fmt, ...)
783 {
784 char *ptr;
785 va_list args;
786 va_start(args, fmt);
787 ptr = linear_vasprintf(parent, fmt, args);
788 va_end(args);
789 return ptr;
790 }
791
792 char *
793 linear_vasprintf(void *parent, const char *fmt, va_list args)
794 {
795 unsigned size = printf_length(fmt, args) + 1;
796
797 char *ptr = linear_alloc_child(parent, size);
798 if (ptr != NULL)
799 vsnprintf(ptr, size, fmt, args);
800
801 return ptr;
802 }
803
804 bool
805 linear_asprintf_append(void *parent, char **str, const char *fmt, ...)
806 {
807 bool success;
808 va_list args;
809 va_start(args, fmt);
810 success = linear_vasprintf_append(parent, str, fmt, args);
811 va_end(args);
812 return success;
813 }
814
815 bool
816 linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args)
817 {
818 size_t existing_length;
819 assert(str != NULL);
820 existing_length = *str ? strlen(*str) : 0;
821 return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args);
822 }
823
824 bool
825 linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
826 const char *fmt, ...)
827 {
828 bool success;
829 va_list args;
830 va_start(args, fmt);
831 success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args);
832 va_end(args);
833 return success;
834 }
835
836 bool
837 linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
838 const char *fmt, va_list args)
839 {
840 size_t new_length;
841 char *ptr;
842
843 assert(str != NULL);
844
845 if (unlikely(*str == NULL)) {
846 *str = linear_vasprintf(parent, fmt, args);
847 *start = strlen(*str);
848 return true;
849 }
850
851 new_length = printf_length(fmt, args);
852
853 ptr = linear_realloc(parent, *str, *start + new_length + 1);
854 if (unlikely(ptr == NULL))
855 return false;
856
857 vsnprintf(ptr + *start, new_length + 1, fmt, args);
858 *str = ptr;
859 *start += new_length;
860 return true;
861 }
862
863 /* helper routine for strcat/strncat - n is the exact amount to copy */
864 static bool
865 linear_cat(void *parent, char **dest, const char *str, unsigned n)
866 {
867 char *both;
868 unsigned existing_length;
869 assert(dest != NULL && *dest != NULL);
870
871 existing_length = strlen(*dest);
872 both = linear_realloc(parent, *dest, existing_length + n + 1);
873 if (unlikely(both == NULL))
874 return false;
875
876 memcpy(both + existing_length, str, n);
877 both[existing_length + n] = '\0';
878
879 *dest = both;
880 return true;
881 }
882
883 bool
884 linear_strcat(void *parent, char **dest, const char *str)
885 {
886 return linear_cat(parent, dest, str, strlen(str));
887 }