Skip to main content

canic_core/
memory_macros.rs

1/// Declare a stable-memory slot with an explicit ABI-stable key.
2///
3/// Use this for every Canic-managed memory. The stable key, not crate or Rust
4/// type identity, is the durable allocation identity.
5#[macro_export]
6macro_rules! ic_memory_key {
7    ($stable_key:literal, $label:path, $id:expr) => {{
8        $crate::memory::runtime::assert_memory_bootstrap_ready(stringify!($label), $id);
9        $crate::__reexports::ic_memory::ic_memory_key!($stable_key, $label, $id)
10    }};
11}
12
13/// Declare a MemoryManager ID range owned by the declaring crate.
14#[macro_export]
15macro_rules! ic_memory_range {
16    (start = $start:expr, end = $end:expr $(,)?) => {
17        $crate::__reexports::ic_memory::ic_memory_range!(start = $start, end = $end,);
18    };
19    (start = $start:expr, end = $end:expr, mode = $mode:ident $(,)?) => {
20        $crate::__reexports::ic_memory::ic_memory_range!(start = $start, end = $end, mode = $mode,);
21    };
22}
23
24/// Register one eager-init body for execution during lifecycle bootstrap.
25#[macro_export]
26macro_rules! eager_init {
27    ($body:block) => {
28        $crate::__reexports::ic_memory::eager_init!($body);
29    };
30}
31
32/// Declare a thread-local static and schedule an eager initialization touch.
33#[macro_export]
34macro_rules! eager_static {
35    ($vis:vis static $name:ident : $ty:ty = $init:expr;) => {
36        thread_local! {
37            $vis static $name: $ty = $init;
38        }
39
40        const _: () = {
41            fn __canic_touch_tls() {
42                $name.with(|_| {});
43            }
44
45            #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
46            fn __canic_register_eager_tls() {
47                $crate::memory::runtime::defer_tls_initializer(__canic_touch_tls);
48            }
49        };
50    };
51}