diff options
Diffstat (limited to 'src/util')
| -rw-r--r-- | src/util/fifo_queue.rs | 159 | ||||
| -rw-r--r-- | src/util/lifo_queue.rs | 13 | ||||
| -rw-r--r-- | src/util/mod.rs | 1 | ||||
| -rw-r--r-- | src/util/queue.rs | 8 |
4 files changed, 93 insertions, 88 deletions
diff --git a/src/util/fifo_queue.rs b/src/util/fifo_queue.rs index 3e27fc3..0f1f737 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::util::node::*; use core::fmt; -use core::fmt::{Debug,Formatter}; +use core::fmt::{Debug, Formatter}; /// # Initialize Queue /// - Name: Symbol name @@ -28,10 +28,10 @@ macro_rules! init_fifo_queue { /// /// Structure to store a pool of allocated data structures. pub struct FifoQueue<'a, T: Sized, const COUNT: usize> { - /// # Synchronized Pool of items - /// - /// Stores synchronization wrapper around the data pool - pub inner: NullLock<[Node<'a, T>;COUNT]>, + /// # Synchronized Pool of items + /// + /// Stores synchronization wrapper around the data pool + pub inner: NullLock<[Node<'a, T>; COUNT]>, } impl<'a, T: Sized, const COUNT: usize> FifoQueue<'a, T, COUNT> { @@ -42,85 +42,80 @@ impl<'a, T: Sized, const COUNT: usize> FifoQueue<'a, T, COUNT> { } /// # Sharing Thread Safety for FifoQueue -unsafe impl<T,const COUNT: usize> Send for FifoQueue<'_,T,COUNT> {} +unsafe impl<T, const COUNT: usize> Send for FifoQueue<'_, T, COUNT> {} -impl<'a, T: Sized,const COUNT: usize> FifoQueue<'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) { - self.inner.lock(|queue| { - for idx in 2..COUNT { - if idx != COUNT-1 { - queue[idx].next = Some(&mut queue[idx+1] as *mut Node<'a, T>); - } else { - queue[idx].next = None; - } - } - queue[0].next = Some(&mut queue[2] as *mut Node<'a, T>); - queue[1].next = Some(&mut queue[COUNT-1] as *mut Node<'a, T>); - }); - } +impl<'a, T: Sized, const COUNT: usize> crate::util::queue::Queue<'a> for FifoQueue<'a, T, COUNT> { + type Data = T; + /// # 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` + fn init(&self) { + self.inner.lock(|queue| { + for idx in 2..COUNT { + if idx != COUNT - 1 { + queue[idx].next = Some(&mut queue[idx + 1] as *mut Node<'a, Self::Data>); + } else { + queue[idx].next = None; + } + } + queue[0].next = Some(&mut queue[2] as *mut Node<'a, Self::Data>); + queue[1].next = Some(&mut queue[COUNT - 1] as *mut Node<'a, Self::Data>); + }); + } - /// # Allocate Data - /// - /// If there is a data chunk available, - /// return it, otherwise return `None` - #[allow(dead_code)] - pub fn alloc(&self) -> Option<&mut Node<'a,T>> { - return self.inner.lock(|pool| { - if let Some(entry) = pool[0].next { - pool[0].next = unsafe { (*entry).next }; - unsafe { - (*entry).next = None; - } - match pool[0].next { - None => { - pool[1].next = None - } - _ => {} - } - return Some(unsafe{&mut *entry as &mut Node<'a,T>}); - } else { - return None; - } - }); - } + /// # Pop + /// + /// If there is a data chunk available, + /// return it, otherwise return `None` + fn pop(&self) -> Option<&mut Node<'a, Self::Data>> { + return self.inner.lock(|pool| { + if let Some(entry) = pool[0].next { + pool[0].next = unsafe { (*entry).next }; + unsafe { + (*entry).next = None; + } + match pool[0].next { + None => pool[1].next = None, + _ => {} + } + return Some(unsafe { &mut *entry as &mut Node<'a, Self::Data> }); + } else { + 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 Node<'a,T>) { - self.inner.lock(|pool| { - freed_item.next = None; - match pool[1].next { - None => { - pool[0].next = Some(freed_item as *mut Node<'a,T>); - } - Some(entry) => { - unsafe { - (*entry).next = Some(freed_item as *mut Node<'a,T>); - } - } - } - pool[1].next = Some(freed_item as *mut Node<'a,T>); - }); - } + /// # Push + /// + /// Add the item to the end of the queue. + /// If there were no items, set it as the head. + fn push(&self, freed_item: &mut Node<'a, Self::Data>) { + self.inner.lock(|pool| { + freed_item.next = None; + match pool[1].next { + None => { + pool[0].next = Some(freed_item as *mut Node<'a, Self::Data>); + } + Some(entry) => unsafe { + (*entry).next = Some(freed_item as *mut Node<'a, Self::Data>); + }, + } + pool[1].next = Some(freed_item as *mut Node<'a, Self::Data>); + }); + } } -impl<T: Debug,const COUNT: usize> Debug for FifoQueue<'_,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| { - return write!(f, "{:?}", queue); - }) - } +impl<T: Debug, const COUNT: usize> Debug for FifoQueue<'_, 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| { + return write!(f, "{:?}", queue); + }) + } } diff --git a/src/util/lifo_queue.rs b/src/util/lifo_queue.rs index 2fa7304..07b0669 100644 --- a/src/util/lifo_queue.rs +++ b/src/util/lifo_queue.rs @@ -1,11 +1,11 @@ //! # Queue //! //! Queue structure -use crate::sync::NullLock; use crate::sync::interface::Mutex; +use crate::sync::NullLock; use crate::util::node::*; use core::fmt; -use core::fmt::{Debug,Formatter}; +use core::fmt::{Debug, Formatter}; /// # Initialize Queue /// - Name: Symbol name @@ -44,14 +44,15 @@ impl<'a, T: Sized, const COUNT: usize> LifoQueue<'a, T, COUNT> { /// # Sharing Thread Safety for LifoQueue unsafe impl<T, const COUNT: usize> Send for LifoQueue<'_, T, COUNT> {} -impl<'a, T: Sized, const COUNT: usize> LifoQueue<'a, T, COUNT> { +impl<'a, T: Sized, const COUNT: usize> crate::util::queue::Queue<'a> for LifoQueue<'a, T, COUNT> { + type Data = T; /// # 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) { + fn init(&self) { self.inner.lock(|queue| { for idx in 1..COUNT { if idx != COUNT - 1 { @@ -67,7 +68,7 @@ impl<'a, T: Sized, const COUNT: usize> LifoQueue<'a, T, COUNT> { /// # Pop /// /// Get an item from the queue - pub fn pop(&self) -> Option<&mut Node<'a, T>> { + fn pop(&self) -> Option<&mut Node<'a, T>> { return self.inner.lock(|pool| { if let Some(entry) = pool[0].next { unsafe { @@ -84,7 +85,7 @@ impl<'a, T: Sized, const COUNT: usize> LifoQueue<'a, T, COUNT> { /// # Push /// /// Add the item to the start of the queue. - pub fn push(&self, freed_item: &mut Node<'a, T>) { + fn push(&self, freed_item: &mut Node<'a, T>) { self.inner.lock(|pool| { freed_item.next = pool[0].next; pool[0].next = Some(freed_item as *mut Node<'a, T>); diff --git a/src/util/mod.rs b/src/util/mod.rs index 5656a3e..9c2a774 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -2,3 +2,4 @@ pub mod fifo_queue; pub mod lifo_queue; pub mod node; +pub mod queue; diff --git a/src/util/queue.rs b/src/util/queue.rs new file mode 100644 index 0000000..b9e6edc --- /dev/null +++ b/src/util/queue.rs @@ -0,0 +1,8 @@ +use super::node::Node; +pub trait Queue<'a> { + type Data; + + fn init(&self); + fn pop(&self) -> Option<&mut Node<'a,Self::Data>>; + fn push(&self, freed_item: &mut Node<'a,Self::Data>); +} |
