From ce4585a574c638f4dfa0b482074d4c3134cab2f9 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Sat, 20 Aug 2022 12:09:16 -0700 Subject: Allocate in-between --- src/mem/alloc.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/mem') diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs index 74f5f1d..ed73e72 100644 --- a/src/mem/alloc.rs +++ b/src/mem/alloc.rs @@ -168,11 +168,13 @@ use alloc::alloc::{GlobalAlloc,Layout}; pub struct GrandAllocator { } const GRAND_ALLOC_SIZE: usize = 64; + init_queue!(U8_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u8); init_queue!(U16_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u16); init_queue!(U32_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u32); init_queue!(U64_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u64); init_queue!(U128_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u128); + impl GrandAllocator { pub fn init(&self) { U8_GRAND_ALLOC.init(); @@ -184,7 +186,11 @@ impl GrandAllocator { } unsafe impl GlobalAlloc for GrandAllocator { + /// # Allocator + /// + /// Allocate the fixed size chunks unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + crate::println!("Size: {}", layout.size()); match layout.size() { 1 => { match U8_GRAND_ALLOC.alloc() { @@ -206,7 +212,7 @@ unsafe impl GlobalAlloc for GrandAllocator { } } } - 4 => { + 3..4 => { match U32_GRAND_ALLOC.alloc() { None => { panic!("No cells to allocate!"); @@ -216,7 +222,7 @@ unsafe impl GlobalAlloc for GrandAllocator { } } } - 8 => { + 5..8 => { match U64_GRAND_ALLOC.alloc() { None => { panic!("No cells to allocate!"); @@ -226,7 +232,7 @@ unsafe impl GlobalAlloc for GrandAllocator { } } } - 16 => { + 9..16 => { match U128_GRAND_ALLOC.alloc() { None => { panic!("No cells to allocate!"); @@ -242,6 +248,9 @@ unsafe impl GlobalAlloc for GrandAllocator { } } + /// # Deallocate + /// + /// Deallocate the fixed size chunks by searching for them unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { match layout.size() { 1 => { @@ -266,7 +275,7 @@ unsafe impl GlobalAlloc for GrandAllocator { panic!("Didn't deallocate!"); }); } - 4 => { + 3..4 => { U32_GRAND_ALLOC.inner.lock(|pool| { for idx in 2..pool.len() { if pool[idx].ptr() == ptr { @@ -277,7 +286,7 @@ unsafe impl GlobalAlloc for GrandAllocator { panic!("Didn't deallocate!"); }); } - 8 => { + 5..8 => { U64_GRAND_ALLOC.inner.lock(|pool| { for idx in 2..pool.len() { if pool[idx].ptr() == ptr { @@ -288,7 +297,7 @@ unsafe impl GlobalAlloc for GrandAllocator { panic!("Didn't deallocate!"); }); } - 16 => { + 9..16 => { U128_GRAND_ALLOC.inner.lock(|pool| { for idx in 2..pool.len() { if pool[idx].ptr() == ptr { @@ -306,5 +315,8 @@ unsafe impl GlobalAlloc for GrandAllocator { } } +/// # Grand Allocator +/// +/// The allocator of allocators. It hands out fixed sized memory chunks. #[global_allocator] pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; -- cgit v1.2.1