xref: /aosp_15_r20/external/igt-gpu-tools/assembler/ralloc.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
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 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdint.h>
30 
31 /* Some versions of MinGW are missing _vscprintf's declaration, although they
32  * still provide the symbol in the import library. */
33 #ifdef __MINGW32__
34 _CRTIMP int _vscprintf(const char *format, va_list argptr);
35 #endif
36 
37 #include "ralloc.h"
38 
39 #ifndef va_copy
40 #ifdef __va_copy
41 #define va_copy(dest, src) __va_copy((dest), (src))
42 #else
43 #define va_copy(dest, src) (dest) = (src)
44 #endif
45 #endif
46 
47 #define CANARY 0x5A1106
48 
49 struct ralloc_header
50 {
51    /* A canary value used to determine whether a pointer is ralloc'd. */
52    unsigned canary;
53 
54    struct ralloc_header *parent;
55 
56    /* The first child (head of a linked list) */
57    struct ralloc_header *child;
58 
59    /* Linked list of siblings */
60    struct ralloc_header *prev;
61    struct ralloc_header *next;
62 
63    void (*destructor)(void *);
64 };
65 
66 typedef struct ralloc_header ralloc_header;
67 
68 static void unlink_block(ralloc_header *info);
69 static void unsafe_free(ralloc_header *info);
70 
71 static ralloc_header *
get_header(const void * ptr)72 get_header(const void *ptr)
73 {
74    ralloc_header *info = (ralloc_header *) (((char *) ptr) -
75 					    sizeof(ralloc_header));
76    assert(info->canary == CANARY);
77    return info;
78 }
79 
80 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
81 
82 static void
add_child(ralloc_header * parent,ralloc_header * info)83 add_child(ralloc_header *parent, ralloc_header *info)
84 {
85    if (parent != NULL) {
86       info->parent = parent;
87       info->next = parent->child;
88       parent->child = info;
89 
90       if (info->next != NULL)
91 	 info->next->prev = info;
92    }
93 }
94 
95 void *
ralloc_context(const void * ctx)96 ralloc_context(const void *ctx)
97 {
98    return ralloc_size(ctx, 0);
99 }
100 
101 void *
ralloc_size(const void * ctx,size_t size)102 ralloc_size(const void *ctx, size_t size)
103 {
104    void *block = calloc(1, size + sizeof(ralloc_header));
105 
106    ralloc_header *info = (ralloc_header *) block;
107    ralloc_header *parent = ctx != NULL ? get_header(ctx) : NULL;
108 
109    add_child(parent, info);
110 
111    info->canary = CANARY;
112 
113    return PTR_FROM_HEADER(info);
114 }
115 
116 void *
rzalloc_size(const void * ctx,size_t size)117 rzalloc_size(const void *ctx, size_t size)
118 {
119    void *ptr = ralloc_size(ctx, size);
120    if (likely(ptr != NULL))
121       memset(ptr, 0, size);
122    return ptr;
123 }
124 
125 /* helper function - assumes ptr != NULL */
126 static void *
resize(void * ptr,size_t size)127 resize(void *ptr, size_t size)
128 {
129    ralloc_header *child, *old, *info;
130 
131    old = get_header(ptr);
132    info = realloc(old, size + sizeof(ralloc_header));
133 
134    if (info == NULL)
135       return NULL;
136 
137    /* Update parent and sibling's links to the reallocated node. */
138    if (info != old && info->parent != NULL) {
139       if (info->parent->child == old)
140 	 info->parent->child = info;
141 
142       if (info->prev != NULL)
143 	 info->prev->next = info;
144 
145       if (info->next != NULL)
146 	 info->next->prev = info;
147    }
148 
149    /* Update child->parent links for all children */
150    for (child = info->child; child != NULL; child = child->next)
151       child->parent = info;
152 
153    return PTR_FROM_HEADER(info);
154 }
155 
156 void *
reralloc_size(const void * ctx,void * ptr,size_t size)157 reralloc_size(const void *ctx, void *ptr, size_t size)
158 {
159    if (unlikely(ptr == NULL))
160       return ralloc_size(ctx, size);
161 
162    assert(ralloc_parent(ptr) == ctx);
163    return resize(ptr, size);
164 }
165 
166 void *
ralloc_array_size(const void * ctx,size_t size,unsigned count)167 ralloc_array_size(const void *ctx, size_t size, unsigned count)
168 {
169    if (count > SIZE_MAX/size)
170       return NULL;
171 
172    return ralloc_size(ctx, size * count);
173 }
174 
175 void *
rzalloc_array_size(const void * ctx,size_t size,unsigned count)176 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
177 {
178    if (count > SIZE_MAX/size)
179       return NULL;
180 
181    return rzalloc_size(ctx, size * count);
182 }
183 
184 void *
reralloc_array_size(const void * ctx,void * ptr,size_t size,unsigned count)185 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
186 {
187    if (count > SIZE_MAX/size)
188       return NULL;
189 
190    return reralloc_size(ctx, ptr, size * count);
191 }
192 
193 void
ralloc_free(void * ptr)194 ralloc_free(void *ptr)
195 {
196    ralloc_header *info;
197 
198    if (ptr == NULL)
199       return;
200 
201    info = get_header(ptr);
202    unlink_block(info);
203    unsafe_free(info);
204 }
205 
206 static void
unlink_block(ralloc_header * info)207 unlink_block(ralloc_header *info)
208 {
209    /* Unlink from parent & siblings */
210    if (info->parent != NULL) {
211       if (info->parent->child == info)
212 	 info->parent->child = info->next;
213 
214       if (info->prev != NULL)
215 	 info->prev->next = info->next;
216 
217       if (info->next != NULL)
218 	 info->next->prev = info->prev;
219    }
220    info->parent = NULL;
221    info->prev = NULL;
222    info->next = NULL;
223 }
224 
225 static void
unsafe_free(ralloc_header * info)226 unsafe_free(ralloc_header *info)
227 {
228    /* Recursively free any children...don't waste time unlinking them. */
229    ralloc_header *temp;
230    while (info->child != NULL) {
231       temp = info->child;
232       info->child = temp->next;
233       unsafe_free(temp);
234    }
235 
236    /* Free the block itself.  Call the destructor first, if any. */
237    if (info->destructor != NULL)
238       info->destructor(PTR_FROM_HEADER(info));
239 
240    free(info);
241 }
242 
243 void
ralloc_steal(const void * new_ctx,void * ptr)244 ralloc_steal(const void *new_ctx, void *ptr)
245 {
246    ralloc_header *info, *parent;
247 
248    if (unlikely(ptr == NULL))
249       return;
250 
251    info = get_header(ptr);
252    parent = get_header(new_ctx);
253 
254    unlink_block(info);
255 
256    add_child(parent, info);
257 }
258 
259 void *
ralloc_parent(const void * ptr)260 ralloc_parent(const void *ptr)
261 {
262    ralloc_header *info;
263 
264    if (unlikely(ptr == NULL))
265       return NULL;
266 
267    info = get_header(ptr);
268    return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
269 }
270 
271 static void *autofree_context = NULL;
272 
273 static void
autofree(void)274 autofree(void)
275 {
276    ralloc_free(autofree_context);
277 }
278 
279 void *
ralloc_autofree_context(void)280 ralloc_autofree_context(void)
281 {
282    if (unlikely(autofree_context == NULL)) {
283       autofree_context = ralloc_context(NULL);
284       atexit(autofree);
285    }
286    return autofree_context;
287 }
288 
289 void
ralloc_set_destructor(const void * ptr,void (* destructor)(void *))290 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
291 {
292    ralloc_header *info = get_header(ptr);
293    info->destructor = destructor;
294 }
295 
296 char *
ralloc_strdup(const void * ctx,const char * str)297 ralloc_strdup(const void *ctx, const char *str)
298 {
299    size_t n;
300    char *ptr;
301 
302    if (unlikely(str == NULL))
303       return NULL;
304 
305    n = strlen(str);
306    ptr = ralloc_array(ctx, char, n + 1);
307    memcpy(ptr, str, n);
308    ptr[n] = '\0';
309    return ptr;
310 }
311 
312 char *
ralloc_strndup(const void * ctx,const char * str,size_t max)313 ralloc_strndup(const void *ctx, const char *str, size_t max)
314 {
315    size_t n;
316    char *ptr;
317 
318    if (unlikely(str == NULL))
319       return NULL;
320 
321    n = strlen(str);
322    if (n > max)
323       n = max;
324 
325    ptr = ralloc_array(ctx, char, n + 1);
326    memcpy(ptr, str, n);
327    ptr[n] = '\0';
328    return ptr;
329 }
330 
331 /* helper routine for strcat/strncat - n is the exact amount to copy */
332 static bool
cat(char ** dest,const char * str,size_t n)333 cat(char **dest, const char *str, size_t n)
334 {
335    char *both;
336    size_t existing_length;
337    assert(dest != NULL && *dest != NULL);
338 
339    existing_length = strlen(*dest);
340    both = resize(*dest, existing_length + n + 1);
341    if (unlikely(both == NULL))
342       return false;
343 
344    memcpy(both + existing_length, str, n);
345    both[existing_length + n] = '\0';
346 
347    *dest = both;
348    return true;
349 }
350 
351 
352 bool
ralloc_strcat(char ** dest,const char * str)353 ralloc_strcat(char **dest, const char *str)
354 {
355    return cat(dest, str, strlen(str));
356 }
357 
358 bool
ralloc_strncat(char ** dest,const char * str,size_t n)359 ralloc_strncat(char **dest, const char *str, size_t n)
360 {
361    /* Clamp n to the string length */
362    size_t str_length = strlen(str);
363    if (str_length < n)
364       n = str_length;
365 
366    return cat(dest, str, n);
367 }
368 
369 char *
ralloc_asprintf(const void * ctx,const char * fmt,...)370 ralloc_asprintf(const void *ctx, const char *fmt, ...)
371 {
372    char *ptr;
373    va_list args;
374    va_start(args, fmt);
375    ptr = ralloc_vasprintf(ctx, fmt, args);
376    va_end(args);
377    return ptr;
378 }
379 
380 /* Return the length of the string that would be generated by a printf-style
381  * format and argument list, not including the \0 byte.
382  */
383 static size_t
printf_length(const char * fmt,va_list untouched_args)384 printf_length(const char *fmt, va_list untouched_args)
385 {
386    int size;
387    char junk;
388 
389    /* Make a copy of the va_list so the original caller can still use it */
390    va_list args;
391    va_copy(args, untouched_args);
392 
393 #ifdef _WIN32
394    /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
395     * if the number of characters to write is greater than count.
396     */
397    size = _vscprintf(fmt, args);
398    (void)junk;
399 #else
400    size = vsnprintf(&junk, 1, fmt, args);
401 #endif
402    assert(size >= 0);
403 
404    va_end(args);
405 
406    return size;
407 }
408 
409 char *
ralloc_vasprintf(const void * ctx,const char * fmt,va_list args)410 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
411 {
412    size_t size = printf_length(fmt, args) + 1;
413 
414    char *ptr = ralloc_size(ctx, size);
415    if (ptr != NULL)
416       vsnprintf(ptr, size, fmt, args);
417 
418    return ptr;
419 }
420 
421 bool
ralloc_asprintf_append(char ** str,const char * fmt,...)422 ralloc_asprintf_append(char **str, const char *fmt, ...)
423 {
424    bool success;
425    va_list args;
426    va_start(args, fmt);
427    success = ralloc_vasprintf_append(str, fmt, args);
428    va_end(args);
429    return success;
430 }
431 
432 bool
ralloc_vasprintf_append(char ** str,const char * fmt,va_list args)433 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
434 {
435    size_t existing_length;
436    assert(str != NULL);
437    existing_length = *str ? strlen(*str) : 0;
438    return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
439 }
440 
441 bool
ralloc_asprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,...)442 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
443 {
444    bool success;
445    va_list args;
446    va_start(args, fmt);
447    success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
448    va_end(args);
449    return success;
450 }
451 
452 bool
ralloc_vasprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,va_list args)453 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
454 			      va_list args)
455 {
456    size_t new_length;
457    char *ptr;
458 
459    assert(str != NULL);
460 
461    if (unlikely(*str == NULL)) {
462       // Assuming a NULL context is probably bad, but it's expected behavior.
463       *str = ralloc_vasprintf(NULL, fmt, args);
464       return true;
465    }
466 
467    new_length = printf_length(fmt, args);
468 
469    ptr = resize(*str, *start + new_length + 1);
470    if (unlikely(ptr == NULL))
471       return false;
472 
473    vsnprintf(ptr + *start, new_length + 1, fmt, args);
474    *str = ptr;
475    *start += new_length;
476    return true;
477 }
478