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
use core::ptr::RawPtr;
use core::mem::transmute;
use core::ptr::{set_memory, copy_memory};
use core::intrinsics::offset;
use core::intrinsics::ctlz32;
use util::bitv::Bitv;
#[repr(u8)]
enum Node {
UNUSED = 0,
USED = 1,
SPLIT = 2,
FULL = 3
}
pub trait Allocator {
fn alloc(&mut self, size: uint) -> (*mut u8, uint);
fn zero_alloc(&mut self, s: uint) -> (*mut u8, uint) {
let (ptr, size) = self.alloc(s);
unsafe { set_memory(ptr, 0, size); }
(ptr, size)
}
fn realloc(&mut self, src: *mut u8, size: uint) -> (*mut u8, uint) {
self.free(src);
let (ptr, sz) = self.alloc(size);
unsafe { copy_memory(ptr, src as *const u8, sz); }
(ptr, sz)
}
fn free(&mut self, ptr: *mut u8);
}
pub struct BuddyAlloc {
pub order: uint,
pub tree: Bitv
}
pub struct Alloc {
pub parent: BuddyAlloc,
pub base: *mut u8,
pub el_size: uint
}
impl BuddyAlloc {
pub fn new(order: uint, storage: Bitv) -> BuddyAlloc {
storage.clear(1 << (order + 1));
BuddyAlloc { order: order, tree: storage }
}
#[inline]
fn offset(&self, index: uint, level: uint) -> uint {
(index + 1 - (1 << self.order >> level)) << level
}
fn alloc(&mut self, mut size: uint) -> (uint, uint) {
if size == 0 {
size = 1;
}
let lg2_size = 32 - unsafe { ctlz32(size as u32 - 1) } as uint;
let mut index = 0;
let mut level = self.order;
loop {
match (self.get(index), level == lg2_size) {
(UNUSED, true) => {
self.set(index, USED);
let mut parent = index;
loop {
let buddy = parent - 1 + (parent & 1) * 2;
match self.get(buddy) {
USED | FULL if parent > 0 => {
parent = (parent + 1) / 2 - 1;
self.set(parent, FULL);
}
_ => break
}
}
return (
self.offset(index, level),
1 << lg2_size
);
}
(UNUSED, false) => {
self.set(index, SPLIT);
self.set(index*2 + 1, UNUSED);
self.set(index*2 + 2, UNUSED);
index = index * 2 + 1;
level -= 1;
}
(SPLIT, false) => {
index = index * 2 + 1;
level -= 1;
}
_ => loop {
if index & 1 == 1 {
index += 1;
break;
}
level += 1;
if index == 0 {
return (0, 0);
}
index = (index + 1) / 2 - 1;
}
}
}
}
fn free(&mut self, offset: uint) {
let mut length = 1 << self.order;
let mut left = 0;
let mut index = 0;
loop {
match self.get(index) {
UNUSED => return,
USED => loop {
if index == 0 {
self.set(0, UNUSED);
return;
}
let buddy = index - 1 + (index & 1) * 2;
match self.get(buddy) {
UNUSED => {}
_ => {
self.set(index, UNUSED);
loop {
let parent = (index + 1) / 2 - 1;
match self.get(parent) {
FULL if index > 0 => {
self.set(parent, SPLIT);
}
_ => return
}
index = parent;
}
}
}
index = (index + 1) / 2 - 1;
},
_ => {
length /= 2;
if offset < left + length {
index = index * 2 + 1;
}
else {
left += length;
index = index * 2 + 2;
}
}
}
}
}
fn get(&self, i: uint) -> Node {
unsafe {
transmute(self.tree.get(i))
}
}
fn set(&self, i: uint, x: Node) {
self.tree.set(i, x as u8);
}
}
impl Allocator for Alloc {
fn alloc(&mut self, size: uint) -> (*mut u8, uint) {
let (offset, size) = self.parent.alloc(size);
unsafe {
return (
self.base.offset((offset << self.el_size) as int),
size << self.el_size
)
}
}
fn free(&mut self, ptr: *mut u8) {
let length = 1 << self.parent.order << self.el_size;
unsafe {
if ptr < self.base || ptr >= self.base.offset(length) {
return;
}
}
let offset = (ptr as uint - self.base as uint) >> self.el_size;
self.parent.free(offset);
}
}
impl Alloc {
pub fn new(parent: BuddyAlloc, base: *mut u8, el_size: uint) -> Alloc {
Alloc { parent: parent, base: base, el_size: el_size }
}
}