mesa/SConscript: Use Makefile.sources instead of duplicating the file lists
[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 * 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 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #include <stddef.h>
54 #include <stdarg.h>
55 #include <stdbool.h>
56 #include "main/compiler.h"
57
58 /**
59 * \def ralloc(ctx, type)
60 * Allocate a new object chained off of the given context.
61 *
62 * This is equivalent to:
63 * \code
64 * ((type *) ralloc_size(ctx, sizeof(type))
65 * \endcode
66 */
67 #define ralloc(ctx, type) ((type *) ralloc_size(ctx, sizeof(type)))
68
69 /**
70 * \def rzalloc(ctx, type)
71 * Allocate a new object out of the given context and initialize it to zero.
72 *
73 * This is equivalent to:
74 * \code
75 * ((type *) rzalloc_size(ctx, sizeof(type))
76 * \endcode
77 */
78 #define rzalloc(ctx, type) ((type *) rzalloc_size(ctx, sizeof(type)))
79
80 /**
81 * Allocate a new ralloc context.
82 *
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.
85 *
86 * It is equivalent to:
87 * \code
88 * ((type *) ralloc_size(ctx, 0)
89 * \endcode
90 */
91 void *ralloc_context(const void *ctx);
92
93 /**
94 * Allocate memory chained off of the given context.
95 *
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.
99 */
100 void *ralloc_size(const void *ctx, size_t size);
101
102 /**
103 * Allocate zero-initialized memory chained off of the given context.
104 *
105 * This is similar to \c calloc with a size of 1.
106 */
107 void *rzalloc_size(const void *ctx, size_t size);
108
109 /**
110 * Resize a piece of ralloc-managed memory, preserving data.
111 *
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.
115 *
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.
120 */
121 void *reralloc_size(const void *ctx, void *ptr, size_t size);
122
123 /// \defgroup array Array Allocators @{
124
125 /**
126 * \def ralloc_array(ctx, type, count)
127 * Allocate an array of objects chained off the given context.
128 *
129 * Similar to \c calloc, but does not initialize the memory to zero.
130 *
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.
133 *
134 * This is equivalent to:
135 * \code
136 * ((type *) ralloc_array_size(ctx, sizeof(type), count)
137 * \endcode
138 */
139 #define ralloc_array(ctx, type, count) \
140 ((type *) ralloc_array_size(ctx, sizeof(type), count))
141
142 /**
143 * \def rzalloc_array(ctx, type, count)
144 * Allocate a zero-initialized array chained off the given context.
145 *
146 * Similar to \c calloc.
147 *
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.
150 *
151 * This is equivalent to:
152 * \code
153 * ((type *) rzalloc_array_size(ctx, sizeof(type), count)
154 * \endcode
155 */
156 #define rzalloc_array(ctx, type, count) \
157 ((type *) rzalloc_array_size(ctx, sizeof(type), count))
158
159 /**
160 * \def reralloc(ctx, ptr, type, count)
161 * Resize a ralloc-managed array, preserving data.
162 *
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.
166 *
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.
169 *
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.
175 */
176 #define reralloc(ctx, ptr, type, count) \
177 ((type *) reralloc_array_size(ctx, ptr, sizeof(type), count))
178
179 /**
180 * Allocate memory for an array chained off the given context.
181 *
182 * Similar to \c calloc, but does not initialize the memory to zero.
183 *
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.
186 */
187 void *ralloc_array_size(const void *ctx, size_t size, unsigned count);
188
189 /**
190 * Allocate a zero-initialized array chained off the given context.
191 *
192 * Similar to \c calloc.
193 *
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.
196 */
197 void *rzalloc_array_size(const void *ctx, size_t size, unsigned count);
198
199 /**
200 * Resize a ralloc-managed array, preserving data.
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 size The size of an individual element.
213 * \param count The number of elements to allocate.
214 *
215 * \return True unless allocation failed.
216 */
217 void *reralloc_array_size(const void *ctx, void *ptr, size_t size,
218 unsigned count);
219 /// @}
220
221 /**
222 * Free a piece of ralloc-managed memory.
223 *
224 * This will also free the memory of any children allocated this context.
225 */
226 void ralloc_free(void *ptr);
227
228 /**
229 * "Steal" memory from one context, changing it to another.
230 *
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.
233 */
234 void ralloc_steal(const void *new_ctx, void *ptr);
235
236 /**
237 * Return the given pointer's ralloc context.
238 */
239 void *ralloc_parent(const void *ptr);
240
241 /**
242 * Return a context whose memory will be automatically freed at program exit.
243 *
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.
247 */
248 void *ralloc_autofree_context(void);
249
250 /**
251 * Set a callback to occur just before an object is freed.
252 */
253 void ralloc_set_destructor(const void *ptr, void(*destructor)(void *));
254
255 /// \defgroup array String Functions @{
256 /**
257 * Duplicate a string, allocating the memory from the given context.
258 */
259 char *ralloc_strdup(const void *ctx, const char *str);
260
261 /**
262 * Duplicate a string, allocating the memory from the given context.
263 *
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.
266 */
267 char *ralloc_strndup(const void *ctx, const char *str, size_t n);
268
269 /**
270 * Concatenate two strings, allocating the necessary space.
271 *
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.
275 *
276 * The result will always be null-terminated.
277 *
278 * \return True unless allocation failed.
279 */
280 bool ralloc_strcat(char **dest, const char *str);
281
282 /**
283 * Concatenate two strings, allocating the necessary space.
284 *
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.
288 *
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.
291 *
292 * \return True unless allocation failed.
293 */
294 bool ralloc_strncat(char **dest, const char *str, size_t n);
295
296 /**
297 * Print to a string.
298 *
299 * This is analogous to \c sprintf, but allocates enough space (using \p ctx
300 * as the context) for the resulting string.
301 *
302 * \return The newly allocated string.
303 */
304 char *ralloc_asprintf (const void *ctx, const char *fmt, ...) PRINTFLIKE(2, 3);
305
306 /**
307 * Print to a string, given a va_list.
308 *
309 * This is analogous to \c vsprintf, but allocates enough space (using \p ctx
310 * as the context) for the resulting string.
311 *
312 * \return The newly allocated string.
313 */
314 char *ralloc_vasprintf(const void *ctx, const char *fmt, va_list args);
315
316 /**
317 * Rewrite the tail of an existing string, starting at a given index.
318 *
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.
321 *
322 * This can be used to append formatted text when the length of the existing
323 * string is already known, saving a strlen() call.
324 *
325 * \sa ralloc_asprintf_append
326 *
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
330 *
331 * \p str will be updated to the new pointer unless allocation fails.
332 * \p start will be increased by the length of the newly formatted text.
333 *
334 * \return True unless allocation failed.
335 */
336 bool ralloc_asprintf_rewrite_tail(char **str, size_t *start,
337 const char *fmt, ...)
338 PRINTFLIKE(3, 4);
339
340 /**
341 * Rewrite the tail of an existing string, starting at a given index.
342 *
343 * Overwrites the contents of *str starting at \p start with newly formatted
344 * text, including a new null-terminator. Allocates more memory as necessary.
345 *
346 * This can be used to append formatted text when the length of the existing
347 * string is already known, saving a strlen() call.
348 *
349 * \sa ralloc_vasprintf_append
350 *
351 * \param str The string to be updated.
352 * \param start The index to start appending new data at.
353 * \param fmt A printf-style formatting string
354 * \param args A va_list containing the data to be formatted
355 *
356 * \p str will be updated to the new pointer unless allocation fails.
357 * \p start will be increased by the length of the newly formatted text.
358 *
359 * \return True unless allocation failed.
360 */
361 bool ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
362 va_list args);
363
364 /**
365 * Append formatted text to the supplied string.
366 *
367 * This is equivalent to
368 * \code
369 * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
370 * \endcode
371 *
372 * \sa ralloc_asprintf
373 * \sa ralloc_asprintf_rewrite_tail
374 * \sa ralloc_strcat
375 *
376 * \p str will be updated to the new pointer unless allocation fails.
377 *
378 * \return True unless allocation failed.
379 */
380 bool ralloc_asprintf_append (char **str, const char *fmt, ...)
381 PRINTFLIKE(2, 3);
382
383 /**
384 * Append formatted text to the supplied string, given a va_list.
385 *
386 * This is equivalent to
387 * \code
388 * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
389 * \endcode
390 *
391 * \sa ralloc_vasprintf
392 * \sa ralloc_vasprintf_rewrite_tail
393 * \sa ralloc_strcat
394 *
395 * \p str will be updated to the new pointer unless allocation fails.
396 *
397 * \return True unless allocation failed.
398 */
399 bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
400 /// @}
401
402 #ifdef __cplusplus
403 } /* end of extern "C" */
404 #endif
405
406 /**
407 * Declare C++ new and delete operators which use ralloc.
408 *
409 * Placing this macro in the body of a class makes it possible to do:
410 *
411 * TYPE *var = new(mem_ctx) TYPE(...);
412 * delete var;
413 *
414 * which is more idiomatic in C++ than calling ralloc.
415 */
416 #define DECLARE_RALLOC_CXX_OPERATORS(TYPE) \
417 private: \
418 static void _ralloc_destructor(void *p) \
419 { \
420 reinterpret_cast<TYPE *>(p)->~TYPE(); \
421 } \
422 public: \
423 static void* operator new(size_t size, void *mem_ctx) \
424 { \
425 void *p = ralloc_size(mem_ctx, size); \
426 assert(p != NULL); \
427 if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \
428 ralloc_set_destructor(p, _ralloc_destructor); \
429 return p; \
430 } \
431 \
432 static void operator delete(void *p) \
433 { \
434 /* The object's destructor is guaranteed to have already been \
435 * called by the delete operator at this point -- Make sure it's \
436 * not called again. \
437 */ \
438 if (!HAS_TRIVIAL_DESTRUCTOR(TYPE)) \
439 ralloc_set_destructor(p, NULL); \
440 ralloc_free(p); \
441 }
442
443
444 #endif