[DMUSIC]
[reactos.git] / reactos / dll / opengl / mesa / 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 * 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/
45 */
46
47 #ifndef RALLOC_H
48 #define RALLOC_H
49
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53
54 #include <stddef.h>
55 #include <stdarg.h>
56 #include <stdbool.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, ...);
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 *
333 * \return True unless allocation failed.
334 */
335 bool ralloc_asprintf_rewrite_tail(char **str, size_t start,
336 const char *fmt, ...);
337
338 /**
339 * Rewrite the tail of an existing string, starting at a given index.
340 *
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.
343 *
344 * This can be used to append formatted text when the length of the existing
345 * string is already known, saving a strlen() call.
346 *
347 * \sa ralloc_vasprintf_append
348 *
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
353 *
354 * \p str will be updated to the new pointer unless allocation fails.
355 *
356 * \return True unless allocation failed.
357 */
358 bool ralloc_vasprintf_rewrite_tail(char **str, size_t start, const char *fmt,
359 va_list args);
360
361 /**
362 * Append formatted text to the supplied string.
363 *
364 * This is equivalent to
365 * \code
366 * ralloc_asprintf_rewrite_tail(str, strlen(*str), fmt, ...)
367 * \endcode
368 *
369 * \sa ralloc_asprintf
370 * \sa ralloc_asprintf_rewrite_tail
371 * \sa ralloc_strcat
372 *
373 * \p str will be updated to the new pointer unless allocation fails.
374 *
375 * \return True unless allocation failed.
376 */
377 bool ralloc_asprintf_append (char **str, const char *fmt, ...);
378
379 /**
380 * Append formatted text to the supplied string, given a va_list.
381 *
382 * This is equivalent to
383 * \code
384 * ralloc_vasprintf_rewrite_tail(str, strlen(*str), fmt, args)
385 * \endcode
386 *
387 * \sa ralloc_vasprintf
388 * \sa ralloc_vasprintf_rewrite_tail
389 * \sa ralloc_strcat
390 *
391 * \p str will be updated to the new pointer unless allocation fails.
392 *
393 * \return True unless allocation failed.
394 */
395 bool ralloc_vasprintf_append(char **str, const char *fmt, va_list args);
396 /// @}
397
398 #ifdef __cplusplus
399 } /* end of extern "C" */
400 #endif
401
402 #endif