diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/kernel.rs | 53 | ||||
| -rw-r--r-- | src/mem/alloc.rs | 39 | ||||
| -rw-r--r-- | src/print.rs | 26 | 
3 files changed, 107 insertions, 11 deletions
| diff --git a/src/kernel.rs b/src/kernel.rs index 3d93e37..1c3bfac 100644 --- a/src/kernel.rs +++ b/src/kernel.rs @@ -27,7 +27,6 @@ mod print;  mod sync;  mod uart;  use crate::console::console; -use crate::console::interface::Statistics;  use crate::mem::alloc::alloc;  /// # Initialization Code @@ -48,13 +47,49 @@ unsafe fn kernel_init() -> ! {  ///  /// TODO: Figure out what to do here  fn kernel_main() -> ! { -	draw::draw_ukraine_flag(); -	println!(); -	draw::draw_american_flag(); -	println!(); - -	println!("\x1b[91mInitialized\x1b[0m \x1b[92m{}\x1b[0m \x1b[93mv{}\x1b[0m", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); -	println!("\x1b[94mAuthors:\x1b[0m \x1b[95m{}\x1b[0m", env!("CARGO_PKG_AUTHORS")); -	println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); +	#[cfg(not(feature="verbose"))] +	{ +		draw::draw_ukraine_flag(); +		println!(); +		draw::draw_american_flag(); +		println!(); + +		println!("\x1b[91mInitialized\x1b[0m \x1b[92m{}\x1b[0m \x1b[93mv{}\x1b[0m", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")); +		println!("\x1b[94mAuthors:\x1b[0m \x1b[95m{}\x1b[0m", env!("CARGO_PKG_AUTHORS")); +		use crate::console::interface::Statistics; +		println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); +	} + +	#[cfg(feature="verbose")] +	{ +		use alloc::boxed::Box; +		{ +			let a: Box<u8> = Box::new(1); +			println!("Box: {}", a); +		} +		{ +			let a: Box<u8> = Box::new(2); +			let b: Box<u8> = Box::new(3); +			let c: Box<u8> = Box::new(4); +			println!("Boxes: {}, {}, {}", a, b, c); +		} +		{ +			let a: Box<u8> = Box::new(5); +			let b: Box<u8> = Box::new(6); +			let c: Box<u8> = Box::new(7); +			println!("Boxes: {}, {}, {}", a, b, c); +		} +		println!("U8: {:?}", mem::alloc::U8_GRAND_ALLOC); +		use alloc::string::String; +		{ +			let mut s = String::new(); +			for _ in 0..128 { +				s += "TEST"; +			} +			println!("String: Length {}", s.capacity()); +		} +		use crate::console::interface::Statistics; +		println!("Characters written to UART: \x1b[91m{}\x1b[0m", console().chars_written()); +	}  	loop { }  } 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<T: Debug> 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<T: Debug,const COUNT: usize> 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  } diff --git a/src/print.rs b/src/print.rs index 88f39da..1598378 100644 --- a/src/print.rs +++ b/src/print.rs @@ -28,3 +28,29 @@ macro_rules! println {  		$crate::print::_print(format_args_nl!($($arg)*));  	})  } + +/// # Debug print without newline +/// +/// Print formatted arguments without a newline but only with `verbose` feature +#[macro_export] +macro_rules! vprint { +	($($arg:tt)*) => ({ +		#[cfg(feature="verbose")] +		$crate::print::_print(format_args!($($arg)*)) +	}); +} + +/// # Debug print with newline +/// +/// Print formatted arguments with a newline but only with `verbose` feature +#[macro_export] +macro_rules! vprintln { +	() => ({ +		#[cfg(feature="verbose")] +		$crate::print!("\n") +	}); +	($($arg:tt)*) => ({ +		#[cfg(feature="verbose")] +		$crate::print::_print(format_args_nl!($($arg)*)); +	}) +} | 
