ic_memory/slot/
memory_manager.rs1use super::descriptor::{AllocationSlot, AllocationSlotDescriptor};
2use super::range_authority::MemoryManagerIdRange;
3
4pub const MEMORY_MANAGER_MIN_ID: u8 = 0;
6
7pub const MEMORY_MANAGER_MAX_ID: u8 = 254;
9
10pub const MEMORY_MANAGER_INVALID_ID: u8 = u8::MAX;
12
13pub const IC_MEMORY_STABLE_KEY_PREFIX: &str = "ic_memory.";
15
16pub const IC_MEMORY_AUTHORITY_OWNER: &str = "ic-memory";
18
19pub const IC_MEMORY_AUTHORITY_PURPOSE: &str = "ic-memory allocation-governance authority";
21
22pub const IC_MEMORY_LEDGER_STABLE_KEY: &str = "ic_memory.ledger.v1";
24
25pub const IC_MEMORY_LEDGER_LABEL: &str = "MemoryLayoutLedger";
27
28pub const MEMORY_MANAGER_LEDGER_ID: u8 = MEMORY_MANAGER_MIN_ID;
30
31pub const MEMORY_MANAGER_GOVERNANCE_MAX_ID: u8 = 9;
33
34#[must_use]
36pub fn is_ic_memory_stable_key(stable_key: &str) -> bool {
37 stable_key.starts_with(IC_MEMORY_STABLE_KEY_PREFIX)
38}
39
40#[must_use]
42pub const fn memory_manager_governance_range() -> MemoryManagerIdRange {
43 MemoryManagerIdRange {
44 start: MEMORY_MANAGER_MIN_ID,
45 end: MEMORY_MANAGER_GOVERNANCE_MAX_ID,
46 }
47}
48
49impl AllocationSlotDescriptor {
50 pub fn memory_manager(id: u8) -> Result<Self, MemoryManagerSlotError> {
55 validate_memory_manager_id(id)?;
56 Ok(Self::memory_manager_unchecked(id))
57 }
58
59 #[must_use]
61 pub(crate) const fn memory_manager_unchecked(id: u8) -> Self {
62 Self {
63 slot: AllocationSlot::MemoryManagerId(id),
64 }
65 }
66
67 pub fn memory_manager_id(&self) -> Result<u8, MemoryManagerSlotError> {
71 let AllocationSlot::MemoryManagerId(id) = self.slot;
72 validate_memory_manager_id(id)?;
73 Ok(id)
74 }
75}
76
77#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
82pub enum MemoryManagerSlotError {
83 #[error("MemoryManager ID {id} is not a usable allocation slot")]
85 InvalidMemoryManagerId {
86 id: u8,
88 },
89}
90
91pub const fn validate_memory_manager_id(id: u8) -> Result<(), MemoryManagerSlotError> {
93 if id == MEMORY_MANAGER_INVALID_ID {
94 return Err(MemoryManagerSlotError::InvalidMemoryManagerId { id });
95 }
96 Ok(())
97}