From cf016f0c8b28c7198ae6adf3e6c88c61b91b07d0 Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Sat, 20 Aug 2022 23:15:52 -0700 Subject: Debug output TODO: Add more debugging output --- src/mem/alloc.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/mem/alloc.rs') diff --git a/src/mem/alloc.rs b/src/mem/alloc.rs index 1a4ad77..e104293 100644 --- a/src/mem/alloc.rs +++ b/src/mem/alloc.rs @@ -8,6 +8,8 @@ use crate::sync::interface::Mutex; use core::fmt; use core::fmt::{Debug,Formatter}; +use crate::vprintln; + /// # Initialize Queue /// - Name: Symbol name /// - Size: Number of elements @@ -60,7 +62,11 @@ impl Debug for QueueItem<'_,T> { /// /// Output the encapsulated data fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{:?} {:x} {:?}", self.data, self as *const QueueItem<'_,T> as usize, self.next) + #[cfg(feature="verbose")] + return write!(f, "{:?} {:x} {:?}", self.data, self as *const QueueItem<'_,T> as usize, self.next); + + #[cfg(not(feature="verbose"))] + return write!(f, "{:?}", self.data); } } @@ -84,7 +90,9 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// All of the internal elements point to the next /// one and the final element points to `None` pub fn init(&self) { + vprintln!("QA: Initializing Queue Allocator!"); self.inner.lock(|queue| { + vprintln!("QA: Clearing internal references..."); for idx in 2..COUNT { if idx != COUNT-1 { queue[idx].next = Some(&mut queue[idx+1] as *mut QueueItem<'a, T>); @@ -92,9 +100,11 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { queue[idx].next = None; } } + vprintln!("QA: Initializing head and tail..."); queue[0].next = Some(&mut queue[2] as *mut QueueItem<'a, T>); queue[1].next = Some(&mut queue[COUNT-1] as *mut QueueItem<'a, T>); }); + vprintln!("QA: Initialized Queue Allocator!"); } /// # Allocate Data @@ -103,8 +113,10 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// return it, otherwise return `None` #[allow(dead_code)] pub fn alloc(&self) -> Option<&mut QueueItem<'a,T>> { + vprintln!("QA: Allocating chunk!"); return self.inner.lock(|pool| { if let Some(entry) = pool[0].next { + vprintln!("QA: Found chunk!"); pool[0].next = unsafe { (*entry).next }; unsafe { (*entry).next = None; @@ -115,8 +127,10 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { } _ => {} } + vprintln!("QA: Allocating {:x}", unsafe{(*entry).ptr() as usize}); return Some(unsafe{&mut *entry as &mut QueueItem<'a,T>}); } else { + vprintln!("QA: No chunks available!"); return None; } }); @@ -128,6 +142,7 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { /// If there were no items, set it as the head. #[allow(dead_code)] pub fn free(&self, freed_item: &mut QueueItem<'a,T>) { + vprintln!("QA: Deallocating chunk!"); self.inner.lock(|pool| { freed_item.next = None; match pool[1].next { @@ -141,6 +156,7 @@ impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { } } pool[1].next = Some(freed_item as *mut QueueItem<'a,T>); + vprintln!("QA: Deallocated {:x}", freed_item.ptr() as usize); }); } } @@ -152,7 +168,11 @@ impl Debug for QueueAllocator<'_,T,COUNT> { /// its debug formatter. fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { self.inner.lock(|queue| { - write!(f, "{:?}", queue) + #[cfg(feature="verbose")] + return write!(f, "{:?}", queue); + + #[cfg(not(feature="verbose"))] + return write!(f, "{:?}", queue); }) } } @@ -237,16 +257,27 @@ init_queue!(U4096_GRAND_ALLOC, GRAND_ALLOC_SIZE, {U4096::new()}, U4096); impl GrandAllocator { pub fn init(&self) -> Result<(), &'static str> { + vprintln!("GA: Init U8 Pool"); U8_GRAND_ALLOC.init(); + vprintln!("GA: Init U16 Pool"); U16_GRAND_ALLOC.init(); + vprintln!("GA: Init U32 Pool"); U32_GRAND_ALLOC.init(); + vprintln!("GA: Init U64 Pool"); U64_GRAND_ALLOC.init(); + vprintln!("GA: Init U128 Pool"); U128_GRAND_ALLOC.init(); + vprintln!("GA: Init U256 Pool"); U256_GRAND_ALLOC.init(); + vprintln!("GA: Init U512 Pool"); U512_GRAND_ALLOC.init(); + vprintln!("GA: Init U1024 Pool"); U1024_GRAND_ALLOC.init(); + vprintln!("GA: Init U2048 Pool"); U2048_GRAND_ALLOC.init(); + vprintln!("GA: Init U4096 Pool"); U4096_GRAND_ALLOC.init(); + vprintln!("GA: Pools Initialized!"); Ok(()) } } @@ -256,6 +287,7 @@ unsafe impl GlobalAlloc for GrandAllocator { /// /// Allocate the fixed size chunks unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + vprintln!("GA: Allocating chunk of size {}!", layout.size()); match layout.size() { 1 => { match U8_GRAND_ALLOC.alloc() { @@ -367,6 +399,7 @@ unsafe impl GlobalAlloc for GrandAllocator { /// /// Deallocate the fixed size chunks by searching for them unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + vprintln!("GA: Deallocating chunk of size {}!", layout.size()); match layout.size() { 1 => { U8_GRAND_ALLOC.inner.lock(|pool| { @@ -376,6 +409,7 @@ unsafe impl GlobalAlloc for GrandAllocator { assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE); assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing); U8_GRAND_ALLOC.free(&mut pool[index+2]); + vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing); }); } 2 => { @@ -485,5 +519,6 @@ pub static ALLOCATOR: GrandAllocator = GrandAllocator{}; /// /// Returns a borrow for the Global Allocator pub fn alloc() -> &'static crate::mem::alloc::GrandAllocator { + vprintln!("AL: Getting global allocator!"); &crate::mem::alloc::ALLOCATOR } -- cgit v1.2.1