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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/// src/arena.c --- chunked arena allocator
//
// Copyright (C) 2026 by Martin Michalec <martin@michalec.dev>
//
// SPDX-License-Identifier: MIT
//
/// Commentary:
//
/// Code:
#define _GNU_SOURCE // TODO(cmmm): remove hard dependency on GNU extensions
#include <stdint.h>
#include <string.h>
// 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 <assert.h>
#define ASSERT assert
#define ASSERT_PERROR assert_perror
#else
#define ASSERT ARENA_ASSERT
#endif
#include <stdlib.h>
#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 <stdio.h>
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
|