From a1f979a1a9dbd8b5516f61285cc7e9fa0018c733 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 31 Jul 2023 13:50:49 -0700 Subject: [PATCH] Wasmtime: Move `OnDemandInstanceAllocator` to its own module Just code motion, no other changes. --- crates/runtime/src/instance/allocator.rs | 127 +----------------- .../src/instance/allocator/on_demand.rs | 127 ++++++++++++++++++ 2 files changed, 132 insertions(+), 122 deletions(-) create mode 100644 crates/runtime/src/instance/allocator/on_demand.rs diff --git a/crates/runtime/src/instance/allocator.rs b/crates/runtime/src/instance/allocator.rs index a7cbaeef7824..da541957463a 100644 --- a/crates/runtime/src/instance/allocator.rs +++ b/crates/runtime/src/instance/allocator.rs @@ -1,6 +1,6 @@ use crate::imports::Imports; -use crate::instance::{Instance, InstanceHandle, RuntimeMemoryCreator}; -use crate::memory::{DefaultMemoryCreator, Memory}; +use crate::instance::{Instance, InstanceHandle}; +use crate::memory::Memory; use crate::table::Table; use crate::{CompiledModuleId, ModuleRuntimeInfo, Store}; use anyhow::{anyhow, bail, Result}; @@ -15,9 +15,11 @@ use wasmtime_environ::{ WasmType, WASM_PAGE_SIZE, }; +mod on_demand; +pub use self::on_demand::OnDemandInstanceAllocator; + #[cfg(feature = "pooling-allocator")] mod pooling; - #[cfg(feature = "pooling-allocator")] pub use self::pooling::{InstanceLimits, PoolingInstanceAllocator, PoolingInstanceAllocatorConfig}; @@ -410,122 +412,3 @@ pub(super) fn initialize_instance( Ok(()) } - -/// Represents the on-demand instance allocator. -#[derive(Clone)] -pub struct OnDemandInstanceAllocator { - mem_creator: Option>, - #[cfg(feature = "async")] - stack_size: usize, -} - -impl OnDemandInstanceAllocator { - /// Creates a new on-demand instance allocator. - pub fn new(mem_creator: Option>, stack_size: usize) -> Self { - let _ = stack_size; // suppress unused warnings w/o async feature - Self { - mem_creator, - #[cfg(feature = "async")] - stack_size, - } - } -} - -impl Default for OnDemandInstanceAllocator { - fn default() -> Self { - Self { - mem_creator: None, - #[cfg(feature = "async")] - stack_size: 0, - } - } -} - -unsafe impl InstanceAllocator for OnDemandInstanceAllocator { - fn allocate_index(&self, _req: &InstanceAllocationRequest) -> Result { - Ok(0) - } - - fn deallocate_index(&self, index: usize) { - assert_eq!(index, 0); - } - - fn allocate_memories( - &self, - _index: usize, - req: &mut InstanceAllocationRequest, - memories: &mut PrimaryMap, - ) -> Result<()> { - let module = req.runtime_info.module(); - let creator = self - .mem_creator - .as_deref() - .unwrap_or_else(|| &DefaultMemoryCreator); - let num_imports = module.num_imported_memories; - for (memory_idx, plan) in module.memory_plans.iter().skip(num_imports) { - let defined_memory_idx = module - .defined_memory_index(memory_idx) - .expect("Skipped imports, should never be None"); - let image = req.runtime_info.memory_image(defined_memory_idx)?; - - memories.push(Memory::new_dynamic( - plan, - creator, - unsafe { - req.store - .get() - .expect("if module has memory plans, store is not empty") - }, - image, - )?); - } - Ok(()) - } - - fn deallocate_memories( - &self, - _index: usize, - _mems: &mut PrimaryMap, - ) { - // normal destructors do cleanup here - } - - fn allocate_tables( - &self, - _index: usize, - req: &mut InstanceAllocationRequest, - tables: &mut PrimaryMap, - ) -> Result<()> { - let module = req.runtime_info.module(); - let num_imports = module.num_imported_tables; - for (_, table) in module.table_plans.iter().skip(num_imports) { - tables.push(Table::new_dynamic(table, unsafe { - req.store - .get() - .expect("if module has table plans, store is not empty") - })?); - } - Ok(()) - } - - fn deallocate_tables(&self, _index: usize, _tables: &mut PrimaryMap) { - // normal destructors do cleanup here - } - - #[cfg(feature = "async")] - fn allocate_fiber_stack(&self) -> Result { - if self.stack_size == 0 { - bail!("fiber stacks are not supported by the allocator") - } - - let stack = wasmtime_fiber::FiberStack::new(self.stack_size)?; - Ok(stack) - } - - #[cfg(feature = "async")] - unsafe fn deallocate_fiber_stack(&self, _stack: &wasmtime_fiber::FiberStack) { - // The on-demand allocator has no further bookkeeping for fiber stacks - } - - fn purge_module(&self, _: CompiledModuleId) {} -} diff --git a/crates/runtime/src/instance/allocator/on_demand.rs b/crates/runtime/src/instance/allocator/on_demand.rs new file mode 100644 index 000000000000..171562f9d918 --- /dev/null +++ b/crates/runtime/src/instance/allocator/on_demand.rs @@ -0,0 +1,127 @@ +use super::{InstanceAllocationRequest, InstanceAllocator}; +use crate::instance::RuntimeMemoryCreator; +use crate::memory::{DefaultMemoryCreator, Memory}; +use crate::table::Table; +use crate::CompiledModuleId; +use anyhow::Result; +use std::sync::Arc; +use wasmtime_environ::{DefinedMemoryIndex, DefinedTableIndex, PrimaryMap}; + +/// Represents the on-demand instance allocator. +#[derive(Clone)] +pub struct OnDemandInstanceAllocator { + mem_creator: Option>, + #[cfg(feature = "async")] + stack_size: usize, +} + +impl OnDemandInstanceAllocator { + /// Creates a new on-demand instance allocator. + pub fn new(mem_creator: Option>, stack_size: usize) -> Self { + let _ = stack_size; // suppress unused warnings w/o async feature + Self { + mem_creator, + #[cfg(feature = "async")] + stack_size, + } + } +} + +impl Default for OnDemandInstanceAllocator { + fn default() -> Self { + Self { + mem_creator: None, + #[cfg(feature = "async")] + stack_size: 0, + } + } +} + +unsafe impl InstanceAllocator for OnDemandInstanceAllocator { + fn allocate_index(&self, _req: &InstanceAllocationRequest) -> Result { + Ok(0) + } + + fn deallocate_index(&self, index: usize) { + assert_eq!(index, 0); + } + + fn allocate_memories( + &self, + _index: usize, + req: &mut InstanceAllocationRequest, + memories: &mut PrimaryMap, + ) -> Result<()> { + let module = req.runtime_info.module(); + let creator = self + .mem_creator + .as_deref() + .unwrap_or_else(|| &DefaultMemoryCreator); + let num_imports = module.num_imported_memories; + for (memory_idx, plan) in module.memory_plans.iter().skip(num_imports) { + let defined_memory_idx = module + .defined_memory_index(memory_idx) + .expect("Skipped imports, should never be None"); + let image = req.runtime_info.memory_image(defined_memory_idx)?; + + memories.push(Memory::new_dynamic( + plan, + creator, + unsafe { + req.store + .get() + .expect("if module has memory plans, store is not empty") + }, + image, + )?); + } + Ok(()) + } + + fn deallocate_memories( + &self, + _index: usize, + _mems: &mut PrimaryMap, + ) { + // normal destructors do cleanup here + } + + fn allocate_tables( + &self, + _index: usize, + req: &mut InstanceAllocationRequest, + tables: &mut PrimaryMap, + ) -> Result<()> { + let module = req.runtime_info.module(); + let num_imports = module.num_imported_tables; + for (_, table) in module.table_plans.iter().skip(num_imports) { + tables.push(Table::new_dynamic(table, unsafe { + req.store + .get() + .expect("if module has table plans, store is not empty") + })?); + } + Ok(()) + } + + fn deallocate_tables(&self, _index: usize, _tables: &mut PrimaryMap) { + // normal destructors do cleanup here + } + + #[cfg(feature = "async")] + fn allocate_fiber_stack(&self) -> Result { + if self.stack_size == 0 { + anyhow::bail!("fiber stacks are not supported by the allocator") + } + + let stack = wasmtime_fiber::FiberStack::new(self.stack_size)?; + Ok(stack) + } + + #[cfg(feature = "async")] + unsafe fn deallocate_fiber_stack(&self, _stack: &wasmtime_fiber::FiberStack) { + // The on-demand allocator has no further bookkeeping for fiber stacks + } + + fn purge_module(&self, _: CompiledModuleId) {} +}