57e8c7a138a177e0dc2f6234941a035b98842bea
2 * Copyright © 2010 Intel Corporation
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:
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
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.
27 * ralloc: a recursive memory allocator
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
37 * This is currently a wrapper around talloc, but that will change.
52 * \def ralloc(ctx, type)
53 * Allocate a new object chained off of the given context.
55 * This is equivalent to:
57 * ((type *) ralloc_size(ctx, sizeof(type))
60 #define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type)))
63 * \def rzalloc(ctx, type)
64 * Allocate a new object out of the given context and initialize it to zero.
66 * This is equivalent to:
68 * ((type *) rzalloc_size(ctx, sizeof(type))
71 #define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
74 * Allocate a new ralloc context.
76 * While any ralloc'd pointer can be used as a context, sometimes it is useful
77 * to simply allocate a context with no associated memory.
79 * It is equivalent to:
81 * ((type *) ralloc_size(ctx, 0)
84 void *ralloc_context(const void *ctx
);
87 * Allocate memory chained off of the given context.
89 * This is the core allocation routine which is used by all others. It
90 * simply allocates storage for \p size bytes and returns the pointer,
91 * similar to \c malloc.
93 void *ralloc_size(const void *ctx
, size_t size
);
96 * Allocate zero-initialized memory chained off of the given context.
98 * This is similar to \c calloc with a size of 1.
100 void *rzalloc_size(const void *ctx
, size_t size
);
103 * Resize a piece of ralloc-managed memory, preserving data.
105 * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the
106 * memory. Instead, it resizes it to a 0-byte ralloc context, just like
107 * calling ralloc_size(ctx, 0). This is different from talloc.
109 * \param ctx The context to use for new allocation. If \p ptr != NULL,
110 * it must be the same as ralloc_parent(\p ptr).
111 * \param ptr Pointer to the memory to be resized. May be NULL.
112 * \param size The amount of memory to allocate, in bytes.
114 void *reralloc_size(const void *ctx
, void *ptr
, size_t size
);
116 /// \defgroup array Array Allocators @{
119 * \def ralloc_array(ctx, type, count)
120 * Allocate an array of objects chained off the given context.
122 * Similar to \c calloc, but does not initialize the memory to zero.
124 * More than a convenience function, this also checks for integer overflow when
125 * multiplying \c sizeof(type) and \p count. This is necessary for security.
127 * This is equivalent to:
129 * ((type *) ralloc_array_size(ctx, sizeof(type), count)
132 #define ralloc_array(ctx, type, count) \
133 ((type *) ralloc_array_size(ctx, sizeof(type), count))
136 * \def rzalloc_array(ctx, type, count)
137 * Allocate a zero-initialized array chained off the given context.
139 * Similar to \c calloc.
141 * More than a convenience function, this also checks for integer overflow when
142 * multiplying \c sizeof(type) and \p count. This is necessary for security.
144 * This is equivalent to:
146 * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
149 #define rzalloc_array(ctx, type, count) \
150 ((type *) rzalloc_array_size(ctx, sizeof(type), count))
153 * \def reralloc(ctx, ptr, type, count)
154 * Resize a ralloc-managed array, preserving data.
156 * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the
157 * memory. Instead, it resizes it to a 0-byte ralloc context, just like
158 * calling ralloc_size(ctx, 0). This is different from talloc.
160 * More than a convenience function, this also checks for integer overflow when
161 * multiplying \c sizeof(type) and \p count. This is necessary for security.
163 * \param ctx The context to use for new allocation. If \p ptr != NULL,
164 * it must be the same as ralloc_parent(\p ptr).
165 * \param ptr Pointer to the array to be resized. May be NULL.
166 * \param type The element type.
167 * \param count The number of elements to allocate.
169 #define reralloc(ctx, ptr, type, count) \
170 ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
173 * Allocate memory for an array chained off the given context.
175 * Similar to \c calloc, but does not initialize the memory to zero.
177 * More than a convenience function, this also checks for integer overflow when
178 * multiplying \p size and \p count. This is necessary for security.
180 void *ralloc_array_size(const void *ctx
, size_t size
, unsigned count
);
183 * Allocate a zero-initialized array chained off the given context.
185 * Similar to \c calloc.
187 * More than a convenience function, this also checks for integer overflow when
188 * multiplying \p size and \p count. This is necessary for security.
190 void *rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
);
193 * Resize a ralloc-managed array, preserving data.
195 * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the
196 * memory. Instead, it resizes it to a 0-byte ralloc context, just like
197 * calling ralloc_size(ctx, 0). This is different from talloc.
199 * More than a convenience function, this also checks for integer overflow when
200 * multiplying \c sizeof(type) and \p count. This is necessary for security.
202 * \param ctx The context to use for new allocation. If \p ptr != NULL,
203 * it must be the same as ralloc_parent(\p ptr).
204 * \param ptr Pointer to the array to be resized. May be NULL.
205 * \param size The size of an individual element.
206 * \param count The number of elements to allocate.
208 * \return True unless allocation failed.
210 void *reralloc_array_size(const void *ctx
, void *ptr
, size_t size
,
215 * Free a piece of ralloc-managed memory.
217 * This will also free the memory of any children allocated this context.
219 void ralloc_free(void *ptr
);
222 * "Steal" memory from one context, changing it to another.
224 * This changes \p ptr's context to \p new_ctx. This is quite useful if
225 * memory is allocated out of a temporary context.
227 void ralloc_steal(const void *new_ctx
, void *ptr
);
230 * Return the given pointer's ralloc context.
232 void *ralloc_parent(const void *ptr
);
235 * Return a context whose memory will be automatically freed at program exit.
237 * The first call to this function creates a context and registers a handler
238 * to free it using \c atexit. This may cause trouble if used in a library
239 * loaded with \c dlopen.
241 void *ralloc_autofree_context(void);
244 * Set a callback to occur just before an object is freed.
246 void ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *));
248 /// \defgroup array String Functions @{
250 * Duplicate a string, allocating the memory from the given context.
252 char *ralloc_strdup(const void *ctx
, const char *str
);
255 * Duplicate a string, allocating the memory from the given context.
257 * Like \c strndup, at most \p n characters are copied. If \p str is longer
258 * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
260 char *ralloc_strndup(const void *ctx
, const char *str
, size_t n
);
263 * Concatenate two strings, allocating the necessary space.
265 * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
266 * to expand \p *dest to the appropriate size. \p dest will be updated to the
267 * new pointer unless allocation fails.
269 * The result will always be null-terminated.
271 * \return True unless allocation failed.
273 bool ralloc_strcat(char **dest
, const char *str
);
276 * Concatenate two strings, allocating the necessary space.
278 * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
279 * to expand \p *dest to the appropriate size. \p dest will be updated to the
280 * new pointer unless allocation fails.
282 * The result will always be null-terminated; \p str does not need to be null
283 * terminated if it is longer than \p n.
285 * \return True unless allocation failed.
287 bool ralloc_strncat(char **dest
, const char *str
, size_t n
);
292 * This is analogous to \c sprintf, but allocates enough space (using \p ctx
293 * as the context) for the resulting string.
295 * \return The newly allocated string.
297 char *ralloc_asprintf (const void *ctx
, const char *fmt
, ...);
300 * Print to a string, given a va_list.
302 * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
303 * as the context) for the resulting string.
305 * \return The newly allocated string.
307 char *ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
);
310 * Append formatted text to the supplied string.
312 * \sa ralloc_asprintf
315 * \p str will be updated to the new pointer unless allocation fails.
317 * \return True unless allocation failed.
319 bool ralloc_asprintf_append (char **str
, const char *fmt
, ...);
322 * Append formatted text to the supplied string, given a va_list.
324 * \sa ralloc_vasprintf
327 * \p str will be updated to the new pointer unless allocation fails.
329 * \return True unless allocation failed.
331 bool ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
);
335 } /* end of extern "C" */