1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/// src/arena.h --- chunked arena allocator
//
// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
/// Commentary:
//
/// Code:
#ifndef INCLUDED_ARENA_H
#define INCLUDED_ARENA_H
#include <stddef.h>
// #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 <stdarg.h>
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
|