diff options
author | Linnnus <[email protected]> | 2024-02-01 22:59:38 +0100 |
---|---|---|
committer | Linnnus <[email protected]> | 2024-02-04 09:58:06 +0100 |
commit | d38f82f6462af4e5aad6a2c776f5c00ce5b13c87 (patch) | |
tree | 01a222792dfb10473ae4370b4fc90f3a48e1a499 /src/arena.h |
feat: initial commit
Here is a small overview of the state of the project at this first
commit.
I have basic Git Repo -> HTML working, and a plan for how setting up an
actual server would work (mainly, NGINX + a git hook to rebuild).
The main thing I'm working on right now is parsing WikiCreole, though I
am starting to wonder if this is the right langauge. WikiCreole is
pretty irregular and has a lot of edge cases (e.g. around emphasis).
Diffstat (limited to 'src/arena.h')
-rw-r--r-- | src/arena.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/arena.h b/src/arena.h new file mode 100644 index 0000000..cd4aa17 --- /dev/null +++ b/src/arena.h @@ -0,0 +1,43 @@ +#ifndef ARENA_H +#define ARENA_H + +// +// This module defines a simple, fixed-size arena allocator. +// + +#include <stddef.h> // size_t + +struct arena { + void *root; + size_t capacity; + size_t used; +}; + +// Initialize an arena with the given `capacity`. +// Panics on failure to allocate. +struct arena arena_create(size_t capacity); + +// These flags control the behavior of `arena_alloc`. +#define ARENA_NO_ZERO 1 +#define ARENA_NO_PANIC 2 + +// Allocate `size` bytes in `arena`. +// The resulting memory is zeroed unless ARENA_NO_ZERO is passed. +// Unless ARENA_NO_PANIC is specified, the resulting pointer is always valid. +void *arena_alloc(struct arena *arena, size_t size, size_t alignment, unsigned flags); + +// Free the memory associated with the arena using the underlying allocator. +void arena_destroy(struct arena *arena); + +// +// The `new` macro makes the basic allocation case simple. It uses a bit of +// preprocessor magic to simulate default argument values. +// + +#define new(...) newx(__VA_ARGS__,new4,new3,new2)(__VA_ARGS__) +#define newx(a1, a2, a3, a4, a5, ...) a5 +#define new2(arena, typ) (typ *)arena_alloc(arena, sizeof(typ), _Alignof(typ), 0) +#define new3(arena, typ, count) (typ *)arena_alloc(arena, count * sizeof(typ), _Alignof(typ), 0) +#define new4(arena, typ, count, flags) (typ *)arena_alloc(arena, count * sizeof(typ), _Alignof(typ), flags) + +#endif |