From 6e293d029bd60f5565bb18629e3baf2d049e53cf Mon Sep 17 00:00:00 2001 From: Christian Cunningham Date: Wed, 24 Aug 2022 19:29:00 -0700 Subject: *cargo fmt --- src/util/fifo_queue.rs | 273 +++++++++++++++++++++++++------------------------ 1 file changed, 139 insertions(+), 134 deletions(-) (limited to 'src/util/fifo_queue.rs') diff --git a/src/util/fifo_queue.rs b/src/util/fifo_queue.rs index 744b7f9..42d63ac 100644 --- a/src/util/fifo_queue.rs +++ b/src/util/fifo_queue.rs @@ -1,11 +1,11 @@ //! # FIFO Queue //! //! Provides the FIFO queue structure for allocations -use crate::sync::NullLock; use crate::sync::interface::Mutex; +use crate::sync::NullLock; use crate::vprintln; use core::fmt; -use core::fmt::{Debug,Formatter}; +use core::fmt::{Debug, Formatter}; /// # Initialize Queue /// - Name: Symbol name @@ -24,163 +24,168 @@ macro_rules! init_queue { }; } -#[derive(Copy,Clone)] +#[derive(Copy, Clone)] /// # Queue Item /// /// Encapsulates a data element and a pointer to /// the next `Queue` item pub struct QueueItem<'a, T: Sized> { - /// # Data - /// - /// The encapsulated data - data: T, - /// # Pointer to the next item - /// - /// Stores either `None` or points - /// to the next item. - next: Option<*mut QueueItem<'a, T>>, + /// # Data + /// + /// The encapsulated data + data: T, + /// # Pointer to the next item + /// + /// Stores either `None` or points + /// to the next item. + next: Option<*mut QueueItem<'a, T>>, } -impl QueueItem<'_,T> { - /// # Constructor - pub const fn new(data: T) -> Self { - Self { - data: data, - next: None, - } - } - /// # Get the inner data - /// - /// Returns a borrow of the underlying data. - pub fn inner(&mut self) -> &mut T { - &mut self.data - } - /// # Get pointer to inner data - pub fn ptr(&mut self) -> *mut u8 { - self.inner() as *mut T as *mut u8 - } +impl QueueItem<'_, T> { + /// # Constructor + pub const fn new(data: T) -> Self { + Self { + data: data, + next: None, + } + } + /// # Get the inner data + /// + /// Returns a borrow of the underlying data. + pub fn inner(&mut self) -> &mut T { + &mut self.data + } + /// # Get pointer to inner data + pub fn ptr(&mut self) -> *mut u8 { + self.inner() as *mut T as *mut u8 + } } /// # Sharing Thread Safety for QueueItem -unsafe impl Send for QueueItem<'_,T> {} +unsafe impl Send for QueueItem<'_, T> {} -impl Debug for QueueItem<'_,T> { - /// # Debug formatter for `QueueItem` - /// - /// Output the encapsulated data - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - #[cfg(feature="verbose")] - return write!(f, "{:?} {:x} {:?}", self.data, self as *const QueueItem<'_,T> as usize, self.next); +impl Debug for QueueItem<'_, T> { + /// # Debug formatter for `QueueItem` + /// + /// Output the encapsulated data + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + #[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); - } + #[cfg(not(feature = "verbose"))] + return write!(f, "{:?}", self.data); + } } /// # Queue Allocator /// /// Structure to store a pool of allocated data structures. pub struct QueueAllocator<'a, T: Sized, const COUNT: usize> { - /// # Synchronized Pool of items - /// - /// Stores synchronization wrapper around the data pool - pub inner: NullLock<[QueueItem<'a, T>;COUNT]>, + /// # Synchronized Pool of items + /// + /// Stores synchronization wrapper around the data pool + pub inner: NullLock<[QueueItem<'a, T>; COUNT]>, } /// # Sharing Thread Safety for QueueAllocator -unsafe impl Send for QueueAllocator<'_,T,COUNT> {} +unsafe impl Send for QueueAllocator<'_, T, COUNT> {} -impl<'a, T: Sized,const COUNT: usize> QueueAllocator<'a, T, COUNT> { - /// # Initialization of Fixed-Size Pool - /// - /// Establishes the header and footer of the queue - /// as the first and second elements respectively. - /// 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>); - } else { - 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!"); - } +impl<'a, T: Sized, const COUNT: usize> QueueAllocator<'a, T, COUNT> { + /// # Initialization of Fixed-Size Pool + /// + /// Establishes the header and footer of the queue + /// as the first and second elements respectively. + /// 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>); + } else { + 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 - /// - /// If there is a data chunk available, - /// 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; - } - match pool[0].next { - None => { - pool[1].next = None - } - _ => {} - } - vprintln!("QA: \x1b[92mAllocated {:x}\x1b[0m", unsafe{(*entry).ptr() as usize}); - return Some(unsafe{&mut *entry as &mut QueueItem<'a,T>}); - } else { - vprintln!("QA: No chunks available!"); - return None; - } - }); - } + /// # Allocate Data + /// + /// If there is a data chunk available, + /// 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; + } + match pool[0].next { + None => pool[1].next = None, + _ => {} + } + vprintln!("QA: \x1b[92mAllocated {:x}\x1b[0m", unsafe { + (*entry).ptr() as usize + }); + return Some(unsafe { &mut *entry as &mut QueueItem<'a, T> }); + } else { + vprintln!("QA: No chunks available!"); + return None; + } + }); + } - /// # Free - /// - /// Add the item to the end of the queue. - /// 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 { - None => { - pool[0].next = Some(freed_item as *mut QueueItem<'a,T>); - } - Some(entry) => { - unsafe { - (*entry).next = Some(freed_item as *mut QueueItem<'a,T>); - } - } - } - pool[1].next = Some(freed_item as *mut QueueItem<'a,T>); - vprintln!("QA: \x1b[91mDeallocated {:x}\x1b[0m", freed_item.ptr() as usize); - }); - } + /// # Free + /// + /// Add the item to the end of the queue. + /// 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 { + None => { + pool[0].next = Some(freed_item as *mut QueueItem<'a, T>); + } + Some(entry) => unsafe { + (*entry).next = Some(freed_item as *mut QueueItem<'a, T>); + }, + } + pool[1].next = Some(freed_item as *mut QueueItem<'a, T>); + vprintln!( + "QA: \x1b[91mDeallocated {:x}\x1b[0m", + freed_item.ptr() as usize + ); + }); + } } -impl Debug for QueueAllocator<'_,T,COUNT> { - /// # Debug Formatted Output - /// - /// Output each data point in the array with - /// its debug formatter. - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - self.inner.lock(|queue| { - #[cfg(feature="verbose")] - return write!(f, "{:?}", queue); +impl Debug for QueueAllocator<'_, T, COUNT> { + /// # Debug Formatted Output + /// + /// Output each data point in the array with + /// its debug formatter. + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + self.inner.lock(|queue| { + #[cfg(feature = "verbose")] + return write!(f, "{:?}", queue); - #[cfg(not(feature="verbose"))] - return write!(f, "{:?}", queue); - }) - } + #[cfg(not(feature = "verbose"))] + return write!(f, "{:?}", queue); + }) + } } -- cgit v1.2.1