blob: 5fba78a0e1b62a2b139c8cbf1a73ae984eb70530 (
plain)
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
|
#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
|