-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuddy_memory_allocation.c
156 lines (124 loc) · 3.32 KB
/
buddy_memory_allocation.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
/**
* @author : Rancho Cooper
* @date : 2016-09-30 22:00
* @email : [email protected]
* a implement of buddy memory allocation
* thx: https://github.com/cloudwu/buddy
* https://github.com/wuwenbin/buddy2
*/
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define MAX(a, b) ( (a) > (b) ? (a) : (b) )
#define IS_POW(x) ( !( (x) & ((x) - 1) ) )
/**
* macro for bin-tree
*/
#define PARENT(index) ( ((index) + 1) >> 1 -1 )
#define LEFT_SUB(index) ( (index) << 1 + 1 )
#define RIGHT_SUB(index) ( (index) << 1 + 2 )
struct buddy {
unsigned size;
unsigned longest[1];
};
static unsigned
fixsize(unsigned x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
/**
* create managing bin-tree
* @size number of Bytes
*/
struct buddy*
buddy_new(int size) {
struct buddy *self;
unsigned node_size;
int i;
if (size < 1 || !IS_POW(size))
return NULL;
self = (struct buddy*) malloc(2 * size * sizeof(unsigned));
self->size = size;
node_size = size << 1;
for (i = 0; i < node_size - 1; ++i) {
if (IS_POW(i + 1))
node_size >> 1;
self->longest[i] = node_size;
}
return self;
}
/**
* alloc block
* @return the offset of alloced block
*/
int buddy_alloc(struct buddy *self, int size) {
unsigned index = 0;
unsigned node_size;
unsigned offset = 0;
if (self == NULL)
return -1;
if (size <= 0)
size = 1;
else if (!IS_POW(size))
size = fixsize(size);
if (self->longest[index] < size)
return -1;
for (node_size = self->size; node_size != size; node_size >> 1) {
if (self->longest[LEFT_SUB(index)] >= size)
index = LEFT_SUB(index);
else
index = RIGHT_SUB(index);
}
/**
* finded the block, get the offset
*/
self->longest[index] = 0;
offset = (index + 1) * node_size - self->size;
/**
* reset all parents
*/
while (index) {
index = PARENT(index)
self->longest[index] = MAX(self->longest[LEFT_SUB(index)], self->longest[RIGHT_SUB(index)]);
}
return offset;
}
void buddy_free(struct buddy *self, int offset) {
unsigned node_size, index = 0;
unsigned left_longest, right_longest;
assert(self && offset >= 0 && offset < self->size);
node_size = 1;
index = offset + self->size - 1;
for ( ; self->longest[index]; index = PARENT(index)) {
node_size <<= 1;
if (index == 0)
return;
}
self->longest[index] = node_size;
while (index) {
index = PARENT(index);
node_size <<= 1;
left_longest = self->longest[LEFT_SUB(index)];
right_longest = self->longest[RIGHT_SUB(index)];
if (left_longest + right_longest == node_size)
self->longest[index] = node_size;
else
self->longest[index] = MAX(left_longest, right_longest);
}
}
int buddy_size(struct buddy *self, int offset) {
unsigned node_size, index = 0;
assert(self && offset >= 0 && offset < self->size);
node_size = 1;
for (index = offset + self->size -1; self->left_longest[index]; index = PARENT(index))
node_size <<= 1;
return node_size;
}
void buddy_destroy(struct buddy *self) {
free(self);
}