-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyalloc.c
316 lines (283 loc) · 7.13 KB
/
tinyalloc.c
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
* SPDX-License-Identifier: GPL-2.0
*/
#include <stdlib.h>
#include <string.h>
#include "tinyalloc.h"
#define BLK_BASE TINYALLOC_BLK_BASE
#define FREELIST_MAX TINYALLOC_FREELIST_MAX
struct meta {
int size; // sizeof(meta->data)
char data[0];
};
#define META_DATAPTR(m) ((m)->data)
#define META_DATASIZE(m) ((m)->size)
#define META_FULLSIZE(m) ((m)->size + sizeof(struct meta))
#define FREE_NEXT(m) (*(void **)(m)->data)
#define FREE_HEAD(root, i) ((root)->freelist[i])
#define FREE_RESET(fl) (freelist_reset(fl, ARRAYSIZE(fl)))
#define N1024 1024
struct chunk {
int pos;
int size; // sizeof(chunk->mem)
union {
struct chunk *next;
double __x; // align(struct chunk) to 8bytes if compiler is 32bit
};
char mem[0];
};
#define the_base(ator) (&ator->base)
#define chk_next(chk) ((chk)->next)
#define chk_head(base) ((base)->chunk_head)
#define chk_dataptr(chk) ((chk)->mem + (chk)->pos)
static inline void chunk_add(struct chunk *chk, struct allocator_base *base)
{
chk_next(chk) = chk_head(base);
chk_head(base) = chk;
}
static struct chunk *chunk_new(int k, int metasize)
{
struct chunk *chk = rt_malloc(N1024 * k);
if (!chk)
return NULL;
chk->size = N1024 * k - sizeof(struct chunk);
chk_next(chk) = NULL;
int align = ((size_t)chk->mem + metasize) & (BLK_BASE - 1);
chk->pos = align ? BLK_BASE - align : 0;
return chk;
}
static struct chunk *chunk_pickup(struct allocator_base *base, int size)
{
struct chunk *prev = NULL;
struct chunk *chk = chk_head(base);
while (chk) {
if (chk->pos + size <= chk->size)
break;
prev = chk;
chk = chk_next(chk);
}
if (!chk) {
int chksize = size + (sizeof(struct chunk) + BLK_BASE + (N1024 - 1));
chk = chunk_new(chksize / N1024 <= base->chksize ? base->chksize : chksize / N1024, base->metasize);
if (chk)
chunk_add(chk, base);
} else if (prev) {
// moves current "chk" to top
chk_next(prev) = chk_next(chk);
chunk_add(chk, base);
}
return chk;
}
static void freelist_reset(void **freelist, int max)
{
for (int i = 0; i < max; i++)
freelist[i] = NULL;
}
static inline int freelist_index(unsigned int size)
{
int i = size / BLK_BASE - 1;
return i < FREELIST_MAX ? i : FREELIST_MAX;
}
static struct meta *freelist_get(struct tinyalloc_root *root, int size)
{
int i = freelist_index(size);
struct meta *curr = FREE_HEAD(root, i);
if (curr && i < FREELIST_MAX) {
FREE_HEAD(root, i) = FREE_NEXT(curr);
return curr;
}
struct meta *prev = NULL;
while (curr) {
int full = META_FULLSIZE(curr);
if (full < size) {
prev = curr;
curr = FREE_NEXT(curr);
continue;
}
if (full >= size + (BLK_BASE * (int)ARRAYSIZE(root->freelist))) { // Do Splits
struct meta *next = (struct meta *)((char *)curr + size);
META_DATASIZE(curr) = size - sizeof(struct meta);
META_DATASIZE(next) = full - sizeof(struct meta) - size;
FREE_NEXT(next) = FREE_NEXT(curr);
FREE_NEXT(curr) = next;
}
if (prev) {
FREE_NEXT(prev) = FREE_NEXT(curr);
} else {
FREE_HEAD(root, i) = FREE_NEXT(curr);
}
break;
}
return curr;
}
void tinyalloc_init(struct tinyalloc_root *root, int chksize)
{
if (chksize < 8)
chksize = 8;
root->base = (struct allocator_base){
.chksize = chksize,
.metasize = sizeof(struct meta),
.chunk_head = NULL
};
FREE_RESET(root->freelist);
}
void *tinyalloc(struct tinyalloc_root *root, int size)
{
if (size < (16 - sizeof(struct meta))) {
size = 16;
} else {
size = ALIGN_POW2(size + sizeof(struct meta), BLK_BASE);
}
struct meta *meta = freelist_get(root, size);
if (meta)
return META_DATAPTR(meta);
struct chunk *chk = chunk_pickup(the_base(root), size);
if (!chk)
return NULL;
meta = (struct meta *)chk_dataptr(chk);
META_DATASIZE(meta) = size - sizeof(struct meta);
chk->pos += size;
return META_DATAPTR(meta);
}
void tinyfree(struct tinyalloc_root *root, void *ptr)
{
if (NOT_ALIGNED((size_t)ptr, BLK_BASE))
return;
struct meta *meta = container_of(ptr, struct meta, data);
int i = freelist_index(META_FULLSIZE(meta));
FREE_NEXT(meta) = FREE_HEAD(root, i);
FREE_HEAD(root, i) = meta;
}
static void chunks_reset(struct allocator_base *base)
{
struct chunk *chk = chk_head(base);
while (chk) {
int align = ((size_t)chk->mem + base->metasize) & (BLK_BASE - 1);
chk->pos = align ? BLK_BASE - align : 0;
chk = chk_next(chk);
}
}
static void chunks_destroy(struct allocator_base *base)
{
struct chunk *chk = chk_head(base);
struct chunk *next;
while (chk) {
next = chk_next(chk);
rt_free(chk);
chk = next;
}
chk_head(base) = NULL;
}
void tinyreset(struct tinyalloc_root *root)
{
chunks_reset(the_base(root));
FREE_RESET(root->freelist);
}
void tinydestroy(struct tinyalloc_root *root)
{
chunks_destroy(the_base(root));
FREE_RESET(root->freelist);
}
/**
*
* bump allocator
*
*/
void bumpalloc_init(struct bumpalloc_root *bump, int chksize)
{
if (chksize <= 0)
chksize = 1;
bump->base = (struct allocator_base){
.chksize = chksize,
.metasize = 0,
.chunk_head = NULL
};
}
void *bumpalloc(struct bumpalloc_root *bump, int size)
{
if (size < BLK_BASE) {
size = BLK_BASE;
} else {
size = ALIGN_POW2(size, BLK_BASE);
}
struct chunk *chk = chunk_pickup(the_base(bump), size);
if (!chk)
return NULL;
char *ptr = chk_dataptr(chk);
chk->pos += size;
return ptr;
}
void bumpreset(struct bumpalloc_root *bump)
{
chunks_reset(the_base(bump));
}
void bumpdestroy(struct bumpalloc_root *bump)
{
chunks_destroy(the_base(bump));
}
/**
*
* fixed allocator
*
*/
#define FIXED_NEXT(ptr) (*(void **)(ptr))
#define FIXED_HEAD(fixed) ((fixed)->freelist[0])
void fixedalloc_init(struct fixedalloc_root *fixed, int chksize, int size)
{
if (size < BLK_BASE) {
size = BLK_BASE;
} else {
size = ALIGN_POW2(size, BLK_BASE);
}
fixed->size = size;
if (chksize <= 0)
chksize = 1;
fixed->base = (struct allocator_base){
.chksize = chksize,
.metasize = 0,
.chunk_head = NULL
};
FIXED_HEAD(fixed) = NULL;
}
void *fixedalloc(struct fixedalloc_root *fixed)
{
void *result = FIXED_HEAD(fixed);
if (result) {
FIXED_HEAD(fixed) = FIXED_NEXT(result);
return result;
}
const int size = fixed->size;
struct chunk *chk = chunk_pickup(the_base(fixed), size);
if (!chk)
return NULL;
result = chk_dataptr(chk);
chk->pos += size;
// pre-allocation
char *ptr = chk_dataptr(chk);
for (int i = 0; i < 32; i++) {
if (chk->pos + size > chk->size)
break;
FIXED_NEXT(ptr) = FIXED_HEAD(fixed);
FIXED_HEAD(fixed) = ptr;
ptr += size;
chk->pos += size;
}
return result;
}
void fixedfree(struct fixedalloc_root *fixed, void *ptr)
{
if (NOT_ALIGNED((size_t)ptr, BLK_BASE))
return;
FIXED_NEXT(ptr) = FIXED_HEAD(fixed);
FIXED_HEAD(fixed) = ptr;
}
void fixedreset(struct fixedalloc_root *fixed)
{
chunks_reset(the_base(fixed));
FIXED_HEAD(fixed) = NULL;
}
void fixeddestroy(struct fixedalloc_root *fixed)
{
chunks_destroy(the_base(fixed));
FIXED_HEAD(fixed) = NULL;
}