1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2010 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** 25bf215546Sopenharmony_ci * \file ralloc.h 26bf215546Sopenharmony_ci * 27bf215546Sopenharmony_ci * ralloc: a recursive memory allocator 28bf215546Sopenharmony_ci * 29bf215546Sopenharmony_ci * The ralloc memory allocator creates a hierarchy of allocated 30bf215546Sopenharmony_ci * objects. Every allocation is in reference to some parent, and 31bf215546Sopenharmony_ci * every allocated object can in turn be used as the parent of a 32bf215546Sopenharmony_ci * subsequent allocation. This allows for extremely convenient 33bf215546Sopenharmony_ci * discarding of an entire tree/sub-tree of allocations by calling 34bf215546Sopenharmony_ci * ralloc_free on any particular object to free it and all of its 35bf215546Sopenharmony_ci * children. 36bf215546Sopenharmony_ci * 37bf215546Sopenharmony_ci * The conceptual working of ralloc was directly inspired by Andrew 38bf215546Sopenharmony_ci * Tridgell's talloc, but ralloc is an independent implementation 39bf215546Sopenharmony_ci * released under the MIT license and tuned for Mesa. 40bf215546Sopenharmony_ci * 41bf215546Sopenharmony_ci * talloc is more sophisticated than ralloc in that it includes reference 42bf215546Sopenharmony_ci * counting and useful debugging features. However, it is released under 43bf215546Sopenharmony_ci * a non-permissive open source license. 44bf215546Sopenharmony_ci */ 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci#ifndef RALLOC_H 47bf215546Sopenharmony_ci#define RALLOC_H 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci#include <stddef.h> 50bf215546Sopenharmony_ci#include <stdarg.h> 51bf215546Sopenharmony_ci#include <stdbool.h> 52bf215546Sopenharmony_ci 53bf215546Sopenharmony_ci#include "macros.h" 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci#ifdef __cplusplus 56bf215546Sopenharmony_ciextern "C" { 57bf215546Sopenharmony_ci#endif 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci/** 60bf215546Sopenharmony_ci * \def ralloc(ctx, type) 61bf215546Sopenharmony_ci * Allocate a new object chained off of the given context. 62bf215546Sopenharmony_ci * 63bf215546Sopenharmony_ci * This is equivalent to: 64bf215546Sopenharmony_ci * \code 65bf215546Sopenharmony_ci * ((type *) ralloc_size(ctx, sizeof(type)) 66bf215546Sopenharmony_ci * \endcode 67bf215546Sopenharmony_ci */ 68bf215546Sopenharmony_ci#define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type))) 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci/** 71bf215546Sopenharmony_ci * \def rzalloc(ctx, type) 72bf215546Sopenharmony_ci * Allocate a new object out of the given context and initialize it to zero. 73bf215546Sopenharmony_ci * 74bf215546Sopenharmony_ci * This is equivalent to: 75bf215546Sopenharmony_ci * \code 76bf215546Sopenharmony_ci * ((type *) rzalloc_size(ctx, sizeof(type)) 77bf215546Sopenharmony_ci * \endcode 78bf215546Sopenharmony_ci */ 79bf215546Sopenharmony_ci#define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type))) 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_ci/** 82bf215546Sopenharmony_ci * Allocate a new ralloc context. 83bf215546Sopenharmony_ci * 84bf215546Sopenharmony_ci * While any ralloc'd pointer can be used as a context, sometimes it is useful 85bf215546Sopenharmony_ci * to simply allocate a context with no associated memory. 86bf215546Sopenharmony_ci * 87bf215546Sopenharmony_ci * It is equivalent to: 88bf215546Sopenharmony_ci * \code 89bf215546Sopenharmony_ci * ((type *) ralloc_size(ctx, 0) 90bf215546Sopenharmony_ci * \endcode 91bf215546Sopenharmony_ci */ 92bf215546Sopenharmony_civoid *ralloc_context(const void *ctx); 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ci/** 95bf215546Sopenharmony_ci * Allocate memory chained off of the given context. 96bf215546Sopenharmony_ci * 97bf215546Sopenharmony_ci * This is the core allocation routine which is used by all others. It 98bf215546Sopenharmony_ci * simply allocates storage for \p size bytes and returns the pointer, 99bf215546Sopenharmony_ci * similar to \c malloc. 100bf215546Sopenharmony_ci */ 101bf215546Sopenharmony_civoid *ralloc_size(const void *ctx, size_t size) MALLOCLIKE; 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci/** 104bf215546Sopenharmony_ci * Allocate zero-initialized memory chained off of the given context. 105bf215546Sopenharmony_ci * 106bf215546Sopenharmony_ci * This is similar to \c calloc with a size of 1. 107bf215546Sopenharmony_ci */ 108bf215546Sopenharmony_civoid *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci/** 111bf215546Sopenharmony_ci * Resize a piece of ralloc-managed memory, preserving data. 112bf215546Sopenharmony_ci * 113bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 114bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 115bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 116bf215546Sopenharmony_ci * 117bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 118bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 119bf215546Sopenharmony_ci * \param ptr Pointer to the memory to be resized. May be NULL. 120bf215546Sopenharmony_ci * \param size The amount of memory to allocate, in bytes. 121bf215546Sopenharmony_ci */ 122bf215546Sopenharmony_civoid *reralloc_size(const void *ctx, void *ptr, size_t size); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci/** 125bf215546Sopenharmony_ci * Resize a ralloc-managed array, preserving data and initializing any newly 126bf215546Sopenharmony_ci * allocated data to zero. 127bf215546Sopenharmony_ci * 128bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 129bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 130bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 131bf215546Sopenharmony_ci * 132bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 133bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 134bf215546Sopenharmony_ci * \param ptr Pointer to the memory to be resized. May be NULL. 135bf215546Sopenharmony_ci * \param old_size The amount of memory in the previous allocation, in bytes. 136bf215546Sopenharmony_ci * \param new_size The amount of memory to allocate, in bytes. 137bf215546Sopenharmony_ci */ 138bf215546Sopenharmony_civoid *rerzalloc_size(const void *ctx, void *ptr, 139bf215546Sopenharmony_ci size_t old_size, size_t new_size); 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci/// \defgroup array Array Allocators @{ 142bf215546Sopenharmony_ci 143bf215546Sopenharmony_ci/** 144bf215546Sopenharmony_ci * \def ralloc_array(ctx, type, count) 145bf215546Sopenharmony_ci * Allocate an array of objects chained off the given context. 146bf215546Sopenharmony_ci * 147bf215546Sopenharmony_ci * Similar to \c calloc, but does not initialize the memory to zero. 148bf215546Sopenharmony_ci * 149bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 150bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 151bf215546Sopenharmony_ci * 152bf215546Sopenharmony_ci * This is equivalent to: 153bf215546Sopenharmony_ci * \code 154bf215546Sopenharmony_ci * ((type *) ralloc_array_size(ctx, sizeof(type), count) 155bf215546Sopenharmony_ci * \endcode 156bf215546Sopenharmony_ci */ 157bf215546Sopenharmony_ci#define ralloc_array(ctx, type, count) \ 158bf215546Sopenharmony_ci ((type *) ralloc_array_size(ctx, sizeof(type), count)) 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci/** 161bf215546Sopenharmony_ci * \def rzalloc_array(ctx, type, count) 162bf215546Sopenharmony_ci * Allocate a zero-initialized array chained off the given context. 163bf215546Sopenharmony_ci * 164bf215546Sopenharmony_ci * Similar to \c calloc. 165bf215546Sopenharmony_ci * 166bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 167bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 168bf215546Sopenharmony_ci * 169bf215546Sopenharmony_ci * This is equivalent to: 170bf215546Sopenharmony_ci * \code 171bf215546Sopenharmony_ci * ((type *) rzalloc_array_size(ctx, sizeof(type), count) 172bf215546Sopenharmony_ci * \endcode 173bf215546Sopenharmony_ci */ 174bf215546Sopenharmony_ci#define rzalloc_array(ctx, type, count) \ 175bf215546Sopenharmony_ci ((type *) rzalloc_array_size(ctx, sizeof(type), count)) 176bf215546Sopenharmony_ci 177bf215546Sopenharmony_ci/** 178bf215546Sopenharmony_ci * \def reralloc(ctx, ptr, type, count) 179bf215546Sopenharmony_ci * Resize a ralloc-managed array, preserving data. 180bf215546Sopenharmony_ci * 181bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 182bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 183bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 184bf215546Sopenharmony_ci * 185bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 186bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 187bf215546Sopenharmony_ci * 188bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 189bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 190bf215546Sopenharmony_ci * \param ptr Pointer to the array to be resized. May be NULL. 191bf215546Sopenharmony_ci * \param type The element type. 192bf215546Sopenharmony_ci * \param count The number of elements to allocate. 193bf215546Sopenharmony_ci */ 194bf215546Sopenharmony_ci#define reralloc(ctx, ptr, type, count) \ 195bf215546Sopenharmony_ci ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count)) 196bf215546Sopenharmony_ci 197bf215546Sopenharmony_ci/** 198bf215546Sopenharmony_ci * \def rerzalloc(ctx, ptr, type, count) 199bf215546Sopenharmony_ci * Resize a ralloc-managed array, preserving data and initializing any newly 200bf215546Sopenharmony_ci * allocated data to zero. 201bf215546Sopenharmony_ci * 202bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 203bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 204bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 205bf215546Sopenharmony_ci * 206bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 207bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 208bf215546Sopenharmony_ci * 209bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 210bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 211bf215546Sopenharmony_ci * \param ptr Pointer to the array to be resized. May be NULL. 212bf215546Sopenharmony_ci * \param type The element type. 213bf215546Sopenharmony_ci * \param old_count The number of elements in the previous allocation. 214bf215546Sopenharmony_ci * \param new_count The number of elements to allocate. 215bf215546Sopenharmony_ci */ 216bf215546Sopenharmony_ci#define rerzalloc(ctx, ptr, type, old_count, new_count) \ 217bf215546Sopenharmony_ci ((type *) rerzalloc_array_size(ctx, ptr, sizeof(type), old_count, new_count)) 218bf215546Sopenharmony_ci 219bf215546Sopenharmony_ci/** 220bf215546Sopenharmony_ci * Allocate memory for an array chained off the given context. 221bf215546Sopenharmony_ci * 222bf215546Sopenharmony_ci * Similar to \c calloc, but does not initialize the memory to zero. 223bf215546Sopenharmony_ci * 224bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 225bf215546Sopenharmony_ci * multiplying \p size and \p count. This is necessary for security. 226bf215546Sopenharmony_ci */ 227bf215546Sopenharmony_civoid *ralloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE; 228bf215546Sopenharmony_ci 229bf215546Sopenharmony_ci/** 230bf215546Sopenharmony_ci * Allocate a zero-initialized array chained off the given context. 231bf215546Sopenharmony_ci * 232bf215546Sopenharmony_ci * Similar to \c calloc. 233bf215546Sopenharmony_ci * 234bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 235bf215546Sopenharmony_ci * multiplying \p size and \p count. This is necessary for security. 236bf215546Sopenharmony_ci */ 237bf215546Sopenharmony_civoid *rzalloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE; 238bf215546Sopenharmony_ci 239bf215546Sopenharmony_ci/** 240bf215546Sopenharmony_ci * Resize a ralloc-managed array, preserving data. 241bf215546Sopenharmony_ci * 242bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 243bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 244bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 245bf215546Sopenharmony_ci * 246bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 247bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 248bf215546Sopenharmony_ci * 249bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 250bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 251bf215546Sopenharmony_ci * \param ptr Pointer to the array to be resized. May be NULL. 252bf215546Sopenharmony_ci * \param size The size of an individual element. 253bf215546Sopenharmony_ci * \param count The number of elements to allocate. 254bf215546Sopenharmony_ci * 255bf215546Sopenharmony_ci * \return True unless allocation failed. 256bf215546Sopenharmony_ci */ 257bf215546Sopenharmony_civoid *reralloc_array_size(const void *ctx, void *ptr, size_t size, 258bf215546Sopenharmony_ci unsigned count); 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_ci/** 261bf215546Sopenharmony_ci * Resize a ralloc-managed array, preserving data and initializing any newly 262bf215546Sopenharmony_ci * allocated data to zero. 263bf215546Sopenharmony_ci * 264bf215546Sopenharmony_ci * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the 265bf215546Sopenharmony_ci * memory. Instead, it resizes it to a 0-byte ralloc context, just like 266bf215546Sopenharmony_ci * calling ralloc_size(ctx, 0). This is different from talloc. 267bf215546Sopenharmony_ci * 268bf215546Sopenharmony_ci * More than a convenience function, this also checks for integer overflow when 269bf215546Sopenharmony_ci * multiplying \c sizeof(type) and \p count. This is necessary for security. 270bf215546Sopenharmony_ci * 271bf215546Sopenharmony_ci * \param ctx The context to use for new allocation. If \p ptr != NULL, 272bf215546Sopenharmony_ci * it must be the same as ralloc_parent(\p ptr). 273bf215546Sopenharmony_ci * \param ptr Pointer to the array to be resized. May be NULL. 274bf215546Sopenharmony_ci * \param size The size of an individual element. 275bf215546Sopenharmony_ci * \param old_count The number of elements in the previous allocation. 276bf215546Sopenharmony_ci * \param new_count The number of elements to allocate. 277bf215546Sopenharmony_ci * 278bf215546Sopenharmony_ci * \return True unless allocation failed. 279bf215546Sopenharmony_ci */ 280bf215546Sopenharmony_civoid *rerzalloc_array_size(const void *ctx, void *ptr, size_t size, 281bf215546Sopenharmony_ci unsigned old_count, unsigned new_count); 282bf215546Sopenharmony_ci/// @} 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci/** 285bf215546Sopenharmony_ci * Free a piece of ralloc-managed memory. 286bf215546Sopenharmony_ci * 287bf215546Sopenharmony_ci * This will also free the memory of any children allocated this context. 288bf215546Sopenharmony_ci */ 289bf215546Sopenharmony_civoid ralloc_free(void *ptr); 290bf215546Sopenharmony_ci 291bf215546Sopenharmony_ci/** 292bf215546Sopenharmony_ci * "Steal" memory from one context, changing it to another. 293bf215546Sopenharmony_ci * 294bf215546Sopenharmony_ci * This changes \p ptr's context to \p new_ctx. This is quite useful if 295bf215546Sopenharmony_ci * memory is allocated out of a temporary context. 296bf215546Sopenharmony_ci */ 297bf215546Sopenharmony_civoid ralloc_steal(const void *new_ctx, void *ptr); 298bf215546Sopenharmony_ci 299bf215546Sopenharmony_ci/** 300bf215546Sopenharmony_ci * Reparent all children from one context to another. 301bf215546Sopenharmony_ci * 302bf215546Sopenharmony_ci * This effectively calls ralloc_steal(new_ctx, child) for all children of \p old_ctx. 303bf215546Sopenharmony_ci */ 304bf215546Sopenharmony_civoid ralloc_adopt(const void *new_ctx, void *old_ctx); 305bf215546Sopenharmony_ci 306bf215546Sopenharmony_ci/** 307bf215546Sopenharmony_ci * Return the given pointer's ralloc context. 308bf215546Sopenharmony_ci */ 309bf215546Sopenharmony_civoid *ralloc_parent(const void *ptr); 310bf215546Sopenharmony_ci 311bf215546Sopenharmony_ci/** 312bf215546Sopenharmony_ci * Set a callback to occur just before an object is freed. 313bf215546Sopenharmony_ci */ 314bf215546Sopenharmony_civoid ralloc_set_destructor(const void *ptr, void(*destructor)(void *)); 315bf215546Sopenharmony_ci 316bf215546Sopenharmony_ci/// \defgroup array String Functions @{ 317bf215546Sopenharmony_ci/** 318bf215546Sopenharmony_ci * Duplicate a string, allocating the memory from the given context. 319bf215546Sopenharmony_ci */ 320bf215546Sopenharmony_cichar *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE; 321bf215546Sopenharmony_ci 322bf215546Sopenharmony_ci/** 323bf215546Sopenharmony_ci * Duplicate a string, allocating the memory from the given context. 324bf215546Sopenharmony_ci * 325bf215546Sopenharmony_ci * Like \c strndup, at most \p n characters are copied. If \p str is longer 326bf215546Sopenharmony_ci * than \p n characters, \p n are copied, and a termining \c '\0' byte is added. 327bf215546Sopenharmony_ci */ 328bf215546Sopenharmony_cichar *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE; 329bf215546Sopenharmony_ci 330bf215546Sopenharmony_ci/** 331bf215546Sopenharmony_ci * Concatenate two strings, allocating the necessary space. 332bf215546Sopenharmony_ci * 333bf215546Sopenharmony_ci * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize 334bf215546Sopenharmony_ci * to expand \p *dest to the appropriate size. \p dest will be updated to the 335bf215546Sopenharmony_ci * new pointer unless allocation fails. 336bf215546Sopenharmony_ci * 337bf215546Sopenharmony_ci * The result will always be null-terminated. 338bf215546Sopenharmony_ci * 339bf215546Sopenharmony_ci * \return True unless allocation failed. 340bf215546Sopenharmony_ci */ 341bf215546Sopenharmony_cibool ralloc_strcat(char **dest, const char *str); 342bf215546Sopenharmony_ci 343bf215546Sopenharmony_ci/** 344bf215546Sopenharmony_ci * Concatenate two strings, allocating the necessary space. 345bf215546Sopenharmony_ci * 346bf215546Sopenharmony_ci * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize 347bf215546Sopenharmony_ci * to expand \p *dest to the appropriate size. \p dest will be updated to the 348bf215546Sopenharmony_ci * new pointer unless allocation fails. 349bf215546Sopenharmony_ci * 350bf215546Sopenharmony_ci * The result will always be null-terminated; \p str does not need to be null 351bf215546Sopenharmony_ci * terminated if it is longer than \p n. 352bf215546Sopenharmony_ci * 353bf215546Sopenharmony_ci * \return True unless allocation failed. 354bf215546Sopenharmony_ci */ 355bf215546Sopenharmony_cibool ralloc_strncat(char **dest, const char *str, size_t n); 356bf215546Sopenharmony_ci 357bf215546Sopenharmony_ci/** 358bf215546Sopenharmony_ci * Concatenate two strings, allocating the necessary space. 359bf215546Sopenharmony_ci * 360bf215546Sopenharmony_ci * This appends \p n bytes of \p str to \p *dest, using ralloc_resize 361bf215546Sopenharmony_ci * to expand \p *dest to the appropriate size. \p dest will be updated to the 362bf215546Sopenharmony_ci * new pointer unless allocation fails. 363bf215546Sopenharmony_ci * 364bf215546Sopenharmony_ci * The result will always be null-terminated. 365bf215546Sopenharmony_ci * 366bf215546Sopenharmony_ci * This function differs from ralloc_strcat() and ralloc_strncat() in that it 367bf215546Sopenharmony_ci * does not do any strlen() calls which can become costly on large strings. 368bf215546Sopenharmony_ci * 369bf215546Sopenharmony_ci * \return True unless allocation failed. 370bf215546Sopenharmony_ci */ 371bf215546Sopenharmony_cibool 372bf215546Sopenharmony_ciralloc_str_append(char **dest, const char *str, 373bf215546Sopenharmony_ci size_t existing_length, size_t str_size); 374bf215546Sopenharmony_ci 375bf215546Sopenharmony_ci/** 376bf215546Sopenharmony_ci * Print to a string. 377bf215546Sopenharmony_ci * 378bf215546Sopenharmony_ci * This is analogous to \c sprintf, but allocates enough space (using \p ctx 379bf215546Sopenharmony_ci * as the context) for the resulting string. 380bf215546Sopenharmony_ci * 381bf215546Sopenharmony_ci * \return The newly allocated string. 382bf215546Sopenharmony_ci */ 383bf215546Sopenharmony_cichar *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE; 384bf215546Sopenharmony_ci 385bf215546Sopenharmony_ci/** 386bf215546Sopenharmony_ci * Print to a string, given a va_list. 387bf215546Sopenharmony_ci * 388bf215546Sopenharmony_ci * This is analogous to \c vsprintf, but allocates enough space (using \p ctx 389bf215546Sopenharmony_ci * as the context) for the resulting string. 390bf215546Sopenharmony_ci * 391bf215546Sopenharmony_ci * \return The newly allocated string. 392bf215546Sopenharmony_ci */ 393bf215546Sopenharmony_cichar *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE; 394bf215546Sopenharmony_ci 395bf215546Sopenharmony_ci/** 396bf215546Sopenharmony_ci * Rewrite the tail of an existing string, starting at a given index. 397bf215546Sopenharmony_ci * 398bf215546Sopenharmony_ci * Overwrites the contents of *str starting at \p start with newly formatted 399bf215546Sopenharmony_ci * text, including a new null-terminator. Allocates more memory as necessary. 400bf215546Sopenharmony_ci * 401bf215546Sopenharmony_ci * This can be used to append formatted text when the length of the existing 402bf215546Sopenharmony_ci * string is already known, saving a strlen() call. 403bf215546Sopenharmony_ci * 404bf215546Sopenharmony_ci * \sa ralloc_asprintf_append 405bf215546Sopenharmony_ci * 406bf215546Sopenharmony_ci * \param str The string to be updated. 407bf215546Sopenharmony_ci * \param start The index to start appending new data at. 408bf215546Sopenharmony_ci * \param fmt A printf-style formatting string 409bf215546Sopenharmony_ci * 410bf215546Sopenharmony_ci * \p str will be updated to the new pointer unless allocation fails. 411bf215546Sopenharmony_ci * \p start will be increased by the length of the newly formatted text. 412bf215546Sopenharmony_ci * 413bf215546Sopenharmony_ci * \return True unless allocation failed. 414bf215546Sopenharmony_ci */ 415bf215546Sopenharmony_cibool ralloc_asprintf_rewrite_tail(char **str, size_t *start, 416bf215546Sopenharmony_ci const char *fmt, ...) 417bf215546Sopenharmony_ci PRINTFLIKE(3, 4); 418bf215546Sopenharmony_ci 419bf215546Sopenharmony_ci/** 420bf215546Sopenharmony_ci * Rewrite the tail of an existing string, starting at a given index. 421bf215546Sopenharmony_ci * 422bf215546Sopenharmony_ci * Overwrites the contents of *str starting at \p start with newly formatted 423bf215546Sopenharmony_ci * text, including a new null-terminator. Allocates more memory as necessary. 424bf215546Sopenharmony_ci * 425bf215546Sopenharmony_ci * This can be used to append formatted text when the length of the existing 426bf215546Sopenharmony_ci * string is already known, saving a strlen() call. 427bf215546Sopenharmony_ci * 428bf215546Sopenharmony_ci * \sa ralloc_vasprintf_append 429bf215546Sopenharmony_ci * 430bf215546Sopenharmony_ci * \param str The string to be updated. 431bf215546Sopenharmony_ci * \param start The index to start appending new data at. 432bf215546Sopenharmony_ci * \param fmt A printf-style formatting string 433bf215546Sopenharmony_ci * \param args A va_list containing the data to be formatted 434bf215546Sopenharmony_ci * 435bf215546Sopenharmony_ci * \p str will be updated to the new pointer unless allocation fails. 436bf215546Sopenharmony_ci * \p start will be increased by the length of the newly formatted text. 437bf215546Sopenharmony_ci * 438bf215546Sopenharmony_ci * \return True unless allocation failed. 439bf215546Sopenharmony_ci */ 440bf215546Sopenharmony_cibool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt, 441bf215546Sopenharmony_ci va_list args); 442bf215546Sopenharmony_ci 443bf215546Sopenharmony_ci/** 444bf215546Sopenharmony_ci * Append formatted text to the supplied string. 445bf215546Sopenharmony_ci * 446bf215546Sopenharmony_ci * This is equivalent to 447bf215546Sopenharmony_ci * \code 448bf215546Sopenharmony_ci * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...) 449bf215546Sopenharmony_ci * \endcode 450bf215546Sopenharmony_ci * 451bf215546Sopenharmony_ci * \sa ralloc_asprintf 452bf215546Sopenharmony_ci * \sa ralloc_asprintf_rewrite_tail 453bf215546Sopenharmony_ci * \sa ralloc_strcat 454bf215546Sopenharmony_ci * 455bf215546Sopenharmony_ci * \p str will be updated to the new pointer unless allocation fails. 456bf215546Sopenharmony_ci * 457bf215546Sopenharmony_ci * \return True unless allocation failed. 458bf215546Sopenharmony_ci */ 459bf215546Sopenharmony_cibool ralloc_asprintf_append (char **str, const char *fmt, ...) 460bf215546Sopenharmony_ci PRINTFLIKE(2, 3); 461bf215546Sopenharmony_ci 462bf215546Sopenharmony_ci/** 463bf215546Sopenharmony_ci * Append formatted text to the supplied string, given a va_list. 464bf215546Sopenharmony_ci * 465bf215546Sopenharmony_ci * This is equivalent to 466bf215546Sopenharmony_ci * \code 467bf215546Sopenharmony_ci * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args) 468bf215546Sopenharmony_ci * \endcode 469bf215546Sopenharmony_ci * 470bf215546Sopenharmony_ci * \sa ralloc_vasprintf 471bf215546Sopenharmony_ci * \sa ralloc_vasprintf_rewrite_tail 472bf215546Sopenharmony_ci * \sa ralloc_strcat 473bf215546Sopenharmony_ci * 474bf215546Sopenharmony_ci * \p str will be updated to the new pointer unless allocation fails. 475bf215546Sopenharmony_ci * 476bf215546Sopenharmony_ci * \return True unless allocation failed. 477bf215546Sopenharmony_ci */ 478bf215546Sopenharmony_cibool ralloc_vasprintf_append(char **str, const char *fmt, va_list args); 479bf215546Sopenharmony_ci/// @} 480bf215546Sopenharmony_ci 481bf215546Sopenharmony_ci/** 482bf215546Sopenharmony_ci * Declare C++ new and delete operators which use ralloc. 483bf215546Sopenharmony_ci * 484bf215546Sopenharmony_ci * Placing this macro in the body of a class makes it possible to do: 485bf215546Sopenharmony_ci * 486bf215546Sopenharmony_ci * TYPE *var = new(mem_ctx) TYPE(...); 487bf215546Sopenharmony_ci * delete var; 488bf215546Sopenharmony_ci * 489bf215546Sopenharmony_ci * which is more idiomatic in C++ than calling ralloc. 490bf215546Sopenharmony_ci */ 491bf215546Sopenharmony_ci#define DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(TYPE, ALLOC_FUNC) \ 492bf215546Sopenharmony_ciprivate: \ 493bf215546Sopenharmony_ci static void _ralloc_destructor(void *p) \ 494bf215546Sopenharmony_ci { \ 495bf215546Sopenharmony_ci reinterpret_cast<TYPE *>(p)->TYPE::~TYPE(); \ 496bf215546Sopenharmony_ci } \ 497bf215546Sopenharmony_cipublic: \ 498bf215546Sopenharmony_ci static void* operator new(size_t size, void *mem_ctx) \ 499bf215546Sopenharmony_ci { \ 500bf215546Sopenharmony_ci void *p = ALLOC_FUNC(mem_ctx, size); \ 501bf215546Sopenharmony_ci assert(p != NULL); \ 502bf215546Sopenharmony_ci if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \ 503bf215546Sopenharmony_ci ralloc_set_destructor(p, _ralloc_destructor); \ 504bf215546Sopenharmony_ci return p; \ 505bf215546Sopenharmony_ci } \ 506bf215546Sopenharmony_ci \ 507bf215546Sopenharmony_ci static void operator delete(void *p) \ 508bf215546Sopenharmony_ci { \ 509bf215546Sopenharmony_ci /* The object's destructor is guaranteed to have already been \ 510bf215546Sopenharmony_ci * called by the delete operator at this point -- Make sure it's \ 511bf215546Sopenharmony_ci * not called again. \ 512bf215546Sopenharmony_ci */ \ 513bf215546Sopenharmony_ci if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \ 514bf215546Sopenharmony_ci ralloc_set_destructor(p, NULL); \ 515bf215546Sopenharmony_ci ralloc_free(p); \ 516bf215546Sopenharmony_ci } 517bf215546Sopenharmony_ci 518bf215546Sopenharmony_ci#define DECLARE_RALLOC_CXX_OPERATORS(type) \ 519bf215546Sopenharmony_ci DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, ralloc_size) 520bf215546Sopenharmony_ci 521bf215546Sopenharmony_ci#define DECLARE_RZALLOC_CXX_OPERATORS(type) \ 522bf215546Sopenharmony_ci DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, rzalloc_size) 523bf215546Sopenharmony_ci 524bf215546Sopenharmony_ci#define DECLARE_LINEAR_ALLOC_CXX_OPERATORS(type) \ 525bf215546Sopenharmony_ci DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_alloc_child) 526bf215546Sopenharmony_ci 527bf215546Sopenharmony_ci#define DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(type) \ 528bf215546Sopenharmony_ci DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_zalloc_child) 529bf215546Sopenharmony_ci 530bf215546Sopenharmony_ci 531bf215546Sopenharmony_ci/** 532bf215546Sopenharmony_ci * Do a fast allocation from the linear buffer, also known as the child node 533bf215546Sopenharmony_ci * from the allocator's point of view. It can't be freed directly. You have 534bf215546Sopenharmony_ci * to free the parent or the ralloc parent. 535bf215546Sopenharmony_ci * 536bf215546Sopenharmony_ci * \param parent parent node of the linear allocator 537bf215546Sopenharmony_ci * \param size size to allocate (max 32 bits) 538bf215546Sopenharmony_ci */ 539bf215546Sopenharmony_civoid *linear_alloc_child(void *parent, unsigned size); 540bf215546Sopenharmony_ci 541bf215546Sopenharmony_ci/** 542bf215546Sopenharmony_ci * Allocate a parent node that will hold linear buffers. The returned 543bf215546Sopenharmony_ci * allocation is actually the first child node, but it's also the handle 544bf215546Sopenharmony_ci * of the parent node. Use it for all child node allocations. 545bf215546Sopenharmony_ci * 546bf215546Sopenharmony_ci * \param ralloc_ctx ralloc context, must not be NULL 547bf215546Sopenharmony_ci * \param size size to allocate (max 32 bits) 548bf215546Sopenharmony_ci */ 549bf215546Sopenharmony_civoid *linear_alloc_parent(void *ralloc_ctx, unsigned size); 550bf215546Sopenharmony_ci 551bf215546Sopenharmony_ci/** 552bf215546Sopenharmony_ci * Same as linear_alloc_child, but also clears memory. 553bf215546Sopenharmony_ci */ 554bf215546Sopenharmony_civoid *linear_zalloc_child(void *parent, unsigned size); 555bf215546Sopenharmony_ci 556bf215546Sopenharmony_ci/** 557bf215546Sopenharmony_ci * Same as linear_alloc_parent, but also clears memory. 558bf215546Sopenharmony_ci */ 559bf215546Sopenharmony_civoid *linear_zalloc_parent(void *ralloc_ctx, unsigned size); 560bf215546Sopenharmony_ci 561bf215546Sopenharmony_ci/** 562bf215546Sopenharmony_ci * Free the linear parent node. This will free all child nodes too. 563bf215546Sopenharmony_ci * Freeing the ralloc parent will also free this. 564bf215546Sopenharmony_ci */ 565bf215546Sopenharmony_civoid linear_free_parent(void *ptr); 566bf215546Sopenharmony_ci 567bf215546Sopenharmony_ci/** 568bf215546Sopenharmony_ci * Same as ralloc_steal, but steals the linear parent node. 569bf215546Sopenharmony_ci */ 570bf215546Sopenharmony_civoid ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr); 571bf215546Sopenharmony_ci 572bf215546Sopenharmony_ci/** 573bf215546Sopenharmony_ci * Return the ralloc parent of the linear parent node. 574bf215546Sopenharmony_ci */ 575bf215546Sopenharmony_civoid *ralloc_parent_of_linear_parent(void *ptr); 576bf215546Sopenharmony_ci 577bf215546Sopenharmony_ci/** 578bf215546Sopenharmony_ci * Same as realloc except that the linear allocator doesn't free child nodes, 579bf215546Sopenharmony_ci * so it's reduced to memory duplication. It's used in places where 580bf215546Sopenharmony_ci * reallocation is required. Don't use it often. It's much slower than 581bf215546Sopenharmony_ci * realloc. 582bf215546Sopenharmony_ci */ 583bf215546Sopenharmony_civoid *linear_realloc(void *parent, void *old, unsigned new_size); 584bf215546Sopenharmony_ci 585bf215546Sopenharmony_ci/* The functions below have the same semantics as their ralloc counterparts, 586bf215546Sopenharmony_ci * except that they always allocate a linear child node. 587bf215546Sopenharmony_ci */ 588bf215546Sopenharmony_cichar *linear_strdup(void *parent, const char *str); 589bf215546Sopenharmony_cichar *linear_asprintf(void *parent, const char *fmt, ...); 590bf215546Sopenharmony_cichar *linear_vasprintf(void *parent, const char *fmt, va_list args); 591bf215546Sopenharmony_cibool linear_asprintf_append(void *parent, char **str, const char *fmt, ...); 592bf215546Sopenharmony_cibool linear_vasprintf_append(void *parent, char **str, const char *fmt, 593bf215546Sopenharmony_ci va_list args); 594bf215546Sopenharmony_cibool linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start, 595bf215546Sopenharmony_ci const char *fmt, ...); 596bf215546Sopenharmony_cibool linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start, 597bf215546Sopenharmony_ci const char *fmt, va_list args); 598bf215546Sopenharmony_cibool linear_strcat(void *parent, char **dest, const char *str); 599bf215546Sopenharmony_ci 600bf215546Sopenharmony_ci#ifdef __cplusplus 601bf215546Sopenharmony_ci} /* end of extern "C" */ 602bf215546Sopenharmony_ci#endif 603bf215546Sopenharmony_ci 604bf215546Sopenharmony_ci#endif 605