ralloc: Add a fake implementation of ralloc based on talloc.
[mesa.git] / src / glsl / ralloc.h
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 * This is currently a wrapper around talloc, but that will change.
38 */
39
40 #ifndef RALLOC_H
41 #define RALLOC_H
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 #include <stddef.h>
48 #include <stdarg.h>
49 #include <stdbool.h>
50
51 /**
52 * \def ralloc(ctx, type)
53 * Allocate a new object chained off of the given context.
54 *
55 * This is equivalent to:
56 * \code
57 * ((type *) ralloc_size(ctx, sizeof(type))
58 * \endcode
59 */
60 #define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type)))
61
62 /**
63 * \def rzalloc(ctx, type)
64 * Allocate a new object out of the given context and initialize it to zero.
65 *
66 * This is equivalent to:
67 * \code
68 * ((type *) rzalloc_size(ctx, sizeof(type))
69 * \endcode
70 */
71 #define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
72
73 /**
74 * Allocate a new ralloc context.
75 *
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.
78 *
79 * It is equivalent to:
80 * \code
81 * ((type *) ralloc_size(ctx, 0)
82 * \endcode
83 */
84 void *ralloc_context(const void *ctx);
85
86 /**
87 * Allocate memory chained off of the given context.
88 *
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.
92 */
93 void *ralloc_size(const void *ctx, size_t size);
94
95 /**
96 * Allocate zero-initialized memory chained off of the given context.
97 *
98 * This is similar to \c calloc with a size of 1.
99 */
100 void *rzalloc_size(const void *ctx, size_t size);
101
102 /**
103 * Resize a piece of ralloc-managed memory, preserving data.
104 *
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.
108 *
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.
113 */
114 void *reralloc_size(const void *ctx, void *ptr, size_t size);
115
116 /// \defgroup array Array Allocators @{
117
118 /**
119 * \def ralloc_array(ctx, type, count)
120 * Allocate an array of objects chained off the given context.
121 *
122 * Similar to \c calloc, but does not initialize the memory to zero.
123 *
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.
126 *
127 * This is equivalent to:
128 * \code
129 * ((type *) ralloc_array_size(ctx, sizeof(type), count)
130 * \endcode
131 */
132 #define ralloc_array(ctx, type, count) \
133 ((type *) ralloc_array_size(ctx, sizeof(type), count))
134
135 /**
136 * \def rzalloc_array(ctx, type, count)
137 * Allocate a zero-initialized array chained off the given context.
138 *
139 * Similar to \c calloc.
140 *
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.
143 *
144 * This is equivalent to:
145 * \code
146 * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
147 * \endcode
148 */
149 #define rzalloc_array(ctx, type, count) \
150 ((type *) rzalloc_array_size(ctx, sizeof(type), count))
151
152 /**
153 * \def reralloc(ctx, ptr, type, count)
154 * Resize a ralloc-managed array, preserving data.
155 *
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.
159 *
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.
162 *
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.
168 */
169 #define reralloc(ctx, ptr, type, count) \
170 ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
171
172 /**
173 * Allocate memory for an array chained off the given context.
174 *
175 * Similar to \c calloc, but does not initialize the memory to zero.
176 *
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.
179 */
180 void *ralloc_array_size(const void *ctx, size_t size, unsigned count);
181
182 /**
183 * Allocate a zero-initialized array chained off the given context.
184 *
185 * Similar to \c calloc.
186 *
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.
189 */
190 void *rzalloc_array_size(const void *ctx, size_t size, unsigned count);
191
192 /**
193 * Resize a ralloc-managed array, preserving data.
194 *
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.
198 *
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.
201 *
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.
207 *
208 * \return True unless allocation failed.
209 */
210 void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
211 unsigned count);
212 /// @}
213
214 /**
215 * Free a piece of ralloc-managed memory.
216 *
217 * This will also free the memory of any children allocated this context.
218 */
219 void ralloc_free(void *ptr);
220
221 /**
222 * "Steal" memory from one context, changing it to another.
223 *
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.
226 */
227 void ralloc_steal(const void *new_ctx, void *ptr);
228
229 /**
230 * Return the given pointer's ralloc context.
231 */
232 void *ralloc_parent(const void *ptr);
233
234 /**
235 * Return a context whose memory will be automatically freed at program exit.
236 *
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.
240 */
241 void *ralloc_autofree_context(void);
242
243 /**
244 * Set a callback to occur just before an object is freed.
245 */
246 void ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
247
248 /// \defgroup array String Functions @{
249 /**
250 * Duplicate a string, allocating the memory from the given context.
251 */
252 char *ralloc_strdup(const void *ctx, const char *str);
253
254 /**
255 * Duplicate a string, allocating the memory from the given context.
256 *
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.
259 */
260 char *ralloc_strndup(const void *ctx, const char *str, size_t n);
261
262 /**
263 * Concatenate two strings, allocating the necessary space.
264 *
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.
268 *
269 * The result will always be null-terminated.
270 *
271 * \return True unless allocation failed.
272 */
273 bool ralloc_strcat(char **dest, const char *str);
274
275 /**
276 * Concatenate two strings, allocating the necessary space.
277 *
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.
281 *
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.
284 *
285 * \return True unless allocation failed.
286 */
287 bool ralloc_strncat(char **dest, const char *str, size_t n);
288
289 /**
290 * Print to a string.
291 *
292 * This is analogous to \c sprintf, but allocates enough space (using \p ctx
293 * as the context) for the resulting string.
294 *
295 * \return The newly allocated string.
296 */
297 char *ralloc_asprintf (const void *ctx, const char *fmt, ...);
298
299 /**
300 * Print to a string, given a va_list.
301 *
302 * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
303 * as the context) for the resulting string.
304 *
305 * \return The newly allocated string.
306 */
307 char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
308
309 /**
310 * Append formatted text to the supplied string.
311 *
312 * \sa ralloc_asprintf
313 * \sa ralloc_strcat
314 *
315 * \p str will be updated to the new pointer unless allocation fails.
316 *
317 * \return True unless allocation failed.
318 */
319 bool ralloc_asprintf_append (char **str, const char *fmt, ...);
320
321 /**
322 * Append formatted text to the supplied string, given a va_list.
323 *
324 * \sa ralloc_vasprintf
325 * \sa ralloc_strcat
326 *
327 * \p str will be updated to the new pointer unless allocation fails.
328 *
329 * \return True unless allocation failed.
330 */
331 bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
332 /// @}
333
334 #ifdef __cplusplus
335 } /* end of extern "C" */
336 #endif
337
338 #endif