From cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Thu, 12 Nov 2026 01:28:13 +0300 Subject: publish --- src/arena.h | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/arena.h (limited to 'src/arena.h') diff --git a/src/arena.h b/src/arena.h new file mode 100644 index 0000000..b87be87 --- /dev/null +++ b/src/arena.h @@ -0,0 +1,102 @@ +/// src/arena.h --- chunked arena allocator +// +// Copyright (C) 2026 by Martin Michalec +// +// SPDX-License-Identifier: MIT +// +/// Commentary: +// +/// Code: + +#ifndef INCLUDED_ARENA_H +#define INCLUDED_ARENA_H + +#include + +// #define ARENA_NO_STDIO + +#ifndef ARENA_FIRST_CHUNK_SIZE +# define ARENA_FIRST_CHUNK_SIZE (4 << 10) +#endif + +#ifndef ARENA_CHUNK_GROWTH_FACTOR +# define ARENA_CHUNK_GROWTH_FACTOR 2 +#endif + +struct arena_chunk { + struct arena_chunk *next; + void *begin; + void *free; +}; + +struct arena { + struct arena_chunk *head; + struct arena_chunk *tail; +}; + +struct arena_checkpoint { + struct arena_chunk *tail; + void *free; + size_t alignment; +}; + +#ifndef ALIGNOF +# if _STDC_VERSION_ >= 202311L +# define ALIGNOF alignof +# elif _STDC_VERSION_ >= 201112L +# define ALIGNOF _Alignof +# else +# define ALIGNOF(O) offsetof (struct { char _; typeof (O) x; }, x) +# endif +#endif + +#define ARENA_RESERVE( A, T) arena_reserve ((A), sizeof (T), ALIGNOF (T) - 1) +#define ARENA_RESERVE_ARRAY( A, N, T) arena_reserve ((A), (N)*sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ALLOCATE( A, T) arena_allocate ((A), sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ALLOCATE_ARRAY( A, N, T) arena_allocate ((A), (N)*sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ALLOCATE_FAST( A, T) arena_allocate_fast ((A), sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ALLOCATE_ARRAY_FAST(A, N, T) arena_allocate_fast ((A), (N)*sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ZERO( A, T) arena_zero ((A), sizeof (T), ALIGNOF (T) - 1) +#define ARENA_ZERO_ARRAY( A, N, T) arena_zero ((A), (N)*sizeof (T), ALIGNOF (T) - 1) +void *arena_reserve (struct arena *, size_t size, size_t alignment); +void *arena_allocate (struct arena *, size_t size, size_t alignment); +void *arena_allocate_fast (struct arena *, size_t size, size_t alignment); +void *arena_zero (struct arena *, size_t size, size_t alignment); + +size_t arena_used (const struct arena *); +size_t arena_room (const struct arena *, size_t alignment); +void *arena_next_free (const struct arena *, size_t alignment); + +#define ARENA_COPY( A, F ) arena_copy ((A), &(F), sizeof ( F), ALIGNOF ( F) - 1) +#define ARENA_COPY_FAST( A, F ) arena_copy_fast ((A), &(F), sizeof ( F), ALIGNOF ( F) - 1) +#define ARENA_COPY_ARRAY(A, F, N) arena_copy ((A), (F), (N)*sizeof (*F), ALIGNOF (*F) - 1) +void *arena_copy (struct arena *, const void *, size_t size, size_t alignment); +void *arena_copy_fast (struct arena *, const void *, size_t size, size_t alignment); +char *arena_copy_cstr (struct arena *, const char *); + +void arena_reset (struct arena *); + +// NOTE(cmmm): doesn't handle alignments +void *arena_pop (struct arena *, size_t size); + +struct arena_checkpoint arena_checkpoint (struct arena *, size_t alignment); +size_t arena_checkpoint_size_from (struct arena_checkpoint); +void *arena_checkpoint_next_from (struct arena_checkpoint); +void arena_rollback_to (struct arena *, struct arena_checkpoint); +void *arena_coalesce_from (struct arena *, struct arena_checkpoint); +void *arena_coalesce_from_fast (struct arena *, struct arena_checkpoint, size_t size); +void *arena_coalesce_from_into (struct arena *, struct arena_checkpoint, struct arena *); +void *arena_coalesce_from_into_fast (struct arena *, struct arena_checkpoint, struct arena *, size_t size); + +void arena_trim (struct arena *); +void arena_free (struct arena *); + +#ifndef ARENA_NO_STDIO +# include + char *arena_printf (struct arena *, const char *format, ...); + char *arena_vprintf (struct arena *, const char *format, va_list ap); +#endif + +#endif // INCLUDED_ARENA_H + +/// src/arena.h ends here -- cgit v1.3.1