1af69d88dSmrg/*
2af69d88dSmrg * Copyright © 2010 Intel Corporation
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
6af69d88dSmrg * to deal in the Software without restriction, including without limitation
7af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
10af69d88dSmrg *
11af69d88dSmrg * The above copyright notice and this permission notice (including the next
12af69d88dSmrg * paragraph) shall be included in all copies or substantial portions of the
13af69d88dSmrg * Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19af69d88dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20af69d88dSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21af69d88dSmrg * DEALINGS IN THE SOFTWARE.
22af69d88dSmrg */
23af69d88dSmrg
24af69d88dSmrg/**
25af69d88dSmrg * \file ralloc.h
26af69d88dSmrg *
27af69d88dSmrg * ralloc: a recursive memory allocator
28af69d88dSmrg *
29af69d88dSmrg * The ralloc memory allocator creates a hierarchy of allocated
30af69d88dSmrg * objects. Every allocation is in reference to some parent, and
31af69d88dSmrg * every allocated object can in turn be used as the parent of a
32af69d88dSmrg * subsequent allocation. This allows for extremely convenient
33af69d88dSmrg * discarding of an entire tree/sub-tree of allocations by calling
34af69d88dSmrg * ralloc_free on any particular object to free it and all of its
35af69d88dSmrg * children.
36af69d88dSmrg *
37af69d88dSmrg * The conceptual working of ralloc was directly inspired by Andrew
38af69d88dSmrg * Tridgell's talloc, but ralloc is an independent implementation
39af69d88dSmrg * released under the MIT license and tuned for Mesa.
40af69d88dSmrg *
41af69d88dSmrg * talloc is more sophisticated than ralloc in that it includes reference
42af69d88dSmrg * counting and useful debugging features.  However, it is released under
43af69d88dSmrg * a non-permissive open source license.
44af69d88dSmrg */
45af69d88dSmrg
46af69d88dSmrg#ifndef RALLOC_H
47af69d88dSmrg#define RALLOC_H
48af69d88dSmrg
49af69d88dSmrg#include <stddef.h>
50af69d88dSmrg#include <stdarg.h>
51af69d88dSmrg#include <stdbool.h>
52af69d88dSmrg
53af69d88dSmrg#include "macros.h"
54af69d88dSmrg
5501e04c3fSmrg#ifdef __cplusplus
5601e04c3fSmrgextern "C" {
5701e04c3fSmrg#endif
5801e04c3fSmrg
59af69d88dSmrg/**
60af69d88dSmrg * \def ralloc(ctx, type)
61af69d88dSmrg * Allocate a new object chained off of the given context.
62af69d88dSmrg *
63af69d88dSmrg * This is equivalent to:
64af69d88dSmrg * \code
65af69d88dSmrg * ((type *) ralloc_size(ctx, sizeof(type))
66af69d88dSmrg * \endcode
67af69d88dSmrg */
68af69d88dSmrg#define ralloc(ctx, type)  ((type *) ralloc_size(ctx, sizeof(type)))
69af69d88dSmrg
70af69d88dSmrg/**
71af69d88dSmrg * \def rzalloc(ctx, type)
72af69d88dSmrg * Allocate a new object out of the given context and initialize it to zero.
73af69d88dSmrg *
74af69d88dSmrg * This is equivalent to:
75af69d88dSmrg * \code
76af69d88dSmrg * ((type *) rzalloc_size(ctx, sizeof(type))
77af69d88dSmrg * \endcode
78af69d88dSmrg */
79af69d88dSmrg#define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
80af69d88dSmrg
81af69d88dSmrg/**
82af69d88dSmrg * Allocate a new ralloc context.
83af69d88dSmrg *
84af69d88dSmrg * While any ralloc'd pointer can be used as a context, sometimes it is useful
85af69d88dSmrg * to simply allocate a context with no associated memory.
86af69d88dSmrg *
87af69d88dSmrg * It is equivalent to:
88af69d88dSmrg * \code
89af69d88dSmrg * ((type *) ralloc_size(ctx, 0)
90af69d88dSmrg * \endcode
91af69d88dSmrg */
92af69d88dSmrgvoid *ralloc_context(const void *ctx);
93af69d88dSmrg
94af69d88dSmrg/**
95af69d88dSmrg * Allocate memory chained off of the given context.
96af69d88dSmrg *
97af69d88dSmrg * This is the core allocation routine which is used by all others.  It
98af69d88dSmrg * simply allocates storage for \p size bytes and returns the pointer,
99af69d88dSmrg * similar to \c malloc.
100af69d88dSmrg */
10101e04c3fSmrgvoid *ralloc_size(const void *ctx, size_t size) MALLOCLIKE;
102af69d88dSmrg
103af69d88dSmrg/**
104af69d88dSmrg * Allocate zero-initialized memory chained off of the given context.
105af69d88dSmrg *
106af69d88dSmrg * This is similar to \c calloc with a size of 1.
107af69d88dSmrg */
10801e04c3fSmrgvoid *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE;
109af69d88dSmrg
110af69d88dSmrg/**
111af69d88dSmrg * Resize a piece of ralloc-managed memory, preserving data.
112af69d88dSmrg *
113af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
114af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
115af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
116af69d88dSmrg *
117af69d88dSmrg * \param ctx  The context to use for new allocation.  If \p ptr != NULL,
118af69d88dSmrg *             it must be the same as ralloc_parent(\p ptr).
119af69d88dSmrg * \param ptr  Pointer to the memory to be resized.  May be NULL.
120af69d88dSmrg * \param size The amount of memory to allocate, in bytes.
121af69d88dSmrg */
122af69d88dSmrgvoid *reralloc_size(const void *ctx, void *ptr, size_t size);
123af69d88dSmrg
1247ec681f3Smrg/**
1257ec681f3Smrg * Resize a ralloc-managed array, preserving data and initializing any newly
1267ec681f3Smrg * allocated data to zero.
1277ec681f3Smrg *
1287ec681f3Smrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
1297ec681f3Smrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
1307ec681f3Smrg * calling ralloc_size(ctx, 0).  This is different from talloc.
1317ec681f3Smrg *
1327ec681f3Smrg * \param ctx        The context to use for new allocation.  If \p ptr != NULL,
1337ec681f3Smrg *                   it must be the same as ralloc_parent(\p ptr).
1347ec681f3Smrg * \param ptr        Pointer to the memory to be resized.  May be NULL.
1357ec681f3Smrg * \param old_size   The amount of memory in the previous allocation, in bytes.
1367ec681f3Smrg * \param new_size   The amount of memory to allocate, in bytes.
1377ec681f3Smrg */
1387ec681f3Smrgvoid *rerzalloc_size(const void *ctx, void *ptr,
1397ec681f3Smrg                     size_t old_size, size_t new_size);
1407ec681f3Smrg
141af69d88dSmrg/// \defgroup array Array Allocators @{
142af69d88dSmrg
143af69d88dSmrg/**
144af69d88dSmrg * \def ralloc_array(ctx, type, count)
145af69d88dSmrg * Allocate an array of objects chained off the given context.
146af69d88dSmrg *
147af69d88dSmrg * Similar to \c calloc, but does not initialize the memory to zero.
148af69d88dSmrg *
149af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
150af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
151af69d88dSmrg *
152af69d88dSmrg * This is equivalent to:
153af69d88dSmrg * \code
154af69d88dSmrg * ((type *) ralloc_array_size(ctx, sizeof(type), count)
155af69d88dSmrg * \endcode
156af69d88dSmrg */
157af69d88dSmrg#define ralloc_array(ctx, type, count) \
158af69d88dSmrg   ((type *) ralloc_array_size(ctx, sizeof(type), count))
159af69d88dSmrg
160af69d88dSmrg/**
161af69d88dSmrg * \def rzalloc_array(ctx, type, count)
162af69d88dSmrg * Allocate a zero-initialized array chained off the given context.
163af69d88dSmrg *
164af69d88dSmrg * Similar to \c calloc.
165af69d88dSmrg *
166af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
167af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
168af69d88dSmrg *
169af69d88dSmrg * This is equivalent to:
170af69d88dSmrg * \code
171af69d88dSmrg * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
172af69d88dSmrg * \endcode
173af69d88dSmrg */
174af69d88dSmrg#define rzalloc_array(ctx, type, count) \
175af69d88dSmrg   ((type *) rzalloc_array_size(ctx, sizeof(type), count))
176af69d88dSmrg
177af69d88dSmrg/**
178af69d88dSmrg * \def reralloc(ctx, ptr, type, count)
179af69d88dSmrg * Resize a ralloc-managed array, preserving data.
180af69d88dSmrg *
181af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
182af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
183af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
184af69d88dSmrg *
185af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
186af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
187af69d88dSmrg *
188af69d88dSmrg * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
189af69d88dSmrg *              it must be the same as ralloc_parent(\p ptr).
190af69d88dSmrg * \param ptr   Pointer to the array to be resized.  May be NULL.
191af69d88dSmrg * \param type  The element type.
192af69d88dSmrg * \param count The number of elements to allocate.
193af69d88dSmrg */
194af69d88dSmrg#define reralloc(ctx, ptr, type, count) \
195af69d88dSmrg   ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
196af69d88dSmrg
1977ec681f3Smrg/**
1987ec681f3Smrg * \def rerzalloc(ctx, ptr, type, count)
1997ec681f3Smrg * Resize a ralloc-managed array, preserving data and initializing any newly
2007ec681f3Smrg * allocated data to zero.
2017ec681f3Smrg *
2027ec681f3Smrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
2037ec681f3Smrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
2047ec681f3Smrg * calling ralloc_size(ctx, 0).  This is different from talloc.
2057ec681f3Smrg *
2067ec681f3Smrg * More than a convenience function, this also checks for integer overflow when
2077ec681f3Smrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
2087ec681f3Smrg *
2097ec681f3Smrg * \param ctx        The context to use for new allocation.  If \p ptr != NULL,
2107ec681f3Smrg *                   it must be the same as ralloc_parent(\p ptr).
2117ec681f3Smrg * \param ptr        Pointer to the array to be resized.  May be NULL.
2127ec681f3Smrg * \param type       The element type.
2137ec681f3Smrg * \param old_count  The number of elements in the previous allocation.
2147ec681f3Smrg * \param new_count  The number of elements to allocate.
2157ec681f3Smrg */
2167ec681f3Smrg#define rerzalloc(ctx, ptr, type, old_count, new_count) \
2177ec681f3Smrg   ((type *) rerzalloc_array_size(ctx, ptr, sizeof(type), old_count, new_count))
2187ec681f3Smrg
219af69d88dSmrg/**
220af69d88dSmrg * Allocate memory for an array chained off the given context.
221af69d88dSmrg *
222af69d88dSmrg * Similar to \c calloc, but does not initialize the memory to zero.
223af69d88dSmrg *
224af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
225af69d88dSmrg * multiplying \p size and \p count.  This is necessary for security.
226af69d88dSmrg */
22701e04c3fSmrgvoid *ralloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
228af69d88dSmrg
229af69d88dSmrg/**
230af69d88dSmrg * Allocate a zero-initialized array chained off the given context.
231af69d88dSmrg *
232af69d88dSmrg * Similar to \c calloc.
233af69d88dSmrg *
234af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
235af69d88dSmrg * multiplying \p size and \p count.  This is necessary for security.
236af69d88dSmrg */
23701e04c3fSmrgvoid *rzalloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
238af69d88dSmrg
239af69d88dSmrg/**
240af69d88dSmrg * Resize a ralloc-managed array, preserving data.
241af69d88dSmrg *
242af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
243af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
244af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
245af69d88dSmrg *
246af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
247af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
248af69d88dSmrg *
249af69d88dSmrg * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
250af69d88dSmrg *              it must be the same as ralloc_parent(\p ptr).
251af69d88dSmrg * \param ptr   Pointer to the array to be resized.  May be NULL.
252af69d88dSmrg * \param size  The size of an individual element.
253af69d88dSmrg * \param count The number of elements to allocate.
254af69d88dSmrg *
255af69d88dSmrg * \return True unless allocation failed.
256af69d88dSmrg */
257af69d88dSmrgvoid *reralloc_array_size(const void *ctx, void *ptr, size_t size,
258af69d88dSmrg			  unsigned count);
2597ec681f3Smrg
2607ec681f3Smrg/**
2617ec681f3Smrg * Resize a ralloc-managed array, preserving data and initializing any newly
2627ec681f3Smrg * allocated data to zero.
2637ec681f3Smrg *
2647ec681f3Smrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
2657ec681f3Smrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
2667ec681f3Smrg * calling ralloc_size(ctx, 0).  This is different from talloc.
2677ec681f3Smrg *
2687ec681f3Smrg * More than a convenience function, this also checks for integer overflow when
2697ec681f3Smrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
2707ec681f3Smrg *
2717ec681f3Smrg * \param ctx        The context to use for new allocation.  If \p ptr != NULL,
2727ec681f3Smrg *                   it must be the same as ralloc_parent(\p ptr).
2737ec681f3Smrg * \param ptr        Pointer to the array to be resized.  May be NULL.
2747ec681f3Smrg * \param size       The size of an individual element.
2757ec681f3Smrg * \param old_count  The number of elements in the previous allocation.
2767ec681f3Smrg * \param new_count  The number of elements to allocate.
2777ec681f3Smrg *
2787ec681f3Smrg * \return True unless allocation failed.
2797ec681f3Smrg */
2807ec681f3Smrgvoid *rerzalloc_array_size(const void *ctx, void *ptr, size_t size,
2817ec681f3Smrg			   unsigned old_count, unsigned new_count);
282af69d88dSmrg/// @}
283af69d88dSmrg
284af69d88dSmrg/**
285af69d88dSmrg * Free a piece of ralloc-managed memory.
286af69d88dSmrg *
287af69d88dSmrg * This will also free the memory of any children allocated this context.
288af69d88dSmrg */
289af69d88dSmrgvoid ralloc_free(void *ptr);
290af69d88dSmrg
291af69d88dSmrg/**
292af69d88dSmrg * "Steal" memory from one context, changing it to another.
293af69d88dSmrg *
294af69d88dSmrg * This changes \p ptr's context to \p new_ctx.  This is quite useful if
295af69d88dSmrg * memory is allocated out of a temporary context.
296af69d88dSmrg */
297af69d88dSmrgvoid ralloc_steal(const void *new_ctx, void *ptr);
298af69d88dSmrg
299af69d88dSmrg/**
30001e04c3fSmrg * Reparent all children from one context to another.
30101e04c3fSmrg *
30201e04c3fSmrg * This effectively calls ralloc_steal(new_ctx, child) for all children of \p old_ctx.
303af69d88dSmrg */
30401e04c3fSmrgvoid ralloc_adopt(const void *new_ctx, void *old_ctx);
305af69d88dSmrg
306af69d88dSmrg/**
30701e04c3fSmrg * Return the given pointer's ralloc context.
308af69d88dSmrg */
30901e04c3fSmrgvoid *ralloc_parent(const void *ptr);
310af69d88dSmrg
311af69d88dSmrg/**
312af69d88dSmrg * Set a callback to occur just before an object is freed.
313af69d88dSmrg */
314af69d88dSmrgvoid ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
315af69d88dSmrg
316af69d88dSmrg/// \defgroup array String Functions @{
317af69d88dSmrg/**
318af69d88dSmrg * Duplicate a string, allocating the memory from the given context.
319af69d88dSmrg */
32001e04c3fSmrgchar *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE;
321af69d88dSmrg
322af69d88dSmrg/**
323af69d88dSmrg * Duplicate a string, allocating the memory from the given context.
324af69d88dSmrg *
325af69d88dSmrg * Like \c strndup, at most \p n characters are copied.  If \p str is longer
326af69d88dSmrg * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
327af69d88dSmrg */
32801e04c3fSmrgchar *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE;
329af69d88dSmrg
330af69d88dSmrg/**
331af69d88dSmrg * Concatenate two strings, allocating the necessary space.
332af69d88dSmrg *
333af69d88dSmrg * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
334af69d88dSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
335af69d88dSmrg * new pointer unless allocation fails.
336af69d88dSmrg *
337af69d88dSmrg * The result will always be null-terminated.
338af69d88dSmrg *
339af69d88dSmrg * \return True unless allocation failed.
340af69d88dSmrg */
341af69d88dSmrgbool ralloc_strcat(char **dest, const char *str);
342af69d88dSmrg
343af69d88dSmrg/**
344af69d88dSmrg * Concatenate two strings, allocating the necessary space.
345af69d88dSmrg *
346af69d88dSmrg * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
347af69d88dSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
348af69d88dSmrg * new pointer unless allocation fails.
349af69d88dSmrg *
350af69d88dSmrg * The result will always be null-terminated; \p str does not need to be null
351af69d88dSmrg * terminated if it is longer than \p n.
352af69d88dSmrg *
353af69d88dSmrg * \return True unless allocation failed.
354af69d88dSmrg */
355af69d88dSmrgbool ralloc_strncat(char **dest, const char *str, size_t n);
356af69d88dSmrg
35701e04c3fSmrg/**
35801e04c3fSmrg * Concatenate two strings, allocating the necessary space.
35901e04c3fSmrg *
36001e04c3fSmrg * This appends \p n bytes of \p str to \p *dest, using ralloc_resize
36101e04c3fSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
36201e04c3fSmrg * new pointer unless allocation fails.
36301e04c3fSmrg *
36401e04c3fSmrg * The result will always be null-terminated.
36501e04c3fSmrg *
36601e04c3fSmrg * This function differs from ralloc_strcat() and ralloc_strncat() in that it
36701e04c3fSmrg * does not do any strlen() calls which can become costly on large strings.
36801e04c3fSmrg *
36901e04c3fSmrg * \return True unless allocation failed.
37001e04c3fSmrg */
37101e04c3fSmrgbool
37201e04c3fSmrgralloc_str_append(char **dest, const char *str,
37301e04c3fSmrg                  size_t existing_length, size_t str_size);
37401e04c3fSmrg
375af69d88dSmrg/**
376af69d88dSmrg * Print to a string.
377af69d88dSmrg *
378af69d88dSmrg * This is analogous to \c sprintf, but allocates enough space (using \p ctx
379af69d88dSmrg * as the context) for the resulting string.
380af69d88dSmrg *
381af69d88dSmrg * \return The newly allocated string.
382af69d88dSmrg */
38301e04c3fSmrgchar *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
384af69d88dSmrg
385af69d88dSmrg/**
386af69d88dSmrg * Print to a string, given a va_list.
387af69d88dSmrg *
388af69d88dSmrg * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
389af69d88dSmrg * as the context) for the resulting string.
390af69d88dSmrg *
391af69d88dSmrg * \return The newly allocated string.
392af69d88dSmrg */
39301e04c3fSmrgchar *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE;
394af69d88dSmrg
395af69d88dSmrg/**
396af69d88dSmrg * Rewrite the tail of an existing string, starting at a given index.
397af69d88dSmrg *
398af69d88dSmrg * Overwrites the contents of *str starting at \p start with newly formatted
399af69d88dSmrg * text, including a new null-terminator.  Allocates more memory as necessary.
400af69d88dSmrg *
401af69d88dSmrg * This can be used to append formatted text when the length of the existing
402af69d88dSmrg * string is already known, saving a strlen() call.
403af69d88dSmrg *
404af69d88dSmrg * \sa ralloc_asprintf_append
405af69d88dSmrg *
406af69d88dSmrg * \param str   The string to be updated.
407af69d88dSmrg * \param start The index to start appending new data at.
408af69d88dSmrg * \param fmt   A printf-style formatting string
409af69d88dSmrg *
410af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
411af69d88dSmrg * \p start will be increased by the length of the newly formatted text.
412af69d88dSmrg *
413af69d88dSmrg * \return True unless allocation failed.
414af69d88dSmrg */
415af69d88dSmrgbool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
416af69d88dSmrg				  const char *fmt, ...)
417af69d88dSmrg				  PRINTFLIKE(3, 4);
418af69d88dSmrg
419af69d88dSmrg/**
420af69d88dSmrg * Rewrite the tail of an existing string, starting at a given index.
421af69d88dSmrg *
422af69d88dSmrg * Overwrites the contents of *str starting at \p start with newly formatted
423af69d88dSmrg * text, including a new null-terminator.  Allocates more memory as necessary.
424af69d88dSmrg *
425af69d88dSmrg * This can be used to append formatted text when the length of the existing
426af69d88dSmrg * string is already known, saving a strlen() call.
427af69d88dSmrg *
428af69d88dSmrg * \sa ralloc_vasprintf_append
429af69d88dSmrg *
430af69d88dSmrg * \param str   The string to be updated.
431af69d88dSmrg * \param start The index to start appending new data at.
432af69d88dSmrg * \param fmt   A printf-style formatting string
433af69d88dSmrg * \param args  A va_list containing the data to be formatted
434af69d88dSmrg *
435af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
436af69d88dSmrg * \p start will be increased by the length of the newly formatted text.
437af69d88dSmrg *
438af69d88dSmrg * \return True unless allocation failed.
439af69d88dSmrg */
440af69d88dSmrgbool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
441af69d88dSmrg				   va_list args);
442af69d88dSmrg
443af69d88dSmrg/**
444af69d88dSmrg * Append formatted text to the supplied string.
445af69d88dSmrg *
446af69d88dSmrg * This is equivalent to
447af69d88dSmrg * \code
448af69d88dSmrg * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
449af69d88dSmrg * \endcode
450af69d88dSmrg *
451af69d88dSmrg * \sa ralloc_asprintf
452af69d88dSmrg * \sa ralloc_asprintf_rewrite_tail
453af69d88dSmrg * \sa ralloc_strcat
454af69d88dSmrg *
455af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
456af69d88dSmrg *
457af69d88dSmrg * \return True unless allocation failed.
458af69d88dSmrg */
459af69d88dSmrgbool ralloc_asprintf_append (char **str, const char *fmt, ...)
460af69d88dSmrg			     PRINTFLIKE(2, 3);
461af69d88dSmrg
462af69d88dSmrg/**
463af69d88dSmrg * Append formatted text to the supplied string, given a va_list.
464af69d88dSmrg *
465af69d88dSmrg * This is equivalent to
466af69d88dSmrg * \code
467af69d88dSmrg * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
468af69d88dSmrg * \endcode
469af69d88dSmrg *
470af69d88dSmrg * \sa ralloc_vasprintf
471af69d88dSmrg * \sa ralloc_vasprintf_rewrite_tail
472af69d88dSmrg * \sa ralloc_strcat
473af69d88dSmrg *
474af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
475af69d88dSmrg *
476af69d88dSmrg * \return True unless allocation failed.
477af69d88dSmrg */
478af69d88dSmrgbool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
479af69d88dSmrg/// @}
480af69d88dSmrg
481af69d88dSmrg/**
482af69d88dSmrg * Declare C++ new and delete operators which use ralloc.
483af69d88dSmrg *
484af69d88dSmrg * Placing this macro in the body of a class makes it possible to do:
485af69d88dSmrg *
486af69d88dSmrg * TYPE *var = new(mem_ctx) TYPE(...);
487af69d88dSmrg * delete var;
488af69d88dSmrg *
489af69d88dSmrg * which is more idiomatic in C++ than calling ralloc.
490af69d88dSmrg */
49101e04c3fSmrg#define DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(TYPE, ALLOC_FUNC)           \
492af69d88dSmrgprivate:                                                                 \
493af69d88dSmrg   static void _ralloc_destructor(void *p)                               \
494af69d88dSmrg   {                                                                     \
4958a1362adSmaya      reinterpret_cast<TYPE *>(p)->TYPE::~TYPE();                        \
496af69d88dSmrg   }                                                                     \
497af69d88dSmrgpublic:                                                                  \
498af69d88dSmrg   static void* operator new(size_t size, void *mem_ctx)                 \
499af69d88dSmrg   {                                                                     \
50001e04c3fSmrg      void *p = ALLOC_FUNC(mem_ctx, size);                               \
501af69d88dSmrg      assert(p != NULL);                                                 \
502af69d88dSmrg      if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
503af69d88dSmrg         ralloc_set_destructor(p, _ralloc_destructor);                   \
504af69d88dSmrg      return p;                                                          \
505af69d88dSmrg   }                                                                     \
506af69d88dSmrg                                                                         \
507af69d88dSmrg   static void operator delete(void *p)                                  \
508af69d88dSmrg   {                                                                     \
509af69d88dSmrg      /* The object's destructor is guaranteed to have already been      \
510af69d88dSmrg       * called by the delete operator at this point -- Make sure it's   \
511af69d88dSmrg       * not called again.                                               \
512af69d88dSmrg       */                                                                \
513af69d88dSmrg      if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
514af69d88dSmrg         ralloc_set_destructor(p, NULL);                                 \
515af69d88dSmrg      ralloc_free(p);                                                    \
516af69d88dSmrg   }
517af69d88dSmrg
51801e04c3fSmrg#define DECLARE_RALLOC_CXX_OPERATORS(type) \
51901e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, ralloc_size)
52001e04c3fSmrg
52101e04c3fSmrg#define DECLARE_RZALLOC_CXX_OPERATORS(type) \
52201e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, rzalloc_size)
52301e04c3fSmrg
52401e04c3fSmrg#define DECLARE_LINEAR_ALLOC_CXX_OPERATORS(type) \
52501e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_alloc_child)
52601e04c3fSmrg
52701e04c3fSmrg#define DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(type) \
52801e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_zalloc_child)
52901e04c3fSmrg
53001e04c3fSmrg
53101e04c3fSmrg/**
53201e04c3fSmrg * Do a fast allocation from the linear buffer, also known as the child node
53301e04c3fSmrg * from the allocator's point of view. It can't be freed directly. You have
53401e04c3fSmrg * to free the parent or the ralloc parent.
53501e04c3fSmrg *
53601e04c3fSmrg * \param parent   parent node of the linear allocator
53701e04c3fSmrg * \param size     size to allocate (max 32 bits)
53801e04c3fSmrg */
53901e04c3fSmrgvoid *linear_alloc_child(void *parent, unsigned size);
54001e04c3fSmrg
54101e04c3fSmrg/**
54201e04c3fSmrg * Allocate a parent node that will hold linear buffers. The returned
54301e04c3fSmrg * allocation is actually the first child node, but it's also the handle
54401e04c3fSmrg * of the parent node. Use it for all child node allocations.
54501e04c3fSmrg *
54601e04c3fSmrg * \param ralloc_ctx  ralloc context, must not be NULL
54701e04c3fSmrg * \param size        size to allocate (max 32 bits)
54801e04c3fSmrg */
54901e04c3fSmrgvoid *linear_alloc_parent(void *ralloc_ctx, unsigned size);
55001e04c3fSmrg
55101e04c3fSmrg/**
55201e04c3fSmrg * Same as linear_alloc_child, but also clears memory.
55301e04c3fSmrg */
55401e04c3fSmrgvoid *linear_zalloc_child(void *parent, unsigned size);
55501e04c3fSmrg
55601e04c3fSmrg/**
55701e04c3fSmrg * Same as linear_alloc_parent, but also clears memory.
55801e04c3fSmrg */
55901e04c3fSmrgvoid *linear_zalloc_parent(void *ralloc_ctx, unsigned size);
56001e04c3fSmrg
56101e04c3fSmrg/**
56201e04c3fSmrg * Free the linear parent node. This will free all child nodes too.
56301e04c3fSmrg * Freeing the ralloc parent will also free this.
56401e04c3fSmrg */
56501e04c3fSmrgvoid linear_free_parent(void *ptr);
56601e04c3fSmrg
56701e04c3fSmrg/**
56801e04c3fSmrg * Same as ralloc_steal, but steals the linear parent node.
56901e04c3fSmrg */
57001e04c3fSmrgvoid ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr);
57101e04c3fSmrg
57201e04c3fSmrg/**
57301e04c3fSmrg * Return the ralloc parent of the linear parent node.
57401e04c3fSmrg */
57501e04c3fSmrgvoid *ralloc_parent_of_linear_parent(void *ptr);
57601e04c3fSmrg
57701e04c3fSmrg/**
57801e04c3fSmrg * Same as realloc except that the linear allocator doesn't free child nodes,
57901e04c3fSmrg * so it's reduced to memory duplication. It's used in places where
58001e04c3fSmrg * reallocation is required. Don't use it often. It's much slower than
58101e04c3fSmrg * realloc.
58201e04c3fSmrg */
58301e04c3fSmrgvoid *linear_realloc(void *parent, void *old, unsigned new_size);
58401e04c3fSmrg
58501e04c3fSmrg/* The functions below have the same semantics as their ralloc counterparts,
58601e04c3fSmrg * except that they always allocate a linear child node.
58701e04c3fSmrg */
58801e04c3fSmrgchar *linear_strdup(void *parent, const char *str);
58901e04c3fSmrgchar *linear_asprintf(void *parent, const char *fmt, ...);
59001e04c3fSmrgchar *linear_vasprintf(void *parent, const char *fmt, va_list args);
59101e04c3fSmrgbool linear_asprintf_append(void *parent, char **str, const char *fmt, ...);
59201e04c3fSmrgbool linear_vasprintf_append(void *parent, char **str, const char *fmt,
59301e04c3fSmrg                             va_list args);
59401e04c3fSmrgbool linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
59501e04c3fSmrg                                  const char *fmt, ...);
59601e04c3fSmrgbool linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
59701e04c3fSmrg                                   const char *fmt, va_list args);
59801e04c3fSmrgbool linear_strcat(void *parent, char **dest, const char *str);
59901e04c3fSmrg
60001e04c3fSmrg#ifdef __cplusplus
60101e04c3fSmrg} /* end of extern "C" */
60201e04c3fSmrg#endif
603af69d88dSmrg
604af69d88dSmrg#endif
605