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 <stdarg.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "util/list.h"
32 #include "util/macros.h"
33 #include "util/u_math.h"
34 #include "util/u_printf.h"
35
36 #include "ralloc.h"
37
38 #define CANARY 0x5A1106
39
40 #if defined(__LP64__) || defined(_WIN64)
41 #define HEADER_ALIGN 16
42 #else
43 #define HEADER_ALIGN 8
44 #endif
45
46 /* Align the header's size so that ralloc() allocations will return with the
47 * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on
48 * 64-bit), avoiding performance penalities on x86 and alignment faults on
49 * ARM.
50 */
51 struct ralloc_header
52 {
53 alignas(HEADER_ALIGN)
54
55 #ifndef NDEBUG
56 /* A canary value used to determine whether a pointer is ralloc'd. */
57 unsigned canary;
58 #endif
59
60 struct ralloc_header *parent;
61
62 /* The first child (head of a linked list) */
63 struct ralloc_header *child;
64
65 /* Linked list of siblings */
66 struct ralloc_header *prev;
67 struct ralloc_header *next;
68
69 void (*destructor)(void *);
70 };
71
72 typedef struct ralloc_header ralloc_header;
73
74 static void unlink_block(ralloc_header *info);
75 static void unsafe_free(ralloc_header *info);
76
77 static ralloc_header *
get_header(const void * ptr)78 get_header(const void *ptr)
79 {
80 ralloc_header *info = (ralloc_header *) (((char *) ptr) -
81 sizeof(ralloc_header));
82 assert(info->canary == CANARY);
83 return info;
84 }
85
86 #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
87
88 static void
add_child(ralloc_header * parent,ralloc_header * info)89 add_child(ralloc_header *parent, ralloc_header *info)
90 {
91 if (parent != NULL) {
92 info->parent = parent;
93 info->next = parent->child;
94 parent->child = info;
95
96 if (info->next != NULL)
97 info->next->prev = info;
98 }
99 }
100
101 void *
ralloc_context(const void * ctx)102 ralloc_context(const void *ctx)
103 {
104 return ralloc_size(ctx, 0);
105 }
106
107 void *
ralloc_size(const void * ctx,size_t size)108 ralloc_size(const void *ctx, size_t size)
109 {
110 /* Some malloc allocation doesn't always align to 16 bytes even on 64 bits
111 * system, from Android bionic/tests/malloc_test.cpp:
112 * - Allocations of a size that rounds up to a multiple of 16 bytes
113 * must have at least 16 byte alignment.
114 * - Allocations of a size that rounds up to a multiple of 8 bytes and
115 * not 16 bytes, are only required to have at least 8 byte alignment.
116 */
117 void *block = malloc(align64(size + sizeof(ralloc_header),
118 alignof(ralloc_header)));
119 ralloc_header *info;
120 ralloc_header *parent;
121
122 if (unlikely(block == NULL))
123 return NULL;
124
125 info = (ralloc_header *) block;
126 /* measurements have shown that calloc is slower (because of
127 * the multiplication overflow checking?), so clear things
128 * manually
129 */
130 info->parent = NULL;
131 info->child = NULL;
132 info->prev = NULL;
133 info->next = NULL;
134 info->destructor = NULL;
135
136 parent = ctx != NULL ? get_header(ctx) : NULL;
137
138 add_child(parent, info);
139
140 #ifndef NDEBUG
141 info->canary = CANARY;
142 #endif
143
144 return PTR_FROM_HEADER(info);
145 }
146
147 void *
rzalloc_size(const void * ctx,size_t size)148 rzalloc_size(const void *ctx, size_t size)
149 {
150 void *ptr = ralloc_size(ctx, size);
151
152 if (likely(ptr))
153 memset(ptr, 0, size);
154
155 return ptr;
156 }
157
158 /* helper function - assumes ptr != NULL */
159 static void *
resize(void * ptr,size_t size)160 resize(void *ptr, size_t size)
161 {
162 ralloc_header *child, *old, *info;
163
164 old = get_header(ptr);
165 info = realloc(old, align64(size + sizeof(ralloc_header),
166 alignof(ralloc_header)));
167
168 if (info == NULL)
169 return NULL;
170
171 /* Update parent and sibling's links to the reallocated node. */
172 if (info != old && info->parent != NULL) {
173 if (info->parent->child == old)
174 info->parent->child = info;
175
176 if (info->prev != NULL)
177 info->prev->next = info;
178
179 if (info->next != NULL)
180 info->next->prev = info;
181 }
182
183 /* Update child->parent links for all children */
184 for (child = info->child; child != NULL; child = child->next)
185 child->parent = info;
186
187 return PTR_FROM_HEADER(info);
188 }
189
190 void *
reralloc_size(const void * ctx,void * ptr,size_t size)191 reralloc_size(const void *ctx, void *ptr, size_t size)
192 {
193 if (unlikely(ptr == NULL))
194 return ralloc_size(ctx, size);
195
196 assert(ralloc_parent(ptr) == ctx);
197 return resize(ptr, size);
198 }
199
200 void *
rerzalloc_size(const void * ctx,void * ptr,size_t old_size,size_t new_size)201 rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size)
202 {
203 if (unlikely(ptr == NULL))
204 return rzalloc_size(ctx, new_size);
205
206 assert(ralloc_parent(ptr) == ctx);
207 ptr = resize(ptr, new_size);
208
209 if (new_size > old_size)
210 memset((char *)ptr + old_size, 0, new_size - old_size);
211
212 return ptr;
213 }
214
215 void *
ralloc_array_size(const void * ctx,size_t size,unsigned count)216 ralloc_array_size(const void *ctx, size_t size, unsigned count)
217 {
218 if (count > SIZE_MAX/size)
219 return NULL;
220
221 return ralloc_size(ctx, size * count);
222 }
223
224 void *
rzalloc_array_size(const void * ctx,size_t size,unsigned count)225 rzalloc_array_size(const void *ctx, size_t size, unsigned count)
226 {
227 if (count > SIZE_MAX/size)
228 return NULL;
229
230 return rzalloc_size(ctx, size * count);
231 }
232
233 void *
reralloc_array_size(const void * ctx,void * ptr,size_t size,unsigned count)234 reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
235 {
236 if (count > SIZE_MAX/size)
237 return NULL;
238
239 return reralloc_size(ctx, ptr, size * count);
240 }
241
242 void *
rerzalloc_array_size(const void * ctx,void * ptr,size_t size,unsigned old_count,unsigned new_count)243 rerzalloc_array_size(const void *ctx, void *ptr, size_t size,
244 unsigned old_count, unsigned new_count)
245 {
246 if (new_count > SIZE_MAX/size)
247 return NULL;
248
249 return rerzalloc_size(ctx, ptr, size * old_count, size * new_count);
250 }
251
252 void
ralloc_free(void * ptr)253 ralloc_free(void *ptr)
254 {
255 ralloc_header *info;
256
257 if (ptr == NULL)
258 return;
259
260 info = get_header(ptr);
261 unlink_block(info);
262 unsafe_free(info);
263 }
264
265 static void
unlink_block(ralloc_header * info)266 unlink_block(ralloc_header *info)
267 {
268 /* Unlink from parent & siblings */
269 if (info->parent != NULL) {
270 if (info->parent->child == info)
271 info->parent->child = info->next;
272
273 if (info->prev != NULL)
274 info->prev->next = info->next;
275
276 if (info->next != NULL)
277 info->next->prev = info->prev;
278 }
279 info->parent = NULL;
280 info->prev = NULL;
281 info->next = NULL;
282 }
283
284 static void
unsafe_free(ralloc_header * info)285 unsafe_free(ralloc_header *info)
286 {
287 /* Recursively free any children...don't waste time unlinking them. */
288 ralloc_header *temp;
289 while (info->child != NULL) {
290 temp = info->child;
291 info->child = temp->next;
292 unsafe_free(temp);
293 }
294
295 /* Free the block itself. Call the destructor first, if any. */
296 if (info->destructor != NULL)
297 info->destructor(PTR_FROM_HEADER(info));
298
299 free(info);
300 }
301
302 void
ralloc_steal(const void * new_ctx,void * ptr)303 ralloc_steal(const void *new_ctx, void *ptr)
304 {
305 ralloc_header *info, *parent;
306
307 if (unlikely(ptr == NULL))
308 return;
309
310 info = get_header(ptr);
311 parent = new_ctx ? get_header(new_ctx) : NULL;
312
313 unlink_block(info);
314
315 add_child(parent, info);
316 }
317
318 void
ralloc_adopt(const void * new_ctx,void * old_ctx)319 ralloc_adopt(const void *new_ctx, void *old_ctx)
320 {
321 ralloc_header *new_info, *old_info, *child;
322
323 if (unlikely(old_ctx == NULL))
324 return;
325
326 old_info = get_header(old_ctx);
327 new_info = get_header(new_ctx);
328
329 /* If there are no children, bail. */
330 if (unlikely(old_info->child == NULL))
331 return;
332
333 /* Set all the children's parent to new_ctx; get a pointer to the last child. */
334 for (child = old_info->child; child->next != NULL; child = child->next) {
335 child->parent = new_info;
336 }
337 child->parent = new_info;
338
339 /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
340 child->next = new_info->child;
341 if (child->next)
342 child->next->prev = child;
343 new_info->child = old_info->child;
344 old_info->child = NULL;
345 }
346
347 void *
ralloc_parent(const void * ptr)348 ralloc_parent(const void *ptr)
349 {
350 ralloc_header *info;
351
352 if (unlikely(ptr == NULL))
353 return NULL;
354
355 info = get_header(ptr);
356 return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
357 }
358
359 void
ralloc_set_destructor(const void * ptr,void (* destructor)(void *))360 ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
361 {
362 ralloc_header *info = get_header(ptr);
363 info->destructor = destructor;
364 }
365
366 char *
ralloc_strdup(const void * ctx,const char * str)367 ralloc_strdup(const void *ctx, const char *str)
368 {
369 size_t n;
370 char *ptr;
371
372 if (unlikely(str == NULL))
373 return NULL;
374
375 n = strlen(str);
376 ptr = ralloc_array(ctx, char, n + 1);
377 memcpy(ptr, str, n);
378 ptr[n] = '\0';
379 return ptr;
380 }
381
382 char *
ralloc_strndup(const void * ctx,const char * str,size_t max)383 ralloc_strndup(const void *ctx, const char *str, size_t max)
384 {
385 size_t n;
386 char *ptr;
387
388 if (unlikely(str == NULL))
389 return NULL;
390
391 n = strnlen(str, max);
392 ptr = ralloc_array(ctx, char, n + 1);
393 memcpy(ptr, str, n);
394 ptr[n] = '\0';
395 return ptr;
396 }
397
398 /* helper routine for strcat/strncat - n is the exact amount to copy */
399 static bool
cat(char ** dest,const char * str,size_t n)400 cat(char **dest, const char *str, size_t n)
401 {
402 char *both;
403 size_t existing_length;
404 assert(dest != NULL && *dest != NULL);
405
406 existing_length = strlen(*dest);
407 both = resize(*dest, existing_length + n + 1);
408 if (unlikely(both == NULL))
409 return false;
410
411 memcpy(both + existing_length, str, n);
412 both[existing_length + n] = '\0';
413
414 *dest = both;
415 return true;
416 }
417
418
419 bool
ralloc_strcat(char ** dest,const char * str)420 ralloc_strcat(char **dest, const char *str)
421 {
422 return cat(dest, str, strlen(str));
423 }
424
425 bool
ralloc_strncat(char ** dest,const char * str,size_t n)426 ralloc_strncat(char **dest, const char *str, size_t n)
427 {
428 return cat(dest, str, strnlen(str, n));
429 }
430
431 bool
ralloc_str_append(char ** dest,const char * str,size_t existing_length,size_t str_size)432 ralloc_str_append(char **dest, const char *str,
433 size_t existing_length, size_t str_size)
434 {
435 char *both;
436 assert(dest != NULL && *dest != NULL);
437
438 both = resize(*dest, existing_length + str_size + 1);
439 if (unlikely(both == NULL))
440 return false;
441
442 memcpy(both + existing_length, str, str_size);
443 both[existing_length + str_size] = '\0';
444
445 *dest = both;
446
447 return true;
448 }
449
450 char *
ralloc_asprintf(const void * ctx,const char * fmt,...)451 ralloc_asprintf(const void *ctx, const char *fmt, ...)
452 {
453 char *ptr;
454 va_list args;
455 va_start(args, fmt);
456 ptr = ralloc_vasprintf(ctx, fmt, args);
457 va_end(args);
458 return ptr;
459 }
460
461 char *
ralloc_vasprintf(const void * ctx,const char * fmt,va_list args)462 ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
463 {
464 size_t size = u_printf_length(fmt, args) + 1;
465
466 char *ptr = ralloc_size(ctx, size);
467 if (ptr != NULL)
468 vsnprintf(ptr, size, fmt, args);
469
470 return ptr;
471 }
472
473 bool
ralloc_asprintf_append(char ** str,const char * fmt,...)474 ralloc_asprintf_append(char **str, const char *fmt, ...)
475 {
476 bool success;
477 va_list args;
478 va_start(args, fmt);
479 success = ralloc_vasprintf_append(str, fmt, args);
480 va_end(args);
481 return success;
482 }
483
484 bool
ralloc_vasprintf_append(char ** str,const char * fmt,va_list args)485 ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
486 {
487 size_t existing_length;
488 assert(str != NULL);
489 existing_length = *str ? strlen(*str) : 0;
490 return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
491 }
492
493 bool
ralloc_asprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,...)494 ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
495 {
496 bool success;
497 va_list args;
498 va_start(args, fmt);
499 success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
500 va_end(args);
501 return success;
502 }
503
504 bool
ralloc_vasprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,va_list args)505 ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
506 va_list args)
507 {
508 size_t new_length;
509 char *ptr;
510
511 assert(str != NULL);
512
513 if (unlikely(*str == NULL)) {
514 // Assuming a NULL context is probably bad, but it's expected behavior.
515 *str = ralloc_vasprintf(NULL, fmt, args);
516 *start = strlen(*str);
517 return true;
518 }
519
520 new_length = u_printf_length(fmt, args);
521
522 ptr = resize(*str, *start + new_length + 1);
523 if (unlikely(ptr == NULL))
524 return false;
525
526 vsnprintf(ptr + *start, new_length + 1, fmt, args);
527 *str = ptr;
528 *start += new_length;
529 return true;
530 }
531
532 /***************************************************************************
533 * GC context.
534 ***************************************************************************
535 */
536
537 /* The maximum size of an object that will be allocated specially.
538 */
539 #define MAX_FREELIST_SIZE 512
540
541 /* Allocations small enough to be allocated from a freelist will be aligned up
542 * to this size.
543 */
544 #define FREELIST_ALIGNMENT 32
545
546 #define NUM_FREELIST_BUCKETS (MAX_FREELIST_SIZE / FREELIST_ALIGNMENT)
547
548 /* The size of a slab. */
549 #define SLAB_SIZE (32 * 1024)
550
551 #define GC_CANARY 0xAF6B5B72
552
553 enum gc_flags {
554 IS_USED = (1 << 0),
555 CURRENT_GENERATION = (1 << 1),
556 IS_PADDING = (1 << 7),
557 };
558
559 typedef struct
560 {
561 #ifndef NDEBUG
562 /* A canary value used to determine whether a pointer is allocated using gc_alloc. */
563 unsigned canary;
564 #endif
565
566 uint16_t slab_offset;
567 uint8_t bucket;
568 uint8_t flags;
569
570 /* The last padding byte must have IS_PADDING set and is used to store the amount of padding. If
571 * there is no padding, the IS_PADDING bit of "flags" is unset and "flags" is checked instead.
572 * Because of this, "flags" must be the last member of this struct.
573 */
574 uint8_t padding[];
575 } gc_block_header;
576
577 /* This structure is at the start of the slab. Objects inside a slab are
578 * allocated using a freelist backed by a simple linear allocator.
579 */
580 typedef struct gc_slab {
581 alignas(HEADER_ALIGN)
582
583 gc_ctx *ctx;
584
585 /* Objects are allocated using either linear or freelist allocation. "next_available" is the
586 * pointer used for linear allocation, while "freelist" is the next free object for freelist
587 * allocation.
588 */
589 char *next_available;
590 gc_block_header *freelist;
591
592 /* Slabs that handle the same-sized objects. */
593 struct list_head link;
594
595 /* Free slabs that handle the same-sized objects. */
596 struct list_head free_link;
597
598 /* Number of allocated and free objects, recorded so that we can free the slab if it
599 * becomes empty or add one to the freelist if it's no longer full.
600 */
601 unsigned num_allocated;
602 unsigned num_free;
603 } gc_slab;
604
605 struct gc_ctx {
606 /* Array of slabs for fixed-size allocations. Each slab tracks allocations
607 * of specific sized blocks. User allocations are rounded up to the nearest
608 * fixed size. slabs[N] contains allocations of size
609 * FREELIST_ALIGNMENT * (N + 1).
610 */
611 struct {
612 /* List of slabs in this bucket. */
613 struct list_head slabs;
614
615 /* List of slabs with free space in this bucket, so we can quickly choose one when
616 * allocating.
617 */
618 struct list_head free_slabs;
619 } slabs[NUM_FREELIST_BUCKETS];
620
621 uint8_t current_gen;
622 void *rubbish;
623 };
624
625 static gc_block_header *
get_gc_header(const void * ptr)626 get_gc_header(const void *ptr)
627 {
628 uint8_t *c_ptr = (uint8_t *)ptr;
629
630 /* Adjust for padding added to ensure alignment of the allocation. There might also be padding
631 * added by the compiler into gc_block_header, but that isn't counted in the IS_PADDING byte.
632 */
633 if (c_ptr[-1] & IS_PADDING)
634 c_ptr -= c_ptr[-1] & ~IS_PADDING;
635
636 c_ptr -= sizeof(gc_block_header);
637
638 gc_block_header *info = (gc_block_header *)c_ptr;
639 assert(info->canary == GC_CANARY);
640 return info;
641 }
642
643 static gc_block_header *
get_gc_freelist_next(gc_block_header * ptr)644 get_gc_freelist_next(gc_block_header *ptr)
645 {
646 gc_block_header *next;
647 /* work around possible strict aliasing bug using memcpy */
648 memcpy(&next, (void*)(ptr + 1), sizeof(next));
649 return next;
650 }
651
652 static void
set_gc_freelist_next(gc_block_header * ptr,gc_block_header * next)653 set_gc_freelist_next(gc_block_header *ptr, gc_block_header *next)
654 {
655 memcpy((void*)(ptr + 1), &next, sizeof(next));
656 }
657
658 static gc_slab *
get_gc_slab(gc_block_header * header)659 get_gc_slab(gc_block_header *header)
660 {
661 return (gc_slab *)((char *)header - header->slab_offset);
662 }
663
664 gc_ctx *
gc_context(const void * parent)665 gc_context(const void *parent)
666 {
667 gc_ctx *ctx = rzalloc(parent, gc_ctx);
668 for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
669 list_inithead(&ctx->slabs[i].slabs);
670 list_inithead(&ctx->slabs[i].free_slabs);
671 }
672 return ctx;
673 }
674
675 static_assert(UINT32_MAX >= MAX_FREELIST_SIZE, "Freelist sizes use uint32_t");
676
677 static uint32_t
gc_bucket_obj_size(uint32_t bucket)678 gc_bucket_obj_size(uint32_t bucket)
679 {
680 return (bucket + 1) * FREELIST_ALIGNMENT;
681 }
682
683 static uint32_t
gc_bucket_for_size(uint32_t size)684 gc_bucket_for_size(uint32_t size)
685 {
686 return (size - 1) / FREELIST_ALIGNMENT;
687 }
688
689 static_assert(UINT32_MAX >= SLAB_SIZE, "SLAB_SIZE use uint32_t");
690
691 static uint32_t
gc_bucket_num_objs(uint32_t bucket)692 gc_bucket_num_objs(uint32_t bucket)
693 {
694 return (SLAB_SIZE - sizeof(gc_slab)) / gc_bucket_obj_size(bucket);
695 }
696
697 static gc_block_header *
alloc_from_slab(gc_slab * slab,uint32_t bucket)698 alloc_from_slab(gc_slab *slab, uint32_t bucket)
699 {
700 uint32_t size = gc_bucket_obj_size(bucket);
701 gc_block_header *header;
702 if (slab->freelist) {
703 /* Prioritize already-allocated chunks, since they probably have a page
704 * backing them.
705 */
706 header = slab->freelist;
707 slab->freelist = get_gc_freelist_next(slab->freelist);
708 } else if (slab->next_available + size <= ((char *) slab) + SLAB_SIZE) {
709 header = (gc_block_header *) slab->next_available;
710 header->slab_offset = (char *) header - (char *) slab;
711 header->bucket = bucket;
712 slab->next_available += size;
713 } else {
714 return NULL;
715 }
716
717 slab->num_allocated++;
718 slab->num_free--;
719 if (!slab->num_free)
720 list_del(&slab->free_link);
721 return header;
722 }
723
724 static void
free_slab(gc_slab * slab)725 free_slab(gc_slab *slab)
726 {
727 if (list_is_linked(&slab->free_link))
728 list_del(&slab->free_link);
729 list_del(&slab->link);
730 ralloc_free(slab);
731 }
732
733 static void
free_from_slab(gc_block_header * header,bool keep_empty_slabs)734 free_from_slab(gc_block_header *header, bool keep_empty_slabs)
735 {
736 gc_slab *slab = get_gc_slab(header);
737
738 if (slab->num_allocated == 1 && !(keep_empty_slabs && list_is_singular(&slab->free_link))) {
739 /* Free the slab if this is the last object. */
740 free_slab(slab);
741 return;
742 } else if (slab->num_free == 0) {
743 list_add(&slab->free_link, &slab->ctx->slabs[header->bucket].free_slabs);
744 } else {
745 /* Keep the free list sorted by the number of free objects in ascending order. By prefering to
746 * allocate from the slab with the fewest free objects, we help free the slabs with many free
747 * objects.
748 */
749 while (slab->free_link.next != &slab->ctx->slabs[header->bucket].free_slabs &&
750 slab->num_free > list_entry(slab->free_link.next, gc_slab, free_link)->num_free) {
751 gc_slab *next = list_entry(slab->free_link.next, gc_slab, free_link);
752
753 /* Move "slab" to after "next". */
754 list_move_to(&slab->free_link, &next->free_link);
755 }
756 }
757
758 set_gc_freelist_next(header, slab->freelist);
759 slab->freelist = header;
760
761 slab->num_allocated--;
762 slab->num_free++;
763 }
764
765 static uint32_t
get_slab_size(uint32_t bucket)766 get_slab_size(uint32_t bucket)
767 {
768 /* SLAB_SIZE rounded down to a multiple of the object size so that it's not larger than what can
769 * be used.
770 */
771 uint32_t obj_size = gc_bucket_obj_size(bucket);
772 uint32_t num_objs = gc_bucket_num_objs(bucket);
773 return align((uint32_t)sizeof(gc_slab) + num_objs * obj_size, alignof(gc_slab));
774 }
775
776 static gc_slab *
create_slab(gc_ctx * ctx,unsigned bucket)777 create_slab(gc_ctx *ctx, unsigned bucket)
778 {
779 gc_slab *slab = ralloc_size(ctx, get_slab_size(bucket));
780 if (unlikely(!slab))
781 return NULL;
782
783 slab->ctx = ctx;
784 slab->freelist = NULL;
785 slab->next_available = (char*)(slab + 1);
786 slab->num_allocated = 0;
787 slab->num_free = gc_bucket_num_objs(bucket);
788
789 list_addtail(&slab->link, &ctx->slabs[bucket].slabs);
790 list_addtail(&slab->free_link, &ctx->slabs[bucket].free_slabs);
791
792 return slab;
793 }
794
795 void *
gc_alloc_size(gc_ctx * ctx,size_t size,size_t align)796 gc_alloc_size(gc_ctx *ctx, size_t size, size_t align)
797 {
798 assert(ctx);
799 assert(util_is_power_of_two_nonzero(align));
800
801 align = MAX2(align, alignof(gc_block_header));
802
803 /* Alignment will add at most align-alignof(gc_block_header) bytes of padding to the header, and
804 * the IS_PADDING byte can only encode up to 127.
805 */
806 assert((align - alignof(gc_block_header)) <= 127);
807
808 /* We can only align as high as the slab is. */
809 assert(align <= HEADER_ALIGN);
810
811 size_t header_size = align64(sizeof(gc_block_header), align);
812 size = align64(size, align);
813 size += header_size;
814
815 gc_block_header *header = NULL;
816 if (size <= MAX_FREELIST_SIZE) {
817 uint32_t bucket = gc_bucket_for_size((uint32_t)size);
818 if (list_is_empty(&ctx->slabs[bucket].free_slabs) && !create_slab(ctx, bucket))
819 return NULL;
820 gc_slab *slab = list_first_entry(&ctx->slabs[bucket].free_slabs, gc_slab, free_link);
821 header = alloc_from_slab(slab, bucket);
822 } else {
823 header = ralloc_size(ctx, size);
824 if (unlikely(!header))
825 return NULL;
826 /* Mark the header as allocated directly, so we know to actually free it. */
827 header->bucket = NUM_FREELIST_BUCKETS;
828 }
829
830 header->flags = ctx->current_gen | IS_USED;
831 #ifndef NDEBUG
832 header->canary = GC_CANARY;
833 #endif
834
835 uint8_t *ptr = (uint8_t *)header + header_size;
836 if ((header_size - 1) != offsetof(gc_block_header, flags))
837 ptr[-1] = IS_PADDING | (header_size - sizeof(gc_block_header));
838
839 assert(((uintptr_t)ptr & (align - 1)) == 0);
840 return ptr;
841 }
842
843 void *
gc_zalloc_size(gc_ctx * ctx,size_t size,size_t align)844 gc_zalloc_size(gc_ctx *ctx, size_t size, size_t align)
845 {
846 void *ptr = gc_alloc_size(ctx, size, align);
847
848 if (likely(ptr))
849 memset(ptr, 0, size);
850
851 return ptr;
852 }
853
854 void
gc_free(void * ptr)855 gc_free(void *ptr)
856 {
857 if (!ptr)
858 return;
859
860 gc_block_header *header = get_gc_header(ptr);
861 header->flags &= ~IS_USED;
862
863 if (header->bucket < NUM_FREELIST_BUCKETS)
864 free_from_slab(header, true);
865 else
866 ralloc_free(header);
867 }
868
gc_get_context(void * ptr)869 gc_ctx *gc_get_context(void *ptr)
870 {
871 gc_block_header *header = get_gc_header(ptr);
872
873 if (header->bucket < NUM_FREELIST_BUCKETS)
874 return get_gc_slab(header)->ctx;
875 else
876 return ralloc_parent(header);
877 }
878
879 void
gc_sweep_start(gc_ctx * ctx)880 gc_sweep_start(gc_ctx *ctx)
881 {
882 ctx->current_gen ^= CURRENT_GENERATION;
883
884 ctx->rubbish = ralloc_context(NULL);
885 ralloc_adopt(ctx->rubbish, ctx);
886 }
887
888 void
gc_mark_live(gc_ctx * ctx,const void * mem)889 gc_mark_live(gc_ctx *ctx, const void *mem)
890 {
891 gc_block_header *header = get_gc_header(mem);
892 if (header->bucket < NUM_FREELIST_BUCKETS)
893 header->flags ^= CURRENT_GENERATION;
894 else
895 ralloc_steal(ctx, header);
896 }
897
898 void
gc_sweep_end(gc_ctx * ctx)899 gc_sweep_end(gc_ctx *ctx)
900 {
901 assert(ctx->rubbish);
902
903 for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
904 unsigned obj_size = gc_bucket_obj_size(i);
905 list_for_each_entry_safe(gc_slab, slab, &ctx->slabs[i].slabs, link) {
906 if (!slab->num_allocated) {
907 free_slab(slab);
908 continue;
909 }
910
911 for (char *ptr = (char*)(slab + 1); ptr != slab->next_available; ptr += obj_size) {
912 gc_block_header *header = (gc_block_header *)ptr;
913 if (!(header->flags & IS_USED))
914 continue;
915 if ((header->flags & CURRENT_GENERATION) == ctx->current_gen)
916 continue;
917
918 bool last = slab->num_allocated == 1;
919
920 header->flags &= ~IS_USED;
921 free_from_slab(header, false);
922
923 if (last)
924 break;
925 }
926 }
927 }
928
929 for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
930 list_for_each_entry(gc_slab, slab, &ctx->slabs[i].slabs, link) {
931 assert(slab->num_allocated > 0); /* free_from_slab() should free it otherwise */
932 ralloc_steal(ctx, slab);
933 }
934 }
935
936 ralloc_free(ctx->rubbish);
937 ctx->rubbish = NULL;
938 }
939
940 /***************************************************************************
941 * Linear allocator for short-lived allocations.
942 ***************************************************************************
943 *
944 * The allocator consists of a parent node (2K buffer), which requires
945 * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
946 * directly, because the parent doesn't track them. You have to release
947 * the parent node in order to release all its children.
948 *
949 * The allocator uses a fixed-sized buffer with a monotonically increasing
950 * offset after each allocation. If the buffer is all used, another buffer
951 * is allocated, sharing the same ralloc parent, so all buffers are at
952 * the same level in the ralloc hierarchy.
953 *
954 * The linear parent node is always the first buffer and keeps track of all
955 * other buffers.
956 */
957
958 #define MIN_LINEAR_BUFSIZE 2048
959 #define SUBALLOC_ALIGNMENT 8
960 #define LMAGIC 0x87b9c7d3
961
962 struct linear_header {
963
964 alignas(HEADER_ALIGN)
965
966 #ifndef NDEBUG
967 unsigned magic; /* for debugging */
968 #endif
969 unsigned offset; /* points to the first unused byte in the buffer */
970 unsigned size; /* size of the buffer */
971 void *ralloc_parent; /* new buffers will use this */
972 struct linear_header *next; /* next buffer if we have more */
973 struct linear_header *latest; /* the only buffer that has free space */
974
975 /* After this structure, the buffer begins.
976 * Each suballocation consists of linear_size_chunk as its header followed
977 * by the suballocation, so it goes:
978 *
979 * - linear_size_chunk
980 * - allocated space
981 * - linear_size_chunk
982 * - allocated space
983 * etc.
984 *
985 * linear_size_chunk is only needed by linear_realloc.
986 */
987 };
988
989 struct linear_size_chunk {
990 unsigned size; /* for realloc */
991 unsigned _padding;
992 };
993
994 typedef struct linear_header linear_header;
995 typedef struct linear_size_chunk linear_size_chunk;
996
997 #define LINEAR_PARENT_TO_HEADER(parent) \
998 (linear_header*) \
999 ((char*)(parent) - sizeof(linear_size_chunk) - sizeof(linear_header))
1000
1001 /* Allocate the linear buffer with its header. */
1002 static linear_header *
create_linear_node(void * ralloc_ctx,unsigned min_size)1003 create_linear_node(void *ralloc_ctx, unsigned min_size)
1004 {
1005 linear_header *node;
1006
1007 min_size += sizeof(linear_size_chunk);
1008
1009 if (likely(min_size < MIN_LINEAR_BUFSIZE))
1010 min_size = MIN_LINEAR_BUFSIZE;
1011
1012 node = ralloc_size(ralloc_ctx, sizeof(linear_header) + min_size);
1013 if (unlikely(!node))
1014 return NULL;
1015
1016 #ifndef NDEBUG
1017 node->magic = LMAGIC;
1018 #endif
1019 node->offset = 0;
1020 node->size = min_size;
1021 node->ralloc_parent = ralloc_ctx;
1022 node->next = NULL;
1023 node->latest = node;
1024 return node;
1025 }
1026
1027 void *
linear_alloc_child(void * parent,unsigned size)1028 linear_alloc_child(void *parent, unsigned size)
1029 {
1030 linear_header *first = LINEAR_PARENT_TO_HEADER(parent);
1031 linear_header *latest = first->latest;
1032 linear_header *new_node;
1033 linear_size_chunk *ptr;
1034 unsigned full_size;
1035
1036 assert(first->magic == LMAGIC);
1037 assert(!latest->next);
1038
1039 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
1040 full_size = sizeof(linear_size_chunk) + size;
1041
1042 if (unlikely(latest->offset + full_size > latest->size)) {
1043 /* allocate a new node */
1044 new_node = create_linear_node(latest->ralloc_parent, size);
1045 if (unlikely(!new_node))
1046 return NULL;
1047
1048 first->latest = new_node;
1049 latest->latest = new_node;
1050 latest->next = new_node;
1051 latest = new_node;
1052 }
1053
1054 ptr = (linear_size_chunk *)((char*)&latest[1] + latest->offset);
1055 ptr->size = size;
1056 latest->offset += full_size;
1057
1058 assert((uintptr_t)&ptr[1] % SUBALLOC_ALIGNMENT == 0);
1059 return &ptr[1];
1060 }
1061
1062 void *
linear_alloc_parent(void * ralloc_ctx,unsigned size)1063 linear_alloc_parent(void *ralloc_ctx, unsigned size)
1064 {
1065 linear_header *node;
1066
1067 if (unlikely(!ralloc_ctx))
1068 return NULL;
1069
1070 size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
1071
1072 node = create_linear_node(ralloc_ctx, size);
1073 if (unlikely(!node))
1074 return NULL;
1075
1076 return linear_alloc_child((char*)node +
1077 sizeof(linear_header) +
1078 sizeof(linear_size_chunk), size);
1079 }
1080
1081 void *
linear_zalloc_child(void * parent,unsigned size)1082 linear_zalloc_child(void *parent, unsigned size)
1083 {
1084 void *ptr = linear_alloc_child(parent, size);
1085
1086 if (likely(ptr))
1087 memset(ptr, 0, size);
1088 return ptr;
1089 }
1090
1091 void *
linear_zalloc_parent(void * parent,unsigned size)1092 linear_zalloc_parent(void *parent, unsigned size)
1093 {
1094 void *ptr = linear_alloc_parent(parent, size);
1095
1096 if (likely(ptr))
1097 memset(ptr, 0, size);
1098 return ptr;
1099 }
1100
1101 void
linear_free_parent(void * ptr)1102 linear_free_parent(void *ptr)
1103 {
1104 linear_header *node;
1105
1106 if (unlikely(!ptr))
1107 return;
1108
1109 node = LINEAR_PARENT_TO_HEADER(ptr);
1110 assert(node->magic == LMAGIC);
1111
1112 while (node) {
1113 void *ptr = node;
1114
1115 node = node->next;
1116 ralloc_free(ptr);
1117 }
1118 }
1119
1120 void
ralloc_steal_linear_parent(void * new_ralloc_ctx,void * ptr)1121 ralloc_steal_linear_parent(void *new_ralloc_ctx, void *ptr)
1122 {
1123 linear_header *node;
1124
1125 if (unlikely(!ptr))
1126 return;
1127
1128 node = LINEAR_PARENT_TO_HEADER(ptr);
1129 assert(node->magic == LMAGIC);
1130
1131 while (node) {
1132 ralloc_steal(new_ralloc_ctx, node);
1133 node->ralloc_parent = new_ralloc_ctx;
1134 node = node->next;
1135 }
1136 }
1137
1138 void *
ralloc_parent_of_linear_parent(void * ptr)1139 ralloc_parent_of_linear_parent(void *ptr)
1140 {
1141 linear_header *node = LINEAR_PARENT_TO_HEADER(ptr);
1142 assert(node->magic == LMAGIC);
1143 return node->ralloc_parent;
1144 }
1145
1146 void *
linear_realloc(void * parent,void * old,unsigned new_size)1147 linear_realloc(void *parent, void *old, unsigned new_size)
1148 {
1149 unsigned old_size = 0;
1150 ralloc_header *new_ptr;
1151
1152 new_ptr = linear_alloc_child(parent, new_size);
1153
1154 if (unlikely(!old))
1155 return new_ptr;
1156
1157 old_size = ((linear_size_chunk*)old)[-1].size;
1158
1159 if (likely(new_ptr && old_size))
1160 memcpy(new_ptr, old, MIN2(old_size, new_size));
1161
1162 return new_ptr;
1163 }
1164
1165 /* All code below is pretty much copied from ralloc and only the alloc
1166 * calls are different.
1167 */
1168
1169 char *
linear_strdup(void * parent,const char * str)1170 linear_strdup(void *parent, const char *str)
1171 {
1172 unsigned n;
1173 char *ptr;
1174
1175 if (unlikely(!str))
1176 return NULL;
1177
1178 n = strlen(str);
1179 ptr = linear_alloc_child(parent, n + 1);
1180 if (unlikely(!ptr))
1181 return NULL;
1182
1183 memcpy(ptr, str, n);
1184 ptr[n] = '\0';
1185 return ptr;
1186 }
1187
1188 char *
linear_asprintf(void * parent,const char * fmt,...)1189 linear_asprintf(void *parent, const char *fmt, ...)
1190 {
1191 char *ptr;
1192 va_list args;
1193 va_start(args, fmt);
1194 ptr = linear_vasprintf(parent, fmt, args);
1195 va_end(args);
1196 return ptr;
1197 }
1198
1199 char *
linear_vasprintf(void * parent,const char * fmt,va_list args)1200 linear_vasprintf(void *parent, const char *fmt, va_list args)
1201 {
1202 unsigned size = u_printf_length(fmt, args) + 1;
1203
1204 char *ptr = linear_alloc_child(parent, size);
1205 if (ptr != NULL)
1206 vsnprintf(ptr, size, fmt, args);
1207
1208 return ptr;
1209 }
1210
1211 bool
linear_asprintf_append(void * parent,char ** str,const char * fmt,...)1212 linear_asprintf_append(void *parent, char **str, const char *fmt, ...)
1213 {
1214 bool success;
1215 va_list args;
1216 va_start(args, fmt);
1217 success = linear_vasprintf_append(parent, str, fmt, args);
1218 va_end(args);
1219 return success;
1220 }
1221
1222 bool
linear_vasprintf_append(void * parent,char ** str,const char * fmt,va_list args)1223 linear_vasprintf_append(void *parent, char **str, const char *fmt, va_list args)
1224 {
1225 size_t existing_length;
1226 assert(str != NULL);
1227 existing_length = *str ? strlen(*str) : 0;
1228 return linear_vasprintf_rewrite_tail(parent, str, &existing_length, fmt, args);
1229 }
1230
1231 bool
linear_asprintf_rewrite_tail(void * parent,char ** str,size_t * start,const char * fmt,...)1232 linear_asprintf_rewrite_tail(void *parent, char **str, size_t *start,
1233 const char *fmt, ...)
1234 {
1235 bool success;
1236 va_list args;
1237 va_start(args, fmt);
1238 success = linear_vasprintf_rewrite_tail(parent, str, start, fmt, args);
1239 va_end(args);
1240 return success;
1241 }
1242
1243 bool
linear_vasprintf_rewrite_tail(void * parent,char ** str,size_t * start,const char * fmt,va_list args)1244 linear_vasprintf_rewrite_tail(void *parent, char **str, size_t *start,
1245 const char *fmt, va_list args)
1246 {
1247 size_t new_length;
1248 char *ptr;
1249
1250 assert(str != NULL);
1251
1252 if (unlikely(*str == NULL)) {
1253 *str = linear_vasprintf(parent, fmt, args);
1254 *start = strlen(*str);
1255 return true;
1256 }
1257
1258 new_length = u_printf_length(fmt, args);
1259
1260 ptr = linear_realloc(parent, *str, *start + new_length + 1);
1261 if (unlikely(ptr == NULL))
1262 return false;
1263
1264 vsnprintf(ptr + *start, new_length + 1, fmt, args);
1265 *str = ptr;
1266 *start += new_length;
1267 return true;
1268 }
1269
1270 /* helper routine for strcat/strncat - n is the exact amount to copy */
1271 static bool
linear_cat(void * parent,char ** dest,const char * str,unsigned n)1272 linear_cat(void *parent, char **dest, const char *str, unsigned n)
1273 {
1274 char *both;
1275 unsigned existing_length;
1276 assert(dest != NULL && *dest != NULL);
1277
1278 existing_length = strlen(*dest);
1279 both = linear_realloc(parent, *dest, existing_length + n + 1);
1280 if (unlikely(both == NULL))
1281 return false;
1282
1283 memcpy(both + existing_length, str, n);
1284 both[existing_length + n] = '\0';
1285
1286 *dest = both;
1287 return true;
1288 }
1289
1290 bool
linear_strcat(void * parent,char ** dest,const char * str)1291 linear_strcat(void *parent, char **dest, const char *str)
1292 {
1293 return linear_cat(parent, dest, str, strlen(str));
1294 }
1295
1296 void *
linear_alloc_child_array(void * parent,size_t size,unsigned count)1297 linear_alloc_child_array(void *parent, size_t size, unsigned count)
1298 {
1299 if (count > SIZE_MAX/size)
1300 return NULL;
1301
1302 return linear_alloc_child(parent, size * count);
1303 }
1304
1305 void *
linear_zalloc_child_array(void * parent,size_t size,unsigned count)1306 linear_zalloc_child_array(void *parent, size_t size, unsigned count)
1307 {
1308 if (count > SIZE_MAX/size)
1309 return NULL;
1310
1311 return linear_zalloc_child(parent, size * count);
1312 }
1313