ralloc.c revision 7e995a2e
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 */ 54struct 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 81typedef struct ralloc_header ralloc_header; 82 83static void unlink_block(ralloc_header *info); 84static void unsafe_free(ralloc_header *info); 85 86static ralloc_header * 87get_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 99static void 100add_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 112void * 113ralloc_context(const void *ctx) 114{ 115 return ralloc_size(ctx, 0); 116} 117 118void * 119ralloc_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 150void * 151rzalloc_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 */ 162static void * 163resize(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 192void * 193reralloc_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 202void * 203ralloc_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 211void * 212rzalloc_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 220void * 221reralloc_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 229void 230ralloc_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 242static void 243unlink_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 261static void 262unsafe_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 279void 280ralloc_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 = new_ctx ? get_header(new_ctx) : NULL; 289 290 unlink_block(info); 291 292 add_child(parent, info); 293} 294 295void 296ralloc_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 child->parent = new_info; 315 316 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */ 317 child->next = new_info->child; 318 if (child->next) 319 child->next->prev = child; 320 new_info->child = old_info->child; 321 old_info->child = NULL; 322} 323 324void * 325ralloc_parent(const void *ptr) 326{ 327 ralloc_header *info; 328 329 if (unlikely(ptr == NULL)) 330 return NULL; 331 332 info = get_header(ptr); 333 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL; 334} 335 336void 337ralloc_set_destructor(const void *ptr, void(*destructor)(void *)) 338{ 339 ralloc_header *info = get_header(ptr); 340 info->destructor = destructor; 341} 342 343char * 344ralloc_strdup(const void *ctx, const char *str) 345{ 346 size_t n; 347 char *ptr; 348 349 if (unlikely(str == NULL)) 350 return NULL; 351 352 n = strlen(str); 353 ptr = ralloc_array(ctx, char, n + 1); 354 memcpy(ptr, str, n); 355 ptr[n] = '\0'; 356 return ptr; 357} 358 359char * 360ralloc_strndup(const void *ctx, const char *str, size_t max) 361{ 362 size_t n; 363 char *ptr; 364 365 if (unlikely(str == NULL)) 366 return NULL; 367 368 n = strnlen(str, max); 369 ptr = ralloc_array(ctx, char, n + 1); 370 memcpy(ptr, str, n); 371 ptr[n] = '\0'; 372 return ptr; 373} 374 375/* helper routine for strcat/strncat - n is the exact amount to copy */ 376static bool 377cat(char **dest, const char *str, size_t n) 378{ 379 char *both; 380 size_t existing_length; 381 assert(dest != NULL && *dest != NULL); 382 383 existing_length = strlen(*dest); 384 both = resize(*dest, existing_length + n + 1); 385 if (unlikely(both == NULL)) 386 return false; 387 388 memcpy(both + existing_length, str, n); 389 both[existing_length + n] = '\0'; 390 391 *dest = both; 392 return true; 393} 394 395 396bool 397ralloc_strcat(char **dest, const char *str) 398{ 399 return cat(dest, str, strlen(str)); 400} 401 402bool 403ralloc_strncat(char **dest, const char *str, size_t n) 404{ 405 return cat(dest, str, strnlen(str, n)); 406} 407 408bool 409ralloc_str_append(char **dest, const char *str, 410 size_t existing_length, size_t str_size) 411{ 412 char *both; 413 assert(dest != NULL && *dest != NULL); 414 415 both = resize(*dest, existing_length + str_size + 1); 416 if (unlikely(both == NULL)) 417 return false; 418 419 memcpy(both + existing_length, str, str_size); 420 both[existing_length + str_size] = '\0'; 421 422 *dest = both; 423 424 return true; 425} 426 427char * 428ralloc_asprintf(const void *ctx, const char *fmt, ...) 429{ 430 char *ptr; 431 va_list args; 432 va_start(args, fmt); 433 ptr = ralloc_vasprintf(ctx, fmt, args); 434 va_end(args); 435 return ptr; 436} 437 438/* Return the length of the string that would be generated by a printf-style 439 * format and argument list, not including the \0 byte. 440 */ 441static size_t 442printf_length(const char *fmt, va_list untouched_args) 443{ 444 int size; 445 char junk; 446 447 /* Make a copy of the va_list so the original caller can still use it */ 448 va_list args; 449 va_copy(args, untouched_args); 450 451#ifdef _WIN32 452 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1 453 * if the number of characters to write is greater than count. 454 */ 455 size = _vscprintf(fmt, args); 456 (void)junk; 457#else 458 size = vsnprintf(&junk, 1, fmt, args); 459#endif 460 assert(size >= 0); 461 462 va_end(args); 463 464 return size; 465} 466 467char * 468ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) 469{ 470 size_t size = printf_length(fmt, args) + 1; 471 472 char *ptr = ralloc_size(ctx, size); 473 if (ptr != NULL) 474 vsnprintf(ptr, size, fmt, args); 475 476 return ptr; 477} 478 479bool 480ralloc_asprintf_append(char **str, const char *fmt, ...) 481{ 482 bool success; 483 va_list args; 484 va_start(args, fmt); 485 success = ralloc_vasprintf_append(str, fmt, args); 486 va_end(args); 487 return success; 488} 489 490bool 491ralloc_vasprintf_append(char **str, const char *fmt, va_list args) 492{ 493 size_t existing_length; 494 assert(str != NULL); 495 existing_length = *str ? strlen(*str) : 0; 496 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args); 497} 498 499bool 500ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...) 501{ 502 bool success; 503 va_list args; 504 va_start(args, fmt); 505 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args); 506 va_end(args); 507 return success; 508} 509 510bool 511ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, 512 va_list args) 513{ 514 size_t new_length; 515 char *ptr; 516 517 assert(str != NULL); 518 519 if (unlikely(*str == NULL)) { 520 // Assuming a NULL context is probably bad, but it's expected behavior. 521 *str = ralloc_vasprintf(NULL, fmt, args); 522 *start = strlen(*str); 523 return true; 524 } 525 526 new_length = printf_length(fmt, args); 527 528 ptr = resize(*str, *start + new_length + 1); 529 if (unlikely(ptr == NULL)) 530 return false; 531 532 vsnprintf(ptr + *start, new_length + 1, fmt, args); 533 *str = ptr; 534 *start += new_length; 535 return true; 536} 537 538/*************************************************************************** 539 * Linear allocator for short-lived allocations. 540 *************************************************************************** 541 * 542 * The allocator consists of a parent node (2K buffer), which requires 543 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed 544 * directly, because the parent doesn't track them. You have to release 545 * the parent node in order to release all its children. 546 * 547 * The allocator uses a fixed-sized buffer with a monotonically increasing 548 * offset after each allocation. If the buffer is all used, another buffer 549 * is allocated, sharing the same ralloc parent, so all buffers are at 550 * the same level in the ralloc hierarchy. 551 * 552 * The linear parent node is always the first buffer and keeps track of all 553 * other buffers. 554 */ 555 556#define MIN_LINEAR_BUFSIZE 2048 557#define SUBALLOC_ALIGNMENT 8 558#define LMAGIC 0x87b9c7d3 559 560struct 561#ifdef _MSC_VER 562 __declspec(align(8)) 563#elif defined(__LP64__) 564 __attribute__((aligned(16))) 565#else 566 __attribute__((aligned(8))) 567#endif 568 linear_header { 569#ifdef DEBUG 570 unsigned magic; /* for debugging */ 571#endif 572 unsigned offset; /* points to the first unused byte in the buffer */ 573 unsigned size; /* size of the buffer */ 574 void *ralloc_parent; /* new buffers will use this */ 575 struct linear_header *next; /* next buffer if we have more */ 576 struct linear_header *latest; /* the only buffer that has free space */ 577 578 /* After this structure, the buffer begins. 579 * Each suballocation consists of linear_size_chunk as its header followed 580 * by the suballocation, so it goes: 581 * 582 * - linear_size_chunk 583 * - allocated space 584 * - linear_size_chunk 585 * - allocated space 586 * etc. 587 * 588 * linear_size_chunk is only needed by linear_realloc. 589 */ 590}; 591 592struct linear_size_chunk { 593 unsigned size; /* for realloc */ 594 unsigned _padding; 595}; 596 597typedef struct linear_header linear_header; 598typedef struct linear_size_chunk linear_size_chunk; 599 600#define LINEAR_PARENT_TO_HEADER(parent) \ 601 (linear_header*) \ 602 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header)) 603 604/* Allocate the linear buffer with its header. */ 605static linear_header * 606create_linear_node(void *ralloc_ctx, unsigned min_size) 607{ 608 linear_header *node; 609 610 min_size += sizeof(linear_size_chunk); 611 612 if (likely(min_size < MIN_LINEAR_BUFSIZE)) 613 min_size = MIN_LINEAR_BUFSIZE; 614 615 node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size); 616 if (unlikely(!node)) 617 return NULL; 618 619#ifdef DEBUG 620 node->magic = LMAGIC; 621#endif 622 node->offset = 0; 623 node->size = min_size; 624 node->ralloc_parent = ralloc_ctx; 625 node->next = NULL; 626 node->latest = node; 627 return node; 628} 629 630void * 631linear_alloc_child(void *parent, unsigned size) 632{ 633 linear_header *first = LINEAR_PARENT_TO_HEADER(parent); 634 linear_header *latest = first->latest; 635 linear_header *new_node; 636 linear_size_chunk *ptr; 637 unsigned full_size; 638 639#ifdef DEBUG 640 assert(first->magic == LMAGIC); 641#endif 642 assert(!latest->next); 643 644 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT); 645 full_size = sizeof(linear_size_chunk) + size; 646 647 if (unlikely(latest->offset + full_size > latest->size)) { 648 /* allocate a new node */ 649 new_node = create_linear_node(latest->ralloc_parent, size); 650 if (unlikely(!new_node)) 651 return NULL; 652 653 first->latest = new_node; 654 latest->latest = new_node; 655 latest->next = new_node; 656 latest = new_node; 657 } 658 659 ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset); 660 ptr->size = size; 661 latest->offset += full_size; 662 663 assert((uintptr_t)&ptr[1] % SUBALLOC_ALIGNMENT == 0); 664 return &ptr[1]; 665} 666 667void * 668linear_alloc_parent(void *ralloc_ctx, unsigned size) 669{ 670 linear_header *node; 671 672 if (unlikely(!ralloc_ctx)) 673 return NULL; 674 675 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT); 676 677 node = create_linear_node(ralloc_ctx, size); 678 if (unlikely(!node)) 679 return NULL; 680 681 return linear_alloc_child((char*)node + 682 sizeof(linear_header) + 683 sizeof(linear_size_chunk), size); 684} 685 686void * 687linear_zalloc_child(void *parent, unsigned size) 688{ 689 void *ptr = linear_alloc_child(parent, size); 690 691 if (likely(ptr)) 692 memset(ptr, 0, size); 693 return ptr; 694} 695 696void * 697linear_zalloc_parent(void *parent, unsigned size) 698{ 699 void *ptr = linear_alloc_parent(parent, size); 700 701 if (likely(ptr)) 702 memset(ptr, 0, size); 703 return ptr; 704} 705 706void 707linear_free_parent(void *ptr) 708{ 709 linear_header *node; 710 711 if (unlikely(!ptr)) 712 return; 713 714 node = LINEAR_PARENT_TO_HEADER(ptr); 715#ifdef DEBUG 716 assert(node->magic == LMAGIC); 717#endif 718 719 while (node) { 720 void *ptr = node; 721 722 node = node->next; 723 ralloc_free(ptr); 724 } 725} 726 727void 728ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr) 729{ 730 linear_header *node; 731 732 if (unlikely(!ptr)) 733 return; 734 735 node = LINEAR_PARENT_TO_HEADER(ptr); 736#ifdef DEBUG 737 assert(node->magic == LMAGIC); 738#endif 739 740 while (node) { 741 ralloc_steal(new_ralloc_ctx, node); 742 node->ralloc_parent = new_ralloc_ctx; 743 node = node->next; 744 } 745} 746 747void * 748ralloc_parent_of_linear_parent(void *ptr) 749{ 750 linear_header *node = LINEAR_PARENT_TO_HEADER(ptr); 751#ifdef DEBUG 752 assert(node->magic == LMAGIC); 753#endif 754 return node->ralloc_parent; 755} 756 757void * 758linear_realloc(void *parent, void *old, unsigned new_size) 759{ 760 unsigned old_size = 0; 761 ralloc_header *new_ptr; 762 763 new_ptr = linear_alloc_child(parent, new_size); 764 765 if (unlikely(!old)) 766 return new_ptr; 767 768 old_size = ((linear_size_chunk*)old)[-1].size; 769 770 if (likely(new_ptr && old_size)) 771 memcpy(new_ptr, old, MIN2(old_size, new_size)); 772 773 return new_ptr; 774} 775 776/* All code below is pretty much copied from ralloc and only the alloc 777 * calls are different. 778 */ 779 780char * 781linear_strdup(void *parent, const char *str) 782{ 783 unsigned n; 784 char *ptr; 785 786 if (unlikely(!str)) 787 return NULL; 788 789 n = strlen(str); 790 ptr = linear_alloc_child(parent, n + 1); 791 if (unlikely(!ptr)) 792 return NULL; 793 794 memcpy(ptr, str, n); 795 ptr[n] = '\0'; 796 return ptr; 797} 798 799char * 800linear_asprintf(void *parent, const char *fmt, ...) 801{ 802 char *ptr; 803 va_list args; 804 va_start(args, fmt); 805 ptr = linear_vasprintf(parent, fmt, args); 806 va_end(args); 807 return ptr; 808} 809 810char * 811linear_vasprintf(void *parent, const char *fmt, va_list args) 812{ 813 unsigned size = printf_length(fmt, args) + 1; 814 815 char *ptr = linear_alloc_child(parent, size); 816 if (ptr != NULL) 817 vsnprintf(ptr, size, fmt, args); 818 819 return ptr; 820} 821 822bool 823linear_asprintf_append(void *parent, char **str, const char *fmt, ...) 824{ 825 bool success; 826 va_list args; 827 va_start(args, fmt); 828 success = linear_vasprintf_append(parent, str, fmt, args); 829 va_end(args); 830 return success; 831} 832 833bool 834linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args) 835{ 836 size_t existing_length; 837 assert(str != NULL); 838 existing_length = *str ? strlen(*str) : 0; 839 return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args); 840} 841 842bool 843linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start, 844 const char *fmt, ...) 845{ 846 bool success; 847 va_list args; 848 va_start(args, fmt); 849 success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args); 850 va_end(args); 851 return success; 852} 853 854bool 855linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start, 856 const char *fmt, va_list args) 857{ 858 size_t new_length; 859 char *ptr; 860 861 assert(str != NULL); 862 863 if (unlikely(*str == NULL)) { 864 *str = linear_vasprintf(parent, fmt, args); 865 *start = strlen(*str); 866 return true; 867 } 868 869 new_length = printf_length(fmt, args); 870 871 ptr = linear_realloc(parent, *str, *start + new_length + 1); 872 if (unlikely(ptr == NULL)) 873 return false; 874 875 vsnprintf(ptr + *start, new_length + 1, fmt, args); 876 *str = ptr; 877 *start += new_length; 878 return true; 879} 880 881/* helper routine for strcat/strncat - n is the exact amount to copy */ 882static bool 883linear_cat(void *parent, char **dest, const char *str, unsigned n) 884{ 885 char *both; 886 unsigned existing_length; 887 assert(dest != NULL && *dest != NULL); 888 889 existing_length = strlen(*dest); 890 both = linear_realloc(parent, *dest, existing_length + n + 1); 891 if (unlikely(both == NULL)) 892 return false; 893 894 memcpy(both + existing_length, str, n); 895 both[existing_length + n] = '\0'; 896 897 *dest = both; 898 return true; 899} 900 901bool 902linear_strcat(void *parent, char **dest, const char *str) 903{ 904 return linear_cat(parent, dest, str, strlen(str)); 905} 906