remove no longer needed define
[AROS.git] / workbench / libs / mesa / src / glsl / ralloc.c
blob6a5eac6b9a34c5c5f575ab8565fc30c0e9cd6cf6
1 /*
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
13 * Software.
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.
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
31 #include "ralloc.h"
33 #ifdef __GNUC__
34 #define likely(x) __builtin_expect(!!(x),1)
35 #define unlikely(x) __builtin_expect(!!(x),0)
36 #else
37 #define likely(x) !!(x)
38 #define unlikely(x) !!(x)
39 #endif
41 #ifndef va_copy
42 #ifdef __va_copy
43 #define va_copy(dest, src) __va_copy((dest), (src))
44 #else
45 #define va_copy(dest, src) (dest) = (src)
46 #endif
47 #endif
49 #define CANARY 0x5A1106
51 struct ralloc_header
53 /* A canary value used to determine whether a pointer is ralloc'd. */
54 unsigned canary;
56 struct ralloc_header *parent;
58 /* The first child (head of a linked list) */
59 struct ralloc_header *child;
61 /* Linked list of siblings */
62 struct ralloc_header *prev;
63 struct ralloc_header *next;
65 void (*destructor)(void *);
68 typedef struct ralloc_header ralloc_header;
70 static void unlink_block(ralloc_header *info);
71 static void unsafe_free(ralloc_header *info);
73 static ralloc_header *
74 get_header(const void *ptr)
76 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
77 sizeof(ralloc_header));
78 assert(info->canary == CANARY);
79 return info;
82 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
84 static void
85 add_child(ralloc_header *parent, ralloc_header *info)
87 if (parent != NULL) {
88 info->parent = parent;
89 info->next = parent->child;
90 parent->child = info;
92 if (info->next != NULL)
93 info->next->prev = info;
97 void *
98 ralloc_context(const void *ctx)
100 return ralloc_size(ctx, 0);
103 void *
104 ralloc_size(const void *ctx, size_t size)
106 void *block = calloc(1, size + sizeof(ralloc_header));
108 ralloc_header *info = (ralloc_header *) block;
109 ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
111 add_child(parent, info);
113 info->canary = CANARY;
115 return PTR_FROM_HEADER(info);
118 void *
119 rzalloc_size(const void *ctx, size_t size)
121 void *ptr = ralloc_size(ctx, size);
122 if (likely(ptr != NULL))
123 memset(ptr, 0, size);
124 return ptr;
127 /* helper function - assumes ptr != NULL */
128 static void *
129 resize(void *ptr, size_t size)
131 ralloc_header *child, *old, *info;
133 old = get_header(ptr);
134 info = realloc(old, size + sizeof(ralloc_header));
136 if (info == NULL)
137 return NULL;
139 /* Update parent and sibling's links to the reallocated node. */
140 if (info != old && info->parent != NULL) {
141 if (info->parent->child == old)
142 info->parent->child = info;
144 if (info->prev != NULL)
145 info->prev->next = info;
147 if (info->next != NULL)
148 info->next->prev = info;
151 /* Update child->parent links for all children */
152 for (child = info->child; child != NULL; child = child->next)
153 child->parent = info;
155 return PTR_FROM_HEADER(info);
158 void *
159 reralloc_size(const void *ctx, void *ptr, size_t size)
161 if (unlikely(ptr == NULL))
162 return ralloc_size(ctx, size);
164 assert(ralloc_parent(ptr) == ctx);
165 return resize(ptr, size);
168 void *
169 ralloc_array_size(const void *ctx, size_t size, unsigned count)
171 if (count > SIZE_MAX/size)
172 return NULL;
174 return ralloc_size(ctx, size * count);
177 void *
178 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
180 if (count > SIZE_MAX/size)
181 return NULL;
183 return rzalloc_size(ctx, size * count);
186 void *
187 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
189 if (count > SIZE_MAX/size)
190 return NULL;
192 return reralloc_size(ctx, ptr, size * count);
195 void
196 ralloc_free(void *ptr)
198 ralloc_header *info;
200 if (ptr == NULL)
201 return;
203 info = get_header(ptr);
204 unlink_block(info);
205 unsafe_free(info);
208 static void
209 unlink_block(ralloc_header *info)
211 /* Unlink from parent & siblings */
212 if (info->parent != NULL) {
213 if (info->parent->child == info)
214 info->parent->child = info->next;
216 if (info->prev != NULL)
217 info->prev->next = info->next;
219 if (info->next != NULL)
220 info->next->prev = info->prev;
222 info->parent = NULL;
223 info->prev = NULL;
224 info->next = NULL;
227 static void
228 unsafe_free(ralloc_header *info)
230 /* Recursively free any children...don't waste time unlinking them. */
231 ralloc_header *temp;
232 while (info->child != NULL) {
233 temp = info->child;
234 info->child = temp->next;
235 unsafe_free(temp);
238 /* Free the block itself. Call the destructor first, if any. */
239 if (info->destructor != NULL)
240 info->destructor(PTR_FROM_HEADER(info));
242 free(info);
245 void
246 ralloc_steal(const void *new_ctx, void *ptr)
248 ralloc_header *info, *parent;
250 if (unlikely(ptr == NULL))
251 return;
253 info = get_header(ptr);
254 parent = get_header(new_ctx);
256 unlink_block(info);
258 add_child(parent, info);
261 void *
262 ralloc_parent(const void *ptr)
264 ralloc_header *info;
266 if (unlikely(ptr == NULL))
267 return NULL;
269 info = get_header(ptr);
270 return PTR_FROM_HEADER(info->parent);
273 static void *autofree_context = NULL;
275 static void
276 autofree(void)
278 ralloc_free(autofree_context);
281 void *
282 ralloc_autofree_context(void)
284 if (unlikely(autofree_context == NULL)) {
285 autofree_context = ralloc_context(NULL);
286 atexit(autofree);
288 return autofree_context;
291 void
292 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
294 ralloc_header *info = get_header(ptr);
295 info->destructor = destructor;
298 char *
299 ralloc_strdup(const void *ctx, const char *str)
301 size_t n;
302 char *ptr;
304 if (unlikely(str == NULL))
305 return NULL;
307 n = strlen(str);
308 ptr = ralloc_array(ctx, char, n + 1);
309 memcpy(ptr, str, n);
310 ptr[n] = '\0';
311 return ptr;
314 char *
315 ralloc_strndup(const void *ctx, const char *str, size_t max)
317 size_t n;
318 char *ptr;
320 if (unlikely(str == NULL))
321 return NULL;
323 n = strlen(str);
324 if (n > max)
325 n = max;
327 ptr = ralloc_array(ctx, char, n + 1);
328 memcpy(ptr, str, n);
329 ptr[n] = '\0';
330 return ptr;
333 /* helper routine for strcat/strncat - n is the exact amount to copy */
334 static bool
335 cat(char **dest, const char *str, size_t n)
337 char *both;
338 size_t existing_length;
339 assert(dest != NULL && *dest != NULL);
341 existing_length = strlen(*dest);
342 both = resize(*dest, existing_length + n + 1);
343 if (unlikely(both == NULL))
344 return false;
346 memcpy(both + existing_length, str, n);
347 both[existing_length + n] = '\0';
349 *dest = both;
350 return true;
354 bool
355 ralloc_strcat(char **dest, const char *str)
357 return cat(dest, str, strlen(str));
360 bool
361 ralloc_strncat(char **dest, const char *str, size_t n)
363 /* Clamp n to the string length */
364 size_t str_length = strlen(str);
365 if (str_length < n)
366 n = str_length;
368 return cat(dest, str, n);
371 char *
372 ralloc_asprintf(const void *ctx, const char *fmt, ...)
374 char *ptr;
375 va_list args;
376 va_start(args, fmt);
377 ptr = ralloc_vasprintf(ctx, fmt, args);
378 va_end(args);
379 return ptr;
382 /* Return the length of the string that would be generated by a printf-style
383 * format and argument list, not including the \0 byte.
385 static size_t
386 printf_length(const char *fmt, va_list untouched_args)
388 int size;
389 char junk;
391 /* Make a copy of the va_list so the original caller can still use it */
392 va_list args;
393 va_copy(args, untouched_args);
395 #ifdef _MSC_VER
396 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
397 * if the number of characters to write is greater than count.
399 size = _vscprintf(fmt, args);
400 (void)junk;
401 #else
402 size = vsnprintf(&junk, 1, fmt, args);
403 #endif
404 assert(size >= 0);
406 va_end(args);
408 return size;
411 char *
412 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
414 size_t size = printf_length(fmt, args) + 1;
416 char *ptr = ralloc_size(ctx, size);
417 if (ptr != NULL)
418 vsnprintf(ptr, size, fmt, args);
420 return ptr;
423 bool
424 ralloc_asprintf_append(char **str, const char *fmt, ...)
426 bool success;
427 va_list args;
428 va_start(args, fmt);
429 success = ralloc_vasprintf_append(str, fmt, args);
430 va_end(args);
431 return success;
434 bool
435 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
437 size_t existing_length, new_length;
438 char *ptr;
440 assert(str != NULL);
442 if (unlikely(*str == NULL)) {
443 // Assuming a NULL context is probably bad, but it's expected behavior.
444 *str = ralloc_vasprintf(NULL, fmt, args);
445 return true;
448 existing_length = strlen(*str);
449 new_length = printf_length(fmt, args);
451 ptr = resize(*str, existing_length + new_length + 1);
452 if (unlikely(ptr == NULL))
453 return false;
455 vsnprintf(ptr + existing_length, new_length + 1, fmt, args);
456 *str = ptr;
457 return true;