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