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;
84mod capability;
85mod cbor;
86mod constants;
87mod declaration;
88mod diagnostics;
89mod hash;
90mod key;
91mod ledger;
92mod physical;
93mod policy;
94mod registry;
95mod runtime;
96mod schema;
97mod slot;
98mod stable_cell;
99mod text;
100mod validation;
101
102#[cfg(test)]
103mod test_cbor {
104 use serde::{Serialize, de::DeserializeOwned};
105
106 pub use ciborium::Value;
107
108 pub fn to_vec<T: Serialize>(
109 value: &T,
110 ) -> Result<Vec<u8>, ciborium::ser::Error<std::io::Error>> {
111 let mut bytes = Vec::new();
112 ciborium::into_writer(value, &mut bytes)?;
113 Ok(bytes)
114 }
115
116 pub fn from_slice<T: DeserializeOwned>(
117 bytes: &[u8],
118 ) -> Result<T, ciborium::de::Error<std::io::Error>> {
119 crate::cbor::from_slice_exact(bytes)
120 }
121
122 pub fn to_value<T: Serialize>(value: T) -> Result<Value, ciborium::value::Error> {
123 Value::serialized(&value)
124 }
125
126 pub fn map_insert(map: &mut Vec<(Value, Value)>, key: Value, value: Value) {
127 map.push((key, value));
128 }
129
130 pub fn hex_fixture(contents: &str) -> Vec<u8> {
131 let hex = contents
132 .chars()
133 .filter(|char| !char.is_whitespace())
134 .collect::<String>();
135 assert_eq!(hex.len() % 2, 0, "fixture hex must have byte pairs");
136 hex.as_bytes()
137 .as_chunks::<2>()
138 .0
139 .iter()
140 .map(|pair| {
141 let pair = std::str::from_utf8(pair).expect("fixture hex is utf8");
142 u8::from_str_radix(pair, 16).expect("fixture hex byte")
143 })
144 .collect()
145 }
146}
147
148pub use ic_stable_structures;
153
154pub use bootstrap::{
155 AllocationBootstrap, BootstrapError, BootstrapReservationError, BootstrapRetirementError,
156 PendingBootstrapCommit,
157};
158pub use capability::{CommittedAllocations, ValidatedAllocations};
159pub use constants::{
160 MAX_LEDGER_BYTES, MAX_LEDGER_GENERATIONS, MAX_LEDGER_NESTING, MAX_LEDGER_RECORD_BYTES,
161 WASM_PAGE_SIZE_BYTES,
162};
163pub use declaration::{
164 AllocationDeclaration, DeclarationCollector, DeclarationSnapshot, DeclarationSnapshotError,
165};
166pub use diagnostics::{
167 DiagnosticCheck, DiagnosticCode, DiagnosticDeclaration, DiagnosticExport, DiagnosticFailure,
168 DiagnosticGeneration, DiagnosticMemorySize, DiagnosticMemorySizeOutcome,
169 DiagnosticRangeAuthority, DiagnosticRecord, DiagnosticRuntimeBinding, DiagnosticStableCell,
170 DiagnosticStableCellStatus, MemoryRuntimeDoctorReport,
171};
172pub use key::{StableKey, StableKeyError};
173pub use ledger::{
174 AllocationHistory, AllocationLedger, AllocationRecord, AllocationReservationError,
175 AllocationRetirement, AllocationRetirementError, AllocationStageError, AllocationState,
176 GenerationRecord, LEDGER_PAYLOAD_FORMAT_VERSION, LedgerCommitError, LedgerCommitStore,
177 LedgerIntegrityError, LedgerPayloadEnvelope, LedgerPayloadEnvelopeError, RecoveredLedger,
178 SchemaMetadataRecord,
179};
180pub use physical::{
181 CommitRecoveryError, CommitSlotDiagnostic, CommitStoreDiagnostic, CommittedGenerationBytes,
182 DualCommitStore,
183};
184pub use policy::{AllocationPolicy, PolicyIdentity, PolicyIdentityError, RuntimeBootstrapPolicy};
185pub use registry::{
186 MemoryRequest, SealedDeclarationFingerprint, SealedDeclarationSnapshot,
187 StaticMemoryDeclaration, StaticMemoryDeclarationError, StaticMemoryRangeDeclaration,
188 register_memory_request, register_static_memory_declaration,
189 register_static_memory_manager_declaration,
190 register_static_memory_manager_declaration_with_schema, register_static_memory_manager_range,
191 register_static_memory_range_declaration, sealed_declaration_snapshot,
192};
193pub use runtime::{
194 AllocationBinding, AllocationRangeClaim, BootstrapAdmission, BootstrapAdmissionError,
195 GenericRangePolicy, MemoryAllocation, MemoryAllocationSummary, MemoryAllocations,
196 MemoryBindingSummary, MemoryManagerConfig, MemoryManagerLayoutError, MemoryResolutionError,
197 MemoryRuntime, RecoveredAllocationMetadata, RuntimeAdoptionError, RuntimeBootstrapError,
198 RuntimeConstructionError, RuntimeDiagnosticError, RuntimeGrowError, RuntimeMemory,
199 RuntimeOpenError, RuntimePolicyError, RuntimeStateError, bootstrap_default_memory_manager,
200 bootstrap_default_memory_manager_with_config, bootstrap_default_memory_manager_with_policy,
201 committed_allocations, default_memory_manager_commit_recovery_diagnostic,
202 default_memory_manager_diagnostic_export, default_memory_manager_doctor_report,
203 default_memory_manager_doctor_report_with_policy,
204 default_memory_manager_memory_allocation_summary, default_memory_manager_memory_allocations,
205 default_memory_manager_memory_id, is_default_memory_manager_bootstrapped,
206 open_default_memory_manager_memory, open_default_memory_manager_memory_by_key,
207 verify_default_memory_manager_authority,
208};
209pub use schema::{SchemaMetadata, SchemaMetadataError};
210pub use slot::{
211 AllocationSlot, AllocationSlotDescriptor, IC_MEMORY_AUTHORITY_OWNER,
212 IC_MEMORY_AUTHORITY_PURPOSE, IC_MEMORY_LEDGER_LABEL, IC_MEMORY_LEDGER_STABLE_KEY,
213 IC_MEMORY_STABLE_KEY_PREFIX, MEMORY_MANAGER_GOVERNANCE_MAX_ID, MEMORY_MANAGER_INVALID_ID,
214 MEMORY_MANAGER_LEDGER_ID, MEMORY_MANAGER_MAX_ID, MEMORY_MANAGER_MIN_ID,
215 MemoryManagerAuthorityRecord, MemoryManagerIdRange, MemoryManagerRangeAuthority,
216 MemoryManagerRangeAuthorityError, MemoryManagerRangeError, MemoryManagerRangeMode,
217 MemoryManagerSlotError, is_ic_memory_stable_key, memory_manager_governance_range,
218 validate_memory_manager_id,
219};
220pub use stable_cell::{
221 STABLE_CELL_HEADER_SIZE, STABLE_CELL_LAYOUT_VERSION, STABLE_CELL_MAGIC,
222 STABLE_CELL_VALUE_OFFSET, StableCellLedgerError, StableCellLedgerRecord,
223 StableCellPayloadError, decode_stable_cell_ledger_record, decode_stable_cell_payload,
224 validate_stable_cell_ledger_memory,
225};
226pub use validation::{AllocationValidationError, Validate, validate_allocations};
227
228#[doc(hidden)]
229pub use registry::{defer_eager_init, defer_static_memory_registration};
230
231#[doc(hidden)]
232pub mod __reexports {
233 pub use ctor;
234}
235
236#[macro_export]
246macro_rules! ic_memory_declaration {
247 (authority = $authority:expr, key = $stable_key:literal $(,)?) => {
248 const _: () = {
249 fn __ic_memory_register_request() -> Result<(), $crate::StaticMemoryDeclarationError> {
250 $crate::register_memory_request($crate::MemoryRequest::new(
251 $authority, $stable_key, $crate::SchemaMetadata::default(),
252 )?)
253 }
254 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
255 fn __ic_memory_defer_request() {
256 $crate::defer_static_memory_registration(__ic_memory_register_request);
257 }
258 };
259 };
260 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {
261 const _: () = {
262 const __IC_MEMORY_AUTHORITY: &str = $authority;
263
264 fn __ic_memory_register_static_declaration() -> Result<(), $crate::StaticMemoryDeclarationError> {
265 let _ = core::marker::PhantomData::<$label>;
266 $crate::register_static_memory_manager_declaration(
267 $id,
268 __IC_MEMORY_AUTHORITY,
269 stringify!($label),
270 $stable_key,
271 )
272 }
273
274 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
275 fn __ic_memory_defer_static_declaration() {
276 $crate::defer_static_memory_registration(__ic_memory_register_static_declaration);
277 }
278 };
279 };
280 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {
281 const _: () = {
282 const __IC_MEMORY_AUTHORITY: &str = $authority;
283
284 fn __ic_memory_register_static_declaration() -> Result<(), $crate::StaticMemoryDeclarationError> {
285 $crate::register_static_memory_manager_declaration(
286 $id,
287 __IC_MEMORY_AUTHORITY,
288 $label,
289 $stable_key,
290 )
291 }
292
293 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
294 fn __ic_memory_defer_static_declaration() {
295 $crate::defer_static_memory_registration(__ic_memory_register_static_declaration);
296 }
297 };
298 };
299}
300
301#[macro_export]
325macro_rules! ic_memory_range {
326 (authority = $authority:expr, start = $start:expr, end = $end:expr $(,)?) => {
327 $crate::ic_memory_range!(
328 authority = $authority,
329 start = $start,
330 end = $end,
331 mode = Reserved,
332 );
333 };
334 (authority = $authority:expr, start = $start:expr, end = $end:expr, mode = $mode:ident $(,)?) => {
335 const _: () = {
336 const __IC_MEMORY_AUTHORITY: &str = $authority;
337
338 fn __ic_memory_register_static_range() -> Result<(), $crate::StaticMemoryDeclarationError> {
339 $crate::register_static_memory_manager_range(
340 $start,
341 $end,
342 __IC_MEMORY_AUTHORITY,
343 $crate::MemoryManagerRangeMode::$mode,
344 None,
345 )
346 }
347
348 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
349 fn __ic_memory_defer_static_range() {
350 $crate::defer_static_memory_registration(__ic_memory_register_static_range);
351 }
352 };
353 };
354}
355
356#[macro_export]
361macro_rules! ic_memory_key {
362 (authority = $authority:expr, key = $stable_key:literal $(,)?) => {{
363 $crate::ic_memory_declaration!(authority = $authority, key = $stable_key);
364 $crate::open_default_memory_manager_memory_by_key($stable_key)
365 }};
366 (authority = $authority:expr, key = $stable_key:literal, ty = $label:path, id = $id:expr $(,)?) => {{
367 $crate::ic_memory_declaration!(
368 authority = $authority,
369 key = $stable_key,
370 ty = $label,
371 id = $id,
372 );
373 $crate::open_default_memory_manager_memory($stable_key, $id)
374 }};
375 (authority = $authority:expr, key = $stable_key:literal, label = $label:literal, id = $id:expr $(,)?) => {{
376 $crate::ic_memory_declaration!(
377 authority = $authority,
378 key = $stable_key,
379 label = $label,
380 id = $id,
381 );
382 $crate::open_default_memory_manager_memory($stable_key, $id)
383 }};
384}
385
386#[macro_export]
388macro_rules! eager_init {
389 ($body:block) => {
390 const _: () = {
391 fn __ic_memory_registered_eager_init_body() {
392 $body
393 }
394
395 #[ $crate::__reexports::ctor::ctor(unsafe, anonymous, crate_path = $crate::__reexports::ctor) ]
396 fn __ic_memory_register_eager_init() {
397 $crate::defer_eager_init(__ic_memory_registered_eager_init_body);
398 }
399 };
400 };
401}