pub fn stable_memory_init()
Expand description

Initializes the memory allocator.

This function should be called ONLY ONCE during the lifetime of a canister. For canisters, that are being build using this crate from scratch, the most apropriate place to call it is the #[init] canister method. For canisters which are migrating from standard data structures thic function should be added as a first line of #[post_upgrade] canister method and later (right after this canister upgrade happens) in the next code revision it should be replaced with stable_memory_post_upgrade().

Stable memory allocator is stored inside a thread_local! static variable at runtime.

Works the same way as [init_allocator(0)].

Panics

Panics if the allocator is already initialized.

Examples

For new canisters:

#[ic_cdk_macros::init]
fn init() {
    stable_memory_init();

    // the rest of the initialization
}

For migrating canisters:

// canister version N
#[ic_cdk_macros::post_upgrade]
fn post_upgrade() {
    stable_memory_init();

    // move data from standard collections into "stable" ones
}
// canister version N+1
#[ic_cdk_macros::post_upgrade]
fn post_upgrade() {
    stable_memory_post_upgrade();

    // the rest of canister's reinitialization
}