ic_memory/runtime/config.rs
1use super::RuntimeConstructionError;
2
3///
4/// MemoryManagerConfig
5///
6/// Immutable bucket allocation policy for a runtime. The default remains 128
7/// Wasm pages (8 MiB). Smaller sizes trade less rounding slack for a lower
8/// 32,768-bucket capacity and more frequent growth. This policy is separate
9/// from application allocation authorization and never grants memory access.
10///
11/// Explicit construction checks existing memory for an exact match before any
12/// effects. This is a same-release setting, not a migration or shrink operation.
13///
14
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub struct MemoryManagerConfig {
17 bucket_size_pages: u16,
18}
19
20impl Default for MemoryManagerConfig {
21 fn default() -> Self {
22 Self {
23 bucket_size_pages: 128,
24 }
25 }
26}
27
28impl MemoryManagerConfig {
29 pub(super) const fn from_validated(bucket_size_pages: u16) -> Self {
30 Self { bucket_size_pages }
31 }
32
33 /// Validate a nonzero bucket size in Wasm pages before construction effects.
34 pub const fn new(bucket_size_pages: u16) -> Result<Self, RuntimeConstructionError> {
35 if bucket_size_pages == 0 {
36 return Err(RuntimeConstructionError::InvalidBucketSize);
37 }
38 Ok(Self { bucket_size_pages })
39 }
40
41 /// Return the requested bucket size in pages.
42 #[must_use]
43 pub const fn bucket_size_pages(self) -> u16 {
44 self.bucket_size_pages
45 }
46}