Skip to main content

canic_core/memory/
mod.rs

1//! Module: memory
2//!
3//! Responsibility: adapt Canic stable-memory declarations to `ic-memory` bootstrap.
4//! Also owns physical stable-memory extent observation for runtime metrics.
5//! Does not own: stable data schemas, ops storage APIs, or lifecycle orchestration.
6//! Boundary: lifecycle initializes this before stable structures are accessed.
7
8pub(crate) mod ledger;
9mod policy;
10pub mod registry;
11pub mod runtime;
12
13pub use crate::{eager_init, eager_static, ic_memory_key, ic_memory_range};
14
15/// Stable allocation-policy authority for Canic core memory declarations.
16pub const CANIC_CORE_MEMORY_AUTHORITY: &str = "canic-core";
17/// Stable allocation-policy authority for Canic control-plane memory declarations.
18pub const CANIC_CONTROL_PLANE_MEMORY_AUTHORITY: &str = "canic-control-plane";
19
20/// Bucket size compiled into this artifact, in 64 KiB Wasm pages.
21///
22/// # Panics
23/// Panics if the build script supplies an invalid value; normal builds validate it first.
24#[must_use]
25pub fn configured_bucket_pages() -> u16 {
26    env!("CANIC_MEMORY_BUCKET_PAGES")
27        .parse()
28        .expect("build-validated memory bucket size")
29}
30
31/// Observe the current physical stable-memory extent without allocating or reading data.
32#[cfg(target_arch = "wasm32")]
33pub(crate) fn stable_extent_bytes() -> u64 {
34    ic_cdk::api::stable_size().saturating_mul(65_536)
35}
36
37pub(crate) fn bootstrap_default_memory_manager()
38-> Result<(), ic_memory::RuntimeBootstrapError<registry::MemoryRegistryError>> {
39    let config = ic_memory::MemoryManagerConfig::new(configured_bucket_pages())
40        .map_err(ic_memory::RuntimeStateError::from)?;
41    ic_memory::bootstrap_default_memory_manager_with_config(
42        config,
43        &policy::CanicMemoryManagerPolicy::new(),
44    )
45    .map(|_| ())
46}