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
use std::vec;
const NODE_UNUSED: u8 = 0;
const NODE_USED: u8 = 1;
const NODE_SPLIT: u8 = 2;
const NODE_FULL: u8 = 3;
pub struct BuddyAllocator {
levels: usize,
pub tree: vec::Vec<u8>,
}
impl BuddyAllocator {
pub fn new(levels: usize) -> BuddyAllocator {
let size: usize = (1 << levels + 1) - 1;
return BuddyAllocator{
levels: levels,
tree: vec![NODE_UNUSED; size],
};
}
pub fn allocate(&mut self, s: usize) -> isize {
let requested_blocks: f64;
if s == 0 {
requested_blocks = 1.0;
} else {
requested_blocks = s.next_power_of_two() as f64;
}
let requested_level = requested_blocks.log(2.0) as usize;
if requested_level > self.levels {
return -1;
}
let mut index = 0;
let mut current_level = self.levels;
'forward: loop {
let has_buddy = index & 1 == 1;
if current_level != requested_level {
match self.tree[index] {
NODE_USED | NODE_FULL => {
if has_buddy {
index += 1;
}
continue 'forward;
}
NODE_UNUSED => {
self.tree[index] = NODE_SPLIT;
index = index * 2 + 1;
current_level -= 1;
continue 'forward;
}
NODE_SPLIT => {
index = index * 2 + 1;
current_level -= 1;
continue 'forward;
}
_ => panic!("unknkown type {}", self.tree[index])
}
} else {
if self.tree[index] == NODE_UNUSED {
self.tree[index] = NODE_USED;
self.update_parents((index + 1) / 2 - 1);
break 'forward;
}
}
if has_buddy {
index += 1;
continue 'forward;
}
'backward: loop {
index = (index + 1) / 2 - 1;
current_level += 1;
let has_buddy_inner = index & 1 == 1;
if has_buddy_inner {
index += 1;
break 'backward;
}
}
}
return index as isize;
}
pub fn free(&mut self, index_offset: usize) {
if index_offset > self.tree.len() - 1 {
panic!("offset {} is > length of tree {}", index_offset, self.tree.len());
}
self.free_and_combine(index_offset);
self.update_parents((index_offset + 1) / 2 - 1);
}
fn free_and_combine(&mut self, index: usize) {
self.tree[index] = NODE_UNUSED;
if index == 0 {
return;
}
let other_node: usize;
let has_right_buddy = (index & 1) == 1;
if has_right_buddy {
other_node = index + 1;
} else {
other_node = index - 1;
}
if self.tree[other_node] == NODE_UNUSED {
self.free_and_combine((index + 1) / 2 - 1);
}
return;
}
fn update_parents(&mut self, index: usize) {
let left_child = index * 2 + 1;
let right_child = index * 2 + 2;
let left_child_used_or_full = self.tree[left_child] == NODE_FULL || self.tree[left_child] == NODE_USED;
let right_child_used_or_full = self.tree[right_child] == NODE_FULL || self.tree[right_child] == NODE_USED;
if left_child_used_or_full && right_child_used_or_full {
self.tree[index] = NODE_FULL;
} else if self.tree[left_child] == NODE_UNUSED && self.tree[right_child] == NODE_UNUSED {
self.tree[index] = NODE_UNUSED;
} else {
self.tree[index] = NODE_SPLIT;
}
if index == 0 {
return;
}
self.update_parents((index + 1) / 2 - 1);
}
pub fn dump(&self) -> String {
let mut out = "".to_string();
let mut row = "".to_string();
let mut level = 0;
let mut index = 0;
loop {
if index == self.tree.len() {
break
}
match self.tree[index] {
NODE_USED => row += "U",
NODE_UNUSED => row += "O",
NODE_SPLIT => row += "S",
NODE_FULL => row += "F",
_ => panic!("unknown node type {}", self.tree[index]),
}
if row.len() == 1 << level {
out += &(row + "\n");
row = "".to_string();
level += 1;
}
index += 1;
}
return out;
}
}