use super::RuntimeBootstrapError;
use crate::{
AllocationPolicy, MemoryManagerSlot, PolicyIdentity, PolicyIdentityError,
RuntimeBootstrapPolicy, StableKey, slot::IC_MEMORY_LEDGER_STABLE_KEY,
};
use std::convert::Infallible;
pub(super) fn runtime_bootstrap_error_from_bootstrap<P>(
err: crate::BootstrapError<P>,
) -> RuntimeBootstrapError<P> {
match err {
crate::BootstrapError::Ledger(err) => RuntimeBootstrapError::LedgerCommit(err),
crate::BootstrapError::Validation(err) => RuntimeBootstrapError::Validation(err),
crate::BootstrapError::Staging(err) => RuntimeBootstrapError::Staging(err),
}
}
pub(super) struct RuntimeMemoryManagerPolicy<'a, P> {
pub(super) custom_policy: &'a P,
}
impl<P: AllocationPolicy> AllocationPolicy for RuntimeMemoryManagerPolicy<'_, P> {
type Error = P::Error;
fn validate_key(&self, key: &StableKey) -> Result<(), Self::Error> {
if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
return Ok(());
}
self.custom_policy.validate_key(key)
}
fn validate_slot(&self, key: &StableKey, slot: &MemoryManagerSlot) -> Result<(), Self::Error> {
if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
return Ok(());
}
self.custom_policy.validate_slot(key, slot)
}
fn validate_reserved_slot(
&self,
key: &StableKey,
slot: &MemoryManagerSlot,
) -> Result<(), Self::Error> {
if key.as_str() == IC_MEMORY_LEDGER_STABLE_KEY {
return Ok(());
}
self.custom_policy.validate_reserved_slot(key, slot)
}
}
pub struct GenericAllocationPolicy;
impl AllocationPolicy for GenericAllocationPolicy {
type Error = Infallible;
fn validate_key(&self, _key: &StableKey) -> Result<(), Self::Error> {
Ok(())
}
fn validate_slot(
&self,
_key: &StableKey,
_slot: &MemoryManagerSlot,
) -> Result<(), Self::Error> {
Ok(())
}
fn validate_reserved_slot(
&self,
_key: &StableKey,
_slot: &MemoryManagerSlot,
) -> Result<(), Self::Error> {
Ok(())
}
}
impl RuntimeBootstrapPolicy for GenericAllocationPolicy {
fn runtime_bootstrap_identity(&self) -> Result<PolicyIdentity, PolicyIdentityError> {
PolicyIdentity::new("ic-memory.noop-policy", 1)
}
}