From 3c16adc4e12873802b15d77a8357f56893a9c84f Mon Sep 17 00:00:00 2001 From: Daniel Stuart Date: Mon, 9 Sep 2024 19:19:25 -0300 Subject: feat: Custom memory page size (#22) --- crates/types/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) (limited to 'crates/types') diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 94dc94d..ce0d4ac 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -12,6 +12,14 @@ extern crate alloc; use alloc::boxed::Box; use core::{fmt::Debug, ops::Range}; +// Memory defaults +const MEM_PAGE_SIZE: u64 = 65536; +const MAX_MEMORY_SIZE: u64 = 4294967296; + +const fn max_page_count(page_size: u64) -> u64 { + MAX_MEMORY_SIZE / page_size +} + // log for logging (optional). #[cfg(feature = "logging")] #[allow(clippy::single_component_path_imports, unused_imports)] @@ -252,14 +260,39 @@ impl TableType { #[derive(Debug, Copy, Clone, PartialEq)] #[cfg_attr(feature = "archive", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize), archive(check_bytes))] pub struct MemoryType { - pub arch: MemoryArch, - pub page_count_initial: u64, - pub page_count_max: Option, + arch: MemoryArch, + page_count_initial: u64, + page_count_max: Option, + page_size: Option, } impl MemoryType { - pub fn new_32(page_count_initial: u64, page_count_max: Option) -> Self { - Self { arch: MemoryArch::I32, page_count_initial, page_count_max } + pub fn new(arch: MemoryArch, page_count_initial: u64, page_count_max: Option, page_size: Option) -> Self { + Self { arch, page_count_initial, page_count_max, page_size } + } + + pub fn arch(&self) -> MemoryArch { + self.arch + } + + pub fn page_count_initial(&self) -> u64 { + self.page_count_initial + } + + pub fn page_count_max(&self) -> u64 { + self.page_count_max.unwrap_or_else(|| max_page_count(self.page_size())) + } + + pub fn page_size(&self) -> u64 { + self.page_size.unwrap_or(MEM_PAGE_SIZE) + } + + pub fn initial_size(&self) -> u64 { + self.page_count_initial * self.page_size() + } + + pub fn max_size(&self) -> u64 { + self.page_count_max() * self.page_size() } } -- cgit v1.3.1