-
Notifications
You must be signed in to change notification settings - Fork 0
/
bistack_allocator.c
190 lines (169 loc) · 7.79 KB
/
bistack_allocator.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
#include <stdint.h> // uintptr_t
#include <string.h> // memcpy - only needed for realloc
struct allocator {
void *buffer;
int capacity;
int lcursor;
int rcursor;
};
void *allocate_left(struct allocator *allocator, int size, int alignment) {
uintptr_t mask = (uintptr_t)alignment - 1; // Alignment must be a power of 2.
uintptr_t unaligned = (uintptr_t)allocator->buffer + allocator->lcursor;
uintptr_t aligned = (unaligned + mask) & ~mask;
int new_lcursor = allocator->lcursor + size + (int)(aligned - unaligned);
if (new_lcursor >= allocator->capacity - allocator->rcursor)
return 0;
allocator->lcursor = new_lcursor;
return (void *)aligned;
}
void *allocate_right(struct allocator *allocator, int size, int alignment) {
uintptr_t mask = (uintptr_t)alignment - 1; // Alignment must be a power of 2.
uintptr_t unaligned = (uintptr_t)allocator->buffer + allocator->capacity - allocator->rcursor - size - alignment;
uintptr_t aligned = (unaligned + alignment) & ~mask;
int new_rcursor = allocator->rcursor + size + (int)(unaligned + alignment - aligned);
if (allocator->lcursor >= allocator->capacity - new_rcursor)
return 0;
allocator->rcursor = new_rcursor;
return (void *)aligned;
}
void deallocate_left(struct allocator *allocator, void *block, int size) {
if ((char *)block + size == (char *)allocator->buffer + allocator->lcursor)
allocator->lcursor -= size;
}
void deallocate_right(struct allocator *allocator, void *block, int size) {
if (block == (char *)allocator->buffer + allocator->capacity - allocator->rcursor)
allocator->rcursor -= size;
}
void *reallocate_left(struct allocator *allocator, void *block, int old_size, int new_size, int alignment) {
uintptr_t mask = (uintptr_t)alignment - 1;
if ((char *)block + old_size == (char *)allocator->buffer + allocator->lcursor && ((uintptr_t)block & mask) == 0) {
int new_lcursor = allocator->lcursor + new_size - old_size;
if (new_lcursor >= allocator->capacity - allocator->rcursor)
return 0;
allocator->lcursor = new_lcursor;
return block;
}
void *result = allocate_left(allocator, new_size, alignment);
if (result) {
int to_copy = new_size < old_size ? new_size : old_size;
memcpy(result, block, (size_t)to_copy);
}
return result;
}
void *reallocate_right(struct allocator *allocator, void *block, int old_size, int new_size, int alignment) {
uintptr_t mask = (uintptr_t)alignment - 1;
if (block == (char *)allocator->buffer + allocator->capacity - allocator->rcursor && ((uintptr_t)block & mask) == 0) {
int new_rcursor = allocator->rcursor + new_size - old_size;
if (allocator->lcursor >= allocator->capacity - new_rcursor)
return 0;
allocator->rcursor = new_rcursor;
return block;
}
void *result = allocate_right(allocator, new_size, alignment);
if (result) {
int to_copy = new_size < old_size ? new_size : old_size;
memcpy(result, block, (size_t)to_copy);
}
return result;
}
#include <assert.h>
int main(void) {
{
struct allocator allocator = { 0 };
assert(!allocate_left(&allocator, 1, 1));
assert(!allocate_right(&allocator, 1, 1));
assert(!reallocate_left(&allocator, 0, 0, 1, 1));
assert(!reallocate_right(&allocator, 0, 0, 1, 1));
deallocate_left(&allocator, NULL, 0);
deallocate_right(&allocator, NULL, 0);
}
{
_Alignas(16) char buffer[17];
struct allocator allocator = { .buffer = buffer, .capacity = sizeof buffer };
char *c = allocate_left(&allocator, sizeof(char), _Alignof(char));
short *s = allocate_left(&allocator, sizeof(short), _Alignof(short));
int *i = allocate_left(&allocator, sizeof(int), _Alignof(int));
long long *l = allocate_left(&allocator, sizeof(long long), _Alignof(long long));
long long *null = allocate_left(&allocator, sizeof(long long), _Alignof(long long));
assert(c && (uintptr_t)c % _Alignof(char) == 0);
assert(s && (uintptr_t)s % _Alignof(short) == 0);
assert(i && (uintptr_t)i % _Alignof(int) == 0);
assert(l && (uintptr_t)l % _Alignof(long long) == 0);
assert(!null);
}
{
_Alignas(16) char buffer[23];
struct allocator allocator = { .buffer = buffer, .capacity = sizeof buffer };
char *c = allocate_right(&allocator, sizeof(char), _Alignof(char));
short *s = allocate_right(&allocator, sizeof(short), _Alignof(short));
int *i = allocate_right(&allocator, sizeof(int), _Alignof(int));
long long *l = allocate_right(&allocator, sizeof(long long), _Alignof(long long));
long long *null = allocate_right(&allocator, sizeof(long long), _Alignof(long long));
assert(c && (uintptr_t)c % _Alignof(char) == 0);
assert(s && (uintptr_t)s % _Alignof(short) == 0);
assert(i && (uintptr_t)i % _Alignof(int) == 0);
assert(l && (uintptr_t)l % _Alignof(long long) == 0);
assert(!null);
}
{
_Alignas(16) char buffer[40];
struct allocator allocator = { .buffer = buffer, .capacity = sizeof buffer };
char *lc = allocate_left(&allocator, sizeof(char), _Alignof(char));
char *rc = allocate_right(&allocator, sizeof(char), _Alignof(char));
short *ls = allocate_left(&allocator, sizeof(short), _Alignof(short));
short *rs = allocate_right(&allocator, sizeof(short), _Alignof(short));
int *li = allocate_left(&allocator, sizeof(int), _Alignof(int));
int *ri = allocate_right(&allocator, sizeof(int), _Alignof(int));
long long *ll = allocate_left(&allocator, sizeof(long long), _Alignof(long long));
long long *rl = allocate_right(&allocator, sizeof(long long), _Alignof(long long));
long long *lnull = allocate_left(&allocator, sizeof(long long), _Alignof(long long));
long long *rnull = allocate_right(&allocator, sizeof(long long), _Alignof(long long));
assert(lc && (uintptr_t)lc % _Alignof(char) == 0);
assert(rc && (uintptr_t)rc % _Alignof(char) == 0);
assert(ls && (uintptr_t)ls % _Alignof(short) == 0);
assert(rs && (uintptr_t)rs % _Alignof(short) == 0);
assert(li && (uintptr_t)li % _Alignof(int) == 0);
assert(ri && (uintptr_t)ri % _Alignof(int) == 0);
assert(ll && (uintptr_t)ll % _Alignof(long long) == 0);
assert(rl && (uintptr_t)rl % _Alignof(long long) == 0);
assert(!lnull);
assert(!rnull);
}
{
char buffer[3];
struct allocator allocator = { .buffer = buffer, .capacity = sizeof buffer };
char *l = allocate_left(&allocator, 1, 1);
char *r = allocate_right(&allocator, 1, 1);
assert(l && r && l != r);
}
{
_Alignas(8) char buffer[17];
struct allocator allocator = { .buffer = buffer + 1, .capacity = sizeof buffer - 1 };
char *c = reallocate_left(&allocator, NULL, 0, sizeof(char), _Alignof(char));
assert(c && (uintptr_t)c % _Alignof(char) == 0);
short *s = reallocate_left(&allocator, c, sizeof(char), sizeof(short), _Alignof(short));
assert(s && (uintptr_t)s % _Alignof(short) == 0);
int *i = reallocate_left(&allocator, s, sizeof(short), sizeof(int), _Alignof(int));
assert(i && (uintptr_t)i % _Alignof(int) == 0);
long *l = reallocate_left(&allocator, i, sizeof(int), sizeof(long long), _Alignof(long long));
assert(l && (uintptr_t)l % _Alignof(long long) == 0);
int mark = allocator.lcursor;
deallocate_left(&allocator, l, sizeof(long long));
assert(mark - allocator.lcursor >= sizeof(long long));
}
{
_Alignas(8) char buffer[32];
struct allocator allocator = { .buffer = buffer, .capacity = sizeof buffer };
char *c = reallocate_right(&allocator, NULL, 0, sizeof(char), _Alignof(char));
assert(c && (uintptr_t)c % _Alignof(char) == 0);
short *s = reallocate_right(&allocator, c, sizeof(char), sizeof(short), _Alignof(short));
assert(s && (uintptr_t)s % _Alignof(short) == 0);
int *i = reallocate_right(&allocator, s, sizeof(short), sizeof(int), _Alignof(int));
assert(i && (uintptr_t)i % _Alignof(int) == 0);
long *l = reallocate_right(&allocator, i, sizeof(int), sizeof(long long), _Alignof(long long));
assert(l && (uintptr_t)l % _Alignof(long long) == 0);
int mark = allocator.rcursor;
deallocate_right(&allocator, l, sizeof(long long));
assert(mark - allocator.rcursor >= sizeof(long long));
}
}