diff options
| author | Christian Cunningham <c@localhost> | 2022-08-23 22:18:04 -0700 |
|---|---|---|
| committer | Christian Cunningham <c@localhost> | 2022-08-23 22:18:04 -0700 |
| commit | e746ab10da35e5d9ef957c72adf9a3ec7a7df225 (patch) | |
| tree | daf55ae2215e7934292f21de68727ba733c7b3af /src | |
| parent | ec68c0209227dd371b8f1c86890575eac0277695 (diff) | |
Queue trait
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 1 | ||||
| -rw-r--r-- | src/serial.rs | 30 | ||||
| -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 |
6 files changed, 124 insertions, 88 deletions
@@ -3,5 +3,6 @@ //! Processor independent code for operating systems! //! Provides common structures for synchronization, memory, etc. #![no_std] +pub mod serial; pub mod sync; pub mod util; diff --git a/src/serial.rs b/src/serial.rs new file mode 100644 index 0000000..792ddf7 --- /dev/null +++ b/src/serial.rs @@ -0,0 +1,30 @@ +//! # Serial Writer +//! +//! Write to serial +//! +//! ```rust +//! #[doc(hidden)] +//! pub fn _serial_print(args: core::fmt::Arguments) { +//! use core::fmt::Write; +//! SERIAL1.lock(|serial| { +//! ... +//! }); +//! } +//! ``` + +/// Prints to the host through the serial interface. +#[macro_export] +macro_rules! serial_print { + ($($arg:tt)*) => { + $crate::serial::_print(format_args!($($arg)*)); + }; +} + +/// Prints to the host through the serial interface, appending a newline. +#[macro_export] +macro_rules! serial_println { + () => ($crate::serial_print!("\n")); + ($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n"))); + ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( + concat!($fmt, "\n"), $($arg)*)); +} 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>); +} |
