1*d83cc019SAndroid Build Coastguard Worker /* 2*d83cc019SAndroid Build Coastguard Worker * Copyright © 2010 Intel Corporation 3*d83cc019SAndroid Build Coastguard Worker * 4*d83cc019SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a 5*d83cc019SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"), 6*d83cc019SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation 7*d83cc019SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*d83cc019SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the 9*d83cc019SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions: 10*d83cc019SAndroid Build Coastguard Worker * 11*d83cc019SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next 12*d83cc019SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the 13*d83cc019SAndroid Build Coastguard Worker * Software. 14*d83cc019SAndroid Build Coastguard Worker * 15*d83cc019SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16*d83cc019SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17*d83cc019SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18*d83cc019SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19*d83cc019SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20*d83cc019SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21*d83cc019SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE. 22*d83cc019SAndroid Build Coastguard Worker */ 23*d83cc019SAndroid Build Coastguard Worker 24*d83cc019SAndroid Build Coastguard Worker /** 25*d83cc019SAndroid Build Coastguard Worker * \file ralloc.h 26*d83cc019SAndroid Build Coastguard Worker * 27*d83cc019SAndroid Build Coastguard Worker * ralloc: a recursive memory allocator 28*d83cc019SAndroid Build Coastguard Worker * 29*d83cc019SAndroid Build Coastguard Worker * The ralloc memory allocator creates a hierarchy of allocated 30*d83cc019SAndroid Build Coastguard Worker * objects. Every allocation is in reference to some parent, and 31*d83cc019SAndroid Build Coastguard Worker * every allocated object can in turn be used as the parent of a 32*d83cc019SAndroid Build Coastguard Worker * subsequent allocation. This allows for extremely convenient 33*d83cc019SAndroid Build Coastguard Worker * discarding of an entire tree/sub-tree of allocations by calling 34*d83cc019SAndroid Build Coastguard Worker * ralloc_free on any particular object to free it and all of its 35*d83cc019SAndroid Build Coastguard Worker * children. 36*d83cc019SAndroid Build Coastguard Worker * 37*d83cc019SAndroid Build Coastguard Worker * The conceptual working of ralloc was directly inspired by Andrew 38*d83cc019SAndroid Build Coastguard Worker * Tridgell's talloc, but ralloc is an independent implementation 39*d83cc019SAndroid Build Coastguard Worker * released under the MIT license and tuned for Mesa. 40*d83cc019SAndroid Build Coastguard Worker * 41*d83cc019SAndroid Build Coastguard Worker * The talloc implementation is available under the GNU Lesser 42*d83cc019SAndroid Build Coastguard Worker * General Public License (GNU LGPL), version 3 or later. It is 43*d83cc019SAndroid Build Coastguard Worker * more sophisticated than ralloc in that it includes reference 44*d83cc019SAndroid Build Coastguard Worker * counting and debugging features. See: http://talloc.samba.org/ 45*d83cc019SAndroid Build Coastguard Worker */ 46*d83cc019SAndroid Build Coastguard Worker 47*d83cc019SAndroid Build Coastguard Worker #ifndef RALLOC_H 48*d83cc019SAndroid Build Coastguard Worker #define RALLOC_H 49*d83cc019SAndroid Build Coastguard Worker 50*d83cc019SAndroid Build Coastguard Worker #ifdef __cplusplus 51*d83cc019SAndroid Build Coastguard Worker extern "C" { 52*d83cc019SAndroid Build Coastguard Worker #endif 53*d83cc019SAndroid Build Coastguard Worker 54*d83cc019SAndroid Build Coastguard Worker #include <stddef.h> 55*d83cc019SAndroid Build Coastguard Worker #include <stdarg.h> 56*d83cc019SAndroid Build Coastguard Worker #include <stdbool.h> 57*d83cc019SAndroid Build Coastguard Worker #include "brw_compat.h" 58*d83cc019SAndroid Build Coastguard Worker 59*d83cc019SAndroid Build Coastguard Worker /** 60*d83cc019SAndroid Build Coastguard Worker * \def ralloc(ctx, type) 61*d83cc019SAndroid Build Coastguard Worker * Allocate a new object chained off of the given context. 62*d83cc019SAndroid Build Coastguard Worker * 63*d83cc019SAndroid Build Coastguard Worker * This is equivalent to: 64*d83cc019SAndroid Build Coastguard Worker * \code 65*d83cc019SAndroid Build Coastguard Worker * ((type *) ralloc_size(ctx, sizeof(type)) 66*d83cc019SAndroid Build Coastguard Worker * \endcode 67*d83cc019SAndroid Build Coastguard Worker */ 68*d83cc019SAndroid Build Coastguard Worker #define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type))) 69*d83cc019SAndroid Build Coastguard Worker 70*d83cc019SAndroid Build Coastguard Worker /** 71*d83cc019SAndroid Build Coastguard Worker * \def rzalloc(ctx, type) 72*d83cc019SAndroid Build Coastguard Worker * Allocate a new object out of the given context and initialize it to zero. 73*d83cc019SAndroid Build Coastguard Worker * 74*d83cc019SAndroid Build Coastguard Worker * This is equivalent to: 75*d83cc019SAndroid Build Coastguard Worker * \code 76*d83cc019SAndroid Build Coastguard Worker * ((type *) rzalloc_size(ctx, sizeof(type)) 77*d83cc019SAndroid Build Coastguard Worker * \endcode 78*d83cc019SAndroid Build Coastguard Worker */ 79*d83cc019SAndroid Build Coastguard Worker #define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type))) 80*d83cc019SAndroid Build Coastguard Worker 81*d83cc019SAndroid Build Coastguard Worker /** 82*d83cc019SAndroid Build Coastguard Worker * Allocate a new ralloc context. 83*d83cc019SAndroid Build Coastguard Worker * 84*d83cc019SAndroid Build Coastguard Worker * While any ralloc'd pointer can be used as a context, sometimes it is useful 85*d83cc019SAndroid Build Coastguard Worker * to simply allocate a context with no associated memory. 86*d83cc019SAndroid Build Coastguard Worker * 87*d83cc019SAndroid Build Coastguard Worker * It is equivalent to: 88*d83cc019SAndroid Build Coastguard Worker * \code 89*d83cc019SAndroid Build Coastguard Worker * ((type *) ralloc_size(ctx, 0) 90*d83cc019SAndroid Build Coastguard Worker * \endcode 91*d83cc019SAndroid Build Coastguard Worker */ 92*d83cc019SAndroid Build Coastguard Worker void *ralloc_context(const void *ctx); 93*d83cc019SAndroid Build Coastguard Worker 94*d83cc019SAndroid Build Coastguard Worker /** 95*d83cc019SAndroid Build Coastguard Worker * Allocate memory chained off of the given context. 96*d83cc019SAndroid Build Coastguard Worker * 97*d83cc019SAndroid Build Coastguard Worker * This is the core allocation routine which is used by all others. It 98*d83cc019SAndroid Build Coastguard Worker * simply allocates storage for \p size bytes and returns the pointer, 99*d83cc019SAndroid Build Coastguard Worker * similar to \c malloc. 100*d83cc019SAndroid Build Coastguard Worker */ 101*d83cc019SAndroid Build Coastguard Worker void *ralloc_size(const void *ctx, size_t size); 102*d83cc019SAndroid Build Coastguard Worker 103*d83cc019SAndroid Build Coastguard Worker /** 104*d83cc019SAndroid Build Coastguard Worker * Allocate zero-initialized memory chained off of the given context. 105*d83cc019SAndroid Build Coastguard Worker * 106*d83cc019SAndroid Build Coastguard Worker * This is similar to \c calloc with a size of 1. 107*d83cc019SAndroid Build Coastguard Worker */ 108*d83cc019SAndroid Build Coastguard Worker void *rzalloc_size(const void *ctx, size_t size); 109*d83cc019SAndroid Build Coastguard Worker 110*d83cc019SAndroid Build Coastguard Worker /** 111*d83cc019SAndroid Build Coastguard Worker * Resize a piece of ralloc-managed memory, preserving data. 112*d83cc019SAndroid Build Coastguard Worker * 113*d83cc019SAndroid Build Coastguard Worker * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 114*d83cc019SAndroid Build Coastguard Worker * memory. Instead, it resizes it to a 0-byte ralloc context, just like 115*d83cc019SAndroid Build Coastguard Worker * calling ralloc_size(ctx, 0). This is different from talloc. 116*d83cc019SAndroid Build Coastguard Worker * 117*d83cc019SAndroid Build Coastguard Worker * \param ctx The context to use for new allocation. If \p ptr != NULL, 118*d83cc019SAndroid Build Coastguard Worker * it must be the same as ralloc_parent(\p ptr). 119*d83cc019SAndroid Build Coastguard Worker * \param ptr Pointer to the memory to be resized. May be NULL. 120*d83cc019SAndroid Build Coastguard Worker * \param size The amount of memory to allocate, in bytes. 121*d83cc019SAndroid Build Coastguard Worker */ 122*d83cc019SAndroid Build Coastguard Worker void *reralloc_size(const void *ctx, void *ptr, size_t size); 123*d83cc019SAndroid Build Coastguard Worker 124*d83cc019SAndroid Build Coastguard Worker /// \defgroup array Array Allocators @{ 125*d83cc019SAndroid Build Coastguard Worker 126*d83cc019SAndroid Build Coastguard Worker /** 127*d83cc019SAndroid Build Coastguard Worker * \def ralloc_array(ctx, type, count) 128*d83cc019SAndroid Build Coastguard Worker * Allocate an array of objects chained off the given context. 129*d83cc019SAndroid Build Coastguard Worker * 130*d83cc019SAndroid Build Coastguard Worker * Similar to \c calloc, but does not initialize the memory to zero. 131*d83cc019SAndroid Build Coastguard Worker * 132*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 133*d83cc019SAndroid Build Coastguard Worker * multiplying \c sizeof(type) and \p count. This is necessary for security. 134*d83cc019SAndroid Build Coastguard Worker * 135*d83cc019SAndroid Build Coastguard Worker * This is equivalent to: 136*d83cc019SAndroid Build Coastguard Worker * \code 137*d83cc019SAndroid Build Coastguard Worker * ((type *) ralloc_array_size(ctx, sizeof(type), count) 138*d83cc019SAndroid Build Coastguard Worker * \endcode 139*d83cc019SAndroid Build Coastguard Worker */ 140*d83cc019SAndroid Build Coastguard Worker #define ralloc_array(ctx, type, count) \ 141*d83cc019SAndroid Build Coastguard Worker ((type *) ralloc_array_size(ctx, sizeof(type), count)) 142*d83cc019SAndroid Build Coastguard Worker 143*d83cc019SAndroid Build Coastguard Worker /** 144*d83cc019SAndroid Build Coastguard Worker * \def rzalloc_array(ctx, type, count) 145*d83cc019SAndroid Build Coastguard Worker * Allocate a zero-initialized array chained off the given context. 146*d83cc019SAndroid Build Coastguard Worker * 147*d83cc019SAndroid Build Coastguard Worker * Similar to \c calloc. 148*d83cc019SAndroid Build Coastguard Worker * 149*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 150*d83cc019SAndroid Build Coastguard Worker * multiplying \c sizeof(type) and \p count. This is necessary for security. 151*d83cc019SAndroid Build Coastguard Worker * 152*d83cc019SAndroid Build Coastguard Worker * This is equivalent to: 153*d83cc019SAndroid Build Coastguard Worker * \code 154*d83cc019SAndroid Build Coastguard Worker * ((type *) rzalloc_array_size(ctx, sizeof(type), count) 155*d83cc019SAndroid Build Coastguard Worker * \endcode 156*d83cc019SAndroid Build Coastguard Worker */ 157*d83cc019SAndroid Build Coastguard Worker #define rzalloc_array(ctx, type, count) \ 158*d83cc019SAndroid Build Coastguard Worker ((type *) rzalloc_array_size(ctx, sizeof(type), count)) 159*d83cc019SAndroid Build Coastguard Worker 160*d83cc019SAndroid Build Coastguard Worker /** 161*d83cc019SAndroid Build Coastguard Worker * \def reralloc(ctx, ptr, type, count) 162*d83cc019SAndroid Build Coastguard Worker * Resize a ralloc-managed array, preserving data. 163*d83cc019SAndroid Build Coastguard Worker * 164*d83cc019SAndroid Build Coastguard Worker * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 165*d83cc019SAndroid Build Coastguard Worker * memory. Instead, it resizes it to a 0-byte ralloc context, just like 166*d83cc019SAndroid Build Coastguard Worker * calling ralloc_size(ctx, 0). This is different from talloc. 167*d83cc019SAndroid Build Coastguard Worker * 168*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 169*d83cc019SAndroid Build Coastguard Worker * multiplying \c sizeof(type) and \p count. This is necessary for security. 170*d83cc019SAndroid Build Coastguard Worker * 171*d83cc019SAndroid Build Coastguard Worker * \param ctx The context to use for new allocation. If \p ptr != NULL, 172*d83cc019SAndroid Build Coastguard Worker * it must be the same as ralloc_parent(\p ptr). 173*d83cc019SAndroid Build Coastguard Worker * \param ptr Pointer to the array to be resized. May be NULL. 174*d83cc019SAndroid Build Coastguard Worker * \param type The element type. 175*d83cc019SAndroid Build Coastguard Worker * \param count The number of elements to allocate. 176*d83cc019SAndroid Build Coastguard Worker */ 177*d83cc019SAndroid Build Coastguard Worker #define reralloc(ctx, ptr, type, count) \ 178*d83cc019SAndroid Build Coastguard Worker ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count)) 179*d83cc019SAndroid Build Coastguard Worker 180*d83cc019SAndroid Build Coastguard Worker /** 181*d83cc019SAndroid Build Coastguard Worker * Allocate memory for an array chained off the given context. 182*d83cc019SAndroid Build Coastguard Worker * 183*d83cc019SAndroid Build Coastguard Worker * Similar to \c calloc, but does not initialize the memory to zero. 184*d83cc019SAndroid Build Coastguard Worker * 185*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 186*d83cc019SAndroid Build Coastguard Worker * multiplying \p size and \p count. This is necessary for security. 187*d83cc019SAndroid Build Coastguard Worker */ 188*d83cc019SAndroid Build Coastguard Worker void *ralloc_array_size(const void *ctx, size_t size, unsigned count); 189*d83cc019SAndroid Build Coastguard Worker 190*d83cc019SAndroid Build Coastguard Worker /** 191*d83cc019SAndroid Build Coastguard Worker * Allocate a zero-initialized array chained off the given context. 192*d83cc019SAndroid Build Coastguard Worker * 193*d83cc019SAndroid Build Coastguard Worker * Similar to \c calloc. 194*d83cc019SAndroid Build Coastguard Worker * 195*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 196*d83cc019SAndroid Build Coastguard Worker * multiplying \p size and \p count. This is necessary for security. 197*d83cc019SAndroid Build Coastguard Worker */ 198*d83cc019SAndroid Build Coastguard Worker void *rzalloc_array_size(const void *ctx, size_t size, unsigned count); 199*d83cc019SAndroid Build Coastguard Worker 200*d83cc019SAndroid Build Coastguard Worker /** 201*d83cc019SAndroid Build Coastguard Worker * Resize a ralloc-managed array, preserving data. 202*d83cc019SAndroid Build Coastguard Worker * 203*d83cc019SAndroid Build Coastguard Worker * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 204*d83cc019SAndroid Build Coastguard Worker * memory. Instead, it resizes it to a 0-byte ralloc context, just like 205*d83cc019SAndroid Build Coastguard Worker * calling ralloc_size(ctx, 0). This is different from talloc. 206*d83cc019SAndroid Build Coastguard Worker * 207*d83cc019SAndroid Build Coastguard Worker * More than a convenience function, this also checks for integer overflow when 208*d83cc019SAndroid Build Coastguard Worker * multiplying \c sizeof(type) and \p count. This is necessary for security. 209*d83cc019SAndroid Build Coastguard Worker * 210*d83cc019SAndroid Build Coastguard Worker * \param ctx The context to use for new allocation. If \p ptr != NULL, 211*d83cc019SAndroid Build Coastguard Worker * it must be the same as ralloc_parent(\p ptr). 212*d83cc019SAndroid Build Coastguard Worker * \param ptr Pointer to the array to be resized. May be NULL. 213*d83cc019SAndroid Build Coastguard Worker * \param size The size of an individual element. 214*d83cc019SAndroid Build Coastguard Worker * \param count The number of elements to allocate. 215*d83cc019SAndroid Build Coastguard Worker * 216*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 217*d83cc019SAndroid Build Coastguard Worker */ 218*d83cc019SAndroid Build Coastguard Worker void *reralloc_array_size(const void *ctx, void *ptr, size_t size, 219*d83cc019SAndroid Build Coastguard Worker unsigned count); 220*d83cc019SAndroid Build Coastguard Worker /// @} 221*d83cc019SAndroid Build Coastguard Worker 222*d83cc019SAndroid Build Coastguard Worker /** 223*d83cc019SAndroid Build Coastguard Worker * Free a piece of ralloc-managed memory. 224*d83cc019SAndroid Build Coastguard Worker * 225*d83cc019SAndroid Build Coastguard Worker * This will also free the memory of any children allocated this context. 226*d83cc019SAndroid Build Coastguard Worker */ 227*d83cc019SAndroid Build Coastguard Worker void ralloc_free(void *ptr); 228*d83cc019SAndroid Build Coastguard Worker 229*d83cc019SAndroid Build Coastguard Worker /** 230*d83cc019SAndroid Build Coastguard Worker * "Steal" memory from one context, changing it to another. 231*d83cc019SAndroid Build Coastguard Worker * 232*d83cc019SAndroid Build Coastguard Worker * This changes \p ptr's context to \p new_ctx. This is quite useful if 233*d83cc019SAndroid Build Coastguard Worker * memory is allocated out of a temporary context. 234*d83cc019SAndroid Build Coastguard Worker */ 235*d83cc019SAndroid Build Coastguard Worker void ralloc_steal(const void *new_ctx, void *ptr); 236*d83cc019SAndroid Build Coastguard Worker 237*d83cc019SAndroid Build Coastguard Worker /** 238*d83cc019SAndroid Build Coastguard Worker * Return the given pointer's ralloc context. 239*d83cc019SAndroid Build Coastguard Worker */ 240*d83cc019SAndroid Build Coastguard Worker void *ralloc_parent(const void *ptr); 241*d83cc019SAndroid Build Coastguard Worker 242*d83cc019SAndroid Build Coastguard Worker /** 243*d83cc019SAndroid Build Coastguard Worker * Return a context whose memory will be automatically freed at program exit. 244*d83cc019SAndroid Build Coastguard Worker * 245*d83cc019SAndroid Build Coastguard Worker * The first call to this function creates a context and registers a handler 246*d83cc019SAndroid Build Coastguard Worker * to free it using \c atexit. This may cause trouble if used in a library 247*d83cc019SAndroid Build Coastguard Worker * loaded with \c dlopen. 248*d83cc019SAndroid Build Coastguard Worker */ 249*d83cc019SAndroid Build Coastguard Worker void *ralloc_autofree_context(void); 250*d83cc019SAndroid Build Coastguard Worker 251*d83cc019SAndroid Build Coastguard Worker /** 252*d83cc019SAndroid Build Coastguard Worker * Set a callback to occur just before an object is freed. 253*d83cc019SAndroid Build Coastguard Worker */ 254*d83cc019SAndroid Build Coastguard Worker void ralloc_set_destructor(const void *ptr, void(*destructor)(void *)); 255*d83cc019SAndroid Build Coastguard Worker 256*d83cc019SAndroid Build Coastguard Worker /// \defgroup array String Functions @{ 257*d83cc019SAndroid Build Coastguard Worker /** 258*d83cc019SAndroid Build Coastguard Worker * Duplicate a string, allocating the memory from the given context. 259*d83cc019SAndroid Build Coastguard Worker */ 260*d83cc019SAndroid Build Coastguard Worker char *ralloc_strdup(const void *ctx, const char *str); 261*d83cc019SAndroid Build Coastguard Worker 262*d83cc019SAndroid Build Coastguard Worker /** 263*d83cc019SAndroid Build Coastguard Worker * Duplicate a string, allocating the memory from the given context. 264*d83cc019SAndroid Build Coastguard Worker * 265*d83cc019SAndroid Build Coastguard Worker * Like \c strndup, at most \p n characters are copied. If \p str is longer 266*d83cc019SAndroid Build Coastguard Worker * than \p n characters, \p n are copied, and a termining \c '\0' byte is added. 267*d83cc019SAndroid Build Coastguard Worker */ 268*d83cc019SAndroid Build Coastguard Worker char *ralloc_strndup(const void *ctx, const char *str, size_t n); 269*d83cc019SAndroid Build Coastguard Worker 270*d83cc019SAndroid Build Coastguard Worker /** 271*d83cc019SAndroid Build Coastguard Worker * Concatenate two strings, allocating the necessary space. 272*d83cc019SAndroid Build Coastguard Worker * 273*d83cc019SAndroid Build Coastguard Worker * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize 274*d83cc019SAndroid Build Coastguard Worker * to expand \p *dest to the appropriate size. \p dest will be updated to the 275*d83cc019SAndroid Build Coastguard Worker * new pointer unless allocation fails. 276*d83cc019SAndroid Build Coastguard Worker * 277*d83cc019SAndroid Build Coastguard Worker * The result will always be null-terminated. 278*d83cc019SAndroid Build Coastguard Worker * 279*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 280*d83cc019SAndroid Build Coastguard Worker */ 281*d83cc019SAndroid Build Coastguard Worker bool ralloc_strcat(char **dest, const char *str); 282*d83cc019SAndroid Build Coastguard Worker 283*d83cc019SAndroid Build Coastguard Worker /** 284*d83cc019SAndroid Build Coastguard Worker * Concatenate two strings, allocating the necessary space. 285*d83cc019SAndroid Build Coastguard Worker * 286*d83cc019SAndroid Build Coastguard Worker * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize 287*d83cc019SAndroid Build Coastguard Worker * to expand \p *dest to the appropriate size. \p dest will be updated to the 288*d83cc019SAndroid Build Coastguard Worker * new pointer unless allocation fails. 289*d83cc019SAndroid Build Coastguard Worker * 290*d83cc019SAndroid Build Coastguard Worker * The result will always be null-terminated; \p str does not need to be null 291*d83cc019SAndroid Build Coastguard Worker * terminated if it is longer than \p n. 292*d83cc019SAndroid Build Coastguard Worker * 293*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 294*d83cc019SAndroid Build Coastguard Worker */ 295*d83cc019SAndroid Build Coastguard Worker bool ralloc_strncat(char **dest, const char *str, size_t n); 296*d83cc019SAndroid Build Coastguard Worker 297*d83cc019SAndroid Build Coastguard Worker /** 298*d83cc019SAndroid Build Coastguard Worker * Print to a string. 299*d83cc019SAndroid Build Coastguard Worker * 300*d83cc019SAndroid Build Coastguard Worker * This is analogous to \c sprintf, but allocates enough space (using \p ctx 301*d83cc019SAndroid Build Coastguard Worker * as the context) for the resulting string. 302*d83cc019SAndroid Build Coastguard Worker * 303*d83cc019SAndroid Build Coastguard Worker * \return The newly allocated string. 304*d83cc019SAndroid Build Coastguard Worker */ 305*d83cc019SAndroid Build Coastguard Worker char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3); 306*d83cc019SAndroid Build Coastguard Worker 307*d83cc019SAndroid Build Coastguard Worker /** 308*d83cc019SAndroid Build Coastguard Worker * Print to a string, given a va_list. 309*d83cc019SAndroid Build Coastguard Worker * 310*d83cc019SAndroid Build Coastguard Worker * This is analogous to \c vsprintf, but allocates enough space (using \p ctx 311*d83cc019SAndroid Build Coastguard Worker * as the context) for the resulting string. 312*d83cc019SAndroid Build Coastguard Worker * 313*d83cc019SAndroid Build Coastguard Worker * \return The newly allocated string. 314*d83cc019SAndroid Build Coastguard Worker */ 315*d83cc019SAndroid Build Coastguard Worker char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args); 316*d83cc019SAndroid Build Coastguard Worker 317*d83cc019SAndroid Build Coastguard Worker /** 318*d83cc019SAndroid Build Coastguard Worker * Rewrite the tail of an existing string, starting at a given index. 319*d83cc019SAndroid Build Coastguard Worker * 320*d83cc019SAndroid Build Coastguard Worker * Overwrites the contents of *str starting at \p start with newly formatted 321*d83cc019SAndroid Build Coastguard Worker * text, including a new null-terminator. Allocates more memory as necessary. 322*d83cc019SAndroid Build Coastguard Worker * 323*d83cc019SAndroid Build Coastguard Worker * This can be used to append formatted text when the length of the existing 324*d83cc019SAndroid Build Coastguard Worker * string is already known, saving a strlen() call. 325*d83cc019SAndroid Build Coastguard Worker * 326*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_asprintf_append 327*d83cc019SAndroid Build Coastguard Worker * 328*d83cc019SAndroid Build Coastguard Worker * \param str The string to be updated. 329*d83cc019SAndroid Build Coastguard Worker * \param start The index to start appending new data at. 330*d83cc019SAndroid Build Coastguard Worker * \param fmt A printf-style formatting string 331*d83cc019SAndroid Build Coastguard Worker * 332*d83cc019SAndroid Build Coastguard Worker * \p str will be updated to the new pointer unless allocation fails. 333*d83cc019SAndroid Build Coastguard Worker * \p start will be increased by the length of the newly formatted text. 334*d83cc019SAndroid Build Coastguard Worker * 335*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 336*d83cc019SAndroid Build Coastguard Worker */ 337*d83cc019SAndroid Build Coastguard Worker bool ralloc_asprintf_rewrite_tail(char **str, size_t *start, 338*d83cc019SAndroid Build Coastguard Worker const char *fmt, ...) 339*d83cc019SAndroid Build Coastguard Worker PRINTFLIKE(3, 4); 340*d83cc019SAndroid Build Coastguard Worker 341*d83cc019SAndroid Build Coastguard Worker /** 342*d83cc019SAndroid Build Coastguard Worker * Rewrite the tail of an existing string, starting at a given index. 343*d83cc019SAndroid Build Coastguard Worker * 344*d83cc019SAndroid Build Coastguard Worker * Overwrites the contents of *str starting at \p start with newly formatted 345*d83cc019SAndroid Build Coastguard Worker * text, including a new null-terminator. Allocates more memory as necessary. 346*d83cc019SAndroid Build Coastguard Worker * 347*d83cc019SAndroid Build Coastguard Worker * This can be used to append formatted text when the length of the existing 348*d83cc019SAndroid Build Coastguard Worker * string is already known, saving a strlen() call. 349*d83cc019SAndroid Build Coastguard Worker * 350*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_vasprintf_append 351*d83cc019SAndroid Build Coastguard Worker * 352*d83cc019SAndroid Build Coastguard Worker * \param str The string to be updated. 353*d83cc019SAndroid Build Coastguard Worker * \param start The index to start appending new data at. 354*d83cc019SAndroid Build Coastguard Worker * \param fmt A printf-style formatting string 355*d83cc019SAndroid Build Coastguard Worker * \param args A va_list containing the data to be formatted 356*d83cc019SAndroid Build Coastguard Worker * 357*d83cc019SAndroid Build Coastguard Worker * \p str will be updated to the new pointer unless allocation fails. 358*d83cc019SAndroid Build Coastguard Worker * \p start will be increased by the length of the newly formatted text. 359*d83cc019SAndroid Build Coastguard Worker * 360*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 361*d83cc019SAndroid Build Coastguard Worker */ 362*d83cc019SAndroid Build Coastguard Worker bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, 363*d83cc019SAndroid Build Coastguard Worker va_list args); 364*d83cc019SAndroid Build Coastguard Worker 365*d83cc019SAndroid Build Coastguard Worker /** 366*d83cc019SAndroid Build Coastguard Worker * Append formatted text to the supplied string. 367*d83cc019SAndroid Build Coastguard Worker * 368*d83cc019SAndroid Build Coastguard Worker * This is equivalent to 369*d83cc019SAndroid Build Coastguard Worker * \code 370*d83cc019SAndroid Build Coastguard Worker * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...) 371*d83cc019SAndroid Build Coastguard Worker * \endcode 372*d83cc019SAndroid Build Coastguard Worker * 373*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_asprintf 374*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_asprintf_rewrite_tail 375*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_strcat 376*d83cc019SAndroid Build Coastguard Worker * 377*d83cc019SAndroid Build Coastguard Worker * \p str will be updated to the new pointer unless allocation fails. 378*d83cc019SAndroid Build Coastguard Worker * 379*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 380*d83cc019SAndroid Build Coastguard Worker */ 381*d83cc019SAndroid Build Coastguard Worker bool ralloc_asprintf_append (char **str, const char *fmt, ...) 382*d83cc019SAndroid Build Coastguard Worker PRINTFLIKE(2, 3); 383*d83cc019SAndroid Build Coastguard Worker 384*d83cc019SAndroid Build Coastguard Worker /** 385*d83cc019SAndroid Build Coastguard Worker * Append formatted text to the supplied string, given a va_list. 386*d83cc019SAndroid Build Coastguard Worker * 387*d83cc019SAndroid Build Coastguard Worker * This is equivalent to 388*d83cc019SAndroid Build Coastguard Worker * \code 389*d83cc019SAndroid Build Coastguard Worker * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args) 390*d83cc019SAndroid Build Coastguard Worker * \endcode 391*d83cc019SAndroid Build Coastguard Worker * 392*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_vasprintf 393*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_vasprintf_rewrite_tail 394*d83cc019SAndroid Build Coastguard Worker * \sa ralloc_strcat 395*d83cc019SAndroid Build Coastguard Worker * 396*d83cc019SAndroid Build Coastguard Worker * \p str will be updated to the new pointer unless allocation fails. 397*d83cc019SAndroid Build Coastguard Worker * 398*d83cc019SAndroid Build Coastguard Worker * \return True unless allocation failed. 399*d83cc019SAndroid Build Coastguard Worker */ 400*d83cc019SAndroid Build Coastguard Worker bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args); 401*d83cc019SAndroid Build Coastguard Worker /// @} 402*d83cc019SAndroid Build Coastguard Worker 403*d83cc019SAndroid Build Coastguard Worker #ifdef __cplusplus 404*d83cc019SAndroid Build Coastguard Worker } /* end of extern "C" */ 405*d83cc019SAndroid Build Coastguard Worker #endif 406*d83cc019SAndroid Build Coastguard Worker 407*d83cc019SAndroid Build Coastguard Worker #endif 408