From 5802f94daff4daf007fe41ccc425334c36a6f812 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sun, 18 Jun 2023 19:51:35 +0200 Subject: [PATCH] get rid of the empty memory_manager module --- common/state/src/plugin/memory_manager.rs | 235 ++++++++++------------ common/state/src/plugin/module.rs | 13 +- common/state/src/plugin/wasm_env.rs | 22 +- 3 files changed, 119 insertions(+), 151 deletions(-) diff --git a/common/state/src/plugin/memory_manager.rs b/common/state/src/plugin/memory_manager.rs index e6332a702a..6a7da95f86 100644 --- a/common/state/src/plugin/memory_manager.rs +++ b/common/state/src/plugin/memory_manager.rs @@ -103,137 +103,124 @@ impl EcsAccessManager { } } -#[derive(Default)] -pub struct MemoryManager; +/// This function check if the buffer is wide enough if not it realloc the +/// buffer calling the `wasm_prepare_buffer` function Note: There is +/// probably optimizations that can be done using less restrictive +/// ordering +pub fn get_pointer( + store: &mut StoreMut, + object_length: ::Offset, + allocator: &TypedFunction< + ::Offset, + WasmPtr, + >, +) -> Result, MemoryAllocationError> { + allocator + .call(store, object_length) + .map_err(MemoryAllocationError::CantAllocate) +} -impl MemoryManager { - /// This function check if the buffer is wide enough if not it realloc the - /// buffer calling the `wasm_prepare_buffer` function Note: There is - /// probably optimizations that can be done using less restrictive - /// ordering - pub fn get_pointer( - &self, - store: &mut StoreMut, - object_length: ::Offset, - allocator: &TypedFunction< - ::Offset, - WasmPtr, - >, - ) -> Result, MemoryAllocationError> { - allocator - .call(store, object_length) - .map_err(MemoryAllocationError::CantAllocate) - } - - /// This function writes an object to WASM memory returning a pointer and a - /// length. Will realloc the buffer is not wide enough - pub fn write_data( - &self, - store: &mut StoreMut, - memory: &Memory, - allocator: &TypedFunction< - ::Offset, - WasmPtr, - >, - object: &T, - ) -> Result< +/// This function writes an object to WASM memory returning a pointer and a +/// length. Will realloc the buffer is not wide enough +pub fn write_data( + store: &mut StoreMut, + memory: &Memory, + allocator: &TypedFunction< + ::Offset, + WasmPtr, + >, + object: &T, +) -> Result< + ( + WasmPtr, + ::Offset, + ), + PluginModuleError, +> { + write_bytes( + store, + memory, + allocator, ( - WasmPtr, - ::Offset, + &bincode::serialize(object).map_err(PluginModuleError::Encoding)?, + &[], ), - PluginModuleError, - > { - self.write_bytes( - store, - memory, - allocator, - ( - &bincode::serialize(object).map_err(PluginModuleError::Encoding)?, - &[], - ), - ) - } + ) +} - /// This functions wraps the serialization process - pub fn serialize_data(object: &T) -> Result, PluginModuleError> { - bincode::serialize(object).map_err(PluginModuleError::Encoding) - } +/// This functions wraps the serialization process +pub fn serialize_data(object: &T) -> Result, PluginModuleError> { + bincode::serialize(object).map_err(PluginModuleError::Encoding) +} - /// This function writes an object to the wasm memory using the allocator if - /// necessary using length padding. - /// - /// With length padding the first 8 bytes written are the length of the the - /// following slice (The object serialized). - pub fn write_data_as_pointer( - &self, - store: &mut StoreMut, - memory: &Memory, - allocator: &TypedFunction< - ::Offset, - WasmPtr, - >, - object: &T, - ) -> Result, PluginModuleError> { - self.write_bytes_as_pointer(store, memory, allocator, &Self::serialize_data(object)?) - } +/// This function writes an object to the wasm memory using the allocator if +/// necessary using length padding. +/// +/// With length padding the first 8 bytes written are the length of the the +/// following slice (The object serialized). +pub fn write_data_as_pointer( + store: &mut StoreMut, + memory: &Memory, + allocator: &TypedFunction< + ::Offset, + WasmPtr, + >, + object: &T, +) -> Result, PluginModuleError> { + write_bytes_as_pointer(store, memory, allocator, &serialize_data(object)?) +} - /// This function writes an raw bytes to WASM memory returning a pointer and - /// a length. Will realloc the buffer is not wide enough - pub fn write_bytes( - &self, - store: &mut StoreMut, - memory: &Memory, - allocator: &TypedFunction< - ::Offset, - WasmPtr, - >, - bytes: (&[u8], &[u8]), - ) -> Result< - ( - WasmPtr, - ::Offset, - ), - PluginModuleError, - > { - let len = (bytes.0.len() + bytes.1.len()) as ::Offset; - let ptr = self - .get_pointer(store, len, allocator) - .map_err(PluginModuleError::MemoryAllocation)?; - ptr.slice( - &memory.view(store), - len as ::Offset, - ) - .and_then(|s| { - if !bytes.1.is_empty() { - s.subslice(0..bytes.0.len() as u64).write_slice(bytes.0)?; - s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1) - } else { - s.write_slice(bytes.0) - } - }) - .map_err(|_| PluginModuleError::InvalidPointer)?; - Ok((ptr, len)) - } +/// This function writes an raw bytes to WASM memory returning a pointer and +/// a length. Will realloc the buffer is not wide enough +pub fn write_bytes( + store: &mut StoreMut, + memory: &Memory, + allocator: &TypedFunction< + ::Offset, + WasmPtr, + >, + bytes: (&[u8], &[u8]), +) -> Result< + ( + WasmPtr, + ::Offset, + ), + PluginModuleError, +> { + let len = (bytes.0.len() + bytes.1.len()) as ::Offset; + let ptr = get_pointer(store, len, allocator).map_err(PluginModuleError::MemoryAllocation)?; + ptr.slice( + &memory.view(store), + len as ::Offset, + ) + .and_then(|s| { + if !bytes.1.is_empty() { + s.subslice(0..bytes.0.len() as u64).write_slice(bytes.0)?; + s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1) + } else { + s.write_slice(bytes.0) + } + }) + .map_err(|_| PluginModuleError::InvalidPointer)?; + Ok((ptr, len)) +} - /// This function writes bytes to the wasm memory using the allocator if - /// necessary using length padding. - /// - /// With length padding the first 8 bytes written are the length of the the - /// following slice. - pub fn write_bytes_as_pointer( - &self, - store: &mut StoreMut, - memory: &Memory, - allocator: &TypedFunction< - ::Offset, - WasmPtr, - >, - bytes: &[u8], - ) -> Result, PluginModuleError> { - let len = bytes.len() as ::Offset; - self.write_bytes(store, memory, allocator, (&len.to_le_bytes(), &bytes)) - .map(|val| val.0) - } +/// This function writes bytes to the wasm memory using the allocator if +/// necessary using length padding. +/// +/// With length padding the first 8 bytes written are the length of the the +/// following slice. +pub fn write_bytes_as_pointer( + store: &mut StoreMut, + memory: &Memory, + allocator: &TypedFunction< + ::Offset, + WasmPtr, + >, + bytes: &[u8], +) -> Result, PluginModuleError> { + let len = bytes.len() as ::Offset; + write_bytes(store, memory, allocator, (&len.to_le_bytes(), bytes)).map(|val| val.0) } /// This function reads data from memory at a position with the array length and diff --git a/common/state/src/plugin/module.rs b/common/state/src/plugin/module.rs index 54a1533068..1477c80c46 100644 --- a/common/state/src/plugin/module.rs +++ b/common/state/src/plugin/module.rs @@ -9,7 +9,7 @@ use wasmer::{ use super::{ errors::{PluginError, PluginModuleError}, exports, - memory_manager::{self, EcsAccessManager, EcsWorld, MemoryManager}, + memory_manager::{self, EcsAccessManager, EcsWorld}, wasm_env::HostFunctionEnvironment, MemoryModel, }; @@ -21,7 +21,6 @@ use plugin_api::{Action, EcsAccessError, Event, Retrieve, RetrieveError, Retriev pub struct PluginModule { ecs: Arc, wasm_state: Arc, - memory_manager: Arc, events: HashSet, allocator: TypedFunction<::Offset, WasmPtr>, memory: Memory, @@ -80,16 +79,11 @@ impl PluginModule { } let ecs = Arc::new(EcsAccessManager::default()); - let memory_manager = Arc::new(MemoryManager::default()); // Environment to pass ecs and memory_manager to callbacks let env = FunctionEnv::new( &mut store, - HostFunctionEnvironment::new( - name.clone(), - Arc::clone(&ecs), - Arc::clone(&memory_manager), - ), + HostFunctionEnvironment::new(name.clone(), Arc::clone(&ecs)), ); // Create an import object. let import_object = imports! { @@ -113,7 +107,6 @@ impl PluginModule { .map_err(PluginModuleError::FindFunction)?; env.as_mut(&mut store).init_with_instance(init_args); Ok(Self { - memory_manager, ecs, memory: instance .exports @@ -219,7 +212,7 @@ fn execute_raw( // This write into memory `bytes` using allocation if necessary returning a // pointer and a length - let (ptr, len) = module.memory_manager.write_bytes( + let (ptr, len) = memory_manager::write_bytes( &mut module.store.as_store_mut(), &module.memory, &module.allocator, diff --git a/common/state/src/plugin/wasm_env.rs b/common/state/src/plugin/wasm_env.rs index 6164ad9ebe..480784d3d0 100644 --- a/common/state/src/plugin/wasm_env.rs +++ b/common/state/src/plugin/wasm_env.rs @@ -5,7 +5,7 @@ use wasmer::{ExportError, Instance, Memory, Store, StoreMut, StoreRef, TypedFunc use super::{ errors::PluginModuleError, - memory_manager::{self, EcsAccessManager, MemoryManager}, + memory_manager::{self, EcsAccessManager}, MemoryModel, }; @@ -18,9 +18,7 @@ pub struct HostFunctionEnvironment { pub allocator: Option< TypedFunction<::Offset, WasmPtr>, >, /* Linked to: wasm_prepare_buffer */ - pub memory_manager: Arc, /* This object represent the current buffer size and - * pointer */ - pub name: String, // This represent the plugin name + pub name: String, // This represent the plugin name } pub struct HostFunctionEnvironmentInit { @@ -29,13 +27,8 @@ pub struct HostFunctionEnvironmentInit { } impl HostFunctionEnvironment { - pub fn new( - name: String, - ecs: Arc, - memory_manager: Arc, - ) -> Self { + pub fn new(name: String, ecs: Arc) -> Self { Self { - memory_manager, ecs, allocator: Default::default(), memory: Default::default(), @@ -56,9 +49,6 @@ impl HostFunctionEnvironment { self.allocator.as_ref().unwrap() } - #[inline] - pub fn memory_manager(&self) -> &Arc { &self.memory_manager } - #[inline] pub fn name(&self) -> &str { &self.name } @@ -75,8 +65,7 @@ impl HostFunctionEnvironment { ), PluginModuleError, > { - self.memory_manager - .write_data(store, self.memory(), self.allocator(), object) + memory_manager::write_data(store, self.memory(), self.allocator(), object) } /// This function is a safe interface to WASM memory that writes data to the @@ -86,8 +75,7 @@ impl HostFunctionEnvironment { store: &mut StoreMut, object: &T, ) -> Result, PluginModuleError> { - self.memory_manager - .write_data_as_pointer(store, self.memory(), self.allocator(), object) + memory_manager::write_data_as_pointer(store, self.memory(), self.allocator(), object) } /// This function is a safe interface to WASM memory that reads memory from