diff --git a/src/alloca.rs b/src/alloca.rs new file mode 100644 index 0000000..1637d3c --- /dev/null +++ b/src/alloca.rs @@ -0,0 +1,40 @@ +use spin::Mutex; +use sparkle_bump_alloc::Heap; +use alloc::allocator::{Alloc, Layout, AllocErr}; + +pub const HEAP_START: usize = 0o_000_001_000_000_0000; +pub const HEAP_SIZE: usize = 100 * 1024; // 100 KiB + +static HEAP: Mutex> = Mutex::new(None); + +pub fn heap_init(start: usize, size: usize) { + *HEAP.lock() = Some(Heap::new(start, size)); +} + +pub struct Allocator; + +unsafe impl<'a> Alloc for &'a Allocator { + #[inline] + unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { + if let Some(ref mut heap) = *HEAP.lock() { + heap.allocate(layout.size(), layout.align()) + .ok_or(AllocErr::Exhausted { request: layout }) + } else { + panic!("kheap: attempting alloc w/ uninitialized heap"); + } + } + + #[inline] + unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) { + if let Some(ref mut heap) = *HEAP.lock() { + heap.deallocate(ptr, layout.size(), layout.align()); + } else { + panic!("kheap: attempting dealloc w/ uninitialized heap"); + } + } + + #[inline] + fn oom(&mut self, err: AllocErr) -> ! { + panic!("kheap OOM: {:?}", err); + } +} diff --git a/src/lib.rs b/src/lib.rs index 2695f68..76c4d2c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,8 @@ extern crate log; #[macro_use] extern crate once; +#[macro_use] +extern crate alloc; extern crate rlibc; extern crate spin; extern crate volatile; @@ -26,6 +28,7 @@ extern crate sparkle_bump_alloc; #[macro_use] pub mod macros; +mod alloca; mod misc; mod logger; mod arch;