/
bump_ptr.rs
49 lines (45 loc) · 1.51 KB
/
bump_ptr.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
//
// SOS: the Stupid Operating System
// by Eliza Weisman (hi@hawkweisman.me)
//
// Copyright (c) 2015-2017 Eliza Weisman
// Released under the terms of the MIT license. See `LICENSE` in the root
// directory of this repository for more information.
//
use memory::{Addr, PAddr};
use super::{Address, Allocator, AllocErr, Layout};
/// A simple bump pointer allocator.
///
/// This allocator has few "moving parts" and is very fast. However, it doesn't
/// allow objects to be deallocated. We use this allocator for early kernel
/// objects before we can set up a more sophisticated heap allocator.
#[derive(Debug)]
pub struct BumpPtr { start: PAddr
, end: PAddr
, ptr: PAddr
}
impl BumpPtr {
pub const fn new(start: PAddr, end: PAddr) -> Self {
BumpPtr { start: start
, end: end
, ptr: start
}
}
}
unsafe impl Allocator for BumpPtr {
unsafe fn alloc(&mut self, layout: Layout) -> Result<Address, AllocErr> {
let start = self.ptr.align_up(layout.align() as <PAddr as Addr>::Repr);
// TODO: can this be a saturating add?
let end = start + layout.size() as <PAddr as Addr>::Repr;
if end > self.end {
Err(AllocErr::Exhausted{ request: layout.clone() })
} else {
// bump
self.ptr = end;
Ok(start.as_mut_ptr())
}
}
unsafe fn dealloc(&mut self, _ptr: Address, _layout: Layout) {
// just leak it
}
}