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