swage_core/memory/
pfn_offset.rs1use crate::memory::keyed_cache::KeyedCache;
2use crate::memory::mem_configuration::MemConfiguration;
3use std::cell::RefCell;
4
5type CacheKey = (MemConfiguration, u64);
6type CacheValue = Option<usize>;
7
8#[derive(Clone, Debug)]
13pub enum PfnOffset {
14 Fixed(usize),
16 Dynamic(Box<RefCell<Option<(CacheValue, CacheKey)>>>),
20}
21
22pub trait CachedPfnOffset {
24 fn cached_offset(&self) -> &PfnOffset;
26}
27
28impl<T> KeyedCache<usize, (MemConfiguration, u64)> for T
31where
32 T: CachedPfnOffset,
33{
34 fn get_cached(&self, key: (MemConfiguration, u64)) -> Option<usize> {
35 match self.cached_offset() {
36 PfnOffset::Fixed(offset) => Some(*offset),
37 PfnOffset::Dynamic(pfn_offset) => {
38 let state = pfn_offset.borrow();
39 match state.as_ref() {
40 Some((offset, cfg)) if offset.is_some() && *cfg == key => Some(offset.unwrap()),
41 _ => None,
42 }
43 }
44 }
45 }
46 fn put(&self, state: Option<usize>, key: (MemConfiguration, u64)) -> Option<usize> {
47 match self.cached_offset() {
48 PfnOffset::Fixed(_) => panic!("Fixed offset should not be set"),
49 PfnOffset::Dynamic(cell) => {
50 let mut cell = cell.borrow_mut();
51 *cell = Some((state, key));
52 state
53 }
54 }
55 }
56}