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