-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharena.h
71 lines (51 loc) · 1.43 KB
/
arena.h
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
*
* This file describes a non-freeing memory arena.
*
* These arenas hold only one type of data within them.
* An arena is initialized with the type of data it expects
* to hold and a hint as to how many allocations may be
* needed.
*
* When the number of allocations is met, the arena will
* allocate another space of equal size to continue allocating
* from.
*
* Memory is freed only from destroying the arena.
*
*/
#include <stdlib.h>
typedef struct {
// The start of this arena's memory.
void *start;
// The current end of this arena's memory.
// This is where the next allocation will be placed.
void *end;
// How many more allocations fit in this arena.
int allocations_left;
} _Arena;
typedef struct {
// The size of individual allocations within the arena
size_t allocation_size;
// The size of each arena.
size_t arena_size;
// How many allocations fit in each arena.
int allocations_per_arena;
// The number of arenas allocated.
int number_of_arenas;
// The array of actual memory arenas.
_Arena *arenas;
struct {
int number_of_allocations;
} stats;
} Arena;
Arena *Arena_create(size_t allocation_size, int allocations_hint);
void *Arena_delete(Arena *arena);
/**
* Allocates a single new object in this arena.
*/
void *Arena_allocate(Arena *arena);
/**
* Prints the statistics of this arena.
*/
void Arena_print(Arena *arena);