1#![deny(unsafe_code, unsafe_op_in_unsafe_fn)]
2#![deny(rustdoc::broken_intra_doc_links)]
3#![doc = include_str!("../README.md")]
4
5mod bootstrap;
82mod capability;
83mod cbor;
84mod constants;
85mod declaration;
86mod diagnostics;
87mod key;
88mod ledger;
89mod physical;
90mod policy;
91mod registry;
92mod runtime;
93mod schema;
94mod slot;
95mod stable_cell;
96mod validation;
97
98#[cfg(test)]
99mod test_cbor {
100 use serde::{Serialize, de::DeserializeOwned};
101
102 pub use ciborium::Value;
103
104 pub fn to_vec<T: Serialize>(
105 value: &T,
106 ) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
107 let mut bytes = Vec::new();
108 ciborium::into_writer(value, &mut bytes)?;
109 Ok(bytes)
110 }
111
112 pub fn from_slice<T: DeserializeOwned>(
113 bytes: &[u8],
114 ) -> Result<T, ciborium::de::Error<std::io::Error>> {
115 crate::cbor::from_slice_exact(bytes)
116 }
117
118 pub fn to_value<T: Serialize>(value: T) -> Result<Value, ciborium::value::Error> {
119 Value::serialized(&value)
120 }
121
122 pub fn map_insert(map: &mut Vec<(Value, Value)>, key: Value, value: Value) {
123 map.push((key, value));
124 }
125}
126
127pub use ic_stable_structures;
132
133pub use bootstrap::{
134 AllocationBootstrap, BootstrapError, BootstrapReservationError, BootstrapRetirementError,
135 PendingBootstrapCommit,
136};
137pub use capability::{CommittedAllocations, ValidatedAllocations};
138pub use constants::{
139 MAX_LEDGER_BYTES, MAX_LEDGER_GENERATIONS, MAX_LEDGER_NESTING, MAX_LEDGER_RECORD_BYTES,
140 WASM_PAGE_SIZE_BYTES,
141};
142pub use declaration::{
143 AllocationDeclaration, DeclarationCollector, DeclarationSnapshot, DeclarationSnapshotError,
144};
145pub use diagnostics::{
146 DiagnosticCheck, DiagnosticCode, DiagnosticDeclaration, DiagnosticExport, DiagnosticFailure,
147 DiagnosticGeneration, DiagnosticMemorySize, DiagnosticMemorySizeOutcome,
148 DiagnosticRangeAuthority, DiagnosticRecord, DiagnosticRuntimeBinding, DiagnosticStableCell,
149 DiagnosticStableCellStatus, MemoryRuntimeDoctorReport,
150};
151pub use key::{StableKey, StableKeyError};
152pub use ledger::{
153 AllocationHistory, AllocationLedger, AllocationRecord, AllocationReservationError,
154 AllocationRetirement, AllocationRetirementError, AllocationStageError, AllocationState,
155 GenerationRecord, LEDGER_PAYLOAD_FORMAT_VERSION, LedgerCommitError, LedgerCommitStore,
156 LedgerIntegrityError, LedgerPayloadEnvelope, LedgerPayloadEnvelopeError, RecoveredLedger,
157 SchemaMetadataRecord,
158};
159pub use physical::{
160 CommitRecoveryError, CommitSlotDiagnostic, CommitStoreDiagnostic, CommittedGenerationBytes,
161 DualCommitStore,
162};
163pub use policy::{AllocationPolicy, PolicyIdentity, PolicyIdentityError, RuntimeBootstrapPolicy};
164pub use registry::{
165 MemoryRequest, SealedDeclarationFingerprint, SealedDeclarationSnapshot,
166 StaticMemoryDeclaration, StaticMemoryDeclarationError, StaticMemoryRangeDeclaration,
167 register_memory_request, register_static_memory_declaration,
168 register_static_memory_manager_declaration,
169 register_static_memory_manager_declaration_with_schema, register_static_memory_manager_range,
170 register_static_memory_range_declaration, sealed_declaration_snapshot,
171};
172pub use runtime::{
173 AllocationBinding, AllocationRangeClaim, BootstrapAdmission, BootstrapAdmissionError,
174 GenericRangePolicy, MemoryAllocation, MemoryAllocationSummary, MemoryAllocations,
175 MemoryBindingSummary, MemoryManagerConfig, MemoryManagerLayoutError, MemoryResolutionError,
176 MemoryRuntime, RecoveredAllocationMetadata, RuntimeAdoptionError, RuntimeBootstrapError,
177 RuntimeConstructionError, RuntimeDiagnosticError, RuntimeGrowError, RuntimeMemory,
178 RuntimeOpenError, RuntimePolicyError, RuntimeStateError, bootstrap_default_memory_manager,
179 bootstrap_default_memory_manager_with_config, bootstrap_default_memory_manager_with_policy,
180 committed_allocations, default_memory_manager_commit_recovery_diagnostic,
181 default_memory_manager_diagnostic_export, default_memory_manager_doctor_report,
182 default_memory_manager_doctor_report_with_policy,
183 default_memory_manager_memory_allocation_summary, default_memory_manager_memory_allocations,
184 default_memory_manager_memory_id, is_default_memory_manager_bootstrapped,
185 open_default_memory_manager_memory, open_default_memory_manager_memory_by_key,
186 verify_default_memory_manager_authority,
187};
188pub use schema::{SchemaMetadata, SchemaMetadataError};
189pub use slot::{
190 AllocationSlot, AllocationSlotDescriptor, IC_MEMORY_AUTHORITY_OWNER,
191 IC_MEMORY_AUTHORITY_PURPOSE, IC_MEMORY_LEDGER_LABEL, IC_MEMORY_LEDGER_STABLE_KEY,
192 IC_MEMORY_STABLE_KEY_PREFIX, MEMORY_MANAGER_GOVERNANCE_MAX_ID, MEMORY_MANAGER_INVALID_ID,
193 MEMORY_MANAGER_LEDGER_ID, MEMORY_MANAGER_MAX_ID, MEMORY_MANAGER_MIN_ID,
194 MemoryManagerAuthorityRecord, MemoryManagerIdRange, MemoryManagerRangeAuthority,
195 MemoryManagerRangeAuthorityError, MemoryManagerRangeError, MemoryManagerRangeMode,
196 MemoryManagerSlotError, is_ic_memory_stable_key, memory_manager_governance_range,
197 validate_memory_manager_id,
198};
199pub use stable_cell::{
200 STABLE_CELL_HEADER_SIZE, STABLE_CELL_LAYOUT_VERSION, STABLE_CELL_MAGIC,
201 STABLE_CELL_VALUE_OFFSET, StableCellLedgerError, StableCellLedgerRecord,
202 StableCellPayloadError, decode_stable_cell_ledger_record, decode_stable_cell_payload,
203 validate_stable_cell_ledger_memory,
204};
205pub use validation::{AllocationValidationError, Validate, validate_allocations};
206
207#[doc(hidden)]
208pub use registry::{defer_eager_init, defer_static_memory_registration};
209
210#[doc(hidden)]
211pub mod __reexports {
212 pub use ctor;
213}
214
215#[macro_export]
225macro_rules! ic_memory_declaration {
226 (authority = $authority:expr, key = $stable_key:literal $(,)?) => {
227 const _: () = {
228 fn __ic_memory_register_request() -> Result<(), $crate::StaticMemoryDeclarationError> {
229 $crate::register_memory_request($crate::MemoryRequest::new(
230 $authority, $stable_key, $crate::SchemaMetadata::default(),
231 )?)
232 }
233 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
234 fn __ic_memory_defer_request() {
235 $crate::defer_static_memory_registration(__ic_memory_register_request);
236 }
237 };
238 };
239 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {
240 const _: () = {
241 const __IC_MEMORY_AUTHORITY: &str = $authority;
242
243 fn __ic_memory_register_static_declaration() -> Result<(), $crate::StaticMemoryDeclarationError> {
244 let _ = core::marker::PhantomData::<$label>;
245 $crate::register_static_memory_manager_declaration(
246 $id,
247 __IC_MEMORY_AUTHORITY,
248 stringify!($label),
249 $stable_key,
250 )
251 }
252
253 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
254 fn __ic_memory_defer_static_declaration() {
255 $crate::defer_static_memory_registration(__ic_memory_register_static_declaration);
256 }
257 };
258 };
259 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {
260 const _: () = {
261 const __IC_MEMORY_AUTHORITY: &str = $authority;
262
263 fn __ic_memory_register_static_declaration() -> Result<(), $crate::StaticMemoryDeclarationError> {
264 $crate::register_static_memory_manager_declaration(
265 $id,
266 __IC_MEMORY_AUTHORITY,
267 $label,
268 $stable_key,
269 )
270 }
271
272 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
273 fn __ic_memory_defer_static_declaration() {
274 $crate::defer_static_memory_registration(__ic_memory_register_static_declaration);
275 }
276 };
277 };
278}
279
280#[macro_export]
285macro_rules! ic_memory_range {
286 (authority = $authority:expr, start = $start:expr, end = $end:expr $(,)?) => {
287 $crate::ic_memory_range!(
288 authority = $authority,
289 start = $start,
290 end = $end,
291 mode = Reserved,
292 );
293 };
294 (authority = $authority:expr, start = $start:expr, end = $end:expr, mode = $mode:ident $(,)?) => {
295 const _: () = {
296 const __IC_MEMORY_AUTHORITY: &str = $authority;
297
298 fn __ic_memory_register_static_range() -> Result<(), $crate::StaticMemoryDeclarationError> {
299 $crate::register_static_memory_manager_range(
300 $start,
301 $end,
302 __IC_MEMORY_AUTHORITY,
303 $crate::MemoryManagerRangeMode::$mode,
304 None,
305 )
306 }
307
308 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
309 fn __ic_memory_defer_static_range() {
310 $crate::defer_static_memory_registration(__ic_memory_register_static_range);
311 }
312 };
313 };
314}
315
316#[macro_export]
321macro_rules! ic_memory_key {
322 (authority = $authority:expr, key = $stable_key:literal $(,)?) => {{
323 $crate::ic_memory_declaration!(authority = $authority, key = $stable_key);
324 $crate::open_default_memory_manager_memory_by_key($stable_key)
325 }};
326 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {{
327 $crate::ic_memory_declaration!(
328 authority = $authority,
329 key = $stable_key,
330 ty = $label,
331 id = $id,
332 );
333 $crate::open_default_memory_manager_memory($stable_key, $id)
334 }};
335 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {{
336 $crate::ic_memory_declaration!(
337 authority = $authority,
338 key = $stable_key,
339 label = $label,
340 id = $id,
341 );
342 $crate::open_default_memory_manager_memory($stable_key, $id)
343 }};
344}
345
346#[macro_export]
348macro_rules! eager_init {
349 ($body:block) => {
350 const _: () = {
351 fn __ic_memory_registered_eager_init_body() {
352 $body
353 }
354
355 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
356 fn __ic_memory_register_eager_init() {
357 $crate::defer_eager_init(__ic_memory_registered_eager_init_body);
358 }
359 };
360 };
361}