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 * 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.
41 * The talloc implementation is available under the GNU Lesser
42 * General Public License (GNU LGPL), version 3 or later. It is
43 * more sophisticated than ralloc in that it includes reference
44 * counting and debugging features. See: http://talloc.samba.org/
59 * \def ralloc(ctx, type)
60 * Allocate a new object chained off of the given context.
62 * This is equivalent to:
64 * ((type *) ralloc_size(ctx, sizeof(type))
67 #define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type)))
70 * \def rzalloc(ctx, type)
71 * Allocate a new object out of the given context and initialize it to zero.
73 * This is equivalent to:
75 * ((type *) rzalloc_size(ctx, sizeof(type))
78 #define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
81 * Allocate a new ralloc context.
83 * While any ralloc'd pointer can be used as a context, sometimes it is useful
84 * to simply allocate a context with no associated memory.
86 * It is equivalent to:
88 * ((type *) ralloc_size(ctx, 0)
91 void *ralloc_context(const void *ctx
);
94 * Allocate memory chained off of the given context.
96 * This is the core allocation routine which is used by all others. It
97 * simply allocates storage for \p size bytes and returns the pointer,
98 * similar to \c malloc.
100 void *ralloc_size(const void *ctx
, size_t size
);
103 * Allocate zero-initialized memory chained off of the given context.
105 * This is similar to \c calloc with a size of 1.
107 void *rzalloc_size(const void *ctx
, size_t size
);
110 * Resize a piece of ralloc-managed memory, preserving data.
112 * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the
113 * memory. Instead, it resizes it to a 0-byte ralloc context, just like
114 * calling ralloc_size(ctx, 0). This is different from talloc.
116 * \param ctx The context to use for new allocation. If \p ptr != NULL,
117 * it must be the same as ralloc_parent(\p ptr).
118 * \param ptr Pointer to the memory to be resized. May be NULL.
119 * \param size The amount of memory to allocate, in bytes.
121 void *reralloc_size(const void *ctx
, void *ptr
, size_t size
);
123 /// \defgroup array Array Allocators @{
126 * \def ralloc_array(ctx, type, count)
127 * Allocate an array of objects chained off the given context.
129 * Similar to \c calloc, but does not initialize the memory to zero.
131 * More than a convenience function, this also checks for integer overflow when
132 * multiplying \c sizeof(type) and \p count. This is necessary for security.
134 * This is equivalent to:
136 * ((type *) ralloc_array_size(ctx, sizeof(type), count)
139 #define ralloc_array(ctx, type, count) \
140 ((type *) ralloc_array_size(ctx, sizeof(type), count))
143 * \def rzalloc_array(ctx, type, count)
144 * Allocate a zero-initialized array chained off the given context.
146 * Similar to \c calloc.
148 * More than a convenience function, this also checks for integer overflow when
149 * multiplying \c sizeof(type) and \p count. This is necessary for security.
151 * This is equivalent to:
153 * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
156 #define rzalloc_array(ctx, type, count) \
157 ((type *) rzalloc_array_size(ctx, sizeof(type), count))
160 * \def reralloc(ctx, ptr, type, count)
161 * Resize a ralloc-managed array, preserving data.
163 * Similar to \c realloc. Unlike C89, passing 0 for \p size does not free the
164 * memory. Instead, it resizes it to a 0-byte ralloc context, just like
165 * calling ralloc_size(ctx, 0). This is different from talloc.
167 * More than a convenience function, this also checks for integer overflow when
168 * multiplying \c sizeof(type) and \p count. This is necessary for security.
170 * \param ctx The context to use for new allocation. If \p ptr != NULL,
171 * it must be the same as ralloc_parent(\p ptr).
172 * \param ptr Pointer to the array to be resized. May be NULL.
173 * \param type The element type.
174 * \param count The number of elements to allocate.
176 #define reralloc(ctx, ptr, type, count) \
177 ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
180 * Allocate memory for an array chained off the given context.
182 * Similar to \c calloc, but does not initialize the memory to zero.
184 * More than a convenience function, this also checks for integer overflow when
185 * multiplying \p size and \p count. This is necessary for security.
187 void *ralloc_array_size(const void *ctx
, size_t size
, unsigned count
);
190 * Allocate a zero-initialized array chained off the given context.
192 * Similar to \c calloc.
194 * More than a convenience function, this also checks for integer overflow when
195 * multiplying \p size and \p count. This is necessary for security.
197 void *rzalloc_array_size(const void *ctx
, size_t size
, unsigned count
);
200 * Resize a ralloc-managed array, preserving data.
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.
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.
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 size The size of an individual element.
213 * \param count The number of elements to allocate.
215 * \return True unless allocation failed.
217 void *reralloc_array_size(const void *ctx
, void *ptr
, size_t size
,
222 * Free a piece of ralloc-managed memory.
224 * This will also free the memory of any children allocated this context.
226 void ralloc_free(void *ptr
);
229 * "Steal" memory from one context, changing it to another.
231 * This changes \p ptr's context to \p new_ctx. This is quite useful if
232 * memory is allocated out of a temporary context.
234 void ralloc_steal(const void *new_ctx
, void *ptr
);
237 * Return the given pointer's ralloc context.
239 void *ralloc_parent(const void *ptr
);
242 * Return a context whose memory will be automatically freed at program exit.
244 * The first call to this function creates a context and registers a handler
245 * to free it using \c atexit. This may cause trouble if used in a library
246 * loaded with \c dlopen.
248 void *ralloc_autofree_context(void);
251 * Set a callback to occur just before an object is freed.
253 void ralloc_set_destructor(const void *ptr
, void(*destructor
)(void *));
255 /// \defgroup array String Functions @{
257 * Duplicate a string, allocating the memory from the given context.
259 char *ralloc_strdup(const void *ctx
, const char *str
);
262 * Duplicate a string, allocating the memory from the given context.
264 * Like \c strndup, at most \p n characters are copied. If \p str is longer
265 * than \p n characters, \p n are copied, and a termining \c '\0' byte is added.
267 char *ralloc_strndup(const void *ctx
, const char *str
, size_t n
);
270 * Concatenate two strings, allocating the necessary space.
272 * This appends \p str to \p *dest, similar to \c strcat, using ralloc_resize
273 * to expand \p *dest to the appropriate size. \p dest will be updated to the
274 * new pointer unless allocation fails.
276 * The result will always be null-terminated.
278 * \return True unless allocation failed.
280 bool ralloc_strcat(char **dest
, const char *str
);
283 * Concatenate two strings, allocating the necessary space.
285 * This appends at most \p n bytes of \p str to \p *dest, using ralloc_resize
286 * to expand \p *dest to the appropriate size. \p dest will be updated to the
287 * new pointer unless allocation fails.
289 * The result will always be null-terminated; \p str does not need to be null
290 * terminated if it is longer than \p n.
292 * \return True unless allocation failed.
294 bool ralloc_strncat(char **dest
, const char *str
, size_t n
);
299 * This is analogous to \c sprintf, but allocates enough space (using \p ctx
300 * as the context) for the resulting string.
302 * \return The newly allocated string.
304 char *ralloc_asprintf (const void *ctx
, const char *fmt
, ...);
307 * Print to a string, given a va_list.
309 * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
310 * as the context) for the resulting string.
312 * \return The newly allocated string.
314 char *ralloc_vasprintf(const void *ctx
, const char *fmt
, va_list args
);
317 * Rewrite the tail of an existing string, starting at a given index.
319 * Overwrites the contents of *str starting at \p start with newly formatted
320 * text, including a new null-terminator. Allocates more memory as necessary.
322 * This can be used to append formatted text when the length of the existing
323 * string is already known, saving a strlen() call.
325 * \sa ralloc_asprintf_append
327 * \param str The string to be updated.
328 * \param start The index to start appending new data at.
329 * \param fmt A printf-style formatting string
331 * \p str will be updated to the new pointer unless allocation fails.
333 * \return True unless allocation failed.
335 bool ralloc_asprintf_rewrite_tail(char **str
, size_t start
,
336 const char *fmt
, ...);
339 * Rewrite the tail of an existing string, starting at a given index.
341 * Overwrites the contents of *str starting at \p start with newly formatted
342 * text, including a new null-terminator. Allocates more memory as necessary.
344 * This can be used to append formatted text when the length of the existing
345 * string is already known, saving a strlen() call.
347 * \sa ralloc_vasprintf_append
349 * \param str The string to be updated.
350 * \param start The index to start appending new data at.
351 * \param fmt A printf-style formatting string
352 * \param args A va_list containing the data to be formatted
354 * \p str will be updated to the new pointer unless allocation fails.
356 * \return True unless allocation failed.
358 bool ralloc_vasprintf_rewrite_tail(char **str
, size_t start
, const char *fmt
,
362 * Append formatted text to the supplied string.
364 * This is equivalent to
366 * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
369 * \sa ralloc_asprintf
370 * \sa ralloc_asprintf_rewrite_tail
373 * \p str will be updated to the new pointer unless allocation fails.
375 * \return True unless allocation failed.
377 bool ralloc_asprintf_append (char **str
, const char *fmt
, ...);
380 * Append formatted text to the supplied string, given a va_list.
382 * This is equivalent to
384 * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
387 * \sa ralloc_vasprintf
388 * \sa ralloc_vasprintf_rewrite_tail
391 * \p str will be updated to the new pointer unless allocation fails.
393 * \return True unless allocation failed.
395 bool ralloc_vasprintf_append(char **str
, const char *fmt
, va_list args
);
399 } /* end of extern "C" */