ic_memory/runtime/
policy.rs1use super::RuntimeBootstrapError;
2use crate::{
3 AllocationPolicy, MemoryManagerSlot, PolicyIdentity, PolicyIdentityError,
4 RuntimeBootstrapPolicy, StableKey, slot::IC_MEMORY_LEDGER_STABLE_KEY,
5};
6use std::convert::Infallible;
7
8pub(super) fn runtime_bootstrap_error_from_bootstrap<P>(
9 err: crate::BootstrapError<P>,
10) -> RuntimeBootstrapError<P> {
11 match err {
12 crate::BootstrapError::Ledger(err) => RuntimeBootstrapError::LedgerCommit(err),
13 crate::BootstrapError::Validation(err) => RuntimeBootstrapError::Validation(err),
14 crate::BootstrapError::Staging(err) => RuntimeBootstrapError::Staging(err),
15 }
16}
17
18pub(super) struct RuntimeMemoryManagerPolicy<'a, P> {
22 pub(super) custom_policy: &'a P,
23}
24
25impl<P: AllocationPolicy> AllocationPolicy for RuntimeMemoryManagerPolicy<'_, P> {
26 type Error = P::Error;
27
28 fn validate_key(&self, key: &StableKey) -> Result<(), Self::Error> {
29 if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
30 return Ok(());
31 }
32 self.custom_policy.validate_key(key)
33 }
34
35 fn validate_slot(&self, key: &StableKey, slot: &MemoryManagerSlot) -> Result<(), Self::Error> {
36 if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
37 return Ok(());
38 }
39 self.custom_policy.validate_slot(key, slot)
40 }
41}
42
43pub struct GenericAllocationPolicy;
56
57impl AllocationPolicy for GenericAllocationPolicy {
58 type Error = Infallible;
59
60 fn validate_key(&self, _key: &StableKey) -> Result<(), Self::Error> {
61 Ok(())
62 }
63
64 fn validate_slot(
65 &self,
66 _key: &StableKey,
67 _slot: &MemoryManagerSlot,
68 ) -> Result<(), Self::Error> {
69 Ok(())
70 }
71}
72
73impl RuntimeBootstrapPolicy for GenericAllocationPolicy {
74 fn runtime_bootstrap_identity(&self) -> Result<PolicyIdentity, PolicyIdentityError> {
75 PolicyIdentity::new("ic-memory.noop-policy", 1)
76 }
77}