-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuddy.c
290 lines (251 loc) · 5.7 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
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
/*_
* Copyright (c) 2014-2015 Hirochika Asai <[email protected]>
* All rights reserved.
*/
#include "buddy.h"
#include "poptrie.h"
#include <stdlib.h>
#include <string.h>
#define BUDDY_EOL 0xffffffffUL
/*
* Initialize buddy system
*/
int
buddy_init(struct buddy *bs, int sz, int level, int bsz)
{
int i;
u8 *b;
u32 *buddy;
void *blocks;
u64 off;
/* Block size must be >= 32 bits */
if ( bsz < 4 ) {
return -1;
}
/* Heads */
buddy = malloc(sizeof(u32) * level);
if ( NULL == buddy ) {
return -1;
}
/* Pre allocated nodes */
blocks = malloc(bsz * (1 << sz));
if ( NULL == blocks ) {
free(buddy);
return -1;
}
/* Bitmap */
b = malloc(((1 << (sz)) + 7) / 8);
if ( NULL == b ) {
free(blocks);
free(buddy);
return -1;
}
(void)memset(b, 0, ((1 << (sz)) + 7) / 8);
/* Initialize buddy system */
for ( i = 0; i < level; i++ ) {
buddy[i] = BUDDY_EOL;
}
if ( sz < level ) {
buddy[sz] = 0;
*(u32 *)blocks = 0; /* Terminate */
} else {
buddy[level - 1] = 0;
for ( i = 0; i < (1 << (sz - level + 1)); i++ ) {
off = bsz * (i * (1 << (level - 1)));
if ( i == (1 << (sz - level + 1)) - 1 ) {
*(u32 *)(blocks + off) = BUDDY_EOL;
} else {
*(u32 *)(blocks + off)
= (u32)((i + 1) * (1 << (level - 1)));
}
}
}
/* Set */
bs->sz = sz;
bs->bsz = bsz;
bs->level = level;
bs->buddy = buddy;
bs->blocks = blocks;
bs->b = b;
return 0;
}
/*
* Release the buddy system
*/
void
buddy_release(struct buddy *bs)
{
free(bs->buddy);
free(bs->blocks);
free(bs->b);
}
/*
* Split the block at the level n
*/
static int
_split_buddy(struct buddy *bs, int lv)
{
int ret;
u32 next;
/* Check the head of the current level */
if ( BUDDY_EOL != bs->buddy[lv] ) {
return 0;
}
/* Check the size */
if ( lv + 1 >= bs->level ) {
return -1;
}
/* Check the head of the upper level */
if ( BUDDY_EOL == bs->buddy[lv + 1] ) {
ret = _split_buddy(bs, lv + 1);
if ( ret < 0 ) {
return ret;
}
}
/* Split */
bs->buddy[lv] = bs->buddy[lv + 1];
bs->buddy[lv + 1] = *(u32 *)((u64)bs->blocks + bs->bsz * bs->buddy[lv]);
next = bs->buddy[lv] + (1<<lv);
*(u32 *)((u64)bs->blocks + bs->bsz * next) = BUDDY_EOL;
*(u32 *)((u64)bs->blocks + bs->bsz * bs->buddy[lv]) = next;
return 0;
}
/*
* Allocate (2**sz) blocks
*/
void *
buddy_alloc(struct buddy *bs, int n)
{
int ret;
ret = buddy_alloc2(bs, n);
if ( ret < 0 ) {
return NULL;;
}
return (void *)((u64)bs->blocks + bs->bsz * ret);
}
int
buddy_alloc2(struct buddy *bs, int sz)
{
int ret;
u32 a;
u32 b;
/* Check the argument */
if ( sz < 0 ) {
return -1;
}
/* Check the size */
if ( sz >= bs->level ) {
return -1;
}
/* Split first if needed */
ret = _split_buddy(bs, sz);
if ( ret < 0 ) {
return -1;
}
/* Obtain from the head */
a = bs->buddy[sz];
b = *(u32 *)(bs->blocks + bs->bsz * a);
#if 0
printf("ALLOC %p %x, %x [%x/%d]\n", bs, a, b, sz, bs->bsz);
#endif
bs->buddy[sz] = b;
/* Flag the tail block in bitmap */
bs->b[(a + (1 << sz) - 1) >> 3] |= 1 << ((a + (1 << sz) - 1) & 0x7);
return a;
}
/*
* Merge blocks onto an upper level if possible
*/
static void
_merge(struct buddy *bs, int off, int lv)
{
int i;
u32 s;
u32 *n;
if ( lv + 1 >= bs->level ) {
/* Reached maximum */
return;
}
s = off / (1 << (lv + 1)) * (1 << (lv + 1));
for ( i = 0; i < (1 << (lv + 1)); i++ ) {
if ( bs->b[(s + i) >> 3] & (1 << ((s + i) & 7)) ) {
/* Found one */
return;
}
}
/* All bits were zero, then take the corresponding two blocks from the
current level */
n = &bs->buddy[lv];
while ( BUDDY_EOL != *n ) {
if ( s == *n || (s + (1 << lv)) == *n ) {
/* Remove this */
*n = *(u32 *)(bs->blocks + bs->bsz * (*n));
} else {
n = (u32 *)(bs->blocks + bs->bsz * (*n));
}
}
/* Append it to the upper level */
*(u32 *)(bs->blocks + bs->bsz * s) = bs->buddy[lv + 1];
bs->buddy[lv + 1] = s;
/* Try to merge the upper level */
_merge(bs, s, lv + 1);
}
/*
* Free
*/
void
buddy_free(struct buddy *bs, void *a)
{
int off;
/* Calculate the offset */
off = ((u64)a - (u64)bs->blocks) / bs->bsz;
buddy_free2(bs, off);
}
void
buddy_free2(struct buddy *bs, int a)
{
int sz;
u32 next;
u32 *n;
/* Find the size */
sz = 0;
for ( ;; ) {
if ( bs->b[(a + (1 << sz) - 1) >> 3]
& (1 << ((a + (1 << sz) - 1) & 0x7)) ) {
break;
}
sz++;
}
if ( sz >= bs->level ) {
/* Something is wrong... */
return;
}
/* Unflag the tail block in bitmap */
bs->b[(a + (1 << sz) - 1) >> 3] &= ~(1 << ((a + (1 << sz) - 1) & 0x7));
/* Return to the buddy system */
n = &bs->buddy[sz];
while ( BUDDY_EOL != *n ) {
if ( (u32)a < *n ) {
next = *n;
*n = a;
*(u32 *)(bs->blocks + bs->bsz * a) = next;
n = NULL;
break;
} else {
n = (u32 *)(bs->blocks + bs->bsz * (*n));
}
}
if ( n != NULL ) {
*n = a;
*(u32 *)(bs->blocks + bs->bsz * a) = BUDDY_EOL;
}
_merge(bs, a, sz);
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/