summaryrefslogtreecommitdiff
path: root/src/util/fifo_queue.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/fifo_queue.rs')
-rw-r--r--src/util/fifo_queue.rs159
1 files changed, 77 insertions, 82 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);
+ })
+ }
}