ic_memory/runtime/
policy.rs1use crate::{
2 AllocationPolicy, MemoryManagerSlot, PolicyIdentity, PolicyIdentityError,
3 RuntimeBootstrapPolicy, StableKey, slot::IC_MEMORY_LEDGER_STABLE_KEY,
4};
5use std::convert::Infallible;
6
7pub(super) struct RuntimeMemoryManagerPolicy<'a, P> {
11 pub(super) custom_policy: &'a P,
12}
13
14impl<P: AllocationPolicy> AllocationPolicy for RuntimeMemoryManagerPolicy<'_, P> {
15 type Error = P::Error;
16
17 fn validate_key(&self, key: &StableKey) -> Result<(), Self::Error> {
18 if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
19 return Ok(());
20 }
21 self.custom_policy.validate_key(key)
22 }
23
24 fn validate_slot(&self, key: &StableKey, slot: &MemoryManagerSlot) -> Result<(), Self::Error> {
25 if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
26 return Ok(());
27 }
28 self.custom_policy.validate_slot(key, slot)
29 }
30}
31
32pub struct GenericAllocationPolicy;
45
46impl AllocationPolicy for GenericAllocationPolicy {
47 type Error = Infallible;
48
49 fn validate_key(&self, _key: &StableKey) -> Result<(), Self::Error> {
50 Ok(())
51 }
52
53 fn validate_slot(
54 &self,
55 _key: &StableKey,
56 _slot: &MemoryManagerSlot,
57 ) -> Result<(), Self::Error> {
58 Ok(())
59 }
60}
61
62impl RuntimeBootstrapPolicy for GenericAllocationPolicy {
63 fn runtime_bootstrap_identity(&self) -> Result<PolicyIdentity, PolicyIdentityError> {
64 PolicyIdentity::new("ic-memory.noop-policy", 1)
65 }
66}