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