-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddy_allocator.c
242 lines (208 loc) · 6.21 KB
/
buddy_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
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
// O(log N) allocation and deallocation
// 1/4 memory wasted on average, best fit
// 2 pointer header, 16/8 byte on 64/32-bit
// 16/8 byte min allocation on 64/32-bit
// cannot be expanded at runtime
#include <stdint.h> // intptr_t
#include <string.h> // memcpy
#include <assert.h>
union node {
struct usednode {
intptr_t free;
intptr_t size;
};
struct freenode {
union node *next;
union node *prev;
};
};
struct heap {
void *memory;
int capacity;
union node freelists[32];
};
int ceillog2(int x) {
int log2 = 0;
while ((1 << log2) < x)
++log2;
return log2;
}
void initialize(struct heap *heap, void *memory, int capacity) {
// capacity must be a power of 2
assert(capacity > 0 && (capacity & (capacity - 1)) == 0);
heap->memory = memory;
heap->capacity = capacity;
for (int i = 0; i < 32; ++i) {
union node *list = &heap->freelists[i];
list->next = list;
list->prev = list;
}
int available = capacity - sizeof(union node);
int log2 = ceillog2(available);
union node *list = &heap->freelists[log2];
union node *node = memory;
list->next = node;
list->prev = node;
node->next = list;
node->prev = list;
}
void *allocate(struct heap *heap, int size) {
// you could clamp to 0, or return NULL
assert(size >= 0);
int needed = size + sizeof(union node);
for (int log2 = ceillog2(needed); log2 < 32; ++log2) {
union node *list = &heap->freelists[log2];
if (list->next == list)
continue;
union node *node = list->next;
list->next = node->next;
list->next->prev = list;
assert(node->free);
// split node to smallest size that fits
while ((1 << (log2 - 1)) >= needed) {
--log2;
void *memory = (char *)node + ((intptr_t)1 << log2);
union node *buddy = memory;
list = &heap->freelists[log2];
buddy->next = list->next;
buddy->prev = list;
list->next->prev = buddy;
list->next = buddy;
}
node->free = 0;
node->size = (intptr_t)1 << log2;
return (char *)node + sizeof(union node);
}
return 0;
}
void deallocate(struct heap *heap, void *block) {
if (!block)
return;
assert(block >= heap->memory); // block isn't from this heap
void *header = (char *)block - sizeof(union node);
union node *node = header;
assert(!node->free); // double free
assert((char *)node + node->size <= (char *)heap->memory + heap->capacity); // block isn't from this heap.
// combine neighboring free nodes
while (node->size < heap->capacity) {
// the buddy node is always just a bitflip away
uintptr_t base = (uintptr_t)heap->memory;
uintptr_t nodep = (uintptr_t)node - base;
uintptr_t buddyp = nodep ^ node->size;
union node *buddy = (union node *)(buddyp + base);
if (!buddy->free)
break;
buddy->next->prev = buddy->prev;
buddy->prev->next = buddy->next;
intptr_t size = node->size;
node = node < buddy ? node : buddy;
node->size = 2 * size;
}
int log2 = ceillog2((int)node->size);
union node *list = &heap->freelists[log2];
node->next = list->next;
node->prev = list;
list->next->prev = node;
list->next = node;
}
void *reallocate(struct heap *heap, void *block, int size) {
// you could clamp to 0, or return NULL
assert(size >= 0);
if (!block)
return allocate(heap, size);
if (!size) {
deallocate(heap, block);
return 0;
}
assert(block >= heap->memory); // block isn't from this heap
void *header = (char *)block - sizeof(union node);
union node *node = header;
assert(!node->free); // double free
assert((char *)node + node->size <= (char *)heap->memory + heap->capacity); // block isn't from this heap.
int needed = size + sizeof(union node);
if (needed > node->size) {
if (needed > heap->capacity)
return 0; // allocation doesn't fit in the heap
// try to merge with neighboring free buddies
int oldsize = (int)node->size;
for (;;) {
// we can only merge with the buddy if we are the "left" buddy
uintptr_t base = (uintptr_t)heap->memory;
uintptr_t nodep = (uintptr_t)node - base;
if (nodep & node->size)
break; // we are the "right" buddy so we can't merge
uintptr_t buddyp = nodep ^ node->size;
union node *buddy = (union node *)(buddyp + base);
if (!buddy->free)
break; // buddy isn't free so we can't merge
// ok we can merge with this buddy
buddy->next->prev = buddy->prev;
buddy->prev->next = buddy->next;
node->size *= 2;
if (node->size >= needed)
return block;
}
// we couldn't reallocate in-place so undo any growth we've done
while (node->size > oldsize) {
node->size /= 2;
void *memory = (char *)node + node->size;
union node *buddy = memory;
buddy->free = 1;
buddy->size = node->size;
// add buddy back to the freelist
int log2 = ceillog2((int)node->size);
union node *list = &heap->freelists[log2];
buddy->next = list->next;
buddy->prev = list;
list->next->prev = buddy;
list->next = buddy;
}
// make a new allocation and copy the old one
void *copy = allocate(heap, size);
if (!copy)
return 0; // out of memory
memcpy(copy, block, (size_t)node->size);
deallocate(heap, block);
return copy;
}
else {
// split off as many buddies from the node as we can
int log2 = ceillog2((int)node->size);
while ((1 << (log2 - 1)) >= needed) {
--log2;
void *memory = (char *)node + ((intptr_t)1 << log2);
union node *buddy = memory;
union node *list = &heap->freelists[log2];
buddy->next = list->next;
buddy->prev = list;
list->next->prev = buddy;
list->next = buddy;
}
return block;
}
}
int main(void) {
static char memory[1024];
struct heap heap;
initialize(&heap, memory, sizeof memory);
char *a = allocate(&heap, 256); memset(a, 1, 256);
char *b = allocate(&heap, 256); memset(b, 1, 256);
deallocate(&heap, a);
char *c = allocate(&heap, 256); memset(c, 1, 256);
deallocate(&heap, c);
deallocate(&heap, b);
char *d = allocate(&heap, 0); memset(d, 1, 0);
char *e = allocate(&heap, 1); memset(e, 1, 1);
char *f = allocate(&heap, 2); memset(f, 1, 2);
char *g = allocate(&heap, 3); memset(g, 1, 3);
char *h = allocate(&heap, 4); memset(h, 1, 4);
char *i = allocate(&heap, 5); memset(i, 1, 5);
d = reallocate(&heap, d, 256); memset(d, 1, 256);
i = reallocate(&heap, i, 100); memset(i, 1, 100);
deallocate(&heap, d);
deallocate(&heap, i);
deallocate(&heap, e);
deallocate(&heap, h);
deallocate(&heap, f);
deallocate(&heap, g);
}