Create a basic BumpAllocator type

This commit is contained in:
Philipp Oppermann
2020-01-09 15:25:37 +01:00
parent 882c83f9de
commit 7c84dbaa1d
2 changed files with 30 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ use x86_64::{
VirtAddr, VirtAddr,
}; };
pub mod bump;
pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_START: usize = 0x_4444_4444_0000;
pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB

28
src/allocator/bump.rs Normal file
View File

@@ -0,0 +1,28 @@
pub struct BumpAllocator {
heap_start: usize,
heap_end: usize,
next: usize,
allocations: usize,
}
impl BumpAllocator {
/// Creates a new empty bump allocator.
pub const fn new() -> Self {
BumpAllocator {
heap_start: 0,
heap_end: 0,
next: 0,
allocations: 0,
}
}
/// Initializes the bump allocator with the given heap bounds.
///
/// This method is unsafe because the caller must ensure that the given
/// memory range is unused. Also, this method must be called only once.
pub unsafe fn init(&mut self, heap_start: usize, heap_size: usize) {
self.heap_start = heap_start;
self.heap_end = heap_start + heap_size;
self.next = heap_start;
}
}