introduce heapless memory pools

This commit is contained in:
Robin Müller 2024-05-21 15:14:15 +02:00
parent 0b2d4f6187
commit 53bba36ea0
Signed by: muellerr
GPG Key ID: A649FB78196E3849
9 changed files with 542 additions and 85 deletions

View File

@ -182,7 +182,7 @@ pub mod pool {
use super::*; use super::*;
pub fn create_static_pools() -> (StaticMemoryPool, StaticMemoryPool) { pub fn create_static_pools() -> (StaticMemoryPool, StaticMemoryPool) {
( (
StaticMemoryPool::new(StaticPoolConfig::new( StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![ vec![
(30, 32), (30, 32),
(15, 64), (15, 64),
@ -193,7 +193,7 @@ pub mod pool {
], ],
true, true,
)), )),
StaticMemoryPool::new(StaticPoolConfig::new( StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![ vec![
(30, 32), (30, 32),
(15, 64), (15, 64),
@ -208,7 +208,7 @@ pub mod pool {
} }
pub fn create_sched_tc_pool() -> StaticMemoryPool { pub fn create_sched_tc_pool() -> StaticMemoryPool {
StaticMemoryPool::new(StaticPoolConfig::new( StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![ vec![
(30, 32), (30, 32),
(15, 64), (15, 64),

View File

@ -100,6 +100,9 @@ tempfile = "3"
[dev-dependencies.postcard] [dev-dependencies.postcard]
version = "1" version = "1"
[dev-dependencies.heapless]
version = "0.7"
[features] [features]
default = ["std"] default = ["std"]
std = [ std = [

View File

@ -75,6 +75,9 @@
pub use alloc_mod::*; pub use alloc_mod::*;
use core::fmt::{Display, Formatter}; use core::fmt::{Display, Formatter};
use delegate::delegate; use delegate::delegate;
use derive_new::new;
#[cfg(feature = "heapless")]
pub use heapless_mod::*;
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use spacepackets::ByteConversionError; use spacepackets::ByteConversionError;
@ -351,10 +354,269 @@ impl<'a, MemProvider: PoolProvider> PoolRwGuard<'a, MemProvider> {
); );
} }
type UsedBlockSize = usize;
#[deprecated(since = "0.2.2", note = "use UsedBlockSize instead")]
type PoolSize = UsedBlockSize;
const STORE_FREE: UsedBlockSize = UsedBlockSize::MAX;
pub const MAX_BLOCK_SIZE: UsedBlockSize = STORE_FREE - 1;
#[derive(Copy, Clone, Debug, PartialEq, Eq, new)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct SubpoolConfig {
num_blocks: NumBlocks,
block_size: usize,
}
// #[cfg(feature = "heapless")]
pub mod heapless_mod {
use std::println;
use super::*;
pub struct PoolIsFull;
pub struct StaticHeaplessMemoryPool<const MAX_NUM_SUBPOOLS: usize> {
pool: heapless::Vec<(SubpoolConfig, &'static mut [u8]), MAX_NUM_SUBPOOLS>,
sizes_lists: heapless::Vec<&'static mut [UsedBlockSize], MAX_NUM_SUBPOOLS>,
spill_to_higher_subpools: bool,
}
impl<const MAX_NUM_SUBPOOLS: usize> StaticHeaplessMemoryPool<MAX_NUM_SUBPOOLS> {
pub fn new(spill_to_higher_subpools: bool) -> Self {
Self {
pool: heapless::Vec::new(),
sizes_lists: heapless::Vec::new(),
spill_to_higher_subpools,
}
}
pub fn grow(
&mut self,
pool_slice: &'static mut [u8],
sizes_list_slice: &'static mut [UsedBlockSize],
num_blocks: NumBlocks,
) -> Result<(), PoolIsFull> {
assert!(
(pool_slice.len() % num_blocks as usize) == 0,
"pool slice length must be multiple of number of blocks"
);
assert!(
num_blocks as usize == sizes_list_slice.len(),
"used block size list slice must be of same length as number of blocks"
);
let subpool_config = SubpoolConfig {
num_blocks,
block_size: pool_slice.len() / num_blocks as usize,
};
self.pool
.push((subpool_config, pool_slice))
.map_err(|_| PoolIsFull)?;
self.sizes_lists
.push(sizes_list_slice)
.map_err(|_| PoolIsFull)?;
Ok(())
}
fn reserve(&mut self, data_len: usize) -> Result<StaticPoolAddr, PoolError> {
if self.pool.is_empty() {
return Err(PoolError::StoreFull(0));
}
let mut subpool_idx = self.find_subpool(data_len, 0)?;
if self.spill_to_higher_subpools {
while let Err(PoolError::StoreFull(_)) = self.find_empty(subpool_idx) {
if (subpool_idx + 1) as usize == self.sizes_lists.len() {
return Err(PoolError::StoreFull(subpool_idx));
}
subpool_idx += 1;
}
}
let (slot, size_slot_ref) = self.find_empty(subpool_idx)?;
*size_slot_ref = data_len;
Ok(StaticPoolAddr {
pool_idx: subpool_idx,
packet_idx: slot,
})
}
fn addr_check(&self, addr: &StaticPoolAddr) -> Result<usize, PoolError> {
self.validate_addr(addr)?;
let pool_idx = addr.pool_idx as usize;
let size_list = self.sizes_lists.get(pool_idx).unwrap();
let curr_size = size_list[addr.packet_idx as usize];
if curr_size == STORE_FREE {
return Err(PoolError::DataDoesNotExist(PoolAddr::from(*addr)));
}
Ok(curr_size)
}
fn validate_addr(&self, addr: &StaticPoolAddr) -> Result<(), PoolError> {
let pool_idx = addr.pool_idx as usize;
if pool_idx >= self.pool.len() {
return Err(PoolError::InvalidStoreId(
StoreIdError::InvalidSubpool(addr.pool_idx),
Some(PoolAddr::from(*addr)),
));
}
if addr.packet_idx >= self.pool[addr.pool_idx as usize].0.num_blocks {
return Err(PoolError::InvalidStoreId(
StoreIdError::InvalidPacketIdx(addr.packet_idx),
Some(PoolAddr::from(*addr)),
));
}
Ok(())
}
fn find_subpool(&self, req_size: usize, start_at_subpool: u16) -> Result<u16, PoolError> {
for (i, &(pool_cfg, _)) in self.pool.iter().enumerate() {
if i < start_at_subpool as usize {
continue;
}
if pool_cfg.block_size as usize >= req_size {
return Ok(i as u16);
}
}
Err(PoolError::DataTooLarge(req_size))
}
fn write(&mut self, addr: &StaticPoolAddr, data: &[u8]) -> Result<(), PoolError> {
let packet_pos = self.raw_pos(addr).ok_or(PoolError::InternalError(0))?;
let (_elem_size, subpool) = self
.pool
.get_mut(addr.pool_idx as usize)
.ok_or(PoolError::InternalError(1))?;
let pool_slice = &mut subpool[packet_pos..packet_pos + data.len()];
pool_slice.copy_from_slice(data);
Ok(())
}
fn find_empty(&mut self, subpool: u16) -> Result<(u16, &mut usize), PoolError> {
if let Some(size_list) = self.sizes_lists.get_mut(subpool as usize) {
for (i, elem_size) in size_list.iter_mut().enumerate() {
if *elem_size == STORE_FREE {
return Ok((i as u16, elem_size));
}
}
} else {
return Err(PoolError::InvalidStoreId(
StoreIdError::InvalidSubpool(subpool),
None,
));
}
Err(PoolError::StoreFull(subpool))
}
fn raw_pos(&self, addr: &StaticPoolAddr) -> Option<usize> {
let (pool_cfg, _) = self.pool.get(addr.pool_idx as usize)?;
Some(addr.packet_idx as usize * pool_cfg.block_size as usize)
}
}
impl<const MAX_NUM_SUBPOOLS: usize> PoolProvider for StaticHeaplessMemoryPool<MAX_NUM_SUBPOOLS> {
fn add(&mut self, data: &[u8]) -> Result<PoolAddr, PoolError> {
let data_len = data.len();
if data_len > MAX_BLOCK_SIZE {
return Err(PoolError::DataTooLarge(data_len));
}
let addr = self.reserve(data_len)?;
self.write(&addr, data)?;
Ok(addr.into())
}
fn free_element<W: FnMut(&mut [u8])>(
&mut self,
len: usize,
mut writer: W,
) -> Result<PoolAddr, PoolError> {
if len > MAX_BLOCK_SIZE {
return Err(PoolError::DataTooLarge(len));
}
let addr = self.reserve(len)?;
let raw_pos = self.raw_pos(&addr).unwrap();
let block =
&mut self.pool.get_mut(addr.pool_idx as usize).unwrap().1[raw_pos..raw_pos + len];
writer(block);
Ok(addr.into())
}
fn modify<U: FnMut(&mut [u8])>(
&mut self,
addr: &PoolAddr,
mut updater: U,
) -> Result<(), PoolError> {
let addr = StaticPoolAddr::from(*addr);
let curr_size = self.addr_check(&addr)?;
let raw_pos = self.raw_pos(&addr).unwrap();
let block = &mut self.pool.get_mut(addr.pool_idx as usize).unwrap().1
[raw_pos..raw_pos + curr_size];
updater(block);
Ok(())
}
fn read(&self, addr: &PoolAddr, buf: &mut [u8]) -> Result<usize, PoolError> {
let addr = StaticPoolAddr::from(*addr);
let curr_size = self.addr_check(&addr)?;
if buf.len() < curr_size {
return Err(ByteConversionError::ToSliceTooSmall {
found: buf.len(),
expected: curr_size,
}
.into());
}
let raw_pos = self.raw_pos(&addr).unwrap();
let block =
&self.pool.get(addr.pool_idx as usize).unwrap().1[raw_pos..raw_pos + curr_size];
//block.copy_from_slice(&src);
buf[..curr_size].copy_from_slice(block);
Ok(curr_size)
}
fn delete(&mut self, addr: PoolAddr) -> Result<(), PoolError> {
let addr = StaticPoolAddr::from(addr);
self.addr_check(&addr)?;
let subpool_cfg = self.pool.get(addr.pool_idx as usize).unwrap().0;
let raw_pos = self.raw_pos(&addr).unwrap();
let block = &mut self.pool.get_mut(addr.pool_idx as usize).unwrap().1
[raw_pos..raw_pos + subpool_cfg.block_size as usize];
let size_list = self.sizes_lists.get_mut(addr.pool_idx as usize).unwrap();
size_list[addr.packet_idx as usize] = STORE_FREE;
block.fill(0);
Ok(())
}
fn has_element_at(&self, addr: &PoolAddr) -> Result<bool, PoolError> {
let addr = StaticPoolAddr::from(*addr);
self.validate_addr(&addr)?;
let pool_idx = addr.pool_idx as usize;
let size_list = self.sizes_lists.get(pool_idx).unwrap();
let curr_size = size_list[addr.packet_idx as usize];
if curr_size == STORE_FREE {
return Ok(false);
}
Ok(true)
}
fn len_of_data(&self, addr: &PoolAddr) -> Result<usize, PoolError> {
let addr = StaticPoolAddr::from(*addr);
self.validate_addr(&addr)?;
let pool_idx = addr.pool_idx as usize;
let size_list = self.sizes_lists.get(pool_idx).unwrap();
let size = size_list[addr.packet_idx as usize];
Ok(match size {
STORE_FREE => 0,
_ => size,
})
}
}
}
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
mod alloc_mod { mod alloc_mod {
use super::{PoolGuard, PoolProvider, PoolProviderWithGuards, PoolRwGuard, StaticPoolAddr}; use super::*;
use crate::pool::{NumBlocks, PoolAddr, PoolError, StoreIdError}; use crate::pool::{PoolAddr, PoolError, StoreIdError};
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use spacepackets::ByteConversionError; use spacepackets::ByteConversionError;
@ -364,10 +626,6 @@ mod alloc_mod {
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub type SharedStaticMemoryPool = Arc<RwLock<StaticMemoryPool>>; pub type SharedStaticMemoryPool = Arc<RwLock<StaticMemoryPool>>;
type PoolSize = usize;
const STORE_FREE: PoolSize = PoolSize::MAX;
pub const POOL_MAX_SIZE: PoolSize = STORE_FREE - 1;
/// Configuration structure of the [static memory pool][StaticMemoryPool] /// Configuration structure of the [static memory pool][StaticMemoryPool]
/// ///
/// # Parameters /// # Parameters
@ -380,27 +638,42 @@ mod alloc_mod {
/// the chocking of larger subpools by underdimensioned smaller subpools. /// the chocking of larger subpools by underdimensioned smaller subpools.
#[derive(Clone)] #[derive(Clone)]
pub struct StaticPoolConfig { pub struct StaticPoolConfig {
cfg: Vec<(NumBlocks, usize)>, cfg: Vec<SubpoolConfig>,
spill_to_higher_subpools: bool, spill_to_higher_subpools: bool,
} }
impl StaticPoolConfig { impl StaticPoolConfig {
pub fn new(cfg: Vec<(NumBlocks, usize)>, spill_to_higher_subpools: bool) -> Self { pub fn new(cfg: Vec<SubpoolConfig>, spill_to_higher_subpools: bool) -> Self {
StaticPoolConfig { StaticPoolConfig {
cfg, cfg,
spill_to_higher_subpools, spill_to_higher_subpools,
} }
} }
pub fn cfg(&self) -> &Vec<(NumBlocks, usize)> { pub fn new_from_subpool_cfg_tuples(
cfg: Vec<(NumBlocks, usize)>,
spill_to_higher_subpools: bool,
) -> Self {
StaticPoolConfig {
cfg: cfg
.iter()
.map(|(num_blocks, block_size)| SubpoolConfig::new(*num_blocks, *block_size))
.collect(),
spill_to_higher_subpools,
}
}
pub fn subpool_cfg(&self) -> &Vec<SubpoolConfig> {
&self.cfg &self.cfg
} }
pub fn sanitize(&mut self) -> usize { pub fn sanitize(&mut self) -> usize {
self.cfg self.cfg.retain(|&subpool_cfg| {
.retain(|&(bucket_num, size)| bucket_num > 0 && size < POOL_MAX_SIZE); subpool_cfg.num_blocks > 0 && subpool_cfg.block_size < MAX_BLOCK_SIZE
self.cfg });
.sort_unstable_by(|(_, sz0), (_, sz1)| sz0.partial_cmp(sz1).unwrap()); self.cfg.sort_unstable_by(|&cfg0, &cfg1| {
cfg0.block_size.partial_cmp(&cfg1.block_size).unwrap()
});
self.cfg.len() self.cfg.len()
} }
} }
@ -425,7 +698,7 @@ mod alloc_mod {
pub struct StaticMemoryPool { pub struct StaticMemoryPool {
pool_cfg: StaticPoolConfig, pool_cfg: StaticPoolConfig,
pool: Vec<Vec<u8>>, pool: Vec<Vec<u8>>,
sizes_lists: Vec<Vec<PoolSize>>, sizes_lists: Vec<Vec<UsedBlockSize>>,
} }
impl StaticMemoryPool { impl StaticMemoryPool {
@ -438,10 +711,10 @@ mod alloc_mod {
pool: Vec::with_capacity(subpools_num), pool: Vec::with_capacity(subpools_num),
sizes_lists: Vec::with_capacity(subpools_num), sizes_lists: Vec::with_capacity(subpools_num),
}; };
for &(num_elems, elem_size) in local_pool.pool_cfg.cfg.iter() { for &subpool_cfg in local_pool.pool_cfg.cfg.iter() {
let next_pool_len = elem_size * num_elems as usize; let next_pool_len = subpool_cfg.num_blocks as usize * subpool_cfg.block_size;
local_pool.pool.push(vec![0; next_pool_len]); local_pool.pool.push(vec![0; next_pool_len]);
let next_sizes_list_len = num_elems as usize; let next_sizes_list_len = subpool_cfg.num_blocks as usize;
local_pool local_pool
.sizes_lists .sizes_lists
.push(vec![STORE_FREE; next_sizes_list_len]); .push(vec![STORE_FREE; next_sizes_list_len]);
@ -468,7 +741,7 @@ mod alloc_mod {
Some(PoolAddr::from(*addr)), Some(PoolAddr::from(*addr)),
)); ));
} }
if addr.packet_idx >= self.pool_cfg.cfg[addr.pool_idx as usize].0 { if addr.packet_idx >= self.pool_cfg.cfg[addr.pool_idx as usize].num_blocks {
return Err(PoolError::InvalidStoreId( return Err(PoolError::InvalidStoreId(
StoreIdError::InvalidPacketIdx(addr.packet_idx), StoreIdError::InvalidPacketIdx(addr.packet_idx),
Some(PoolAddr::from(*addr)), Some(PoolAddr::from(*addr)),
@ -498,11 +771,11 @@ mod alloc_mod {
} }
fn find_subpool(&self, req_size: usize, start_at_subpool: u16) -> Result<u16, PoolError> { fn find_subpool(&self, req_size: usize, start_at_subpool: u16) -> Result<u16, PoolError> {
for (i, &(_, elem_size)) in self.pool_cfg.cfg.iter().enumerate() { for (i, &config) in self.pool_cfg.cfg.iter().enumerate() {
if i < start_at_subpool as usize { if i < start_at_subpool as usize {
continue; continue;
} }
if elem_size >= req_size { if config.block_size >= req_size {
return Ok(i as u16); return Ok(i as u16);
} }
} }
@ -537,15 +810,15 @@ mod alloc_mod {
} }
fn raw_pos(&self, addr: &StaticPoolAddr) -> Option<usize> { fn raw_pos(&self, addr: &StaticPoolAddr) -> Option<usize> {
let (_, size) = self.pool_cfg.cfg.get(addr.pool_idx as usize)?; let cfg = self.pool_cfg.cfg.get(addr.pool_idx as usize)?;
Some(addr.packet_idx as usize * size) Some(addr.packet_idx as usize * cfg.block_size)
} }
} }
impl PoolProvider for StaticMemoryPool { impl PoolProvider for StaticMemoryPool {
fn add(&mut self, data: &[u8]) -> Result<PoolAddr, PoolError> { fn add(&mut self, data: &[u8]) -> Result<PoolAddr, PoolError> {
let data_len = data.len(); let data_len = data.len();
if data_len > POOL_MAX_SIZE { if data_len > MAX_BLOCK_SIZE {
return Err(PoolError::DataTooLarge(data_len)); return Err(PoolError::DataTooLarge(data_len));
} }
let addr = self.reserve(data_len)?; let addr = self.reserve(data_len)?;
@ -558,7 +831,7 @@ mod alloc_mod {
len: usize, len: usize,
mut writer: W, mut writer: W,
) -> Result<PoolAddr, PoolError> { ) -> Result<PoolAddr, PoolError> {
if len > POOL_MAX_SIZE { if len > MAX_BLOCK_SIZE {
return Err(PoolError::DataTooLarge(len)); return Err(PoolError::DataTooLarge(len));
} }
let addr = self.reserve(len)?; let addr = self.reserve(len)?;
@ -604,7 +877,12 @@ mod alloc_mod {
fn delete(&mut self, addr: PoolAddr) -> Result<(), PoolError> { fn delete(&mut self, addr: PoolAddr) -> Result<(), PoolError> {
let addr = StaticPoolAddr::from(addr); let addr = StaticPoolAddr::from(addr);
self.addr_check(&addr)?; self.addr_check(&addr)?;
let block_size = self.pool_cfg.cfg.get(addr.pool_idx as usize).unwrap().1; let block_size = self
.pool_cfg
.cfg
.get(addr.pool_idx as usize)
.unwrap()
.block_size;
let raw_pos = self.raw_pos(&addr).unwrap(); let raw_pos = self.raw_pos(&addr).unwrap();
let block = &mut self.pool.get_mut(addr.pool_idx as usize).unwrap() let block = &mut self.pool.get_mut(addr.pool_idx as usize).unwrap()
[raw_pos..raw_pos + block_size]; [raw_pos..raw_pos + block_size];
@ -652,34 +930,70 @@ mod alloc_mod {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::pool::{ use alloc::vec::Vec;
PoolError, PoolGuard, PoolProvider, PoolProviderWithGuards, PoolRwGuard, StaticMemoryPool,
StaticPoolAddr, StaticPoolConfig, StoreIdError, POOL_MAX_SIZE, use super::*;
}; use core::mem::MaybeUninit;
use std::vec; use std::vec;
fn basic_small_pool() -> StaticMemoryPool { fn basic_small_pool() -> StaticMemoryPool {
let mut pool_cfg_vec = Vec::new();
pool_cfg_vec.push(SubpoolConfig::new(4, 4));
pool_cfg_vec.push(SubpoolConfig::new(2, 8));
pool_cfg_vec.push(SubpoolConfig::new(1, 16));
// 4 buckets of 4 bytes, 2 of 8 bytes and 1 of 16 bytes // 4 buckets of 4 bytes, 2 of 8 bytes and 1 of 16 bytes
let pool_cfg = StaticPoolConfig::new(vec![(4, 4), (2, 8), (1, 16)], false); let pool_cfg = StaticPoolConfig::new(pool_cfg_vec, false);
StaticMemoryPool::new(pool_cfg) StaticMemoryPool::new(pool_cfg)
} }
#[test] #[test]
fn test_cfg() { fn test_cfg() {
let mut pool_cfg_vec = Vec::new();
pool_cfg_vec.push(SubpoolConfig::new(0, 0));
pool_cfg_vec.push(SubpoolConfig::new(1, 0));
pool_cfg_vec.push(SubpoolConfig::new(2, MAX_BLOCK_SIZE));
// Values where number of buckets is 0 or size is too large should be removed // Values where number of buckets is 0 or size is too large should be removed
let mut pool_cfg = StaticPoolConfig::new(vec![(0, 0), (1, 0), (2, POOL_MAX_SIZE)], false); let mut pool_cfg = StaticPoolConfig::new(pool_cfg_vec, false);
pool_cfg.sanitize(); pool_cfg.sanitize();
assert_eq!(*pool_cfg.cfg(), vec![(1, 0)]); assert_eq!(*pool_cfg.subpool_cfg(), vec![SubpoolConfig::new(1, 0)]);
let mut pool_cfg_vec = Vec::new();
pool_cfg_vec.push(SubpoolConfig::new(16, 6));
pool_cfg_vec.push(SubpoolConfig::new(32, 3));
pool_cfg_vec.push(SubpoolConfig::new(8, 12));
// Entries should be ordered according to bucket size // Entries should be ordered according to bucket size
pool_cfg = StaticPoolConfig::new(vec![(16, 6), (32, 3), (8, 12)], false); pool_cfg = StaticPoolConfig::new(pool_cfg_vec, false);
pool_cfg.sanitize(); pool_cfg.sanitize();
assert_eq!(*pool_cfg.cfg(), vec![(32, 3), (16, 6), (8, 12)]); assert_eq!(
*pool_cfg.subpool_cfg(),
vec![
SubpoolConfig::new(32, 3),
SubpoolConfig::new(16, 6),
SubpoolConfig::new(8, 12)
]
);
let mut pool_cfg_vec = Vec::new();
pool_cfg_vec.push(SubpoolConfig::new(12, 12));
pool_cfg_vec.push(SubpoolConfig::new(14, 16));
pool_cfg_vec.push(SubpoolConfig::new(10, 12));
// Unstable sort is used, so order of entries with same block length should not matter // Unstable sort is used, so order of entries with same block length should not matter
pool_cfg = StaticPoolConfig::new(vec![(12, 12), (14, 16), (10, 12)], false); pool_cfg = StaticPoolConfig::new(pool_cfg_vec, false);
pool_cfg.sanitize(); pool_cfg.sanitize();
assert!( assert!(
*pool_cfg.cfg() == vec![(12, 12), (10, 12), (14, 16)] *pool_cfg.subpool_cfg()
|| *pool_cfg.cfg() == vec![(10, 12), (12, 12), (14, 16)] == vec![
SubpoolConfig::new(12, 12),
SubpoolConfig::new(10, 12),
SubpoolConfig::new(14, 16)
]
|| *pool_cfg.subpool_cfg()
== vec![
SubpoolConfig::new(10, 12),
SubpoolConfig::new(12, 12),
SubpoolConfig::new(14, 16)
]
); );
} }
@ -691,7 +1005,7 @@ mod tests {
*val = i as u8; *val = i as u8;
} }
let mut other_buf: [u8; 16] = [0; 16]; let mut other_buf: [u8; 16] = [0; 16];
let addr = local_pool.add(&test_buf).expect("Adding data failed"); let addr = local_pool.add(&test_buf).expect("adding data failed");
// Read back data and verify correctness // Read back data and verify correctness
let res = local_pool.read(&addr, &mut other_buf); let res = local_pool.read(&addr, &mut other_buf);
assert!(res.is_ok()); assert!(res.is_ok());
@ -706,7 +1020,7 @@ mod tests {
fn test_add_smaller_than_full_slot() { fn test_add_smaller_than_full_slot() {
let mut local_pool = basic_small_pool(); let mut local_pool = basic_small_pool();
let test_buf: [u8; 12] = [0; 12]; let test_buf: [u8; 12] = [0; 12];
let addr = local_pool.add(&test_buf).expect("Adding data failed"); let addr = local_pool.add(&test_buf).expect("adding data failed");
let res = local_pool let res = local_pool
.read(&addr, &mut [0; 12]) .read(&addr, &mut [0; 12])
.expect("Read back failed"); .expect("Read back failed");
@ -866,9 +1180,12 @@ mod tests {
#[test] #[test]
fn test_data_too_large_1() { fn test_data_too_large_1() {
let mut local_pool = basic_small_pool(); let mut local_pool = basic_small_pool();
let res = local_pool.free_element(POOL_MAX_SIZE + 1, |_| {}); let res = local_pool.free_element(MAX_BLOCK_SIZE + 1, |_| {});
assert!(res.is_err()); assert!(res.is_err());
assert_eq!(res.unwrap_err(), PoolError::DataTooLarge(POOL_MAX_SIZE + 1)); assert_eq!(
res.unwrap_err(),
PoolError::DataTooLarge(MAX_BLOCK_SIZE + 1)
);
} }
#[test] #[test]
@ -968,7 +1285,10 @@ mod tests {
#[test] #[test]
fn test_spills_to_higher_subpools() { fn test_spills_to_higher_subpools() {
let pool_cfg = StaticPoolConfig::new(vec![(2, 8), (2, 16)], true); let mut subpool_config_vec = Vec::new();
subpool_config_vec.push(SubpoolConfig::new(2, 8));
subpool_config_vec.push(SubpoolConfig::new(2, 16));
let pool_cfg = StaticPoolConfig::new(subpool_config_vec, true);
let mut local_pool = StaticMemoryPool::new(pool_cfg); let mut local_pool = StaticMemoryPool::new(pool_cfg);
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
@ -990,7 +1310,10 @@ mod tests {
#[test] #[test]
fn test_spillage_fails_as_well() { fn test_spillage_fails_as_well() {
let pool_cfg = StaticPoolConfig::new(vec![(1, 8), (1, 16)], true); let mut subpool_config_vec = Vec::new();
subpool_config_vec.push(SubpoolConfig::new(1, 8));
subpool_config_vec.push(SubpoolConfig::new(1, 16));
let pool_cfg = StaticPoolConfig::new(subpool_config_vec, true);
let mut local_pool = StaticMemoryPool::new(pool_cfg); let mut local_pool = StaticMemoryPool::new(pool_cfg);
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
@ -1005,7 +1328,11 @@ mod tests {
#[test] #[test]
fn test_spillage_works_across_multiple_subpools() { fn test_spillage_works_across_multiple_subpools() {
let pool_cfg = StaticPoolConfig::new(vec![(1, 8), (1, 12), (1, 16)], true); let mut subpool_config_vec = Vec::new();
subpool_config_vec.push(SubpoolConfig::new(1, 8));
subpool_config_vec.push(SubpoolConfig::new(1, 12));
subpool_config_vec.push(SubpoolConfig::new(1, 16));
let pool_cfg = StaticPoolConfig::new(subpool_config_vec, true);
let mut local_pool = StaticMemoryPool::new(pool_cfg); let mut local_pool = StaticMemoryPool::new(pool_cfg);
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
local_pool.free_element(12, |_| {}).unwrap(); local_pool.free_element(12, |_| {}).unwrap();
@ -1020,7 +1347,8 @@ mod tests {
#[test] #[test]
fn test_spillage_fails_across_multiple_subpools() { fn test_spillage_fails_across_multiple_subpools() {
let pool_cfg = StaticPoolConfig::new(vec![(1, 8), (1, 12), (1, 16)], true); let pool_cfg =
StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(1, 8), (1, 12), (1, 16)], true);
let mut local_pool = StaticMemoryPool::new(pool_cfg); let mut local_pool = StaticMemoryPool::new(pool_cfg);
local_pool.free_element(8, |_| {}).unwrap(); local_pool.free_element(8, |_| {}).unwrap();
local_pool.free_element(12, |_| {}).unwrap(); local_pool.free_element(12, |_| {}).unwrap();
@ -1033,4 +1361,40 @@ mod tests {
panic!("unexpected store address"); panic!("unexpected store address");
} }
} }
const SUBPOOL_1_BLOCK_SIZE: usize = 16;
const SUBPOOL_1_NUM_ELEMENTS: u16 = 5;
static mut SUBPOOL_1: MaybeUninit<
[u8; SUBPOOL_1_NUM_ELEMENTS as usize * SUBPOOL_1_BLOCK_SIZE],
> = MaybeUninit::new([0; SUBPOOL_1_NUM_ELEMENTS as usize * SUBPOOL_1_BLOCK_SIZE]);
static mut SUBPOOL_1_SIZES: MaybeUninit<[usize; SUBPOOL_1_NUM_ELEMENTS as usize]> =
MaybeUninit::new([0; SUBPOOL_1_NUM_ELEMENTS as usize]);
#[test]
// #[cfg(feature = "heapless")]
fn test_heapless_pool() {
let mut heapless_pool: StaticHeaplessMemoryPool<2> = StaticHeaplessMemoryPool::new(true);
assert!(heapless_pool
.grow(
unsafe { SUBPOOL_1.assume_init_mut() },
unsafe { SUBPOOL_1_SIZES.assume_init_mut() },
SUBPOOL_1_NUM_ELEMENTS,
)
.is_ok());
let mut test_buf: [u8; 16] = [0; 16];
for (i, val) in test_buf.iter_mut().enumerate() {
*val = i as u8;
}
// Add and read test.
let mut other_buf: [u8; 16] = [0; 16];
let addr = heapless_pool.add(&test_buf).expect("adding data failed");
// Read back data and verify correctness
let res = heapless_pool.read(&addr, &mut other_buf);
assert!(res.is_ok());
let read_len = res.unwrap();
assert_eq!(read_len, 16);
for (i, &val) in other_buf.iter().enumerate() {
assert_eq!(val, i as u8);
}
}
} }

View File

@ -1356,7 +1356,10 @@ pub mod tests {
/// ///
/// The PUS service handler is instantiated with a [EcssTcInStoreConverter]. /// The PUS service handler is instantiated with a [EcssTcInStoreConverter].
pub fn new(id: ComponentId) -> (Self, PusServiceHelperStatic) { pub fn new(id: ComponentId) -> (Self, PusServiceHelperStatic) {
let pool_cfg = StaticPoolConfig::new(alloc::vec![(16, 16), (8, 32), (4, 64)], false); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(
alloc::vec![(16, 16), (8, 32), (4, 64)],
false,
);
let tc_pool = StaticMemoryPool::new(pool_cfg.clone()); let tc_pool = StaticMemoryPool::new(pool_cfg.clone());
let tm_pool = StaticMemoryPool::new(pool_cfg); let tm_pool = StaticMemoryPool::new(pool_cfg);
let shared_tc_pool = SharedStaticMemoryPool::new(RwLock::new(tc_pool)); let shared_tc_pool = SharedStaticMemoryPool::new(RwLock::new(tc_pool));

View File

@ -939,7 +939,10 @@ mod tests {
#[test] #[test]
fn test_reset() { fn test_reset() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
@ -1088,7 +1091,10 @@ mod tests {
} }
#[test] #[test]
fn test_release_telecommands() { fn test_release_telecommands() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
@ -1153,7 +1159,10 @@ mod tests {
#[test] #[test]
fn release_multi_with_same_time() { fn release_multi_with_same_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
@ -1210,7 +1219,10 @@ mod tests {
#[test] #[test]
fn release_with_scheduler_disabled() { fn release_with_scheduler_disabled() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
scheduler.disable(); scheduler.disable();
@ -1278,7 +1290,10 @@ mod tests {
fn insert_unwrapped_tc() { fn insert_unwrapped_tc() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1325,7 +1340,10 @@ mod tests {
fn insert_wrapped_tc() { fn insert_wrapped_tc() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc = scheduled_tc(UnixTime::new_only_secs(100), &mut buf); let tc = scheduled_tc(UnixTime::new_only_secs(100), &mut buf);
@ -1374,7 +1392,10 @@ mod tests {
fn insert_wrong_service() { fn insert_wrong_service() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc = wrong_tc_service(UnixTime::new_only_secs(100), &mut buf); let tc = wrong_tc_service(UnixTime::new_only_secs(100), &mut buf);
@ -1396,7 +1417,10 @@ mod tests {
fn insert_wrong_subservice() { fn insert_wrong_subservice() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc = wrong_tc_subservice(UnixTime::new_only_secs(100), &mut buf); let tc = wrong_tc_subservice(UnixTime::new_only_secs(100), &mut buf);
@ -1417,7 +1441,10 @@ mod tests {
#[test] #[test]
fn insert_wrapped_tc_faulty_app_data() { fn insert_wrapped_tc_faulty_app_data() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let tc = invalid_time_tagged_cmd(); let tc = invalid_time_tagged_cmd();
let insert_res = scheduler.insert_wrapped_tc::<cds::CdsTime>(&tc, &mut pool); let insert_res = scheduler.insert_wrapped_tc::<cds::CdsTime>(&tc, &mut pool);
assert!(insert_res.is_err()); assert!(insert_res.is_err());
@ -1431,7 +1458,10 @@ mod tests {
#[test] #[test]
fn insert_doubly_wrapped_time_tagged_cmd() { fn insert_doubly_wrapped_time_tagged_cmd() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 64] = [0; 64]; let mut buf: [u8; 64] = [0; 64];
let tc = double_wrapped_time_tagged_tc(UnixTime::new_only_secs(50), &mut buf); let tc = double_wrapped_time_tagged_tc(UnixTime::new_only_secs(50), &mut buf);
let insert_res = scheduler.insert_wrapped_tc::<cds::CdsTime>(&tc, &mut pool); let insert_res = scheduler.insert_wrapped_tc::<cds::CdsTime>(&tc, &mut pool);
@ -1465,7 +1495,10 @@ mod tests {
fn release_time_within_time_margin() { fn release_time_within_time_margin() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
@ -1489,7 +1522,10 @@ mod tests {
#[test] #[test]
fn test_store_error_propagation_release() { fn test_store_error_propagation_release() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1523,7 +1559,10 @@ mod tests {
#[test] #[test]
fn test_store_error_propagation_reset() { fn test_store_error_propagation_reset() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1546,7 +1585,10 @@ mod tests {
#[test] #[test]
fn test_delete_by_req_id_simple_retrieve_addr() { fn test_delete_by_req_id_simple_retrieve_addr() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1564,7 +1606,10 @@ mod tests {
#[test] #[test]
fn test_delete_by_req_id_simple_delete_all() { fn test_delete_by_req_id_simple_delete_all() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1582,7 +1627,10 @@ mod tests {
#[test] #[test]
fn test_delete_by_req_id_complex() { fn test_delete_by_req_id_complex() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]); let tc_info_0 = ping_tc_to_store(&mut pool, &mut buf, 0, &[]);
@ -1627,7 +1675,10 @@ mod tests {
fn insert_full_store_test() { fn insert_full_store_test() {
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(1, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(1, 64)],
false,
));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];
// Store is full after this. // Store is full after this.
@ -1663,7 +1714,10 @@ mod tests {
#[test] #[test]
fn test_time_window_retrieval_select_all() { fn test_time_window_retrieval_select_all() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let tc_info_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let tc_info_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1692,7 +1746,10 @@ mod tests {
#[test] #[test]
fn test_time_window_retrieval_select_from_stamp() { fn test_time_window_retrieval_select_from_stamp() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let _ = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let _ = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1724,7 +1781,10 @@ mod tests {
#[test] #[test]
fn test_time_window_retrieval_select_to_time() { fn test_time_window_retrieval_select_to_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let tc_info_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let tc_info_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1756,7 +1816,10 @@ mod tests {
#[test] #[test]
fn test_time_window_retrieval_select_from_time_to_time() { fn test_time_window_retrieval_select_from_time_to_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let _ = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let _ = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let tc_info_1 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1794,7 +1857,10 @@ mod tests {
#[test] #[test]
fn test_deletion_all() { fn test_deletion_all() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1820,7 +1886,10 @@ mod tests {
#[test] #[test]
fn test_deletion_from_start_time() { fn test_deletion_from_start_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1842,7 +1911,10 @@ mod tests {
#[test] #[test]
fn test_deletion_to_end_time() { fn test_deletion_to_end_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let cmd_1_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let cmd_1_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1865,7 +1937,10 @@ mod tests {
#[test] #[test]
fn test_deletion_from_start_time_to_end_time() { fn test_deletion_from_start_time_to_end_time() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let cmd_out_of_range_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50); let cmd_out_of_range_0 = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 50);
let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100); let cmd_0_to_delete = insert_command_with_release_time(&mut pool, &mut scheduler, 0, 100);
@ -1897,7 +1972,10 @@ mod tests {
#[test] #[test]
fn test_release_without_deletion() { fn test_release_without_deletion() {
let mut pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(10, 32), (5, 64)], false)); let mut pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(10, 32), (5, 64)],
false,
));
let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5)); let mut scheduler = PusScheduler::new(UnixTime::new_only_secs(0), Duration::from_secs(5));
let mut buf: [u8; 32] = [0; 32]; let mut buf: [u8; 32] = [0; 32];

View File

@ -286,7 +286,10 @@ mod tests {
impl Pus11HandlerWithStoreTester { impl Pus11HandlerWithStoreTester {
pub fn new() -> Self { pub fn new() -> Self {
let test_scheduler = TestScheduler::default(); let test_scheduler = TestScheduler::default();
let pool_cfg = StaticPoolConfig::new(alloc::vec![(16, 16), (8, 32), (4, 64)], false); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(
alloc::vec![(16, 16), (8, 32), (4, 64)],
false,
);
let sched_tc_pool = StaticMemoryPool::new(pool_cfg.clone()); let sched_tc_pool = StaticMemoryPool::new(pool_cfg.clone());
let (common, srv_handler) = PusServiceHandlerWithSharedStoreCommon::new(0); let (common, srv_handler) = PusServiceHandlerWithSharedStoreCommon::new(0);
Self { Self {

View File

@ -2069,7 +2069,10 @@ pub mod tests {
#[test] #[test]
fn test_mpsc_verif_send() { fn test_mpsc_verif_send() {
let pool = StaticMemoryPool::new(StaticPoolConfig::new(vec![(8, 8)], false)); let pool = StaticMemoryPool::new(StaticPoolConfig::new_from_subpool_cfg_tuples(
vec![(8, 8)],
false,
));
let shared_tm_store = let shared_tm_store =
SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(pool))); SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(pool)));
let (tx, _) = mpsc::sync_channel(10); let (tx, _) = mpsc::sync_channel(10);

View File

@ -233,7 +233,7 @@ pub mod std_mod {
use super::*; use super::*;
/// Newtype wrapper around the [SharedStaticMemoryPool] to enable extension helper traits on /// Newtype wrapper around the [SharedStaticPreallocMemoryPool] to enable extension helper traits on
/// top of the regular shared memory pool API. /// top of the regular shared memory pool API.
#[derive(Clone)] #[derive(Clone)]
pub struct SharedPacketPool(pub SharedStaticMemoryPool); pub struct SharedPacketPool(pub SharedStaticMemoryPool);
@ -486,7 +486,9 @@ pub(crate) mod tests {
use std::sync::RwLock; use std::sync::RwLock;
use crate::pool::{PoolProviderWithGuards, StaticMemoryPool, StaticPoolConfig}; use crate::pool::{
PoolProviderWithGuards, SharedStaticMemoryPool, StaticMemoryPool, StaticPoolConfig,
};
use super::*; use super::*;
use std::sync::mpsc; use std::sync::mpsc;
@ -554,7 +556,7 @@ pub(crate) mod tests {
#[test] #[test]
fn test_basic_shared_store_sender_unbounded_sender() { fn test_basic_shared_store_sender_unbounded_sender() {
let (tc_tx, tc_rx) = mpsc::channel(); let (tc_tx, tc_rx) = mpsc::channel();
let pool_cfg = StaticPoolConfig::new(vec![(2, 8)], true); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(2, 8)], true);
let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new( let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(
StaticMemoryPool::new(pool_cfg), StaticMemoryPool::new(pool_cfg),
))); )));
@ -571,7 +573,7 @@ pub(crate) mod tests {
#[test] #[test]
fn test_basic_shared_store_sender() { fn test_basic_shared_store_sender() {
let (tc_tx, tc_rx) = mpsc::sync_channel(10); let (tc_tx, tc_rx) = mpsc::sync_channel(10);
let pool_cfg = StaticPoolConfig::new(vec![(2, 8)], true); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(2, 8)], true);
let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new( let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(
StaticMemoryPool::new(pool_cfg), StaticMemoryPool::new(pool_cfg),
))); )));
@ -588,7 +590,7 @@ pub(crate) mod tests {
#[test] #[test]
fn test_basic_shared_store_sender_rx_dropped() { fn test_basic_shared_store_sender_rx_dropped() {
let (tc_tx, tc_rx) = mpsc::sync_channel(10); let (tc_tx, tc_rx) = mpsc::sync_channel(10);
let pool_cfg = StaticPoolConfig::new(vec![(2, 8)], true); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(2, 8)], true);
let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new( let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(
StaticMemoryPool::new(pool_cfg), StaticMemoryPool::new(pool_cfg),
))); )));
@ -606,7 +608,7 @@ pub(crate) mod tests {
#[test] #[test]
fn test_basic_shared_store_sender_queue_full() { fn test_basic_shared_store_sender_queue_full() {
let (tc_tx, tc_rx) = mpsc::sync_channel(1); let (tc_tx, tc_rx) = mpsc::sync_channel(1);
let pool_cfg = StaticPoolConfig::new(vec![(2, 8)], true); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(2, 8)], true);
let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new( let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(
StaticMemoryPool::new(pool_cfg), StaticMemoryPool::new(pool_cfg),
))); )));
@ -629,7 +631,7 @@ pub(crate) mod tests {
#[test] #[test]
fn test_basic_shared_store_store_error() { fn test_basic_shared_store_store_error() {
let (tc_tx, tc_rx) = mpsc::sync_channel(1); let (tc_tx, tc_rx) = mpsc::sync_channel(1);
let pool_cfg = StaticPoolConfig::new(vec![(1, 8)], true); let pool_cfg = StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(1, 8)], true);
let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new( let shared_pool = SharedPacketPool::new(&SharedStaticMemoryPool::new(RwLock::new(
StaticMemoryPool::new(pool_cfg), StaticMemoryPool::new(pool_cfg),
))); )));

View File

@ -9,7 +9,8 @@ const DUMMY_DATA: [u8; 4] = [0, 1, 2, 3];
#[test] #[test]
fn threaded_usage() { fn threaded_usage() {
let pool_cfg = StaticPoolConfig::new(vec![(16, 6), (32, 3), (8, 12)], false); let pool_cfg =
StaticPoolConfig::new_from_subpool_cfg_tuples(vec![(16, 6), (32, 3), (8, 12)], false);
let shared_pool = Arc::new(RwLock::new(StaticMemoryPool::new(pool_cfg))); let shared_pool = Arc::new(RwLock::new(StaticMemoryPool::new(pool_cfg)));
let shared_clone = shared_pool.clone(); let shared_clone = shared_pool.clone();
let (tx, rx): (Sender<PoolAddr>, Receiver<PoolAddr>) = mpsc::channel(); let (tx, rx): (Sender<PoolAddr>, Receiver<PoolAddr>) = mpsc::channel();