From d38f82f6462af4e5aad6a2c776f5c00ce5b13c87 Mon Sep 17 00:00:00 2001 From: Linnnus Date: Thu, 1 Feb 2024 22:59:38 +0100 Subject: 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). --- src/arena.h | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/arena.h (limited to 'src/arena.h') 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 // 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 -- cgit v1.2.3