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