1#![forbid(unsafe_code)]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![doc = include_str!("../README.md")]
4
5mod bootstrap;
67mod capability;
68mod cbor;
69mod constants;
70mod declaration;
71mod diagnostics;
72mod key;
73mod ledger;
74mod physical;
75mod policy;
76mod registry;
77mod runtime;
78mod schema;
79mod slot;
80mod stable_cell;
81mod validation;
82
83#[cfg(test)]
84mod test_cbor {
85 use serde::{Serialize, de::DeserializeOwned};
86
87 pub use ciborium::Value;
88
89 pub fn to_vec<T: Serialize>(
90 value: &T,
91 ) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
92 let mut bytes = Vec::new();
93 ciborium::into_writer(value, &mut bytes)?;
94 Ok(bytes)
95 }
96
97 pub fn from_slice<T: DeserializeOwned>(
98 bytes: &[u8],
99 ) -> Result<T, ciborium::de::Error<std::io::Error>> {
100 crate::cbor::from_slice_exact(bytes)
101 }
102
103 pub fn to_value<T: Serialize>(value: T) -> Result<Value, ciborium::value::Error> {
104 Value::serialized(&value)
105 }
106
107 pub fn map_insert(map: &mut Vec<(Value, Value)>, key: Value, value: Value) {
108 map.push((key, value));
109 }
110}
111
112pub use bootstrap::{
113 AllocationBootstrap, BootstrapError, BootstrapReservationError, BootstrapRetirementError,
114 PendingBootstrapCommit,
115};
116pub use capability::{CommittedAllocations, ValidatedAllocations};
117pub use constants::WASM_PAGE_SIZE_BYTES;
118pub use declaration::{
119 AllocationDeclaration, DeclarationCollector, DeclarationSnapshot, DeclarationSnapshotError,
120};
121pub use diagnostics::{
122 DefaultMemoryManagerDoctorReport, DiagnosticCheck, DiagnosticCode, DiagnosticDeclaration,
123 DiagnosticExport, DiagnosticFailure, DiagnosticGeneration, DiagnosticMemorySize,
124 DiagnosticRangeAuthority, DiagnosticRecord, DiagnosticStableCell, DiagnosticStableCellStatus,
125};
126pub use key::{StableKey, StableKeyError};
127pub use ledger::{
128 AllocationHistory, AllocationLedger, AllocationRecord, AllocationReservationError,
129 AllocationRetirement, AllocationRetirementError, AllocationStageError, AllocationState,
130 GenerationRecord, LEDGER_PAYLOAD_FORMAT_VERSION, LedgerCommitError, LedgerCommitStore,
131 LedgerIntegrityError, LedgerPayloadEnvelope, LedgerPayloadEnvelopeError, RecoveredLedger,
132 SchemaMetadataRecord,
133};
134pub use physical::{
135 CommitRecoveryError, CommitSlotDiagnostic, CommitStoreDiagnostic, CommittedGenerationBytes,
136 DualCommitStore,
137};
138pub use policy::AllocationPolicy;
139pub use registry::{
140 StaticMemoryDeclaration, StaticMemoryDeclarationError, StaticMemoryRangeDeclaration,
141 collect_static_memory_declarations, register_static_memory_declaration,
142 register_static_memory_manager_declaration,
143 register_static_memory_manager_declaration_with_schema, register_static_memory_manager_range,
144 register_static_memory_range_declaration, static_memory_declaration_snapshot,
145 static_memory_declarations, static_memory_range_authority, static_memory_range_declarations,
146};
147pub use runtime::{
148 RuntimeBootstrapError, RuntimeDiagnosticError, RuntimeOpenError, RuntimePolicyError,
149 bootstrap_default_memory_manager, bootstrap_default_memory_manager_with_policy,
150 committed_allocations, default_memory_manager_commit_recovery_diagnostic,
151 default_memory_manager_diagnostic_export, default_memory_manager_doctor_report,
152 is_default_memory_manager_bootstrapped, open_default_memory_manager_memory,
153};
154pub use schema::{SchemaMetadata, SchemaMetadataError};
155pub use slot::{
156 AllocationSlot, AllocationSlotDescriptor, IC_MEMORY_AUTHORITY_OWNER,
157 IC_MEMORY_AUTHORITY_PURPOSE, IC_MEMORY_LEDGER_LABEL, IC_MEMORY_LEDGER_STABLE_KEY,
158 IC_MEMORY_STABLE_KEY_PREFIX, MEMORY_MANAGER_GOVERNANCE_MAX_ID, MEMORY_MANAGER_INVALID_ID,
159 MEMORY_MANAGER_LEDGER_ID, MEMORY_MANAGER_MAX_ID, MEMORY_MANAGER_MIN_ID,
160 MemoryManagerAuthorityRecord, MemoryManagerIdRange, MemoryManagerRangeAuthority,
161 MemoryManagerRangeAuthorityError, MemoryManagerRangeError, MemoryManagerRangeMode,
162 MemoryManagerSlotError, is_ic_memory_stable_key, memory_manager_governance_range,
163 validate_memory_manager_id,
164};
165pub use stable_cell::{
166 STABLE_CELL_HEADER_SIZE, STABLE_CELL_LAYOUT_VERSION, STABLE_CELL_MAGIC,
167 STABLE_CELL_VALUE_OFFSET, StableCellLedgerError, StableCellLedgerRecord,
168 StableCellPayloadError, decode_stable_cell_ledger_record, decode_stable_cell_payload,
169 validate_stable_cell_ledger_memory,
170};
171pub use validation::{AllocationValidationError, Validate, validate_allocations};
172
173#[doc(hidden)]
174pub use runtime::defer_eager_init;
175
176#[doc(hidden)]
177pub mod __reexports {
178 pub use ctor;
179}
180
181#[macro_export]
191macro_rules! ic_memory_declaration {
192 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {
193 const _: () = {
194 const __IC_MEMORY_AUTHORITY: &str = $authority;
195
196 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
197 fn __ic_memory_register_static_declaration() {
198 let _ = core::marker::PhantomData::<$label>;
199 $crate::register_static_memory_manager_declaration(
200 $id,
201 __IC_MEMORY_AUTHORITY,
202 stringify!($label),
203 $stable_key,
204 )
205 .expect("ic-memory static memory declaration failed");
206 }
207 };
208 };
209 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {
210 const _: () = {
211 const __IC_MEMORY_AUTHORITY: &str = $authority;
212
213 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
214 fn __ic_memory_register_static_declaration() {
215 $crate::register_static_memory_manager_declaration(
216 $id,
217 __IC_MEMORY_AUTHORITY,
218 $label,
219 $stable_key,
220 )
221 .expect("ic-memory static memory declaration failed");
222 }
223 };
224 };
225}
226
227#[macro_export]
232macro_rules! ic_memory_range {
233 (authority = $authority:expr, start = $start:expr, end = $end:expr $(,)?) => {
234 $crate::ic_memory_range!(
235 authority = $authority,
236 start = $start,
237 end = $end,
238 mode = Reserved,
239 );
240 };
241 (authority = $authority:expr, start = $start:expr, end = $end:expr, mode = $mode:ident $(,)?) => {
242 const _: () = {
243 const __IC_MEMORY_AUTHORITY: &str = $authority;
244
245 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
246 fn __ic_memory_register_static_range() {
247 $crate::register_static_memory_manager_range(
248 $start,
249 $end,
250 __IC_MEMORY_AUTHORITY,
251 $crate::MemoryManagerRangeMode::$mode,
252 None,
253 )
254 .expect("ic-memory static memory range declaration failed");
255 }
256 };
257 };
258}
259
260#[macro_export]
265macro_rules! ic_memory_key {
266 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {{
267 $crate::ic_memory_declaration!(authority = $authority, key = $stable_key, ty = $label, id = $id,);
268 $crate::open_default_memory_manager_memory($stable_key, $id)
269 .expect("ic-memory failed to open committed stable memory; bootstrap must run first and the stable key/id must match the committed declaration")
270 }};
271 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {{
272 $crate::ic_memory_declaration!(authority = $authority, key = $stable_key, label = $label, id = $id,);
273 $crate::open_default_memory_manager_memory($stable_key, $id)
274 .expect("ic-memory failed to open committed stable memory; bootstrap must run first and the stable key/id must match the committed declaration")
275 }};
276}
277
278#[macro_export]
280macro_rules! eager_init {
281 ($body:block) => {
282 const _: () = {
283 fn __ic_memory_registered_eager_init_body() {
284 $body
285 }
286
287 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
288 fn __ic_memory_register_eager_init() {
289 $crate::defer_eager_init(__ic_memory_registered_eager_init_body);
290 }
291 };
292 };
293}