diff --git a/crate/memory/src/swap/mock_swapper.rs b/crate/memory/src/swap/mock_swapper.rs new file mode 100644 index 0000000..af8d8ba --- /dev/null +++ b/crate/memory/src/swap/mock_swapper.rs @@ -0,0 +1,53 @@ +use super::Swapper; +use alloc::btree_map::BTreeMap; + +pub struct MockSwapper { + map: BTreeMap, +} + +impl Swapper for MockSwapper { + fn swap_out(&mut self, data: &[u8; 4096]) -> usize { + let id = self.alloc_id(); + self.map.insert(id, data.clone()); + id + } + + fn swap_in(&mut self, token: usize, data: &mut [u8; 4096]) { + *data = self.map.remove(&token).unwrap(); + } +} + +impl MockSwapper { + pub fn new() -> Self { + MockSwapper {map: BTreeMap::new()} + } + fn alloc_id(&self) -> usize { + (0 .. 100usize).find(|i| !self.map.contains_key(i)).unwrap() + } +} + +#[cfg(test)] +mod test { + use super::*; + use core::mem::uninitialized; + + #[test] + fn test() { + let mut swapper = MockSwapper::new(); + let data: [u8; 4096] = unsafe{ uninitialized() }; + let mut data1: [u8; 4096] = unsafe{ uninitialized() }; + let token = swapper.swap_out(&data); + swapper.swap_in(token, &mut data1); + for (&a, &b) in data.iter().zip(data1.iter()) { + assert_eq!(a, b); + } + } + + #[test] + #[should_panic] + fn invalid_token() { + let mut swapper = MockSwapper::new(); + let mut data: [u8; 4096] = unsafe{ uninitialized() }; + swapper.swap_in(0, &mut data); + } +} \ No newline at end of file diff --git a/crate/memory/src/swap/mod.rs b/crate/memory/src/swap/mod.rs index 80b1e5a..214779c 100644 --- a/crate/memory/src/swap/mod.rs +++ b/crate/memory/src/swap/mod.rs @@ -1,5 +1,6 @@ pub mod fifo; mod mock_page_table; +mod mock_swapper; type Addr = usize; @@ -19,4 +20,9 @@ trait SwapManager { trait PageTable { fn accessed(&self, addr: Addr) -> bool; fn dirty(&self, addr: Addr) -> bool; +} + +trait Swapper { + fn swap_out(&mut self, data: &[u8; 4096]) -> usize; + fn swap_in(&mut self, token: usize, data: &mut [u8; 4096]); } \ No newline at end of file