get rid of the empty memory_manager module

This commit is contained in:
Christof Petig 2023-06-18 19:51:35 +02:00
parent cfaffd0ac2
commit 5802f94daf
3 changed files with 119 additions and 151 deletions

View File

@ -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: <MemoryModel as wasmer::MemorySize>::Offset,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
) -> Result<WasmPtr<u8, MemoryModel>, 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: <MemoryModel as wasmer::MemorySize>::Offset,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
) -> Result<WasmPtr<u8, MemoryModel>, 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<T: Serialize>(
&self,
store: &mut StoreMut,
memory: &Memory,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
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<T: Serialize>(
store: &mut StoreMut,
memory: &Memory,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
object: &T,
) -> Result<
(
WasmPtr<u8, MemoryModel>,
<MemoryModel as wasmer::MemorySize>::Offset,
),
PluginModuleError,
> {
write_bytes(
store,
memory,
allocator,
(
WasmPtr<u8, MemoryModel>,
<MemoryModel as wasmer::MemorySize>::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<T: Serialize>(object: &T) -> Result<Vec<u8>, PluginModuleError> {
bincode::serialize(object).map_err(PluginModuleError::Encoding)
}
/// This functions wraps the serialization process
pub fn serialize_data<T: Serialize>(object: &T) -> Result<Vec<u8>, 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<T: Serialize>(
&self,
store: &mut StoreMut,
memory: &Memory,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
object: &T,
) -> Result<WasmPtr<u8, MemoryModel>, 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<T: Serialize>(
store: &mut StoreMut,
memory: &Memory,
allocator: &TypedFunction<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
object: &T,
) -> Result<WasmPtr<u8, MemoryModel>, 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<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
bytes: (&[u8], &[u8]),
) -> Result<
(
WasmPtr<u8, MemoryModel>,
<MemoryModel as wasmer::MemorySize>::Offset,
),
PluginModuleError,
> {
let len = (bytes.0.len() + bytes.1.len()) as <MemoryModel as wasmer::MemorySize>::Offset;
let ptr = self
.get_pointer(store, len, allocator)
.map_err(PluginModuleError::MemoryAllocation)?;
ptr.slice(
&memory.view(store),
len as <MemoryModel as wasmer::MemorySize>::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<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
bytes: (&[u8], &[u8]),
) -> Result<
(
WasmPtr<u8, MemoryModel>,
<MemoryModel as wasmer::MemorySize>::Offset,
),
PluginModuleError,
> {
let len = (bytes.0.len() + bytes.1.len()) as <MemoryModel as wasmer::MemorySize>::Offset;
let ptr = get_pointer(store, len, allocator).map_err(PluginModuleError::MemoryAllocation)?;
ptr.slice(
&memory.view(store),
len as <MemoryModel as wasmer::MemorySize>::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<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
bytes: &[u8],
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
let len = bytes.len() as <MemoryModel as wasmer::MemorySize>::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<
<MemoryModel as wasmer::MemorySize>::Offset,
WasmPtr<u8, MemoryModel>,
>,
bytes: &[u8],
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
let len = bytes.len() as <MemoryModel as wasmer::MemorySize>::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

View File

@ -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<EcsAccessManager>,
wasm_state: Arc<Instance>,
memory_manager: Arc<MemoryManager>,
events: HashSet<String>,
allocator: TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
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,

View File

@ -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<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
>, /* Linked to: wasm_prepare_buffer */
pub memory_manager: Arc<MemoryManager>, /* 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<EcsAccessManager>,
memory_manager: Arc<MemoryManager>,
) -> Self {
pub fn new(name: String, ecs: Arc<EcsAccessManager>) -> 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<MemoryManager> { &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<WasmPtr<u8, MemoryModel>, 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