-
Notifications
You must be signed in to change notification settings - Fork 2
/
buddy_allocator_lists.rs
543 lines (461 loc) · 15.6 KB
/
buddy_allocator_lists.rs
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
use super::{top_level_blocks, PageSize, PhysicalAllocator, MAX_ORDER, BASE_ORDER, LEVEL_COUNT};
use array_init;
#[cfg(feature = "flame_profile")]
use flame;
use std::collections::LinkedList;
use std::vec::Vec;
use std::time::{Instant, Duration};
#[derive(Debug, Eq, PartialEq)]
pub struct Block {
begin_address: usize,
order: u8,
state: BlockState,
}
#[repr(u8)]
#[derive(Debug, Eq, PartialEq)]
pub enum BlockState {
Used,
Free,
}
pub trait BlockList {
fn push(&mut self, item: Block);
fn position<P: FnMut(&Block) -> bool>(&mut self, pred: P) -> Option<usize>;
fn len(&self) -> usize;
fn get(&self, index: usize) -> Option<&Block>;
fn get_mut(&mut self, index: usize) -> Option<&mut Block>;
fn remove(&mut self, index: usize);
}
impl BlockList for LinkedList<Block> {
fn push(&mut self, item: Block) {
self.push_back(item)
}
fn len(&self) -> usize {
LinkedList::len(self)
}
fn position<P: FnMut(&Block) -> bool>(&mut self, pred: P) -> Option<usize> {
self.iter().position(pred)
}
fn get(&self, index: usize) -> Option<&Block> {
let len = self.len();
if len == 0 {
return None;
}
if index < len / 2 {
self.iter().nth(index)
} else {
self.iter().rev().nth(len - 1 - index)
}
}
fn get_mut(&mut self, index: usize) -> Option<&mut Block> {
let len = self.len();
if len == 0 {
return None;
}
if index < len / 2 {
self.iter_mut().nth(index)
} else {
self.iter_mut().rev().nth(len - 1 - index)
}
}
fn remove(&mut self, index: usize) {
let mut second_part = self.split_off(index);
second_part.pop_front();
self.append(&mut second_part);
}
}
impl BlockList for Vec<Block> {
fn push(&mut self, item: Block) {
Vec::push(self, item);
}
fn position<P: FnMut(&Block) -> bool>(&mut self, pred: P) -> Option<usize> {
self.iter().position(pred)
}
fn len(&self) -> usize {
Vec::len(self)
}
fn get(&self, index: usize) -> Option<&Block> {
if self.len() > index {
Some(&self[index])
} else {
None
}
}
fn get_mut(&mut self, index: usize) -> Option<&mut Block> {
if self.len() > index {
Some(&mut self[index])
} else {
None
}
}
fn remove(&mut self, index: usize) {
self.remove(index);
}
}
pub struct BuddyAllocator<L: BlockList> {
lists: [L; LEVEL_COUNT as usize],
}
/// A very temporary block index. Is not to be trusted to remain pointing to the same block. Use at
/// own risk!
#[derive(Debug, Copy, Clone)]
struct BlockIndex {
order: u8,
index: usize,
}
impl BuddyAllocator<LinkedList<Block>> {
pub fn new() -> Self {
BuddyAllocator {
lists: array_init::array_init(|_| LinkedList::new()),
}
}
}
impl BuddyAllocator<Vec<Block>> {
pub fn new() -> Self {
BuddyAllocator {
lists: array_init::array_init(|_| Vec::new()),
}
}
}
impl<L: BlockList> BuddyAllocator<L> {
/// Get a block by its index.
///
/// # Panicking
///
/// Panics if the order is larger than maximum. This indicates a programming error.
fn get(&self, block: &BlockIndex) -> Option<&Block> {
let list = &self.lists[block.order as usize];
list.get(block.index)
}
/// Get a block by its index mutably.
///
/// # Panicking
///
/// Panics if the order is larger than maximum. This indicates a programming error.
fn get_mut(&mut self, block: &BlockIndex) -> Option<&mut Block> {
let list = &mut self.lists[block.order as usize];
list.get_mut(block.index)
}
/// Modify a block by setting its state to a new one. This will not merge blocks if set to free,
/// it will just mark the block as freed.
///
/// # Panicking
///
/// This function will panic if the index is incorrect
fn modify(&mut self, index: &mut BlockIndex, new_state: BlockState) {
let block = self.get_mut(index).unwrap();
block.state = new_state;
}
/// Create a top level block
pub fn create_top_level(&mut self, begin_address: usize) {
self.lists[MAX_ORDER as usize].push(Block {
begin_address,
order: MAX_ORDER,
state: BlockState::Free,
});
}
/// Splits a block in place. Index will be invalidated. Returns index of first buddy
///
/// # Panicking
///
/// 1. Index incorrect (doesn't point to block or order > max)
/// 2. Attempt to split used block
/// 3. List state bad (order x in list order of y != x)
fn split(&mut self, index: BlockIndex) -> Result<BlockIndex, BlockSplitError> {
let block = self.get(&index).unwrap();
if block.state == BlockState::Used {
panic!("Attempted to split used block at index {:?}", index);
}
debug_assert_eq!(
block.order, index.order,
"Index should have order equal to block!"
);
let original_order = block.order;
let order = original_order - 1;
if index.order == 0 {
return Err(BlockSplitError::BlockSmallestPossible);
}
let buddies: [Block; 2] = array_init::array_init(|n| Block {
begin_address: if n == 0 {
block.begin_address
} else {
block.begin_address + 2usize.pow(u32::from(order + BASE_ORDER))
},
order,
state: BlockState::Free,
});
self.lists[original_order as usize].remove(index.index);
let [first, second] = buddies;
self.lists[order as usize].push(first);
self.lists[order as usize].push(second);
Ok(BlockIndex {
order,
index: self.lists[order as usize].len() - 2,
})
}
#[cfg_attr(feature = "flame_profile", flame)]
fn allocate_exact(&mut self, order: u8) -> Result<BlockIndex, BlockAllocateError> {
if order > MAX_ORDER {
return Err(BlockAllocateError::OrderTooLarge(order));
}
#[cfg(feature = "flame_profile")]
flame::note("allocate begin", None);
let mut index = self.find_or_split(order)?;
self.modify(&mut index, BlockState::Used);
Ok(index)
}
/// Find a frame of a given order or splits other frames recursively until one is made. Does not
/// set state to used.
///
/// # Panicking
///
/// Panics if the order is greater than max or if a programming error is encountered such as
/// attempting to split a block of the smallest possible size.
fn find_or_split(&mut self, order: u8) -> Result<BlockIndex, BlockAllocateError> {
if order > MAX_ORDER {
panic!("Order {} larger than max of {}!", order, MAX_ORDER);
}
let opt: Option<BlockIndex> = self.lists[order as usize]
.position(|block| block.state == BlockState::Free)
.map(|index| BlockIndex { order, index });
let block = match opt {
Some(thing) => Ok(thing),
None => {
if order >= MAX_ORDER {
Err(BlockAllocateError::NoBlocksAvailable)
} else {
let block_index = self.find_or_split(order + 1)?;
let first = self.split(block_index).unwrap();
Ok(first)
}
}
}?;
Ok(block)
}
}
#[derive(Debug, Copy, Clone)]
pub enum BlockSplitError {
BlockSmallestPossible,
}
#[derive(Debug, Copy, Clone)]
pub enum BlockAllocateError {
NoBlocksAvailable,
OrderTooLarge(u8),
}
impl<L: BlockList> PhysicalAllocator for BuddyAllocator<L> {
fn alloc(&mut self, size: PageSize) -> *const u8 {
let index = self.allocate_exact(size.power_of_two() - BASE_ORDER)
.unwrap();
let block = self.get(&index).unwrap();
block.begin_address as *const u8
}
fn dealloc(&mut self, _frame: *const u8) {
unimplemented!()
}
}
pub fn demo_linked_lists(print_addresses: bool, blocks: u32, block_size: u8) -> Duration {
let allocator = BuddyAllocator::<LinkedList<Block>>::new();
demo(allocator, print_addresses, blocks, block_size)
}
pub fn demo_vecs(print_addresses: bool, blocks: u32, block_size: u8) -> Duration {
let allocator = BuddyAllocator::<Vec<Block>>::new();
demo(allocator, print_addresses, blocks, block_size)
}
fn demo<L: BlockList>(
mut allocator: BuddyAllocator<L>,
print_addresses: bool,
blocks: u32,
block_size: u8,
) -> Duration {
let top_level_blocks = top_level_blocks(blocks, block_size);
for block_number in 0..top_level_blocks {
allocator
.create_top_level(2usize.pow(u32::from(MAX_ORDER + BASE_ORDER)) * block_number as usize);
}
let start = Instant::now();
for _ in 0..blocks {
let index = allocator.allocate_exact(block_size).unwrap();
let addr = allocator.get(&index).unwrap().begin_address;
if print_addresses {
println!("Address: {:#x}", addr);
}
}
start.elapsed()
}
#[cfg(test)]
mod test {
use super::*;
use ::MAX_ORDER_SIZE;
#[test]
fn test_create_top_level() {
let mut allocator = BuddyAllocator::<Vec<Block>>::new();
allocator.create_top_level(0);
allocator.create_top_level(2usize.pow(MAX_ORDER_SIZE as u32));
let expected = vec![
Block {
begin_address: 0,
order: MAX_ORDER,
state: BlockState::Free,
},
Block {
begin_address: 2usize.pow(MAX_ORDER_SIZE as u32),
order: MAX_ORDER,
state: BlockState::Free,
},
];
assert_eq!(allocator.lists[MAX_ORDER as usize - 1].len(), 0);
assert_eq!(allocator.lists[MAX_ORDER as usize], expected);
}
#[test]
fn test_split() {
let mut allocator = BuddyAllocator::<Vec<Block>>::new();
allocator.create_top_level(0);
allocator
.split(BlockIndex {
index: 0,
order: MAX_ORDER,
})
.unwrap();
let expected_blocks = [
Block {
begin_address: 0,
order: MAX_ORDER - 1,
state: BlockState::Free,
},
Block {
begin_address: 2usize.pow(MAX_ORDER_SIZE as u32 - 1),
order: MAX_ORDER - 1,
state: BlockState::Free,
},
];
assert_eq!(allocator.lists[MAX_ORDER as usize - 1].len(), 2);
assert_eq!(allocator.lists[MAX_ORDER as usize].len(), 0);
allocator.lists[MAX_ORDER as usize - 1]
.iter()
.zip(expected_blocks.iter())
.for_each(|(block, expected)| assert_eq!(block, expected));
}
#[test]
fn test_get_linked_list() {
let mut allocator = BuddyAllocator::<LinkedList<Block>>::new();
allocator.create_top_level(0);
allocator.create_top_level(2usize.pow((MAX_ORDER + BASE_ORDER) as u32) as usize);
let mut indices: [BlockIndex; 2] = array_init::array_init(|_| {
allocator
.split(BlockIndex {
index: 0,
order: MAX_ORDER,
})
.unwrap()
});
indices[1].index += 1; // Make sure we iterate from back too
let expected_blocks = [
Block {
begin_address: 0,
order: MAX_ORDER - 1,
state: BlockState::Free,
},
Block {
begin_address: 2usize.pow(MAX_ORDER_SIZE as u32 - 1) * indices[1].index,
order: MAX_ORDER - 1,
state: BlockState::Free,
},
];
for (index, expected) in indices.iter().zip(expected_blocks.iter()) {
let block = allocator.get(index).unwrap();
assert_eq!(block, expected)
}
}
#[test]
fn test_get_mut_linked_list() {
let mut allocator = BuddyAllocator::<LinkedList<Block>>::new();
allocator.create_top_level(0);
allocator.create_top_level(1024 * 1024 * 1024);
let mut indices: [BlockIndex; 2] = array_init::array_init(|_| {
allocator
.split(BlockIndex {
index: 0,
order: MAX_ORDER,
})
.unwrap()
});
indices[1].index += 1; // Make sure we iterate from back too
let expected_blocks = [
Block {
begin_address: 0,
order: MAX_ORDER - 1,
state: BlockState::Free,
},
Block {
begin_address: 2usize.pow((MAX_ORDER_SIZE - 1) as u32) * indices[1].index,
order: MAX_ORDER - 1,
state: BlockState::Free,
},
];
for (index, expected) in indices.iter().zip(expected_blocks.iter()) {
let block = allocator.get_mut(index).unwrap();
assert_eq!(block, expected)
}
}
#[test]
fn test_allocate_exact_with_free() {
let mut allocator = BuddyAllocator::<Vec<Block>>::new();
allocator.create_top_level(0);
let index = allocator.allocate_exact(MAX_ORDER).unwrap();
let expected_block = Block {
begin_address: 0,
order: MAX_ORDER,
state: BlockState::Used,
};
assert_eq!(*allocator.get(&index).unwrap(), expected_block);
}
#[test]
fn test_allocate_exact_no_free() {
let mut allocator = BuddyAllocator::<Vec<Block>>::new();
allocator.create_top_level(0);
let index = allocator.allocate_exact(MAX_ORDER - 2).unwrap();
let expected_block = Block {
begin_address: 0,
order: MAX_ORDER - 2,
state: BlockState::Used,
};
assert_eq!(*allocator.get(&index).unwrap(), expected_block);
}
#[test]
fn test_unique_addresses_linked_lists() {
let mut allocator = BuddyAllocator::<LinkedList<Block>>::new();
for block_number in 0..top_level_blocks(1000, 0) {
allocator.create_top_level(
2usize.pow((MAX_ORDER + BASE_ORDER) as u32) * block_number as usize,
);
}
let mut seen = Vec::with_capacity(1000);
for _ in 0..1000 {
let index = allocator.allocate_exact(0).unwrap();
let addr = allocator.get(&index).unwrap().begin_address;
if seen.contains(&addr) {
panic!("Allocator must return addresses never been allocated before!");
} else {
seen.push(addr);
}
}
}
#[test]
fn test_unique_addresses_vecs() {
let mut allocator = BuddyAllocator::<Vec<Block>>::new();
for block_number in 0..top_level_blocks(1000, 0) {
allocator.create_top_level(
2usize.pow((MAX_ORDER + BASE_ORDER) as u32) * block_number as usize,
);
}
let mut seen = Vec::with_capacity(1000);
for _ in 0..1000 {
let index = allocator.allocate_exact(0).unwrap();
let addr = allocator.get(&index).unwrap().begin_address;
if seen.contains(&addr) {
panic!("Allocator must return addresses never been allocated before!");
} else {
seen.push(addr);
}
}
}
// TODO test allocate_exact failing case propagates error right
}