ic_memory/slot/
memory_manager.rs1use super::descriptor::{AllocationSlot, AllocationSlotDescriptor};
2use super::range_authority::MemoryManagerIdRange;
3
4pub const MEMORY_MANAGER_SUBSTRATE: &str = "ic-stable-structures.memory_manager";
6
7pub const MEMORY_MANAGER_DESCRIPTOR_VERSION: u32 = 1;
9
10pub const MEMORY_MANAGER_MIN_ID: u8 = 0;
12
13pub const MEMORY_MANAGER_MAX_ID: u8 = 254;
15
16pub const MEMORY_MANAGER_INVALID_ID: u8 = u8::MAX;
18
19pub const IC_MEMORY_STABLE_KEY_PREFIX: &str = "ic_memory.";
21
22pub const IC_MEMORY_AUTHORITY_OWNER: &str = "ic-memory";
24
25pub const IC_MEMORY_AUTHORITY_PURPOSE: &str = "ic-memory allocation-governance authority";
27
28pub const IC_MEMORY_LEDGER_STABLE_KEY: &str = "ic_memory.ledger.v1";
30
31pub const IC_MEMORY_LEDGER_LABEL: &str = "MemoryLayoutLedger";
33
34pub const MEMORY_MANAGER_LEDGER_ID: u8 = MEMORY_MANAGER_MIN_ID;
36
37pub const MEMORY_MANAGER_GOVERNANCE_MAX_ID: u8 = 9;
39
40#[must_use]
42pub fn is_ic_memory_stable_key(stable_key: &str) -> bool {
43 stable_key.starts_with(IC_MEMORY_STABLE_KEY_PREFIX)
44}
45
46#[must_use]
48pub const fn memory_manager_governance_range() -> MemoryManagerIdRange {
49 MemoryManagerIdRange {
50 start: MEMORY_MANAGER_MIN_ID,
51 end: MEMORY_MANAGER_GOVERNANCE_MAX_ID,
52 }
53}
54
55impl AllocationSlotDescriptor {
56 pub fn memory_manager(id: u8) -> Result<Self, MemoryManagerSlotError> {
61 validate_memory_manager_id(id)?;
62 Ok(Self::memory_manager_unchecked(id))
63 }
64
65 #[must_use]
67 pub(crate) fn memory_manager_unchecked(id: u8) -> Self {
68 Self {
69 slot: AllocationSlot::MemoryManagerId(id),
70 substrate: MEMORY_MANAGER_SUBSTRATE.to_string(),
71 descriptor_version: MEMORY_MANAGER_DESCRIPTOR_VERSION,
72 }
73 }
74
75 pub fn memory_manager_id(&self) -> Result<u8, MemoryManagerSlotError> {
80 if self.substrate != MEMORY_MANAGER_SUBSTRATE {
81 return Err(MemoryManagerSlotError::UnsupportedSubstrate {
82 substrate: self.substrate.clone(),
83 });
84 }
85 if self.descriptor_version != MEMORY_MANAGER_DESCRIPTOR_VERSION {
86 return Err(MemoryManagerSlotError::UnsupportedDescriptorVersion {
87 version: self.descriptor_version,
88 });
89 }
90
91 let AllocationSlot::MemoryManagerId(id) = self.slot;
92 validate_memory_manager_id(id)?;
93 Ok(id)
94 }
95}
96
97#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
102pub enum MemoryManagerSlotError {
103 #[error("allocation slot substrate '{substrate}' is not supported as a MemoryManager slot")]
105 UnsupportedSubstrate {
106 substrate: String,
108 },
109 #[error("MemoryManager slot descriptor version {version} is unsupported")]
111 UnsupportedDescriptorVersion {
112 version: u32,
114 },
115 #[error("MemoryManager ID {id} is not a usable allocation slot")]
117 InvalidMemoryManagerId {
118 id: u8,
120 },
121}
122
123pub const fn validate_memory_manager_id(id: u8) -> Result<(), MemoryManagerSlotError> {
125 if id == MEMORY_MANAGER_INVALID_ID {
126 return Err(MemoryManagerSlotError::InvalidMemoryManagerId { id });
127 }
128 Ok(())
129}