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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
#include "arena.h"
#include "shared.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>
typedef struct an {
void *membase;
void *memcur;
size_t allocated;
size_t used;
struct an *next;
} arenanode;
typedef struct arena {
arenanode *start;
arenanode *current;
// For keeping track of the largest possible thing that can be allocated to one node
size_t node_memspace;
} arena;
int arenanode_init(arenanode **an, size_t bytes) {
(*an) = xcalloc(1, sizeof(**an));
if(!(*an))
return -1;
(*an)->allocated = bytes;
(*an)->used = 0;
(*an)->next = NULL;
void *mem = xcalloc(bytes, 1);
if(!mem) {
free((*an));
(*an) = NULL;
return -1;
}
(*an)->membase = mem;
(*an)->memcur = mem;
return 0;
}
int arena_init(arena **a, size_t bytes) {
if(!ISPOWOF2(MEM_ALIGN_BYTES))
XALLOC_EXIT("<arena_init> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to create a new arena");
(*a) = xcalloc(1, sizeof(**a));
if(!(*a))
return -1;
(*a)->start = NULL;
(*a)->current = NULL;
(*a)->node_memspace = bytes;
arenanode *base;
if(arenanode_init(&base, bytes) < 0) {
free(*a);
(*a) = NULL;
return -1;
}
(*a)->start = base;
(*a)->current = base;
return 0;
}
void* arena_alloc(arena * const arena, size_t bytes) {
if(!arena) {
errno = EINVAL;
return NULL;
}
if(bytes > arena->node_memspace) {
errno = ENOMEM;
return NULL;
}
if(!ISPOWOF2(MEM_ALIGN_BYTES))
XALLOC_EXIT("<arena_alloc> \"MEM_ALIGN_BYTES\" is not a power of 2. Refusing to allocate any memory");
if(bytes > ((arena->current)->allocated - (arena->current)->used)) {
arenanode *new;
if(arenanode_init(&new, arena->node_memspace) < 0)
return NULL;
arena->current->next = new;
arena->current = new;
}
size_t alignment = MEM_ALIGN(bytes);
size_t offset = bytes + alignment;
void *mem = arena->current->memcur;
arena->current->memcur = (void*)(((uint8_t*)arena->current->memcur) + offset);
arena->current->used += offset;
return mem;
// Note: This implementation assumes that malloc provides already-aligned memory. If it
// doesn't, that sucks and blows everything up
}
int arena_free(arena **arena) {
if(!arena) {
errno = EINVAL;
return -1;
}
if(!(*arena)) {
errno = EINVAL;
return -1;
}
(*arena)->current = (*arena)->start;
for(arenanode *n; (*arena)->current != NULL;) {
n = (*arena)->current->next;
free((*arena)->current->membase);
free((*arena)->current);
(*arena)->current = n;
}
free((*arena));
(*arena) = NULL;
return 0;
}
int arena_clear(arena **arena) {
if(!arena) {
errno = EINVAL;
return -1;
}
if(!(*arena)) {
errno = EINVAL;
return -1;
}
size_t bytes = (*arena)->node_memspace;
int ret = 0;
if((ret = arena_free(arena)) < 0)
return ret;
return arena_init(arena, bytes);
}
// Simple Arena is an arena that can't expand whenever allocating memory, meaning what you originally allocated is what you get
typedef arena simplearena;
int simplearena_init(simplearena **a, size_t bytes) {
return arena_init(a, bytes);
}
void* simplearena_alloc(simplearena * const a, size_t bytes) {
// The criteria to allocate new memory in arena_alloc is 'bytes > ((a->current)->allocated - (a->current)->used)', so if this
// is true, just return NULL & set errno
if(!a) {
errno = EINVAL;
return NULL;
}
if(bytes > ((a->current)->allocated - (a->current)->used)) {
errno = ENOMEM;
return NULL;
}
return arena_alloc(a, bytes);
}
int simplearena_free(simplearena **a) {
return arena_free(a);
}
int simplearena_clear(simplearena **a) {
return arena_clear(a);
}
|