diff options
| author | 2026-11-11 22:28:13 +0000 | |
|---|---|---|
| committer | 2026-11-11 22:47:34 +0000 | |
| commit | cb070b2d3f2b6e241e3be1b7e0eeb90fc755255f (patch) | |
| tree | c7d90c0abe9533c87d51b5196871846855efa1ba /src/arena.c | |
| download | thesis-master.tar.gz | |
Diffstat (limited to 'src/arena.c')
| -rw-r--r-- | src/arena.c | 469 |
1 files changed, 469 insertions, 0 deletions
diff --git a/src/arena.c b/src/arena.c new file mode 100644 index 0000000..461bc7a --- /dev/null +++ b/src/arena.c | |||
| @@ -0,0 +1,469 @@ | |||
| 1 | /// src/arena.c --- 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 | #define _GNU_SOURCE // TODO(cmmm): remove hard dependency on GNU extensions | ||
| 12 | #include <stdint.h> | ||
| 13 | #include <string.h> | ||
| 14 | |||
| 15 | // TODO(cmmm): rethink alignment in relation to coalescing, it seems | ||
| 16 | // really hard to get right... Verdict, use obstack (keeping track of | ||
| 17 | // base) for making sure fields are aligned (by adding extra padding). | ||
| 18 | // Coalescing stays the same and can still be used without obstack, if | ||
| 19 | // you know that there won't be need for extra alignment ensuring | ||
| 20 | // padding | ||
| 21 | |||
| 22 | #include "arena.h" | ||
| 23 | |||
| 24 | #ifndef ARENA_ASSERT | ||
| 25 | #include <assert.h> | ||
| 26 | #define ASSERT assert | ||
| 27 | #define ASSERT_PERROR assert_perror | ||
| 28 | #else | ||
| 29 | #define ASSERT ARENA_ASSERT | ||
| 30 | #endif | ||
| 31 | |||
| 32 | #include <stdlib.h> | ||
| 33 | #define MALLOC malloc | ||
| 34 | #define ALIGNED_ALLOC aligned_alloc | ||
| 35 | #define FREE free | ||
| 36 | |||
| 37 | #define MAX(A, B) ((A) > (B) ? (A) : (B)) | ||
| 38 | |||
| 39 | // NOTE(cmmm): address of chunk == chunk's end | ||
| 40 | #define CHUNK_END(CHUNK) ((void *) (CHUNK)) | ||
| 41 | |||
| 42 | static size_t | ||
| 43 | chunk_size (const struct arena_chunk *chunk) | ||
| 44 | { | ||
| 45 | ASSERT (chunk != NULL); | ||
| 46 | |||
| 47 | return CHUNK_END (chunk) - chunk->begin; | ||
| 48 | } | ||
| 49 | |||
| 50 | static size_t | ||
| 51 | chunk_used (struct arena_chunk *chunk) | ||
| 52 | { | ||
| 53 | ASSERT (chunk != NULL); | ||
| 54 | |||
| 55 | return chunk->free - chunk->begin; | ||
| 56 | } | ||
| 57 | |||
| 58 | static struct arena_chunk * | ||
| 59 | new_chunk (size_t size, size_t alignment) | ||
| 60 | { | ||
| 61 | ASSERT ((alignment & (alignment + 1)) == 0); | ||
| 62 | |||
| 63 | alignment += 1; | ||
| 64 | alignment += alignment % sizeof (void *); | ||
| 65 | |||
| 66 | void *chunk_begin = | ||
| 67 | ALIGNED_ALLOC (alignment, size + sizeof (struct arena_chunk)); | ||
| 68 | ASSERT (chunk_begin != NULL); | ||
| 69 | |||
| 70 | struct arena_chunk *chunk = chunk_begin + size; | ||
| 71 | chunk->next = NULL; | ||
| 72 | chunk->begin = chunk_begin; | ||
| 73 | chunk->free = chunk_begin; | ||
| 74 | |||
| 75 | return chunk; | ||
| 76 | } | ||
| 77 | |||
| 78 | static struct arena_chunk * | ||
| 79 | append_chunk (struct arena *arena, size_t size, size_t alignment) | ||
| 80 | { | ||
| 81 | ASSERT (arena != NULL); | ||
| 82 | |||
| 83 | size = arena->tail != NULL | ||
| 84 | ? MAX (ARENA_CHUNK_GROWTH_FACTOR*chunk_size (arena->tail), size) | ||
| 85 | : MAX (ARENA_FIRST_CHUNK_SIZE, size); | ||
| 86 | |||
| 87 | struct arena_chunk *chunk = new_chunk (size, alignment); | ||
| 88 | |||
| 89 | if (arena->tail != NULL) { | ||
| 90 | arena->tail->next = chunk; | ||
| 91 | arena->tail = chunk; | ||
| 92 | } else { | ||
| 93 | arena->head = chunk; | ||
| 94 | arena->tail = chunk; | ||
| 95 | } | ||
| 96 | |||
| 97 | return chunk; | ||
| 98 | } | ||
| 99 | |||
| 100 | static void * | ||
| 101 | alignto (void *ptr, size_t alignment) | ||
| 102 | { | ||
| 103 | return (void *) ((ptrdiff_t) (ptr + alignment) & ~alignment); | ||
| 104 | } | ||
| 105 | |||
| 106 | void * | ||
| 107 | arena_reserve (struct arena *arena, size_t size, size_t alignment) | ||
| 108 | { | ||
| 109 | ASSERT (arena != NULL); | ||
| 110 | |||
| 111 | if (arena->tail != NULL) { | ||
| 112 | void *aligned_free = alignto (arena->tail->free, alignment); | ||
| 113 | if (CHUNK_END (arena->tail) - aligned_free >= (ssize_t) size) { | ||
| 114 | return aligned_free; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | // FIXME(cmmm): use arena->tail->next when available | ||
| 119 | append_chunk (arena, size, alignment); | ||
| 120 | return alignto (arena->tail->free, alignment); | ||
| 121 | } | ||
| 122 | |||
| 123 | void * | ||
| 124 | arena_allocate (struct arena *arena, size_t size, size_t alignment) | ||
| 125 | { | ||
| 126 | ASSERT (arena != NULL); | ||
| 127 | |||
| 128 | void *ptr = arena_reserve (arena, size, alignment); | ||
| 129 | arena->tail->free = ptr + size; | ||
| 130 | return ptr; | ||
| 131 | } | ||
| 132 | |||
| 133 | void * | ||
| 134 | arena_allocate_fast (struct arena *arena, size_t size, size_t alignment) | ||
| 135 | { | ||
| 136 | ASSERT (arena != NULL); | ||
| 137 | ASSERT (arena->tail != NULL); | ||
| 138 | |||
| 139 | void *ptr = alignto (arena->tail->free, alignment); | ||
| 140 | arena->tail->free = ptr + size; | ||
| 141 | return ptr; | ||
| 142 | } | ||
| 143 | |||
| 144 | size_t | ||
| 145 | arena_used (const struct arena *arena) | ||
| 146 | { | ||
| 147 | ASSERT (arena != NULL); | ||
| 148 | |||
| 149 | size_t used = 0; | ||
| 150 | const struct arena_chunk *chunk; | ||
| 151 | for (chunk = arena->head; chunk; chunk = chunk->next) | ||
| 152 | used += chunk_size (chunk); | ||
| 153 | |||
| 154 | return used; | ||
| 155 | } | ||
| 156 | |||
| 157 | size_t | ||
| 158 | arena_room (const struct arena *arena, size_t alignment) | ||
| 159 | { | ||
| 160 | ASSERT (arena != NULL); | ||
| 161 | |||
| 162 | if (arena->tail == NULL) | ||
| 163 | return 0; | ||
| 164 | |||
| 165 | void *aligned_free = alignto (arena->tail->free, alignment); | ||
| 166 | return CHUNK_END (arena->tail) - aligned_free; | ||
| 167 | } | ||
| 168 | |||
| 169 | static void * | ||
| 170 | next_free (const struct arena_chunk *chunk, size_t alignment) | ||
| 171 | { | ||
| 172 | ASSERT (chunk != NULL); | ||
| 173 | |||
| 174 | void *aligned_free = alignto (chunk->free, alignment); | ||
| 175 | if (aligned_free > CHUNK_END (chunk)) | ||
| 176 | aligned_free = alignto (chunk->next->begin, alignment); | ||
| 177 | |||
| 178 | return aligned_free; | ||
| 179 | } | ||
| 180 | |||
| 181 | // REVIEW(cmmm): probably bugged, contract unclear | ||
| 182 | void * | ||
| 183 | arena_next_free (const struct arena *a, size_t alignment) | ||
| 184 | { | ||
| 185 | ASSERT (a != NULL); | ||
| 186 | ASSERT (a->tail != NULL); | ||
| 187 | |||
| 188 | return next_free (a->tail, alignment); | ||
| 189 | } | ||
| 190 | |||
| 191 | void * | ||
| 192 | arena_copy (struct arena *a, const void *from, | ||
| 193 | size_t size, size_t alignment) | ||
| 194 | { | ||
| 195 | ASSERT (a != NULL); | ||
| 196 | ASSERT (from != NULL); | ||
| 197 | |||
| 198 | void *ptr = arena_allocate (a, size, alignment); | ||
| 199 | memcpy (ptr, from, size); | ||
| 200 | return ptr; | ||
| 201 | } | ||
| 202 | |||
| 203 | void * | ||
| 204 | arena_copy_fast (struct arena *a, const void *from, | ||
| 205 | size_t size, size_t alignment) | ||
| 206 | { | ||
| 207 | ASSERT (a != NULL); | ||
| 208 | ASSERT (from != NULL); | ||
| 209 | |||
| 210 | void *ptr = arena_allocate_fast (a, size, alignment); | ||
| 211 | memcpy (ptr, from, size); | ||
| 212 | return ptr; | ||
| 213 | } | ||
| 214 | |||
| 215 | char * | ||
| 216 | arena_copy_cstr (struct arena *a, const char *cstr) | ||
| 217 | { | ||
| 218 | ASSERT (a != NULL); | ||
| 219 | ASSERT (cstr != NULL); | ||
| 220 | |||
| 221 | size_t size = strlen (cstr) + 1; | ||
| 222 | return arena_copy (a, cstr, size, 1); | ||
| 223 | } | ||
| 224 | |||
| 225 | void | ||
| 226 | arena_reset (struct arena *a) | ||
| 227 | { | ||
| 228 | ASSERT (a != NULL); | ||
| 229 | |||
| 230 | if (a->head == NULL) | ||
| 231 | return; | ||
| 232 | |||
| 233 | a->tail = a->head; | ||
| 234 | a->head->free = a->head->begin; | ||
| 235 | } | ||
| 236 | |||
| 237 | void * | ||
| 238 | arena_pop (struct arena *a, size_t size) | ||
| 239 | { | ||
| 240 | ASSERT (a != NULL); | ||
| 241 | |||
| 242 | // FIXME(cmmm): handle chunk boundaries | ||
| 243 | a->tail->free -= size; | ||
| 244 | |||
| 245 | ASSERT (a->tail->free >= a->tail->begin); | ||
| 246 | |||
| 247 | return a->tail->free; | ||
| 248 | } | ||
| 249 | |||
| 250 | struct arena_checkpoint | ||
| 251 | arena_checkpoint (struct arena *a, size_t alignment) | ||
| 252 | { | ||
| 253 | ASSERT (a != NULL); | ||
| 254 | |||
| 255 | if (a->tail == NULL) | ||
| 256 | arena_reserve (a, 0, alignment); | ||
| 257 | |||
| 258 | void *aligned_free = alignto (a->tail->free, alignment); | ||
| 259 | if (aligned_free > CHUNK_END (a->tail)) { | ||
| 260 | arena_reserve (a, 0, alignment); | ||
| 261 | aligned_free = alignto (a->tail->free, alignment); | ||
| 262 | } | ||
| 263 | |||
| 264 | struct arena_checkpoint cp = { | ||
| 265 | .tail = a->tail, | ||
| 266 | .free = a->tail->free, | ||
| 267 | .alignment = alignment, | ||
| 268 | }; | ||
| 269 | |||
| 270 | a->tail->free = aligned_free; | ||
| 271 | return cp; | ||
| 272 | } | ||
| 273 | |||
| 274 | void | ||
| 275 | arena_rollback_to (struct arena *a, struct arena_checkpoint cp) | ||
| 276 | { | ||
| 277 | ASSERT (a != NULL); | ||
| 278 | ASSERT (cp.tail != NULL); | ||
| 279 | ASSERT (cp.free != NULL); | ||
| 280 | |||
| 281 | a->tail = cp.tail; | ||
| 282 | a->tail->free = cp.free; | ||
| 283 | } | ||
| 284 | |||
| 285 | size_t | ||
| 286 | arena_checkpoint_size_from (struct arena_checkpoint cp) | ||
| 287 | { | ||
| 288 | ASSERT (cp.tail != NULL); | ||
| 289 | ASSERT (cp.free != NULL); | ||
| 290 | |||
| 291 | size_t size = cp.tail->free == cp.free | ||
| 292 | ? 0 : cp.tail->free - alignto (cp.free, cp.alignment); | ||
| 293 | for (struct arena_chunk *chunk = cp.tail; chunk; chunk = chunk->next) | ||
| 294 | size += chunk_size (chunk); | ||
| 295 | return size; | ||
| 296 | } | ||
| 297 | |||
| 298 | void * | ||
| 299 | arena_checkpoint_next_from (struct arena_checkpoint cp) | ||
| 300 | { | ||
| 301 | ASSERT (cp.tail != NULL); | ||
| 302 | ASSERT (cp.free != NULL); | ||
| 303 | |||
| 304 | void *aligned_free = alignto (cp.free, cp.alignment); | ||
| 305 | if (aligned_free > CHUNK_END (cp.tail)) | ||
| 306 | aligned_free = alignto (cp.tail->next->begin, cp.alignment); | ||
| 307 | |||
| 308 | return aligned_free; | ||
| 309 | } | ||
| 310 | |||
| 311 | void * | ||
| 312 | arena_coalesce_from (struct arena *a, struct arena_checkpoint cp) | ||
| 313 | { | ||
| 314 | ASSERT (a != NULL); | ||
| 315 | |||
| 316 | if (a->tail == cp.tail) // already coalesced | ||
| 317 | return cp.free; | ||
| 318 | |||
| 319 | size_t size = arena_checkpoint_size_from (cp); | ||
| 320 | return arena_coalesce_from_fast (a, cp, size); | ||
| 321 | } | ||
| 322 | |||
| 323 | void * | ||
| 324 | arena_coalesce_from_fast (struct arena *a, struct arena_checkpoint cp, | ||
| 325 | size_t size) | ||
| 326 | { | ||
| 327 | ASSERT (a != NULL); | ||
| 328 | |||
| 329 | if (a->tail == cp.tail) // already coalesced | ||
| 330 | return cp.free; | ||
| 331 | |||
| 332 | // make new chunk, add old chunks after it | ||
| 333 | struct arena_chunk *chunk = new_chunk (size, cp.alignment); | ||
| 334 | chunk->next = cp.free == cp.tail->begin ? cp.tail : cp.tail->next; | ||
| 335 | |||
| 336 | // copy old chunks into new chunk | ||
| 337 | // BUG(cmmm): check if data actually starts in checkpoints chunk | ||
| 338 | if (cp.free != cp.tail->free) { | ||
| 339 | size_t other_size = | ||
| 340 | alignto (cp.tail->free, cp.alignment) - arena_checkpoint_next_from (cp); | ||
| 341 | memcpy (chunk->free, cp.free, other_size); | ||
| 342 | chunk->free += other_size; | ||
| 343 | } | ||
| 344 | for (struct arena_chunk *other_chunk = cp.tail->next; | ||
| 345 | other_chunk; other_chunk = other_chunk->next) { | ||
| 346 | size_t other_size = chunk_used (other_chunk); | ||
| 347 | memcpy (chunk->free, other_chunk->begin, other_size); | ||
| 348 | chunk->free += other_size; | ||
| 349 | } | ||
| 350 | |||
| 351 | // link in new chunk | ||
| 352 | for (struct arena_chunk **other_chunk = &a->head; | ||
| 353 | *other_chunk; other_chunk = &(*other_chunk)->next) { | ||
| 354 | if (*other_chunk != chunk->next) | ||
| 355 | continue; | ||
| 356 | *other_chunk = chunk; | ||
| 357 | break; | ||
| 358 | } | ||
| 359 | |||
| 360 | return chunk->begin; | ||
| 361 | } | ||
| 362 | |||
| 363 | void * | ||
| 364 | arena_coalesce_from_into (struct arena *a, struct arena_checkpoint cp, | ||
| 365 | struct arena *into) | ||
| 366 | { | ||
| 367 | size_t size = arena_checkpoint_size_from (cp); | ||
| 368 | return arena_coalesce_from_into_fast (a, cp, into, size); | ||
| 369 | } | ||
| 370 | |||
| 371 | // TODO(cmmm): Handle the case where we can just reassign one chunk to | ||
| 372 | // the other arena. | ||
| 373 | void * | ||
| 374 | arena_coalesce_from_into_fast (struct arena *a, struct arena_checkpoint cp, | ||
| 375 | struct arena *into, size_t size) | ||
| 376 | { | ||
| 377 | ASSERT (a != NULL); | ||
| 378 | ASSERT (into != NULL); | ||
| 379 | |||
| 380 | void *ptr = arena_allocate (into, size, cp.alignment); | ||
| 381 | |||
| 382 | // copy chunks | ||
| 383 | void *free = ptr; | ||
| 384 | void *aligned_src = alignto (cp.free, cp.alignment); | ||
| 385 | if (aligned_src < cp.tail->free) { | ||
| 386 | size_t other_size = cp.tail->free - aligned_src; | ||
| 387 | memcpy (free, aligned_src, other_size); | ||
| 388 | free += other_size; | ||
| 389 | } | ||
| 390 | for (struct arena_chunk *other_chunk = cp.tail->next; | ||
| 391 | other_chunk != a->tail->next; other_chunk = other_chunk->next) { | ||
| 392 | size_t other_size = chunk_used (other_chunk); | ||
| 393 | memcpy (free, other_chunk->begin, other_size); | ||
| 394 | free += other_size; | ||
| 395 | } | ||
| 396 | |||
| 397 | arena_rollback_to (a, cp); | ||
| 398 | return ptr; | ||
| 399 | } | ||
| 400 | |||
| 401 | void | ||
| 402 | arena_trim (struct arena *a) | ||
| 403 | { | ||
| 404 | ASSERT (a != NULL); | ||
| 405 | |||
| 406 | if (a->tail == NULL) | ||
| 407 | return; | ||
| 408 | |||
| 409 | struct arena_chunk *chunk = a->tail->next; | ||
| 410 | while (chunk) { | ||
| 411 | struct arena_chunk *next = chunk->next; | ||
| 412 | FREE (chunk->begin); | ||
| 413 | chunk = next; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | void | ||
| 418 | arena_free (struct arena *a) | ||
| 419 | { | ||
| 420 | ASSERT (a != NULL); | ||
| 421 | |||
| 422 | struct arena_chunk *chunk = a->head; | ||
| 423 | while (chunk) { | ||
| 424 | struct arena_chunk *next = chunk->next; | ||
| 425 | FREE (chunk->begin); | ||
| 426 | chunk = next; | ||
| 427 | } | ||
| 428 | |||
| 429 | a->head = NULL; | ||
| 430 | a->tail = NULL; | ||
| 431 | } | ||
| 432 | |||
| 433 | #ifndef ARENA_NO_STDIO | ||
| 434 | #include <stdio.h> | ||
| 435 | |||
| 436 | char * | ||
| 437 | arena_printf (struct arena *a, const char *format, ...) | ||
| 438 | { | ||
| 439 | ASSERT (a != NULL); | ||
| 440 | ASSERT (format != NULL); | ||
| 441 | |||
| 442 | va_list ap; | ||
| 443 | va_start (ap, format); | ||
| 444 | char *cstr = arena_vprintf (a, format, ap); | ||
| 445 | va_end (ap); | ||
| 446 | |||
| 447 | return cstr; | ||
| 448 | } | ||
| 449 | |||
| 450 | char * | ||
| 451 | arena_vprintf (struct arena *a, const char *format, va_list ap) | ||
| 452 | { | ||
| 453 | ASSERT (a != NULL); | ||
| 454 | ASSERT (format != NULL); | ||
| 455 | |||
| 456 | va_list ap_copy; | ||
| 457 | va_copy (ap_copy, ap); | ||
| 458 | int len = vsnprintf (NULL, 0, format, ap_copy); | ||
| 459 | va_end (ap_copy); | ||
| 460 | |||
| 461 | ASSERT (len >= 0); | ||
| 462 | char *cstr = ARENA_ALLOCATE_ARRAY (a, len + 1, char); | ||
| 463 | vsnprintf (cstr, len + 1, format, ap); | ||
| 464 | |||
| 465 | return cstr; | ||
| 466 | } | ||
| 467 | #endif | ||
| 468 | |||
| 469 | /// src/arena.c ends here | ||
