Skip to content

Commit

Permalink
Remove LinearMemory trait
Browse files Browse the repository at this point in the history
  • Loading branch information
adambratschikaye committed Jan 23, 2024
1 parent 72fe09b commit 273b11a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
4 changes: 0 additions & 4 deletions rs/embedders/src/lib.rs
Expand Up @@ -36,10 +36,6 @@ pub struct InstanceRunResult {
pub exported_globals: Vec<Global>,
}

pub trait LinearMemory {
fn as_ptr(&self) -> *mut libc::c_void;
}

/// The results of compiling a Canister which need to be passed back to the main
/// replica process.
#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down
20 changes: 8 additions & 12 deletions rs/embedders/src/wasmtime_embedder/host_memory.rs
@@ -1,10 +1,8 @@
use anyhow::bail;
use ic_types::MAX_STABLE_MEMORY_IN_BYTES;
use wasmtime::MemoryType;
use wasmtime::{LinearMemory, MemoryType};
use wasmtime_environ::{WASM32_MAX_PAGES, WASM_PAGE_SIZE};

use crate::LinearMemory;

use ic_sys::PAGE_SIZE;

use libc::c_void;
Expand Down Expand Up @@ -60,7 +58,7 @@ unsafe impl wasmtime::MemoryCreator for WasmtimeMemoryCreator {
_maximum: Option<usize>,
reserved_size_in_bytes: Option<usize>,
guard_size: usize,
) -> Result<Box<dyn wasmtime::LinearMemory>, String> {
) -> Result<Box<dyn LinearMemory>, String> {
// We don't use the `reserved_size_in_bytes` because the size of the
// memory allocation is determined based on the memory type: 64-bit
// memories have size at most the maximum stable memory size and 32-bit
Expand Down Expand Up @@ -102,7 +100,7 @@ unsafe impl wasmtime::MemoryCreator for WasmtimeMemoryCreator {
epilogue_guard_size_in_bytes,
);
created_memories.insert(
MemoryStart(wasmtime::LinearMemory::as_ptr(&new_memory) as usize),
MemoryStart(LinearMemory::as_ptr(&new_memory) as usize),
MemoryPageSize(Arc::clone(&new_memory.used)),
);
Ok(Box::new(new_memory))
Expand Down Expand Up @@ -185,9 +183,7 @@ impl MmapMemory {
epilogue_guard_size_in_bytes,
}
}
}

impl LinearMemory for MmapMemory {
fn as_ptr(&self) -> *mut c_void {
self.wasm_memory
}
Expand All @@ -200,17 +196,17 @@ impl Drop for MmapMemory {
}
}

pub struct WasmtimeMemory<M: LinearMemory> {
mem: M,
pub struct WasmtimeMemory {
mem: MmapMemory,
max_size_in_pages: usize,
used: MemoryPageSize,
prologue_guard_size_in_bytes: usize,
epilogue_guard_size_in_bytes: usize,
}

impl<M: LinearMemory + Send> WasmtimeMemory<M> {
impl WasmtimeMemory {
fn new(
mem: M,
mem: MmapMemory,
min_size_in_pages: usize,
max_size_in_pages: usize,
prologue_guard_size_in_bytes: usize,
Expand All @@ -234,7 +230,7 @@ fn convert_pages_to_bytes(pages: usize) -> usize {
result
}

unsafe impl<M: LinearMemory + Send + Sync + 'static> wasmtime::LinearMemory for WasmtimeMemory<M> {
unsafe impl LinearMemory for WasmtimeMemory {
/// Returns the number of allocated wasm pages.
fn byte_size(&self) -> usize {
convert_pages_to_bytes(self.used.load(Ordering::SeqCst))
Expand Down

0 comments on commit 273b11a

Please sign in to comment.