mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Create a basic BumpAllocator type
This commit is contained in:
@@ -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
28
src/allocator/bump.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user