ralloc.h revision 8a1362ad
1af69d88dSmrg/*
2af69d88dSmrg * Copyright © 2010 Intel Corporation
3af69d88dSmrg *
4af69d88dSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5af69d88dSmrg * copy of this software and associated documentation files (the "Software"),
6af69d88dSmrg * to deal in the Software without restriction, including without limitation
7af69d88dSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8af69d88dSmrg * and/or sell copies of the Software, and to permit persons to whom the
9af69d88dSmrg * Software is furnished to do so, subject to the following conditions:
10af69d88dSmrg *
11af69d88dSmrg * The above copyright notice and this permission notice (including the next
12af69d88dSmrg * paragraph) shall be included in all copies or substantial portions of the
13af69d88dSmrg * Software.
14af69d88dSmrg *
15af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19af69d88dSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20af69d88dSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21af69d88dSmrg * DEALINGS IN THE SOFTWARE.
22af69d88dSmrg */
23af69d88dSmrg
24af69d88dSmrg/**
25af69d88dSmrg * \file ralloc.h
26af69d88dSmrg *
27af69d88dSmrg * ralloc: a recursive memory allocator
28af69d88dSmrg *
29af69d88dSmrg * The ralloc memory allocator creates a hierarchy of allocated
30af69d88dSmrg * objects. Every allocation is in reference to some parent, and
31af69d88dSmrg * every allocated object can in turn be used as the parent of a
32af69d88dSmrg * subsequent allocation. This allows for extremely convenient
33af69d88dSmrg * discarding of an entire tree/sub-tree of allocations by calling
34af69d88dSmrg * ralloc_free on any particular object to free it and all of its
35af69d88dSmrg * children.
36af69d88dSmrg *
37af69d88dSmrg * The conceptual working of ralloc was directly inspired by Andrew
38af69d88dSmrg * Tridgell's talloc, but ralloc is an independent implementation
39af69d88dSmrg * released under the MIT license and tuned for Mesa.
40af69d88dSmrg *
41af69d88dSmrg * talloc is more sophisticated than ralloc in that it includes reference
42af69d88dSmrg * counting and useful debugging features.  However, it is released under
43af69d88dSmrg * a non-permissive open source license.
44af69d88dSmrg */
45af69d88dSmrg
46af69d88dSmrg#ifndef RALLOC_H
47af69d88dSmrg#define RALLOC_H
48af69d88dSmrg
49af69d88dSmrg#include <stddef.h>
50af69d88dSmrg#include <stdarg.h>
51af69d88dSmrg#include <stdbool.h>
52af69d88dSmrg
53af69d88dSmrg#include "macros.h"
54af69d88dSmrg
5501e04c3fSmrg#ifdef __cplusplus
5601e04c3fSmrgextern "C" {
5701e04c3fSmrg#endif
5801e04c3fSmrg
59af69d88dSmrg/**
60af69d88dSmrg * \def ralloc(ctx, type)
61af69d88dSmrg * Allocate a new object chained off of the given context.
62af69d88dSmrg *
63af69d88dSmrg * This is equivalent to:
64af69d88dSmrg * \code
65af69d88dSmrg * ((type *) ralloc_size(ctx, sizeof(type))
66af69d88dSmrg * \endcode
67af69d88dSmrg */
68af69d88dSmrg#define ralloc(ctx, type)  ((type *) ralloc_size(ctx, sizeof(type)))
69af69d88dSmrg
70af69d88dSmrg/**
71af69d88dSmrg * \def rzalloc(ctx, type)
72af69d88dSmrg * Allocate a new object out of the given context and initialize it to zero.
73af69d88dSmrg *
74af69d88dSmrg * This is equivalent to:
75af69d88dSmrg * \code
76af69d88dSmrg * ((type *) rzalloc_size(ctx, sizeof(type))
77af69d88dSmrg * \endcode
78af69d88dSmrg */
79af69d88dSmrg#define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
80af69d88dSmrg
81af69d88dSmrg/**
82af69d88dSmrg * Allocate a new ralloc context.
83af69d88dSmrg *
84af69d88dSmrg * While any ralloc'd pointer can be used as a context, sometimes it is useful
85af69d88dSmrg * to simply allocate a context with no associated memory.
86af69d88dSmrg *
87af69d88dSmrg * It is equivalent to:
88af69d88dSmrg * \code
89af69d88dSmrg * ((type *) ralloc_size(ctx, 0)
90af69d88dSmrg * \endcode
91af69d88dSmrg */
92af69d88dSmrgvoid *ralloc_context(const void *ctx);
93af69d88dSmrg
94af69d88dSmrg/**
95af69d88dSmrg * Allocate memory chained off of the given context.
96af69d88dSmrg *
97af69d88dSmrg * This is the core allocation routine which is used by all others.  It
98af69d88dSmrg * simply allocates storage for \p size bytes and returns the pointer,
99af69d88dSmrg * similar to \c malloc.
100af69d88dSmrg */
10101e04c3fSmrgvoid *ralloc_size(const void *ctx, size_t size) MALLOCLIKE;
102af69d88dSmrg
103af69d88dSmrg/**
104af69d88dSmrg * Allocate zero-initialized memory chained off of the given context.
105af69d88dSmrg *
106af69d88dSmrg * This is similar to \c calloc with a size of 1.
107af69d88dSmrg */
10801e04c3fSmrgvoid *rzalloc_size(const void *ctx, size_t size) MALLOCLIKE;
109af69d88dSmrg
110af69d88dSmrg/**
111af69d88dSmrg * Resize a piece of ralloc-managed memory, preserving data.
112af69d88dSmrg *
113af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
114af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
115af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
116af69d88dSmrg *
117af69d88dSmrg * \param ctx  The context to use for new allocation.  If \p ptr != NULL,
118af69d88dSmrg *             it must be the same as ralloc_parent(\p ptr).
119af69d88dSmrg * \param ptr  Pointer to the memory to be resized.  May be NULL.
120af69d88dSmrg * \param size The amount of memory to allocate, in bytes.
121af69d88dSmrg */
122af69d88dSmrgvoid *reralloc_size(const void *ctx, void *ptr, size_t size);
123af69d88dSmrg
124af69d88dSmrg/// \defgroup array Array Allocators @{
125af69d88dSmrg
126af69d88dSmrg/**
127af69d88dSmrg * \def ralloc_array(ctx, type, count)
128af69d88dSmrg * Allocate an array of objects chained off the given context.
129af69d88dSmrg *
130af69d88dSmrg * Similar to \c calloc, but does not initialize the memory to zero.
131af69d88dSmrg *
132af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
133af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
134af69d88dSmrg *
135af69d88dSmrg * This is equivalent to:
136af69d88dSmrg * \code
137af69d88dSmrg * ((type *) ralloc_array_size(ctx, sizeof(type), count)
138af69d88dSmrg * \endcode
139af69d88dSmrg */
140af69d88dSmrg#define ralloc_array(ctx, type, count) \
141af69d88dSmrg   ((type *) ralloc_array_size(ctx, sizeof(type), count))
142af69d88dSmrg
143af69d88dSmrg/**
144af69d88dSmrg * \def rzalloc_array(ctx, type, count)
145af69d88dSmrg * Allocate a zero-initialized array chained off the given context.
146af69d88dSmrg *
147af69d88dSmrg * Similar to \c calloc.
148af69d88dSmrg *
149af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
150af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
151af69d88dSmrg *
152af69d88dSmrg * This is equivalent to:
153af69d88dSmrg * \code
154af69d88dSmrg * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
155af69d88dSmrg * \endcode
156af69d88dSmrg */
157af69d88dSmrg#define rzalloc_array(ctx, type, count) \
158af69d88dSmrg   ((type *) rzalloc_array_size(ctx, sizeof(type), count))
159af69d88dSmrg
160af69d88dSmrg/**
161af69d88dSmrg * \def reralloc(ctx, ptr, type, count)
162af69d88dSmrg * Resize a ralloc-managed array, preserving data.
163af69d88dSmrg *
164af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
165af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
166af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
167af69d88dSmrg *
168af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
169af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
170af69d88dSmrg *
171af69d88dSmrg * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
172af69d88dSmrg *              it must be the same as ralloc_parent(\p ptr).
173af69d88dSmrg * \param ptr   Pointer to the array to be resized.  May be NULL.
174af69d88dSmrg * \param type  The element type.
175af69d88dSmrg * \param count The number of elements to allocate.
176af69d88dSmrg */
177af69d88dSmrg#define reralloc(ctx, ptr, type, count) \
178af69d88dSmrg   ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
179af69d88dSmrg
180af69d88dSmrg/**
181af69d88dSmrg * Allocate memory for an array chained off the given context.
182af69d88dSmrg *
183af69d88dSmrg * Similar to \c calloc, but does not initialize the memory to zero.
184af69d88dSmrg *
185af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
186af69d88dSmrg * multiplying \p size and \p count.  This is necessary for security.
187af69d88dSmrg */
18801e04c3fSmrgvoid *ralloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
189af69d88dSmrg
190af69d88dSmrg/**
191af69d88dSmrg * Allocate a zero-initialized array chained off the given context.
192af69d88dSmrg *
193af69d88dSmrg * Similar to \c calloc.
194af69d88dSmrg *
195af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
196af69d88dSmrg * multiplying \p size and \p count.  This is necessary for security.
197af69d88dSmrg */
19801e04c3fSmrgvoid *rzalloc_array_size(const void *ctx, size_t size, unsigned count) MALLOCLIKE;
199af69d88dSmrg
200af69d88dSmrg/**
201af69d88dSmrg * Resize a ralloc-managed array, preserving data.
202af69d88dSmrg *
203af69d88dSmrg * Similar to \c realloc.  Unlike C89, passing 0 for \p size does not free the
204af69d88dSmrg * memory.  Instead, it resizes it to a 0-byte ralloc context, just like
205af69d88dSmrg * calling ralloc_size(ctx, 0).  This is different from talloc.
206af69d88dSmrg *
207af69d88dSmrg * More than a convenience function, this also checks for integer overflow when
208af69d88dSmrg * multiplying \c sizeof(type) and \p count.  This is necessary for security.
209af69d88dSmrg *
210af69d88dSmrg * \param ctx   The context to use for new allocation.  If \p ptr != NULL,
211af69d88dSmrg *              it must be the same as ralloc_parent(\p ptr).
212af69d88dSmrg * \param ptr   Pointer to the array to be resized.  May be NULL.
213af69d88dSmrg * \param size  The size of an individual element.
214af69d88dSmrg * \param count The number of elements to allocate.
215af69d88dSmrg *
216af69d88dSmrg * \return True unless allocation failed.
217af69d88dSmrg */
218af69d88dSmrgvoid *reralloc_array_size(const void *ctx, void *ptr, size_t size,
219af69d88dSmrg			  unsigned count);
220af69d88dSmrg/// @}
221af69d88dSmrg
222af69d88dSmrg/**
223af69d88dSmrg * Free a piece of ralloc-managed memory.
224af69d88dSmrg *
225af69d88dSmrg * This will also free the memory of any children allocated this context.
226af69d88dSmrg */
227af69d88dSmrgvoid ralloc_free(void *ptr);
228af69d88dSmrg
229af69d88dSmrg/**
230af69d88dSmrg * "Steal" memory from one context, changing it to another.
231af69d88dSmrg *
232af69d88dSmrg * This changes \p ptr's context to \p new_ctx.  This is quite useful if
233af69d88dSmrg * memory is allocated out of a temporary context.
234af69d88dSmrg */
235af69d88dSmrgvoid ralloc_steal(const void *new_ctx, void *ptr);
236af69d88dSmrg
237af69d88dSmrg/**
23801e04c3fSmrg * Reparent all children from one context to another.
23901e04c3fSmrg *
24001e04c3fSmrg * This effectively calls ralloc_steal(new_ctx, child) for all children of \p old_ctx.
241af69d88dSmrg */
24201e04c3fSmrgvoid ralloc_adopt(const void *new_ctx, void *old_ctx);
243af69d88dSmrg
244af69d88dSmrg/**
24501e04c3fSmrg * Return the given pointer's ralloc context.
246af69d88dSmrg */
24701e04c3fSmrgvoid *ralloc_parent(const void *ptr);
248af69d88dSmrg
249af69d88dSmrg/**
250af69d88dSmrg * Set a callback to occur just before an object is freed.
251af69d88dSmrg */
252af69d88dSmrgvoid ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
253af69d88dSmrg
254af69d88dSmrg/// \defgroup array String Functions @{
255af69d88dSmrg/**
256af69d88dSmrg * Duplicate a string, allocating the memory from the given context.
257af69d88dSmrg */
25801e04c3fSmrgchar *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE;
259af69d88dSmrg
260af69d88dSmrg/**
261af69d88dSmrg * Duplicate a string, allocating the memory from the given context.
262af69d88dSmrg *
263af69d88dSmrg * Like \c strndup, at most \p n characters are copied.  If \p str is longer
264af69d88dSmrg * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
265af69d88dSmrg */
26601e04c3fSmrgchar *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE;
267af69d88dSmrg
268af69d88dSmrg/**
269af69d88dSmrg * Concatenate two strings, allocating the necessary space.
270af69d88dSmrg *
271af69d88dSmrg * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
272af69d88dSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
273af69d88dSmrg * new pointer unless allocation fails.
274af69d88dSmrg *
275af69d88dSmrg * The result will always be null-terminated.
276af69d88dSmrg *
277af69d88dSmrg * \return True unless allocation failed.
278af69d88dSmrg */
279af69d88dSmrgbool ralloc_strcat(char **dest, const char *str);
280af69d88dSmrg
281af69d88dSmrg/**
282af69d88dSmrg * Concatenate two strings, allocating the necessary space.
283af69d88dSmrg *
284af69d88dSmrg * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
285af69d88dSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
286af69d88dSmrg * new pointer unless allocation fails.
287af69d88dSmrg *
288af69d88dSmrg * The result will always be null-terminated; \p str does not need to be null
289af69d88dSmrg * terminated if it is longer than \p n.
290af69d88dSmrg *
291af69d88dSmrg * \return True unless allocation failed.
292af69d88dSmrg */
293af69d88dSmrgbool ralloc_strncat(char **dest, const char *str, size_t n);
294af69d88dSmrg
29501e04c3fSmrg/**
29601e04c3fSmrg * Concatenate two strings, allocating the necessary space.
29701e04c3fSmrg *
29801e04c3fSmrg * This appends \p n bytes of \p str to \p *dest, using ralloc_resize
29901e04c3fSmrg * to expand \p *dest to the appropriate size.  \p dest will be updated to the
30001e04c3fSmrg * new pointer unless allocation fails.
30101e04c3fSmrg *
30201e04c3fSmrg * The result will always be null-terminated.
30301e04c3fSmrg *
30401e04c3fSmrg * This function differs from ralloc_strcat() and ralloc_strncat() in that it
30501e04c3fSmrg * does not do any strlen() calls which can become costly on large strings.
30601e04c3fSmrg *
30701e04c3fSmrg * \return True unless allocation failed.
30801e04c3fSmrg */
30901e04c3fSmrgbool
31001e04c3fSmrgralloc_str_append(char **dest, const char *str,
31101e04c3fSmrg                  size_t existing_length, size_t str_size);
31201e04c3fSmrg
313af69d88dSmrg/**
314af69d88dSmrg * Print to a string.
315af69d88dSmrg *
316af69d88dSmrg * This is analogous to \c sprintf, but allocates enough space (using \p ctx
317af69d88dSmrg * as the context) for the resulting string.
318af69d88dSmrg *
319af69d88dSmrg * \return The newly allocated string.
320af69d88dSmrg */
32101e04c3fSmrgchar *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
322af69d88dSmrg
323af69d88dSmrg/**
324af69d88dSmrg * Print to a string, given a va_list.
325af69d88dSmrg *
326af69d88dSmrg * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
327af69d88dSmrg * as the context) for the resulting string.
328af69d88dSmrg *
329af69d88dSmrg * \return The newly allocated string.
330af69d88dSmrg */
33101e04c3fSmrgchar *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE;
332af69d88dSmrg
333af69d88dSmrg/**
334af69d88dSmrg * Rewrite the tail of an existing string, starting at a given index.
335af69d88dSmrg *
336af69d88dSmrg * Overwrites the contents of *str starting at \p start with newly formatted
337af69d88dSmrg * text, including a new null-terminator.  Allocates more memory as necessary.
338af69d88dSmrg *
339af69d88dSmrg * This can be used to append formatted text when the length of the existing
340af69d88dSmrg * string is already known, saving a strlen() call.
341af69d88dSmrg *
342af69d88dSmrg * \sa ralloc_asprintf_append
343af69d88dSmrg *
344af69d88dSmrg * \param str   The string to be updated.
345af69d88dSmrg * \param start The index to start appending new data at.
346af69d88dSmrg * \param fmt   A printf-style formatting string
347af69d88dSmrg *
348af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
349af69d88dSmrg * \p start will be increased by the length of the newly formatted text.
350af69d88dSmrg *
351af69d88dSmrg * \return True unless allocation failed.
352af69d88dSmrg */
353af69d88dSmrgbool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
354af69d88dSmrg				  const char *fmt, ...)
355af69d88dSmrg				  PRINTFLIKE(3, 4);
356af69d88dSmrg
357af69d88dSmrg/**
358af69d88dSmrg * Rewrite the tail of an existing string, starting at a given index.
359af69d88dSmrg *
360af69d88dSmrg * Overwrites the contents of *str starting at \p start with newly formatted
361af69d88dSmrg * text, including a new null-terminator.  Allocates more memory as necessary.
362af69d88dSmrg *
363af69d88dSmrg * This can be used to append formatted text when the length of the existing
364af69d88dSmrg * string is already known, saving a strlen() call.
365af69d88dSmrg *
366af69d88dSmrg * \sa ralloc_vasprintf_append
367af69d88dSmrg *
368af69d88dSmrg * \param str   The string to be updated.
369af69d88dSmrg * \param start The index to start appending new data at.
370af69d88dSmrg * \param fmt   A printf-style formatting string
371af69d88dSmrg * \param args  A va_list containing the data to be formatted
372af69d88dSmrg *
373af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
374af69d88dSmrg * \p start will be increased by the length of the newly formatted text.
375af69d88dSmrg *
376af69d88dSmrg * \return True unless allocation failed.
377af69d88dSmrg */
378af69d88dSmrgbool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
379af69d88dSmrg				   va_list args);
380af69d88dSmrg
381af69d88dSmrg/**
382af69d88dSmrg * Append formatted text to the supplied string.
383af69d88dSmrg *
384af69d88dSmrg * This is equivalent to
385af69d88dSmrg * \code
386af69d88dSmrg * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
387af69d88dSmrg * \endcode
388af69d88dSmrg *
389af69d88dSmrg * \sa ralloc_asprintf
390af69d88dSmrg * \sa ralloc_asprintf_rewrite_tail
391af69d88dSmrg * \sa ralloc_strcat
392af69d88dSmrg *
393af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
394af69d88dSmrg *
395af69d88dSmrg * \return True unless allocation failed.
396af69d88dSmrg */
397af69d88dSmrgbool ralloc_asprintf_append (char **str, const char *fmt, ...)
398af69d88dSmrg			     PRINTFLIKE(2, 3);
399af69d88dSmrg
400af69d88dSmrg/**
401af69d88dSmrg * Append formatted text to the supplied string, given a va_list.
402af69d88dSmrg *
403af69d88dSmrg * This is equivalent to
404af69d88dSmrg * \code
405af69d88dSmrg * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
406af69d88dSmrg * \endcode
407af69d88dSmrg *
408af69d88dSmrg * \sa ralloc_vasprintf
409af69d88dSmrg * \sa ralloc_vasprintf_rewrite_tail
410af69d88dSmrg * \sa ralloc_strcat
411af69d88dSmrg *
412af69d88dSmrg * \p str will be updated to the new pointer unless allocation fails.
413af69d88dSmrg *
414af69d88dSmrg * \return True unless allocation failed.
415af69d88dSmrg */
416af69d88dSmrgbool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
417af69d88dSmrg/// @}
418af69d88dSmrg
419af69d88dSmrg/**
420af69d88dSmrg * Declare C++ new and delete operators which use ralloc.
421af69d88dSmrg *
422af69d88dSmrg * Placing this macro in the body of a class makes it possible to do:
423af69d88dSmrg *
424af69d88dSmrg * TYPE *var = new(mem_ctx) TYPE(...);
425af69d88dSmrg * delete var;
426af69d88dSmrg *
427af69d88dSmrg * which is more idiomatic in C++ than calling ralloc.
428af69d88dSmrg */
42901e04c3fSmrg#define DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(TYPE, ALLOC_FUNC)           \
430af69d88dSmrgprivate:                                                                 \
431af69d88dSmrg   static void _ralloc_destructor(void *p)                               \
432af69d88dSmrg   {                                                                     \
4338a1362adSmaya      reinterpret_cast<TYPE *>(p)->TYPE::~TYPE();                        \
434af69d88dSmrg   }                                                                     \
435af69d88dSmrgpublic:                                                                  \
436af69d88dSmrg   static void* operator new(size_t size, void *mem_ctx)                 \
437af69d88dSmrg   {                                                                     \
43801e04c3fSmrg      void *p = ALLOC_FUNC(mem_ctx, size);                               \
439af69d88dSmrg      assert(p != NULL);                                                 \
440af69d88dSmrg      if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
441af69d88dSmrg         ralloc_set_destructor(p, _ralloc_destructor);                   \
442af69d88dSmrg      return p;                                                          \
443af69d88dSmrg   }                                                                     \
444af69d88dSmrg                                                                         \
445af69d88dSmrg   static void operator delete(void *p)                                  \
446af69d88dSmrg   {                                                                     \
447af69d88dSmrg      /* The object's destructor is guaranteed to have already been      \
448af69d88dSmrg       * called by the delete operator at this point -- Make sure it's   \
449af69d88dSmrg       * not called again.                                               \
450af69d88dSmrg       */                                                                \
451af69d88dSmrg      if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
452af69d88dSmrg         ralloc_set_destructor(p, NULL);                                 \
453af69d88dSmrg      ralloc_free(p);                                                    \
454af69d88dSmrg   }
455af69d88dSmrg
45601e04c3fSmrg#define DECLARE_RALLOC_CXX_OPERATORS(type) \
45701e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, ralloc_size)
45801e04c3fSmrg
45901e04c3fSmrg#define DECLARE_RZALLOC_CXX_OPERATORS(type) \
46001e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, rzalloc_size)
46101e04c3fSmrg
46201e04c3fSmrg#define DECLARE_LINEAR_ALLOC_CXX_OPERATORS(type) \
46301e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_alloc_child)
46401e04c3fSmrg
46501e04c3fSmrg#define DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(type) \
46601e04c3fSmrg   DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_zalloc_child)
46701e04c3fSmrg
46801e04c3fSmrg
46901e04c3fSmrg/**
47001e04c3fSmrg * Do a fast allocation from the linear buffer, also known as the child node
47101e04c3fSmrg * from the allocator's point of view. It can't be freed directly. You have
47201e04c3fSmrg * to free the parent or the ralloc parent.
47301e04c3fSmrg *
47401e04c3fSmrg * \param parent   parent node of the linear allocator
47501e04c3fSmrg * \param size     size to allocate (max 32 bits)
47601e04c3fSmrg */
47701e04c3fSmrgvoid *linear_alloc_child(void *parent, unsigned size);
47801e04c3fSmrg
47901e04c3fSmrg/**
48001e04c3fSmrg * Allocate a parent node that will hold linear buffers. The returned
48101e04c3fSmrg * allocation is actually the first child node, but it's also the handle
48201e04c3fSmrg * of the parent node. Use it for all child node allocations.
48301e04c3fSmrg *
48401e04c3fSmrg * \param ralloc_ctx  ralloc context, must not be NULL
48501e04c3fSmrg * \param size        size to allocate (max 32 bits)
48601e04c3fSmrg */
48701e04c3fSmrgvoid *linear_alloc_parent(void *ralloc_ctx, unsigned size);
48801e04c3fSmrg
48901e04c3fSmrg/**
49001e04c3fSmrg * Same as linear_alloc_child, but also clears memory.
49101e04c3fSmrg */
49201e04c3fSmrgvoid *linear_zalloc_child(void *parent, unsigned size);
49301e04c3fSmrg
49401e04c3fSmrg/**
49501e04c3fSmrg * Same as linear_alloc_parent, but also clears memory.
49601e04c3fSmrg */
49701e04c3fSmrgvoid *linear_zalloc_parent(void *ralloc_ctx, unsigned size);
49801e04c3fSmrg
49901e04c3fSmrg/**
50001e04c3fSmrg * Free the linear parent node. This will free all child nodes too.
50101e04c3fSmrg * Freeing the ralloc parent will also free this.
50201e04c3fSmrg */
50301e04c3fSmrgvoid linear_free_parent(void *ptr);
50401e04c3fSmrg
50501e04c3fSmrg/**
50601e04c3fSmrg * Same as ralloc_steal, but steals the linear parent node.
50701e04c3fSmrg */
50801e04c3fSmrgvoid ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr);
50901e04c3fSmrg
51001e04c3fSmrg/**
51101e04c3fSmrg * Return the ralloc parent of the linear parent node.
51201e04c3fSmrg */
51301e04c3fSmrgvoid *ralloc_parent_of_linear_parent(void *ptr);
51401e04c3fSmrg
51501e04c3fSmrg/**
51601e04c3fSmrg * Same as realloc except that the linear allocator doesn't free child nodes,
51701e04c3fSmrg * so it's reduced to memory duplication. It's used in places where
51801e04c3fSmrg * reallocation is required. Don't use it often. It's much slower than
51901e04c3fSmrg * realloc.
52001e04c3fSmrg */
52101e04c3fSmrgvoid *linear_realloc(void *parent, void *old, unsigned new_size);
52201e04c3fSmrg
52301e04c3fSmrg/* The functions below have the same semantics as their ralloc counterparts,
52401e04c3fSmrg * except that they always allocate a linear child node.
52501e04c3fSmrg */
52601e04c3fSmrgchar *linear_strdup(void *parent, const char *str);
52701e04c3fSmrgchar *linear_asprintf(void *parent, const char *fmt, ...);
52801e04c3fSmrgchar *linear_vasprintf(void *parent, const char *fmt, va_list args);
52901e04c3fSmrgbool linear_asprintf_append(void *parent, char **str, const char *fmt, ...);
53001e04c3fSmrgbool linear_vasprintf_append(void *parent, char **str, const char *fmt,
53101e04c3fSmrg                             va_list args);
53201e04c3fSmrgbool linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
53301e04c3fSmrg                                  const char *fmt, ...);
53401e04c3fSmrgbool linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
53501e04c3fSmrg                                   const char *fmt, va_list args);
53601e04c3fSmrgbool linear_strcat(void *parent, char **dest, const char *str);
53701e04c3fSmrg
53801e04c3fSmrg#ifdef __cplusplus
53901e04c3fSmrg} /* end of extern "C" */
54001e04c3fSmrg#endif
541af69d88dSmrg
542af69d88dSmrg#endif
543