From 7c84dbaa1d06339e2ab84cd19fd6186a51c170e4 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 9 Jan 2020 15:25:37 +0100 Subject: [PATCH] Create a basic BumpAllocator type --- src/allocator.rs | 2 ++ src/allocator/bump.rs | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/allocator/bump.rs diff --git a/src/allocator.rs b/src/allocator.rs index f77fef9d..4b683208 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -8,6 +8,8 @@ use x86_64::{ VirtAddr, }; +pub mod bump; + pub const HEAP_START: usize = 0x_4444_4444_0000; pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB diff --git a/src/allocator/bump.rs b/src/allocator/bump.rs new file mode 100644 index 00000000..5644dab5 --- /dev/null +++ b/src/allocator/bump.rs @@ -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; + } +}