/// src/arena.c --- chunked arena allocator // // Copyright (C) 2026 by Martin Michalec // // SPDX-License-Identifier: MIT // /// Commentary: // /// Code: #define _GNU_SOURCE // TODO(cmmm): remove hard dependency on GNU extensions #include #include // TODO(cmmm): rethink alignment in relation to coalescing, it seems // really hard to get right... Verdict, use obstack (keeping track of // base) for making sure fields are aligned (by adding extra padding). // Coalescing stays the same and can still be used without obstack, if // you know that there won't be need for extra alignment ensuring // padding #include "arena.h" #ifndef ARENA_ASSERT #include #define ASSERT assert #define ASSERT_PERROR assert_perror #else #define ASSERT ARENA_ASSERT #endif #include #define MALLOC malloc #define ALIGNED_ALLOC aligned_alloc #define FREE free #define MAX(A, B) ((A) > (B) ? (A) : (B)) // NOTE(cmmm): address of chunk == chunk's end #define CHUNK_END(CHUNK) ((void *) (CHUNK)) static size_t chunk_size (const struct arena_chunk *chunk) { ASSERT (chunk != NULL); return CHUNK_END (chunk) - chunk->begin; } static size_t chunk_used (struct arena_chunk *chunk) { ASSERT (chunk != NULL); return chunk->free - chunk->begin; } static struct arena_chunk * new_chunk (size_t size, size_t alignment) { ASSERT ((alignment & (alignment + 1)) == 0); alignment += 1; alignment += alignment % sizeof (void *); void *chunk_begin = ALIGNED_ALLOC (alignment, size + sizeof (struct arena_chunk)); ASSERT (chunk_begin != NULL); struct arena_chunk *chunk = chunk_begin + size; chunk->next = NULL; chunk->begin = chunk_begin; chunk->free = chunk_begin; return chunk; } static struct arena_chunk * append_chunk (struct arena *arena, size_t size, size_t alignment) { ASSERT (arena != NULL); size = arena->tail != NULL ? MAX (ARENA_CHUNK_GROWTH_FACTOR*chunk_size (arena->tail), size) : MAX (ARENA_FIRST_CHUNK_SIZE, size); struct arena_chunk *chunk = new_chunk (size, alignment); if (arena->tail != NULL) { arena->tail->next = chunk; arena->tail = chunk; } else { arena->head = chunk; arena->tail = chunk; } return chunk; } static void * alignto (void *ptr, size_t alignment) { return (void *) ((ptrdiff_t) (ptr + alignment) & ~alignment); } void * arena_reserve (struct arena *arena, size_t size, size_t alignment) { ASSERT (arena != NULL); if (arena->tail != NULL) { void *aligned_free = alignto (arena->tail->free, alignment); if (CHUNK_END (arena->tail) - aligned_free >= (ssize_t) size) { return aligned_free; } } // FIXME(cmmm): use arena->tail->next when available append_chunk (arena, size, alignment); return alignto (arena->tail->free, alignment); } void * arena_allocate (struct arena *arena, size_t size, size_t alignment) { ASSERT (arena != NULL); void *ptr = arena_reserve (arena, size, alignment); arena->tail->free = ptr + size; return ptr; } void * arena_allocate_fast (struct arena *arena, size_t size, size_t alignment) { ASSERT (arena != NULL); ASSERT (arena->tail != NULL); void *ptr = alignto (arena->tail->free, alignment); arena->tail->free = ptr + size; return ptr; } size_t arena_used (const struct arena *arena) { ASSERT (arena != NULL); size_t used = 0; const struct arena_chunk *chunk; for (chunk = arena->head; chunk; chunk = chunk->next) used += chunk_size (chunk); return used; } size_t arena_room (const struct arena *arena, size_t alignment) { ASSERT (arena != NULL); if (arena->tail == NULL) return 0; void *aligned_free = alignto (arena->tail->free, alignment); return CHUNK_END (arena->tail) - aligned_free; } static void * next_free (const struct arena_chunk *chunk, size_t alignment) { ASSERT (chunk != NULL); void *aligned_free = alignto (chunk->free, alignment); if (aligned_free > CHUNK_END (chunk)) aligned_free = alignto (chunk->next->begin, alignment); return aligned_free; } // REVIEW(cmmm): probably bugged, contract unclear void * arena_next_free (const struct arena *a, size_t alignment) { ASSERT (a != NULL); ASSERT (a->tail != NULL); return next_free (a->tail, alignment); } void * arena_copy (struct arena *a, const void *from, size_t size, size_t alignment) { ASSERT (a != NULL); ASSERT (from != NULL); void *ptr = arena_allocate (a, size, alignment); memcpy (ptr, from, size); return ptr; } void * arena_copy_fast (struct arena *a, const void *from, size_t size, size_t alignment) { ASSERT (a != NULL); ASSERT (from != NULL); void *ptr = arena_allocate_fast (a, size, alignment); memcpy (ptr, from, size); return ptr; } char * arena_copy_cstr (struct arena *a, const char *cstr) { ASSERT (a != NULL); ASSERT (cstr != NULL); size_t size = strlen (cstr) + 1; return arena_copy (a, cstr, size, 1); } void arena_reset (struct arena *a) { ASSERT (a != NULL); if (a->head == NULL) return; a->tail = a->head; a->head->free = a->head->begin; } void * arena_pop (struct arena *a, size_t size) { ASSERT (a != NULL); // FIXME(cmmm): handle chunk boundaries a->tail->free -= size; ASSERT (a->tail->free >= a->tail->begin); return a->tail->free; } struct arena_checkpoint arena_checkpoint (struct arena *a, size_t alignment) { ASSERT (a != NULL); if (a->tail == NULL) arena_reserve (a, 0, alignment); void *aligned_free = alignto (a->tail->free, alignment); if (aligned_free > CHUNK_END (a->tail)) { arena_reserve (a, 0, alignment); aligned_free = alignto (a->tail->free, alignment); } struct arena_checkpoint cp = { .tail = a->tail, .free = a->tail->free, .alignment = alignment, }; a->tail->free = aligned_free; return cp; } void arena_rollback_to (struct arena *a, struct arena_checkpoint cp) { ASSERT (a != NULL); ASSERT (cp.tail != NULL); ASSERT (cp.free != NULL); a->tail = cp.tail; a->tail->free = cp.free; } size_t arena_checkpoint_size_from (struct arena_checkpoint cp) { ASSERT (cp.tail != NULL); ASSERT (cp.free != NULL); size_t size = cp.tail->free == cp.free ? 0 : cp.tail->free - alignto (cp.free, cp.alignment); for (struct arena_chunk *chunk = cp.tail; chunk; chunk = chunk->next) size += chunk_size (chunk); return size; } void * arena_checkpoint_next_from (struct arena_checkpoint cp) { ASSERT (cp.tail != NULL); ASSERT (cp.free != NULL); void *aligned_free = alignto (cp.free, cp.alignment); if (aligned_free > CHUNK_END (cp.tail)) aligned_free = alignto (cp.tail->next->begin, cp.alignment); return aligned_free; } void * arena_coalesce_from (struct arena *a, struct arena_checkpoint cp) { ASSERT (a != NULL); if (a->tail == cp.tail) // already coalesced return cp.free; size_t size = arena_checkpoint_size_from (cp); return arena_coalesce_from_fast (a, cp, size); } void * arena_coalesce_from_fast (struct arena *a, struct arena_checkpoint cp, size_t size) { ASSERT (a != NULL); if (a->tail == cp.tail) // already coalesced return cp.free; // make new chunk, add old chunks after it struct arena_chunk *chunk = new_chunk (size, cp.alignment); chunk->next = cp.free == cp.tail->begin ? cp.tail : cp.tail->next; // copy old chunks into new chunk // BUG(cmmm): check if data actually starts in checkpoints chunk if (cp.free != cp.tail->free) { size_t other_size = alignto (cp.tail->free, cp.alignment) - arena_checkpoint_next_from (cp); memcpy (chunk->free, cp.free, other_size); chunk->free += other_size; } for (struct arena_chunk *other_chunk = cp.tail->next; other_chunk; other_chunk = other_chunk->next) { size_t other_size = chunk_used (other_chunk); memcpy (chunk->free, other_chunk->begin, other_size); chunk->free += other_size; } // link in new chunk for (struct arena_chunk **other_chunk = &a->head; *other_chunk; other_chunk = &(*other_chunk)->next) { if (*other_chunk != chunk->next) continue; *other_chunk = chunk; break; } return chunk->begin; } void * arena_coalesce_from_into (struct arena *a, struct arena_checkpoint cp, struct arena *into) { size_t size = arena_checkpoint_size_from (cp); return arena_coalesce_from_into_fast (a, cp, into, size); } // TODO(cmmm): Handle the case where we can just reassign one chunk to // the other arena. void * arena_coalesce_from_into_fast (struct arena *a, struct arena_checkpoint cp, struct arena *into, size_t size) { ASSERT (a != NULL); ASSERT (into != NULL); void *ptr = arena_allocate (into, size, cp.alignment); // copy chunks void *free = ptr; void *aligned_src = alignto (cp.free, cp.alignment); if (aligned_src < cp.tail->free) { size_t other_size = cp.tail->free - aligned_src; memcpy (free, aligned_src, other_size); free += other_size; } for (struct arena_chunk *other_chunk = cp.tail->next; other_chunk != a->tail->next; other_chunk = other_chunk->next) { size_t other_size = chunk_used (other_chunk); memcpy (free, other_chunk->begin, other_size); free += other_size; } arena_rollback_to (a, cp); return ptr; } void arena_trim (struct arena *a) { ASSERT (a != NULL); if (a->tail == NULL) return; struct arena_chunk *chunk = a->tail->next; while (chunk) { struct arena_chunk *next = chunk->next; FREE (chunk->begin); chunk = next; } } void arena_free (struct arena *a) { ASSERT (a != NULL); struct arena_chunk *chunk = a->head; while (chunk) { struct arena_chunk *next = chunk->next; FREE (chunk->begin); chunk = next; } a->head = NULL; a->tail = NULL; } #ifndef ARENA_NO_STDIO #include char * arena_printf (struct arena *a, const char *format, ...) { ASSERT (a != NULL); ASSERT (format != NULL); va_list ap; va_start (ap, format); char *cstr = arena_vprintf (a, format, ap); va_end (ap); return cstr; } char * arena_vprintf (struct arena *a, const char *format, va_list ap) { ASSERT (a != NULL); ASSERT (format != NULL); va_list ap_copy; va_copy (ap_copy, ap); int len = vsnprintf (NULL, 0, format, ap_copy); va_end (ap_copy); ASSERT (len >= 0); char *cstr = ARENA_ALLOCATE_ARRAY (a, len + 1, char); vsnprintf (cstr, len + 1, format, ap); return cstr; } #endif /// src/arena.c ends here