diff options
Diffstat (limited to 'src/core/arena_allocator.h')
-rw-r--r-- | src/core/arena_allocator.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/core/arena_allocator.h b/src/core/arena_allocator.h new file mode 100644 index 0000000..5fba78a --- /dev/null +++ b/src/core/arena_allocator.h @@ -0,0 +1,33 @@ +#ifndef SAND_ARENA_ALLOCATOR_H +#define SAND_ARENA_ALLOCATOR_H + +// This module defines an arena allocator which implements `SandAllocator`. It +// is useful if one needs to allocate a lot of objects with the same lifetime +// very quickly. When the arena is destroyed, all objects allocated within are +// freed. + +#include "allocator.h" + +// This should only be used internally by an arena allocator. +// It is only public because of C's stone-age """modules""". +typedef struct _SandRegion { + struct _SandRegion *prev; + size_t capacity; + size_t used; + unsigned char data[]; +} SandRegion; + +typedef struct { + SandAllocator *parent; + SandRegion *current_region; +} SandArena; + +SandArena sand_create_arena(SandAllocator *parent); + +// Returns a `SandAllocator` which is valid as long as the arena is. +// The arena must not be moved afterwards, as the allocator retains a pointer to the arena. +SandAllocator sand_get_allocator_for_arena(SandArena *); + +void sand_destroy_arena(SandArena *); + +#endif |