• 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 /// \defgroup array String Functions @{
317 /**
318  * Duplicate a string, allocating the memory from the given context.
319  */
320 char *ralloc_strdup(const void *ctx, const char *str) MALLOCLIKE;
321 
322 /**
323  * Duplicate a string, allocating the memory from the given context.
324  *
325  * Like \c strndup, at most \p n characters are copied.  If \p str is longer
326  * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
327  */
328 char *ralloc_strndup(const void *ctx, const char *str, size_t n) MALLOCLIKE;
329 
330 /**
331  * Concatenate two strings, allocating the necessary space.
332  *
333  * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
334  * to expand \p *dest to the appropriate size.  \p dest will be updated to the
335  * new pointer unless allocation fails.
336  *
337  * The result will always be null-terminated.
338  *
339  * \return True unless allocation failed.
340  */
341 bool ralloc_strcat(char **dest, const char *str);
342 
343 /**
344  * Concatenate two strings, allocating the necessary space.
345  *
346  * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
347  * to expand \p *dest to the appropriate size.  \p dest will be updated to the
348  * new pointer unless allocation fails.
349  *
350  * The result will always be null-terminated; \p str does not need to be null
351  * terminated if it is longer than \p n.
352  *
353  * \return True unless allocation failed.
354  */
355 bool ralloc_strncat(char **dest, const char *str, size_t n);
356 
357 /**
358  * Concatenate two strings, allocating the necessary space.
359  *
360  * This appends \p n bytes of \p str to \p *dest, using ralloc_resize
361  * to expand \p *dest to the appropriate size.  \p dest will be updated to the
362  * new pointer unless allocation fails.
363  *
364  * The result will always be null-terminated.
365  *
366  * This function differs from ralloc_strcat() and ralloc_strncat() in that it
367  * does not do any strlen() calls which can become costly on large strings.
368  *
369  * \return True unless allocation failed.
370  */
371 bool
372 ralloc_str_append(char **dest, const char *str,
373                   size_t existing_length, size_t str_size);
374 
375 /**
376  * Print to a string.
377  *
378  * This is analogous to \c sprintf, but allocates enough space (using \p ctx
379  * as the context) for the resulting string.
380  *
381  * \return The newly allocated string.
382  */
383 char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3) MALLOCLIKE;
384 
385 /**
386  * Print to a string, given a va_list.
387  *
388  * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
389  * as the context) for the resulting string.
390  *
391  * \return The newly allocated string.
392  */
393 char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args) MALLOCLIKE;
394 
395 /**
396  * Rewrite the tail of an existing string, starting at a given index.
397  *
398  * Overwrites the contents of *str starting at \p start with newly formatted
399  * text, including a new null-terminator.  Allocates more memory as necessary.
400  *
401  * This can be used to append formatted text when the length of the existing
402  * string is already known, saving a strlen() call.
403  *
404  * \sa ralloc_asprintf_append
405  *
406  * \param str   The string to be updated.
407  * \param start The index to start appending new data at.
408  * \param fmt   A printf-style formatting string
409  *
410  * \p str will be updated to the new pointer unless allocation fails.
411  * \p start will be increased by the length of the newly formatted text.
412  *
413  * \return True unless allocation failed.
414  */
415 bool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
416 				  const char *fmt, ...)
417 				  PRINTFLIKE(3, 4);
418 
419 /**
420  * Rewrite the tail of an existing string, starting at a given index.
421  *
422  * Overwrites the contents of *str starting at \p start with newly formatted
423  * text, including a new null-terminator.  Allocates more memory as necessary.
424  *
425  * This can be used to append formatted text when the length of the existing
426  * string is already known, saving a strlen() call.
427  *
428  * \sa ralloc_vasprintf_append
429  *
430  * \param str   The string to be updated.
431  * \param start The index to start appending new data at.
432  * \param fmt   A printf-style formatting string
433  * \param args  A va_list containing the data to be formatted
434  *
435  * \p str will be updated to the new pointer unless allocation fails.
436  * \p start will be increased by the length of the newly formatted text.
437  *
438  * \return True unless allocation failed.
439  */
440 bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
441 				   va_list args);
442 
443 /**
444  * Append formatted text to the supplied string.
445  *
446  * This is equivalent to
447  * \code
448  * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
449  * \endcode
450  *
451  * \sa ralloc_asprintf
452  * \sa ralloc_asprintf_rewrite_tail
453  * \sa ralloc_strcat
454  *
455  * \p str will be updated to the new pointer unless allocation fails.
456  *
457  * \return True unless allocation failed.
458  */
459 bool ralloc_asprintf_append (char **str, const char *fmt, ...)
460 			     PRINTFLIKE(2, 3);
461 
462 /**
463  * Append formatted text to the supplied string, given a va_list.
464  *
465  * This is equivalent to
466  * \code
467  * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
468  * \endcode
469  *
470  * \sa ralloc_vasprintf
471  * \sa ralloc_vasprintf_rewrite_tail
472  * \sa ralloc_strcat
473  *
474  * \p str will be updated to the new pointer unless allocation fails.
475  *
476  * \return True unless allocation failed.
477  */
478 bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
479 /// @}
480 
481 typedef struct gc_ctx gc_ctx;
482 
483 /**
484  * Allocate a new garbage collection context. The children of the
485  * context are not necessarily ralloc'd pointers and cannot be stolen to a ralloc context. Instead,
486  * The user should use the mark-and-sweep interface below to free any unused children. Under the
487  * hood, this restriction lets us manage allocations ourselves, using a freelist. This means that
488  * GC contexts should be used for scenarios where there are many allocations and frees, most of
489  * which use only a few different sizes.
490  */
491 gc_ctx *gc_context(const void *parent);
492 
493 #define gc_alloc(ctx, type, count) gc_alloc_size(ctx, sizeof(type) * (count), alignof(type))
494 #define gc_zalloc(ctx, type, count) gc_zalloc_size(ctx, sizeof(type) * (count), alignof(type))
495 
496 #define gc_alloc_zla(ctx, type, type2, count) \
497    gc_alloc_size(ctx, sizeof(type) + sizeof(type2) * (count), MAX2(alignof(type), alignof(type2)))
498 #define gc_zalloc_zla(ctx, type, type2, count) \
499    gc_zalloc_size(ctx, sizeof(type) + sizeof(type2) * (count), MAX2(alignof(type), alignof(type2)))
500 
501 void *gc_alloc_size(gc_ctx *ctx, size_t size, size_t align) MALLOCLIKE;
502 void *gc_zalloc_size(gc_ctx *ctx, size_t size, size_t align) MALLOCLIKE;
503 void gc_free(void *ptr);
504 gc_ctx *gc_get_context(void *ptr);
505 
506 void gc_sweep_start(gc_ctx *ctx);
507 void gc_mark_live(gc_ctx *ctx, const void *mem);
508 void gc_sweep_end(gc_ctx *ctx);
509 
510 /**
511  * Declare C++ new and delete operators which use ralloc.
512  *
513  * Placing this macro in the body of a class makes it possible to do:
514  *
515  * TYPE *var = new(mem_ctx) TYPE(...);
516  * delete var;
517  *
518  * which is more idiomatic in C++ than calling ralloc.
519  */
520 #define DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(TYPE, ALLOC_FUNC)           \
521 private:                                                                 \
522    static void _ralloc_destructor(void *p)                               \
523    {                                                                     \
524       reinterpret_cast<TYPE *>(p)->TYPE::~TYPE();                        \
525    }                                                                     \
526 public:                                                                  \
527    static void* operator new(size_t size, void *mem_ctx)                 \
528    {                                                                     \
529       void *p = ALLOC_FUNC(mem_ctx, size);                               \
530       assert(p != NULL);                                                 \
531       if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
532          ralloc_set_destructor(p, _ralloc_destructor);                   \
533       return p;                                                          \
534    }                                                                     \
535                                                                          \
536    static void operator delete(void *p)                                  \
537    {                                                                     \
538       /* The object's destructor is guaranteed to have already been      \
539        * called by the delete operator at this point -- Make sure it's   \
540        * not called again.                                               \
541        */                                                                \
542       if (!HAS_TRIVIAL_DESTRUCTOR(TYPE))                                 \
543          ralloc_set_destructor(p, NULL);                                 \
544       ralloc_free(p);                                                    \
545    }
546 
547 #define DECLARE_RALLOC_CXX_OPERATORS(type) \
548    DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, ralloc_size)
549 
550 #define DECLARE_RZALLOC_CXX_OPERATORS(type) \
551    DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, rzalloc_size)
552 
553 #define DECLARE_LINEAR_ALLOC_CXX_OPERATORS(type) \
554    DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_alloc_child)
555 
556 #define DECLARE_LINEAR_ZALLOC_CXX_OPERATORS(type) \
557    DECLARE_ALLOC_CXX_OPERATORS_TEMPLATE(type, linear_zalloc_child)
558 
559 
560 /**
561  * Do a fast allocation from the linear buffer, also known as the child node
562  * from the allocator's point of view. It can't be freed directly. You have
563  * to free the parent or the ralloc parent.
564  *
565  * \param parent   parent node of the linear allocator
566  * \param size     size to allocate (max 32 bits)
567  */
568 void *linear_alloc_child(void *parent, unsigned size);
569 
570 /**
571  * Allocate a parent node that will hold linear buffers. The returned
572  * allocation is actually the first child node, but it's also the handle
573  * of the parent node. Use it for all child node allocations.
574  *
575  * \param ralloc_ctx  ralloc context, must not be NULL
576  * \param size        size to allocate (max 32 bits)
577  */
578 void *linear_alloc_parent(void *ralloc_ctx, unsigned size);
579 
580 /**
581  * Same as linear_alloc_child, but also clears memory.
582  */
583 void *linear_zalloc_child(void *parent, unsigned size);
584 
585 /**
586  * Same as linear_alloc_parent, but also clears memory.
587  */
588 void *linear_zalloc_parent(void *ralloc_ctx, unsigned size);
589 
590 /**
591  * Free the linear parent node. This will free all child nodes too.
592  * Freeing the ralloc parent will also free this.
593  */
594 void linear_free_parent(void *ptr);
595 
596 /**
597  * Same as ralloc_steal, but steals the linear parent node.
598  */
599 void ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr);
600 
601 /**
602  * Return the ralloc parent of the linear parent node.
603  */
604 void *ralloc_parent_of_linear_parent(void *ptr);
605 
606 /**
607  * Same as realloc except that the linear allocator doesn't free child nodes,
608  * so it's reduced to memory duplication. It's used in places where
609  * reallocation is required. Don't use it often. It's much slower than
610  * realloc.
611  */
612 void *linear_realloc(void *parent, void *old, unsigned new_size);
613 
614 /**
615  * Do a fast allocation of an array from the linear buffer and initialize it to zero.
616  *
617  * Similar to \c calloc, but does not initialize the memory to zero.
618  *
619  * More than a convenience function, this also checks for integer overflow when
620  * multiplying \p size and \p count.  This is necessary for security.
621  */
622 void *linear_alloc_child_array(void *parent, size_t size, unsigned count);
623 
624 /**
625  * Do a fast allocation of an array from the linear buffer.
626  *
627  * Similar to \c calloc.
628  *
629  * More than a convenience function, this also checks for integer overflow when
630  * multiplying \p size and \p count.  This is necessary for security.
631  */
632 void *linear_zalloc_child_array(void *parent, size_t size, unsigned count);
633 
634 /* The functions below have the same semantics as their ralloc counterparts,
635  * except that they always allocate a linear child node.
636  */
637 char *linear_strdup(void *parent, const char *str);
638 char *linear_asprintf(void *parent, const char *fmt, ...);
639 char *linear_vasprintf(void *parent, const char *fmt, va_list args);
640 bool linear_asprintf_append(void *parent, char **str, const char *fmt, ...);
641 bool linear_vasprintf_append(void *parent, char **str, const char *fmt,
642                              va_list args);
643 bool linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
644                                   const char *fmt, ...);
645 bool linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
646                                    const char *fmt, va_list args);
647 bool linear_strcat(void *parent, char **dest, const char *str);
648 
649 /**
650  * \def linear_alloc(parent, type)
651  * Do a fast allocation from the linear buffer.
652  *
653  * This is equivalent to:
654  * \code
655  * ((type *) linear_alloc_child(parent, sizeof(type))
656  * \endcode
657  */
658 #define linear_alloc(parent, type)  ((type *) linear_alloc_child(parent, sizeof(type)))
659 
660 /**
661  * \def linear_zalloc(parent, type)
662  * Do a fast allocation from the linear buffer and initialize it to zero.
663  *
664  * This is equivalent to:
665  * \code
666  * ((type *) linear_zalloc_child(parent, sizeof(type))
667  * \endcode
668  */
669 #define linear_zalloc(parent, type) ((type *) linear_zalloc_child(parent, sizeof(type)))
670 
671 /**
672  * \def linear_alloc_array(parent, type, count)
673  * Do a fast allocation of an array from the linear buffer.
674  *
675  * Similar to \c calloc, but does not initialize the memory to zero.
676  *
677  * More than a convenience function, this also checks for integer overflow when
678  * multiplying \c sizeof(type) and \p count.  This is necessary for security.
679  *
680  * This is equivalent to:
681  * \code
682  * ((type *) linear_alloc_child_array(parent, sizeof(type), count)
683  * \endcode
684  */
685 #define linear_alloc_array(parent, type, count) \
686    ((type *) linear_alloc_child_array(parent, sizeof(type), count))
687 
688 /**
689  * \def linear_zalloc_array(parent, type, count)
690  * Do a fast allocation of an array from the linear buffer and initialize it to zero
691  *
692  * Similar to \c calloc.
693  *
694  * More than a convenience function, this also checks for integer overflow when
695  * multiplying \c sizeof(type) and \p count.  This is necessary for security.
696  *
697  * This is equivalent to:
698  * \code
699  * ((type *) linear_zalloc_child_array(parent, sizeof(type), count)
700  * \endcode
701  */
702 #define linear_zalloc_array(parent, type, count) \
703    ((type *) linear_zalloc_child_array(parent, sizeof(type), count))
704 
705 #ifdef __cplusplus
706 } /* end of extern "C" */
707 #endif
708 
709 #endif
710