diff --git a/satrs-core/src/pool.rs b/satrs-core/src/pool.rs index e34db43..4e0de7e 100644 --- a/satrs-core/src/pool.rs +++ b/satrs-core/src/pool.rs @@ -368,11 +368,15 @@ mod alloc_mod { #[derive(Clone)] pub struct StaticPoolConfig { cfg: Vec<(NumBlocks, usize)>, + spill_to_higher_subpools: bool, } impl StaticPoolConfig { - pub fn new(cfg: Vec<(NumBlocks, usize)>) -> Self { - StaticPoolConfig { cfg } + pub fn new(cfg: Vec<(NumBlocks, usize)>, spill_to_higher_subpools: bool) -> Self { + StaticPoolConfig { + cfg, + spill_to_higher_subpools, + } } pub fn cfg(&self) -> &Vec<(NumBlocks, usize)> { @@ -456,7 +460,14 @@ mod alloc_mod { } fn reserve(&mut self, data_len: usize) -> Result { - let subpool_idx = self.find_subpool(data_len, 0)?; + let mut subpool_idx = self.find_subpool(data_len, 0)?; + + if self.pool_cfg.spill_to_higher_subpools { + while let Err(StoreError::StoreFull(_)) = self.find_empty(subpool_idx) { + subpool_idx += 1; + } + } + let (slot, size_slot_ref) = self.find_empty(subpool_idx)?; *size_slot_ref = data_len; Ok(StaticPoolAddr { @@ -628,22 +639,22 @@ mod tests { fn basic_small_pool() -> StaticMemoryPool { // 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)]); + let pool_cfg = StaticPoolConfig::new(vec![(4, 4), (2, 8), (1, 16)], false); StaticMemoryPool::new(pool_cfg) } #[test] fn test_cfg() { // 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)]); + let mut pool_cfg = StaticPoolConfig::new(vec![(0, 0), (1, 0), (2, POOL_MAX_SIZE)], false); pool_cfg.sanitize(); assert_eq!(*pool_cfg.cfg(), vec![(1, 0)]); // Entries should be ordered according to bucket size - pool_cfg = StaticPoolConfig::new(vec![(16, 6), (32, 3), (8, 12)]); + pool_cfg = StaticPoolConfig::new(vec![(16, 6), (32, 3), (8, 12)], false); pool_cfg.sanitize(); assert_eq!(*pool_cfg.cfg(), vec![(32, 3), (16, 6), (8, 12)]); // 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)]); + pool_cfg = StaticPoolConfig::new(vec![(12, 12), (14, 16), (10, 12)], false); pool_cfg.sanitize(); assert!( *pool_cfg.cfg() == vec![(12, 12), (10, 12), (14, 16)]