ic_memory/slot/
memory_manager.rs1use super::range_authority::MemoryManagerIdRange;
2use serde::{Deserialize, Serialize};
3
4#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
18#[serde(try_from = "SlotEncoding", into = "SlotEncoding")]
19pub struct MemoryManagerSlot(u8);
20
21impl MemoryManagerSlot {
22 pub const fn new(id: u8) -> Result<Self, MemoryManagerSlotError> {
24 match validate_memory_manager_id(id) {
25 Ok(()) => Ok(Self(id)),
26 Err(error) => Err(error),
27 }
28 }
29
30 #[must_use]
32 pub const fn id(&self) -> u8 {
33 self.0
34 }
35}
36
37#[derive(Deserialize, Serialize)]
40#[serde(deny_unknown_fields)]
41struct SlotEncoding {
42 slot: SlotIdEncoding,
43}
44
45#[derive(Deserialize, Serialize)]
46#[serde(deny_unknown_fields)]
47enum SlotIdEncoding {
48 MemoryManagerId(u8),
49}
50
51impl From<MemoryManagerSlot> for SlotEncoding {
52 fn from(slot: MemoryManagerSlot) -> Self {
53 Self {
54 slot: SlotIdEncoding::MemoryManagerId(slot.id()),
55 }
56 }
57}
58
59impl TryFrom<SlotEncoding> for MemoryManagerSlot {
60 type Error = MemoryManagerSlotError;
61
62 fn try_from(encoded: SlotEncoding) -> Result<Self, Self::Error> {
63 let SlotIdEncoding::MemoryManagerId(id) = encoded.slot;
64 Self::new(id)
65 }
66}
67
68pub const LEDGER_SLOT: MemoryManagerSlot = match MemoryManagerSlot::new(MEMORY_MANAGER_LEDGER_ID) {
69 Ok(slot) => slot,
70 Err(_) => panic!("the ledger ID must be usable"),
71};
72
73pub const MEMORY_MANAGER_MIN_ID: u8 = 0;
75
76pub const MEMORY_MANAGER_MAX_ID: u8 = 254;
78
79pub const MEMORY_MANAGER_INVALID_ID: u8 = u8::MAX;
81
82pub const IC_MEMORY_STABLE_KEY_PREFIX: &str = "ic_memory.";
84
85pub const IC_MEMORY_AUTHORITY_OWNER: &str = "ic-memory";
87
88pub const IC_MEMORY_AUTHORITY_PURPOSE: &str = "ic-memory allocation-governance authority";
90
91pub const IC_MEMORY_LEDGER_STABLE_KEY: &str = "ic_memory.ledger.v1";
93
94pub const IC_MEMORY_LEDGER_LABEL: &str = "MemoryLayoutLedger";
96
97pub const MEMORY_MANAGER_LEDGER_ID: u8 = MEMORY_MANAGER_MIN_ID;
99
100pub const MEMORY_MANAGER_GOVERNANCE_MAX_ID: u8 = 9;
102
103#[must_use]
105pub fn is_ic_memory_stable_key(stable_key: &str) -> bool {
106 stable_key.starts_with(IC_MEMORY_STABLE_KEY_PREFIX)
107}
108
109#[must_use]
111pub const fn memory_manager_governance_range() -> MemoryManagerIdRange {
112 MemoryManagerIdRange {
113 start: MEMORY_MANAGER_MIN_ID,
114 end: MEMORY_MANAGER_GOVERNANCE_MAX_ID,
115 }
116}
117
118#[non_exhaustive]
125#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
126pub enum MemoryManagerSlotError {
127 #[error("MemoryManager ID {id} is not a usable allocation slot")]
129 InvalidMemoryManagerId {
130 id: u8,
132 },
133}
134
135pub const fn validate_memory_manager_id(id: u8) -> Result<(), MemoryManagerSlotError> {
137 if id == MEMORY_MANAGER_INVALID_ID {
138 return Err(MemoryManagerSlotError::InvalidMemoryManagerId { id });
139 }
140 Ok(())
141}