From 37038b21c3805de5c2117b2bd6e7397e16c999a3 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Fri, 19 Aug 2022 23:20:54 -0700 Subject: Multi size allocation --- src/kernel.rs | 12 ----- src/mem/alloc.rs | 141 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 73 insertions(+), 80 deletions(-) (limited to 'src') diff --git a/src/kernel.rs b/src/kernel.rs index 19c4e5f..e3e9166 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -63,17 +63,5 @@ fn kernel_main() -> ! { } } println!("I should be able to print {} here!", 5); - { - let a: Box = Box::new(5); - println!("{:?}", a); - let b: Box = Box::new(7); - println!("{:?}", b); - } - { - let a: Box = Box::new(8); - println!("{:?}", a); - let b: Box = Box::new(9); - println!("{:?}", b); - } loop { } } diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs index 0b02fbd..927c00d 100644 --- a/src/mem/alloc.rs +++ b/src/mem/alloc.rs @@ -192,44 +192,46 @@ unsafe impl GlobalAlloc for GrandAllocator { } } } - /* 2 => { - U16_GRAND_ALLOC.inner.lock(|pool| { - match pool.alloc() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - (*elem).inner() as *mut u8; - } + match U16_GRAND_ALLOC.alloc() { + None => { + panic!("No cells to allocate!"); } - }) + Some(elem) => { + return &mut (*(*elem).inner() as u8) as *mut u8; + } + } } 4 => { - U32_GRAND_ALLOC.inner.lock(|pool| { - match pool.alloc() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - (*elem).inner() as *mut u8; - } + match U32_GRAND_ALLOC.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return &mut (*(*elem).inner() as u8) as *mut u8; } - }) + } } 8 => { - U64_GRAND_ALLOC.inner.lock(|pool| { - match pool.alloc() { - None => { - panic!("No cells to allocate!"); - } - Some(elem) => { - (*elem).inner() as *mut u8; - } + match U64_GRAND_ALLOC.alloc() { + None => { + panic!("No cells to allocate!"); } - }) + Some(elem) => { + return &mut (*(*elem).inner() as u8) as *mut u8; + } + } + } + 16 => { + match U128_GRAND_ALLOC.alloc() { + None => { + panic!("No cells to allocate!"); + } + Some(elem) => { + return &mut (*(*elem).inner() as u8) as *mut u8; + } + } } - */ _ => { panic!("No allocators for size {}!", layout.size()); } @@ -246,56 +248,59 @@ unsafe impl GlobalAlloc for GrandAllocator { return; } } + panic!("Didn't deallocate!"); }); } - _ => { - panic!("No deallocators for size {}!", layout.size()); + 2 => { + U16_GRAND_ALLOC.inner.lock(|pool| { + for idx in 2..pool.len() { + if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr { + U16_GRAND_ALLOC.free(&mut pool[idx]); + return; + } + } + panic!("Didn't deallocate!"); + }); } - } - } -} - -#[global_allocator] -pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; -/* -unsafe impl GlobalAlloc for NullLock> { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - match layout.size() { - 1 => { - self.lock(|qa| { - match qa.alloc() { - None => { - panic!("No data to allocate!"); + 4 => { + U32_GRAND_ALLOC.inner.lock(|pool| { + for idx in 2..pool.len() { + if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr { + U32_GRAND_ALLOC.free(&mut pool[idx]); + return; + } + } + panic!("Didn't deallocate!"); + }); + } + 8 => { + U64_GRAND_ALLOC.inner.lock(|pool| { + for idx in 2..pool.len() { + if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr { + U64_GRAND_ALLOC.free(&mut pool[idx]); + return; } - Some(elem) => { - return (*elem).inner() as *mut u8; + } + panic!("Didn't deallocate!"); + }); + } + 16 => { + U128_GRAND_ALLOC.inner.lock(|pool| { + for idx in 2..pool.len() { + if &mut (*pool[idx].inner() as u8) as *mut u8 == ptr { + U128_GRAND_ALLOC.free(&mut pool[idx]); + return; } } - }) + panic!("Didn't deallocate!"); + }); } _ => { - panic!("No allocators for size {}!", layout.size()); + panic!("No deallocators for size {}!", layout.size()); } } } - - unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) { - self.lock(|qa| { - qa.inner.lock(|pool| { - for idx in 2..COUNT { - if pool[idx].inner() as *mut u8 == _ptr { - qa.free(&mut pool[idx]); - return; - } - } - }); - }); - } } -const GLOBAL_ALLOCATOR_SIZE: usize = 100; - -// TODO: Add other allocation sizes #[global_allocator] -pub static ALLOCATOR: NullLock> = NullLock::new(QueueAllocator::{inner: NullLock::new([QueueItem{data: 0, next: None}; {GLOBAL_ALLOCATOR_SIZE+2}])}); -*/ +pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; -- cgit v1.2.1