/* arena.c -- Arena allocator for LRL bootstrap transpiler Copyright © 2020 Samuel Lidén Borell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "bootstrap.h" #include #include #include #define ARENA_SIZE 4096 struct ArenaBlock { unsigned char *data, *free, *end; }; struct ArenaHolder { struct ArenaHolder *prev; unsigned capacity; struct ArenaBlock *blocks; struct ArenaBlock *last; struct ArenaBlock *last_capacity; }; static void grow_arena_list(void); static void *large_alloc(size_t size); static struct ArenaHolder *holder; /* As an optimization, we could keep buckets of arenas with <=8, <=16, <=32, ... <=4096 bytes free We could also track arena alignment, so we can allocate small-alignment structures in lesser aligned arenas. We should also add an environment variable for adding "bad" padding between allocations for Valgrind etc. */ void arena_new(void) { struct ArenaHolder *hld = malloc(sizeof(struct ArenaHolder)); if (!hld) out_of_memory(); hld->capacity = 16; hld->blocks = malloc(hld->capacity * sizeof(struct ArenaBlock)); if (!hld->blocks) out_of_memory(); hld->last = hld->blocks; /* first block */ hld->last_capacity = hld->blocks + hld->capacity*sizeof(struct ArenaBlock); hld->prev = holder; holder = hld; } void arena_free(void) { struct ArenaHolder *hld = holder; struct ArenaBlock *block; assert(hld != NULL); holder = hld->prev; for (block = hld->blocks; block < hld->last; block++) { free(block->data); } free(hld->blocks); free(hld); } /* align must be a power of 2 */ #define ALIGN(ptr, align) ((void*)((((uintptr_t)ptr)+((align)-1)) & ~((uintptr_t)(align)-1))) void *aalloc(size_t size, size_t align) { struct ArenaHolder *hld = holder; struct ArenaBlock *block, *lastarena; void *p; if (!hld) { p = malloc(size); if (!p) out_of_memory(); return p; } /* For large allocations, allocate its own arena */ if (size > (ARENA_SIZE/4)*3) return large_alloc(size); /* Find an arena. This is O(n), so we may need to use bucket lists (see top of file) to improve performance if it turns out to be a problem. */ for (block = hld->blocks; block < hld->last; block++) { unsigned char *aligned; /*if (block->end - block->free < size) continue;*/ aligned = ALIGN(block->free, align); if (aligned+size > block->end) continue; /* Found space */ block->free = aligned+size; return aligned; } /* No arena exists. Grab a blank one and allocate */ if (hld->last == hld->last_capacity) { grow_arena_list(); hld = holder; } p = malloc(ARENA_SIZE); if (!p) out_of_memory(); lastarena = hld->last; lastarena->data = p; lastarena->end = lastarena->data + ARENA_SIZE; lastarena->free = lastarena->data + size; return lastarena->data; } static void *large_alloc(size_t size) { struct ArenaBlock *lastarena; void *p; if (holder->last == holder->last_capacity) { grow_arena_list(); } p = malloc(size); if (!p) out_of_memory(); lastarena = holder->last; lastarena->data = p; lastarena->free = lastarena->end = lastarena->data + size; return lastarena->data; } /** Grows the list of arena blocks. */ static void grow_arena_list(void) { size_t size = (size_t)(holder->last - holder->blocks); holder->capacity *= 2; holder->blocks = realloc(holder->blocks, holder->capacity*sizeof(struct ArenaBlock)); if (!holder->blocks) out_of_memory(); holder->last = holder->blocks + size; holder->last_capacity = holder->blocks + holder->capacity*sizeof(struct ArenaBlock); }