aboutsummaryrefslogtreecommitdiff
path: root/src/arena.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:28:13 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-11 22:47:34 +0000
commitcb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch)
treec7d90c0abe9533c87d51b5196871846855efa1ba /src/arena.h
downloadthesis-cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f.tar.gz
publishHEADmaster
Diffstat (limited to 'src/arena.h')
-rw-r--r--src/arena.h102
1 files changed, 102 insertions, 0 deletions
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 @@
1/// src/arena.h --- chunked arena allocator
2//
3// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
4//
5// SPDX-License-Identifier: MIT
6//
7/// Commentary:
8//
9/// Code:
10
11#ifndef INCLUDED_ARENA_H
12#define INCLUDED_ARENA_H
13
14#include <stddef.h>
15
16// #define ARENA_NO_STDIO
17
18#ifndef ARENA_FIRST_CHUNK_SIZE
19# define ARENA_FIRST_CHUNK_SIZE (4 << 10)
20#endif
21
22#ifndef ARENA_CHUNK_GROWTH_FACTOR
23# define ARENA_CHUNK_GROWTH_FACTOR 2
24#endif
25
26struct arena_chunk {
27 struct arena_chunk *next;
28 void *begin;
29 void *free;
30};
31
32struct arena {
33 struct arena_chunk *head;
34 struct arena_chunk *tail;
35};
36
37struct arena_checkpoint {
38 struct arena_chunk *tail;
39 void *free;
40 size_t alignment;
41};
42
43#ifndef ALIGNOF
44# if _STDC_VERSION_ >= 202311L
45# define ALIGNOF alignof
46# elif _STDC_VERSION_ >= 201112L
47# define ALIGNOF _Alignof
48# else
49# define ALIGNOF(O) offsetof (struct { char _; typeof (O) x; }, x)
50# endif
51#endif
52
53#define ARENA_RESERVE( A, T) arena_reserve ((A), sizeof (T), ALIGNOF (T) - 1)
54#define ARENA_RESERVE_ARRAY( A, N, T) arena_reserve ((A), (N)*sizeof (T), ALIGNOF (T) - 1)
55#define ARENA_ALLOCATE( A, T) arena_allocate ((A), sizeof (T), ALIGNOF (T) - 1)
56#define ARENA_ALLOCATE_ARRAY( A, N, T) arena_allocate ((A), (N)*sizeof (T), ALIGNOF (T) - 1)
57#define ARENA_ALLOCATE_FAST( A, T) arena_allocate_fast ((A), sizeof (T), ALIGNOF (T) - 1)
58#define ARENA_ALLOCATE_ARRAY_FAST(A, N, T) arena_allocate_fast ((A), (N)*sizeof (T), ALIGNOF (T) - 1)
59#define ARENA_ZERO( A, T) arena_zero ((A), sizeof (T), ALIGNOF (T) - 1)
60#define ARENA_ZERO_ARRAY( A, N, T) arena_zero ((A), (N)*sizeof (T), ALIGNOF (T) - 1)
61void *arena_reserve (struct arena *, size_t size, size_t alignment);
62void *arena_allocate (struct arena *, size_t size, size_t alignment);
63void *arena_allocate_fast (struct arena *, size_t size, size_t alignment);
64void *arena_zero (struct arena *, size_t size, size_t alignment);
65
66size_t arena_used (const struct arena *);
67size_t arena_room (const struct arena *, size_t alignment);
68void *arena_next_free (const struct arena *, size_t alignment);
69
70#define ARENA_COPY( A, F ) arena_copy ((A), &(F), sizeof ( F), ALIGNOF ( F) - 1)
71#define ARENA_COPY_FAST( A, F ) arena_copy_fast ((A), &(F), sizeof ( F), ALIGNOF ( F) - 1)
72#define ARENA_COPY_ARRAY(A, F, N) arena_copy ((A), (F), (N)*sizeof (*F), ALIGNOF (*F) - 1)
73void *arena_copy (struct arena *, const void *, size_t size, size_t alignment);
74void *arena_copy_fast (struct arena *, const void *, size_t size, size_t alignment);
75char *arena_copy_cstr (struct arena *, const char *);
76
77void arena_reset (struct arena *);
78
79// NOTE(cmmm): doesn't handle alignments
80void *arena_pop (struct arena *, size_t size);
81
82struct arena_checkpoint arena_checkpoint (struct arena *, size_t alignment);
83size_t arena_checkpoint_size_from (struct arena_checkpoint);
84void *arena_checkpoint_next_from (struct arena_checkpoint);
85void arena_rollback_to (struct arena *, struct arena_checkpoint);
86void *arena_coalesce_from (struct arena *, struct arena_checkpoint);
87void *arena_coalesce_from_fast (struct arena *, struct arena_checkpoint, size_t size);
88void *arena_coalesce_from_into (struct arena *, struct arena_checkpoint, struct arena *);
89void *arena_coalesce_from_into_fast (struct arena *, struct arena_checkpoint, struct arena *, size_t size);
90
91void arena_trim (struct arena *);
92void arena_free (struct arena *);
93
94#ifndef ARENA_NO_STDIO
95# include <stdarg.h>
96 char *arena_printf (struct arena *, const char *format, ...);
97 char *arena_vprintf (struct arena *, const char *format, va_list ap);
98#endif
99
100#endif // INCLUDED_ARENA_H
101
102/// src/arena.h ends here