-
Notifications
You must be signed in to change notification settings - Fork 0
/
AU.c
415 lines (342 loc) · 8.99 KB
/
AU.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include "AU.h"
#include "AUConf.h"
#define ISSUE_ERROR(err) xerror(err, # err)
/////////////////////////
//// Alignment Utils ////
/////////////////////////
union AlignmentType {
int i;
long l;
long long ll;
void *vp;
void (*funp)(void);
float f;
double d;
long double ld;
float *fp;
double *dp;
long double *ldp;
};
enum {
ALIGNMENT_BOUNDARY = sizeof (union AlignmentType),
};
static inline size_t
AlignSize(size_t n, size_t boundary) {
assert(boundary > 1);
assert(n <= SIZE_MAX - boundary + 1);
assert(n > 0);
size_t result = ((n - 1) + boundary)/boundary * boundary;
assert(result >= n);
return result;
}
#define PTR_SIZE_ALIGN AlignSize(sizeof (void*), ALIGNMENT_BOUNDARY)
static inline size_t
maxsz(size_t a, size_t b) {
return a < b ? b : a;
}
//////////////////////
//// BYTE Builder ////
//////////////////////
#ifndef NDEBUG
#define ASSERT_VALID_B1(b1) \
do { \
assert(b1); \
assert((b1)->mem); \
assert((b1)->used <= (b1)->cap); \
assert((b1)->cap > 0); \
} while (0)
#else
#define ASSERT_VALID_B1(b1)
#endif
int
AU_B1_Setup(AU_ByteBuilder *b1, size_t cap) {
assert(cap > 0);
b1->cap = cap;
b1->used = 0;
b1->mem = xmalloc(cap);
if (!b1->mem) {
ISSUE_ERROR(AU_ERR_XMALLOC);
return AU_ERR_XMALLOC;
}
ASSERT_VALID_B1(b1);
return 0;
}
int
AU_B1_Append(AU_ByteBuilder *b1, const void *mem, size_t size) {
ASSERT_VALID_B1(b1);
assert(mem);
void *out_addr = AU_B1_AppendForSetup(b1, size);
if (!out_addr) {
return -1;
}
memcpy(out_addr, mem, size);
return 0;
}
void *
AU_B1_AppendForSetup(AU_ByteBuilder *b1, size_t size) {
ASSERT_VALID_B1(b1);
if (b1->used > SIZE_MAX - size) {
ISSUE_ERROR(AU_ERR_OVERFLOW);
return 0;
}
if (b1->used + size > b1->cap) {
if (b1->cap == SIZE_MAX || b1->cap > SIZE_MAX - size) {
// This seems so absurd...
ISSUE_ERROR(AU_ERR_OVERFLOW);
return 0;
}
size_t new_cap = b1->cap > SIZE_MAX/2
? SIZE_MAX
: maxsz(b1->cap*2, size);
void *p = xrealloc(b1->mem, new_cap);
if (!p) {
ISSUE_ERROR(AU_ERR_XREALLOC);
return 0;
}
b1->mem = p;
b1->cap = new_cap;
}
void *out_addr = (char*)b1->mem + b1->used;
b1->used += size;
return out_addr;
}
void *
AU_B1_GetMemory(const AU_ByteBuilder *b1) {
ASSERT_VALID_B1(b1);
return b1->mem;
}
void
AU_B1_DiscardAppends(AU_ByteBuilder *b1) {
ASSERT_VALID_B1(b1);
b1->used = 0;
}
void
AU_B1_DiscardLastBytes(AU_ByteBuilder *b1, size_t n) {
ASSERT_VALID_B1(b1);
assert(b1->used >= n);
b1->used -= n;
}
size_t
AU_B1_GetUsedCount(AU_ByteBuilder *b1) {
return b1->used;
}
////////////////////////////
//// Fixed Size Builder ////
////////////////////////////
#ifndef NDEBUG
#define ASSERT_VALID_FSB(fsb) \
do { \
assert(fsb); \
assert((fsb)->elt_size > 0); \
assert((fsb)->elt_size <= SIZE_MAX / (fsb)->b1.cap); \
ASSERT_VALID_B1(&(fsb)->b1); \
} while (0)
#else
#define ASSERT_VALID_FSB(fsb)
#endif
int
AU_FSB_Setup(AU_FixedSizeBuilder *fsb, size_t elt_size, size_t cap) {
assert(cap > 0);
assert(elt_size > 0);
assert(cap <= SIZE_MAX/elt_size);
int b1_res = AU_B1_Setup(&fsb->b1, elt_size*cap);
if (b1_res < 0) {
return b1_res;
}
fsb->elt_size = elt_size;
ASSERT_VALID_FSB(fsb);
return 0;
}
int
AU_FSB_Append(AU_FixedSizeBuilder *fsb, const void *mem, size_t n) {
ASSERT_VALID_FSB(fsb);
assert(mem);
if (n != 0 && fsb->elt_size > SIZE_MAX/n) {
ISSUE_ERROR(AU_ERR_OVERFLOW);
return AU_ERR_OVERFLOW;
}
int res = AU_B1_Append(&fsb->b1, mem, n*fsb->elt_size);
if (res < 0) {
return res;
}
return 0;
}
void *
AU_FSB_AppendForSetup(AU_FixedSizeBuilder *fsb, size_t n) {
ASSERT_VALID_FSB(fsb);
if (n != 0 && fsb->elt_size > SIZE_MAX/n) {
ISSUE_ERROR(AU_ERR_OVERFLOW);
return 0;
}
void *mem = AU_B1_AppendForSetup(&fsb->b1, n*fsb->elt_size);
if (!mem) {
return 0;
}
return mem;
}
void *
AU_FSB_GetMemory(AU_FixedSizeBuilder *fsb) {
ASSERT_VALID_FSB(fsb);
return AU_B1_GetMemory(&fsb->b1);
}
void
AU_FSB_DiscardAppends(AU_FixedSizeBuilder *fsb) {
ASSERT_VALID_FSB(fsb);
AU_B1_DiscardAppends(&fsb->b1);
}
void
AU_FSB_DiscardLastAppends(AU_FixedSizeBuilder *fsb, size_t n) {
ASSERT_VALID_FSB(fsb);
assert(n == 0 || fsb->elt_size <= SIZE_MAX/n);
assert(fsb->b1.used/fsb->elt_size >= n);
AU_B1_DiscardLastBytes(&fsb->b1, n*fsb->elt_size);
}
size_t
AU_FSB_GetUsedCount(AU_FixedSizeBuilder *fsb) {
return fsb->b1.used/fsb->elt_size;
}
///////////////////////////////
//// Variable Size Builder ////
///////////////////////////////
int
AU_VSB_Setup(AU_VarSizeBuilder *vsb, size_t cap, size_t align) {
vsb->align = align == AU_ALIGN_CONSERVATIVE ? ALIGNMENT_BOUNDARY : align;
return AU_B1_Setup(&vsb->b1, AlignSize(cap, vsb->align));
}
int
AU_VSB_Append(AU_VarSizeBuilder *vsb, const void *mem, size_t n) {
return AU_B1_Append(&vsb->b1, mem, AlignSize(n, vsb->align));
}
void *
AU_VSB_AppendForSetup(AU_VarSizeBuilder *vsb, size_t n) {
return AU_B1_AppendForSetup(&vsb->b1, AlignSize(n, vsb->align));
}
void *
AU_VSB_GetMemory(AU_VarSizeBuilder *vsb) {
return AU_B1_GetMemory(&vsb->b1);
}
void
AU_VSB_DiscardAppends(AU_VarSizeBuilder *vsb) {
AU_B1_DiscardAppends(&vsb->b1);
}
size_t
AU_VSB_GetUsedCount(AU_VarSizeBuilder *vsb) {
return AU_B1_GetUsedCount(&vsb->b1);
}
//////////////////////////////
//// Fixed Size Allocator ////
//////////////////////////////
enum {
// How many pointers to initially allocate for a FSA.
FSA_INITIAL_NUM_PTRS = 4
};
/*
* The idea here is to allocate blocks of about N = sizeof (void*) + elt_size
* bytes. When capacity limit is reached, total_cap + <some_delta> blocks of N
* bytes are allocated, and the pointer to its first byte is appended to
* fsb_base_ptrs so we know where they are in order to free them later.
*
* Having a block of N bytes, we can store a pointer and the bytes for the
* element. If we establish that the first bytes will be for the void* and the
* remaining bytes will be for the element, we can start doing something
* interesting. This is because what we would have would be pretty much a
* memory layout for a linked list. And then, we could just proceed on building
* our own FSA as expected.
*
* We can't simply use a builder here for the *whole* allocator because it
* could invalidate pointers on an allocation, which would be implemented as
* some AppendForSetup call.
*/
inline static size_t
AU_FSA_NewCap(size_t cap) {
// About 1/3 more than it was.
size_t delta = cap/3 + 1;
return cap > SIZE_MAX - delta ? SIZE_MAX : cap + delta;
}
static int
AU_FSA_Expand(AU_FixedSizeAllocator *fsa, size_t new_cap) {
assert(new_cap > fsa->total_cap);
size_t elt_alsize = AlignSize(fsa->elt_size, ALIGNMENT_BOUNDARY);
size_t node_alsize = PTR_SIZE_ALIGN + elt_alsize;
if (node_alsize > SIZE_MAX/new_cap) {
ISSUE_ERROR(AU_ERR_OVERFLOW);
return AU_ERR_OVERFLOW;
}
char *mem = xmalloc(node_alsize*new_cap);
if (!mem) {
ISSUE_ERROR(AU_ERR_XMALLOC);
return AU_ERR_XMALLOC;
}
// Appending the pointer into fsb_base_ptrs so we remember it later when
// we need to destroy this allocator.
int res = AU_FSB_Append(&fsa->fsb_base_ptrs, &mem, 1);
if (res < 0) {
xfree(mem);
return res;
}
// Set up the free list. Make each node point to the next. Last node will
// point to the old head.
void *old_head = fsa->free_head;
fsa->free_head = mem;
assert(new_cap > 0);
for (size_t i = 0; i < new_cap-1; i++) {
*(void**)mem = mem + node_alsize;
mem += node_alsize;
}
*(void**)mem = old_head; // Set up last node.
fsa->total_cap = new_cap;
return 0;
}
int
AU_FSA_Setup(AU_FixedSizeAllocator *fsa, size_t elt_size, size_t cap) {
assert(cap > 0);
assert(elt_size > 0);
assert(cap <= SIZE_MAX/elt_size);
// Basic setup and allocation of an initial memory region.
fsa->total_cap = 0;
fsa->elt_size = elt_size;
fsa->free_head = 0;
int res = AU_FSB_Setup(&fsa->fsb_base_ptrs,
sizeof (void*),
FSA_INITIAL_NUM_PTRS);
if (res < 0) {
return res;
}
return AU_FSA_Expand(fsa, cap);
}
void *
AU_FSA_Alloc(AU_FixedSizeAllocator *fsa) {
if (!fsa->free_head) {
if (fsa->total_cap == SIZE_MAX) {
// Seriously?
ISSUE_ERROR(AU_ERR_OVERFLOW);
return 0;
}
if (AU_FSA_Expand(fsa, AU_FSA_NewCap(fsa->total_cap)) < 0) {
return 0;
}
}
char *free_head = fsa->free_head;
void *new_free_head = *(void**)free_head;
void *out = free_head + PTR_SIZE_ALIGN;
fsa->free_head = new_free_head;
return out;
}
void
AU_FSA_Free(AU_FixedSizeAllocator *fsa, void *mem) {
void *node = (char*)mem - PTR_SIZE_ALIGN;
*(void**)node = fsa->free_head;
fsa->free_head = node;
}
void
AU_FSA_Destroy(AU_FixedSizeAllocator *fsa) {
void **regions = AU_FSB_GetMemory(&fsa->fsb_base_ptrs);
size_t used = AU_FSB_GetUsedCount(&fsa->fsb_base_ptrs);
for (size_t i = 0; i < used; i++) {
xfree(regions[i]);
}
xfree(regions);
}