1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
//! # Queue
//!
//! Queue structure
use crate::sync::interface::Mutex;
use crate::sync::NullLock;
use crate::util::node::*;
use core::fmt;
use core::fmt::{Debug, Formatter};
use core::marker::Sized;
/// # Initialize Queue
/// - Name: Symbol name
/// - Size: Number of elements
/// - Default: Default value
/// - Type: Data Type
#[macro_export]
macro_rules! init_lifo_queue {
($name:tt,$size:tt,$default:tt,$type:ty) => {
init_queue!{@gen [$name,$size,$default,$type,concat!("# ", stringify!($type), " Queue Allocator")]}
};
(@gen [$name:tt,$size:tt,$default:tt,$type:ty,$doc:expr]) => {
#[doc = $doc]
#[link_section = ".data.alloc"]
pub static $name: LifoQueue<'static, $type, {$size+1}> = LifoQueue::new(NullLock::new([Node::new($default); {$size+1}]));
};
}
/// # Queue Allocator
///
/// Structure to store a pool of allocated data structures.
pub struct LifoQueue<'a, T: Sized, const COUNT: usize> {
/// # Synchronized Pool of items
///
/// Stores synchronization wrapper around the data pool
inner: NullLock<[Node<'a, T>; COUNT]>,
}
impl<'a, T: Sized, const COUNT: usize> LifoQueue<'a, T, COUNT> {
pub const fn new(initial: NullLock<[Node<'a, T>; COUNT]>) -> Self {
Self { inner: initial }
}
}
/// # 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> {
/// # 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 1..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[1] as *mut Node<'a, T>);
});
}
/// # Pop
///
/// Get an item from the queue
#[allow(dead_code)]
pub fn pop(&self) -> Option<&mut Node<'a, T>> {
return self.inner.lock(|pool| {
if let Some(entry) = pool[0].next {
unsafe {
pool[0].next = (*entry).next;
(*entry).next = None;
return Some(&mut *entry as &mut Node<'a, T>);
}
} else {
return None;
}
});
}
/// # Push
///
/// Add the item to the start of the queue.
#[allow(dead_code)]
pub 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>);
});
}
}
impl<T: Debug, const COUNT: usize> Debug for LifoQueue<'_, 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);
})
}
}
|