forked from cloudwu/buddy
/
buddy.c
207 lines (189 loc) · 4.23 KB
/
buddy.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
#include "buddy.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
struct node {
int left;
int right;
};
struct buddy {
int size;
int free;
struct node tree[1];
};
struct buddy *
buddy_new(int level) {
int size = 1 << level;
struct buddy * self = malloc(sizeof(struct buddy) + sizeof(struct node) * (size * 2 - 2));
self->size = size;
self->free = 1;
int i;
for (i=1;i<size * 2 - 2;i++) {
self->tree[i].left = i+1;
}
self->tree[size*2-2].left = -1;
self->tree[0].left = self->tree[0].right = 0;
return self;
}
void
buddy_delete(struct buddy * self) {
free(self);
}
static inline int
_alloc_leaf(struct buddy * self) {
int ret = self->free;
self->free = self->tree[self->free].left;
self->tree[ret].left = self->tree[ret].right = 0;
return ret;
}
static inline void
_free_leaf(struct buddy * self, int index) {
self->tree[index].left = self->free;
self->free = index;
}
static inline int
is_pow_of_2(uint32_t x) {
return !(x & (x-1));
}
static inline uint32_t
next_pow_of_2(uint32_t x) {
if ( is_pow_of_2(x) )
return x;
x |= x>>1;
x |= x>>2;
x |= x>>4;
x |= x>>8;
x |= x>>16;
return x+1;
}
static int
_alloc(struct buddy * self, int index, int start, int length, int size) {
if (size > length)
return -1;
struct node * current = &(self->tree[index]);
if (size == length) {
if (current->left == 0 && current->right == 0) {
current->left = -1;
return start;
}
return -1;
}
if (current->left != 0) {
int r = _alloc(self, current->left, start, length/2 , size);
if (r>=0)
return r;
}
if (current->right != 0) {
int r = _alloc(self, current->right, start + length/2 , length/2 , size);
if (r>=0)
return r;
}
if (current->left == 0) {
int index = _alloc_leaf(self);
current->left = index;
return _alloc(self, index, start, length/2 , size);
}
if (current->right == 0) {
int index = _alloc_leaf(self);
current->right = index;
return _alloc(self, index, start + length/2 , length/2 , size);
}
return -1;
}
int
buddy_alloc(struct buddy * self , int size) {
int s = (int)next_pow_of_2(size);
int cap = self->size;
return _alloc(self,0,0,cap,s);
}
static int
_free(struct buddy * self, int index, int start, int length, int offset) {
assert(length > 0);
struct node * current = &(self->tree[index]);
if (start == offset) {
if (current->left == -1) {
return 1;
}
}
if (offset < start + length/2) {
index = current->left;
assert(index > 0);
int empty = _free(self, index, start, length/2 , offset);
if (empty) {
_free_leaf(self, index);
current->left = 0;
if (current->right == 0)
return 1;
}
} else {
index = current->right;
assert(index > 0);
int empty = _free(self, index, start+length/2, length/2 , offset);
if (empty) {
_free_leaf(self, index);
current->right = 0;
if (current->left == 0)
return 1;
}
}
return 0;
}
void
buddy_free(struct buddy * self, int offset) {
if (_free(self, 0, 0, self->size, offset)) {
self->tree[0].left = self->tree[0].right = 0;
}
}
static int
_size(struct buddy * self, int index, int start, int length, int offset) {
assert(length > 0);
struct node * current = &(self->tree[index]);
if (start == offset) {
if (current->left == -1) {
return length;
}
}
if (offset < start + length/2) {
index = current->left;
assert(index > 0);
return _size(self, index, start, length/2 , offset);
} else {
index = current->right;
assert(index > 0);
return _size(self, index, start+length/2, length/2 , offset);
}
}
int
buddy_size(struct buddy * self, int offset) {
return _size(self, 0 , 0 , self->size, offset);
}
static void
_dump(struct buddy * self, int index, int start, int length) {
struct node * current = &(self->tree[index]);
if (current->left < 0) {
printf("[%d:%d]",start,length);
return;
}
if (current->left == 0 && current->right == 0) {
printf("(%d:%d)",start,length);
return;
}
printf("(");
if (current->left == 0) {
printf("(%d:%d)",start,length/2);
} else {
_dump(self,current->left, start , length/2);
}
if (current->right == 0) {
printf("(%d:%d)",start + length/2 ,length/2);
} else {
_dump(self,current->right, start + length/2 , length/2);
}
printf(")");
}
void
buddy_dump(struct buddy * self) {
_dump(self, 0, 0, self->size);
printf("\n");
}