mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
get rid of the empty memory_manager module
This commit is contained in:
parent
cfaffd0ac2
commit
5802f94daf
@ -103,32 +103,26 @@ impl EcsAccessManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
/// This function check if the buffer is wide enough if not it realloc the
|
||||||
pub struct MemoryManager;
|
/// buffer calling the `wasm_prepare_buffer` function Note: There is
|
||||||
|
/// probably optimizations that can be done using less restrictive
|
||||||
impl MemoryManager {
|
/// ordering
|
||||||
/// This function check if the buffer is wide enough if not it realloc the
|
pub fn get_pointer(
|
||||||
/// 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,
|
store: &mut StoreMut,
|
||||||
object_length: <MemoryModel as wasmer::MemorySize>::Offset,
|
object_length: <MemoryModel as wasmer::MemorySize>::Offset,
|
||||||
allocator: &TypedFunction<
|
allocator: &TypedFunction<
|
||||||
<MemoryModel as wasmer::MemorySize>::Offset,
|
<MemoryModel as wasmer::MemorySize>::Offset,
|
||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
>,
|
>,
|
||||||
) -> Result<WasmPtr<u8, MemoryModel>, MemoryAllocationError> {
|
) -> Result<WasmPtr<u8, MemoryModel>, MemoryAllocationError> {
|
||||||
allocator
|
allocator
|
||||||
.call(store, object_length)
|
.call(store, object_length)
|
||||||
.map_err(MemoryAllocationError::CantAllocate)
|
.map_err(MemoryAllocationError::CantAllocate)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function writes an object to WASM memory returning a pointer and a
|
/// This function writes an object to WASM memory returning a pointer and a
|
||||||
/// length. Will realloc the buffer is not wide enough
|
/// length. Will realloc the buffer is not wide enough
|
||||||
pub fn write_data<T: Serialize>(
|
pub fn write_data<T: Serialize>(
|
||||||
&self,
|
|
||||||
store: &mut StoreMut,
|
store: &mut StoreMut,
|
||||||
memory: &Memory,
|
memory: &Memory,
|
||||||
allocator: &TypedFunction<
|
allocator: &TypedFunction<
|
||||||
@ -136,14 +130,14 @@ impl MemoryManager {
|
|||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
>,
|
>,
|
||||||
object: &T,
|
object: &T,
|
||||||
) -> Result<
|
) -> Result<
|
||||||
(
|
(
|
||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
<MemoryModel as wasmer::MemorySize>::Offset,
|
<MemoryModel as wasmer::MemorySize>::Offset,
|
||||||
),
|
),
|
||||||
PluginModuleError,
|
PluginModuleError,
|
||||||
> {
|
> {
|
||||||
self.write_bytes(
|
write_bytes(
|
||||||
store,
|
store,
|
||||||
memory,
|
memory,
|
||||||
allocator,
|
allocator,
|
||||||
@ -152,20 +146,19 @@ impl MemoryManager {
|
|||||||
&[],
|
&[],
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This functions wraps the serialization process
|
/// This functions wraps the serialization process
|
||||||
pub fn serialize_data<T: Serialize>(object: &T) -> Result<Vec<u8>, PluginModuleError> {
|
pub fn serialize_data<T: Serialize>(object: &T) -> Result<Vec<u8>, PluginModuleError> {
|
||||||
bincode::serialize(object).map_err(PluginModuleError::Encoding)
|
bincode::serialize(object).map_err(PluginModuleError::Encoding)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function writes an object to the wasm memory using the allocator if
|
/// This function writes an object to the wasm memory using the allocator if
|
||||||
/// necessary using length padding.
|
/// necessary using length padding.
|
||||||
///
|
///
|
||||||
/// With length padding the first 8 bytes written are the length of the the
|
/// With length padding the first 8 bytes written are the length of the the
|
||||||
/// following slice (The object serialized).
|
/// following slice (The object serialized).
|
||||||
pub fn write_data_as_pointer<T: Serialize>(
|
pub fn write_data_as_pointer<T: Serialize>(
|
||||||
&self,
|
|
||||||
store: &mut StoreMut,
|
store: &mut StoreMut,
|
||||||
memory: &Memory,
|
memory: &Memory,
|
||||||
allocator: &TypedFunction<
|
allocator: &TypedFunction<
|
||||||
@ -173,14 +166,13 @@ impl MemoryManager {
|
|||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
>,
|
>,
|
||||||
object: &T,
|
object: &T,
|
||||||
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
||||||
self.write_bytes_as_pointer(store, memory, allocator, &Self::serialize_data(object)?)
|
write_bytes_as_pointer(store, memory, allocator, &serialize_data(object)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function writes an raw bytes to WASM memory returning a pointer and
|
/// This function writes an raw bytes to WASM memory returning a pointer and
|
||||||
/// a length. Will realloc the buffer is not wide enough
|
/// a length. Will realloc the buffer is not wide enough
|
||||||
pub fn write_bytes(
|
pub fn write_bytes(
|
||||||
&self,
|
|
||||||
store: &mut StoreMut,
|
store: &mut StoreMut,
|
||||||
memory: &Memory,
|
memory: &Memory,
|
||||||
allocator: &TypedFunction<
|
allocator: &TypedFunction<
|
||||||
@ -188,17 +180,15 @@ impl MemoryManager {
|
|||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
>,
|
>,
|
||||||
bytes: (&[u8], &[u8]),
|
bytes: (&[u8], &[u8]),
|
||||||
) -> Result<
|
) -> Result<
|
||||||
(
|
(
|
||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
<MemoryModel as wasmer::MemorySize>::Offset,
|
<MemoryModel as wasmer::MemorySize>::Offset,
|
||||||
),
|
),
|
||||||
PluginModuleError,
|
PluginModuleError,
|
||||||
> {
|
> {
|
||||||
let len = (bytes.0.len() + bytes.1.len()) as <MemoryModel as wasmer::MemorySize>::Offset;
|
let len = (bytes.0.len() + bytes.1.len()) as <MemoryModel as wasmer::MemorySize>::Offset;
|
||||||
let ptr = self
|
let ptr = get_pointer(store, len, allocator).map_err(PluginModuleError::MemoryAllocation)?;
|
||||||
.get_pointer(store, len, allocator)
|
|
||||||
.map_err(PluginModuleError::MemoryAllocation)?;
|
|
||||||
ptr.slice(
|
ptr.slice(
|
||||||
&memory.view(store),
|
&memory.view(store),
|
||||||
len as <MemoryModel as wasmer::MemorySize>::Offset,
|
len as <MemoryModel as wasmer::MemorySize>::Offset,
|
||||||
@ -213,15 +203,14 @@ impl MemoryManager {
|
|||||||
})
|
})
|
||||||
.map_err(|_| PluginModuleError::InvalidPointer)?;
|
.map_err(|_| PluginModuleError::InvalidPointer)?;
|
||||||
Ok((ptr, len))
|
Ok((ptr, len))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function writes bytes to the wasm memory using the allocator if
|
/// This function writes bytes to the wasm memory using the allocator if
|
||||||
/// necessary using length padding.
|
/// necessary using length padding.
|
||||||
///
|
///
|
||||||
/// With length padding the first 8 bytes written are the length of the the
|
/// With length padding the first 8 bytes written are the length of the the
|
||||||
/// following slice.
|
/// following slice.
|
||||||
pub fn write_bytes_as_pointer(
|
pub fn write_bytes_as_pointer(
|
||||||
&self,
|
|
||||||
store: &mut StoreMut,
|
store: &mut StoreMut,
|
||||||
memory: &Memory,
|
memory: &Memory,
|
||||||
allocator: &TypedFunction<
|
allocator: &TypedFunction<
|
||||||
@ -229,11 +218,9 @@ impl MemoryManager {
|
|||||||
WasmPtr<u8, MemoryModel>,
|
WasmPtr<u8, MemoryModel>,
|
||||||
>,
|
>,
|
||||||
bytes: &[u8],
|
bytes: &[u8],
|
||||||
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
||||||
let len = bytes.len() as <MemoryModel as wasmer::MemorySize>::Offset;
|
let len = bytes.len() as <MemoryModel as wasmer::MemorySize>::Offset;
|
||||||
self.write_bytes(store, memory, allocator, (&len.to_le_bytes(), &bytes))
|
write_bytes(store, memory, allocator, (&len.to_le_bytes(), bytes)).map(|val| val.0)
|
||||||
.map(|val| val.0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function reads data from memory at a position with the array length and
|
/// This function reads data from memory at a position with the array length and
|
||||||
|
@ -9,7 +9,7 @@ use wasmer::{
|
|||||||
use super::{
|
use super::{
|
||||||
errors::{PluginError, PluginModuleError},
|
errors::{PluginError, PluginModuleError},
|
||||||
exports,
|
exports,
|
||||||
memory_manager::{self, EcsAccessManager, EcsWorld, MemoryManager},
|
memory_manager::{self, EcsAccessManager, EcsWorld},
|
||||||
wasm_env::HostFunctionEnvironment,
|
wasm_env::HostFunctionEnvironment,
|
||||||
MemoryModel,
|
MemoryModel,
|
||||||
};
|
};
|
||||||
@ -21,7 +21,6 @@ use plugin_api::{Action, EcsAccessError, Event, Retrieve, RetrieveError, Retriev
|
|||||||
pub struct PluginModule {
|
pub struct PluginModule {
|
||||||
ecs: Arc<EcsAccessManager>,
|
ecs: Arc<EcsAccessManager>,
|
||||||
wasm_state: Arc<Instance>,
|
wasm_state: Arc<Instance>,
|
||||||
memory_manager: Arc<MemoryManager>,
|
|
||||||
events: HashSet<String>,
|
events: HashSet<String>,
|
||||||
allocator: TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
|
allocator: TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
|
||||||
memory: Memory,
|
memory: Memory,
|
||||||
@ -80,16 +79,11 @@ impl PluginModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let ecs = Arc::new(EcsAccessManager::default());
|
let ecs = Arc::new(EcsAccessManager::default());
|
||||||
let memory_manager = Arc::new(MemoryManager::default());
|
|
||||||
|
|
||||||
// Environment to pass ecs and memory_manager to callbacks
|
// Environment to pass ecs and memory_manager to callbacks
|
||||||
let env = FunctionEnv::new(
|
let env = FunctionEnv::new(
|
||||||
&mut store,
|
&mut store,
|
||||||
HostFunctionEnvironment::new(
|
HostFunctionEnvironment::new(name.clone(), Arc::clone(&ecs)),
|
||||||
name.clone(),
|
|
||||||
Arc::clone(&ecs),
|
|
||||||
Arc::clone(&memory_manager),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
// Create an import object.
|
// Create an import object.
|
||||||
let import_object = imports! {
|
let import_object = imports! {
|
||||||
@ -113,7 +107,6 @@ impl PluginModule {
|
|||||||
.map_err(PluginModuleError::FindFunction)?;
|
.map_err(PluginModuleError::FindFunction)?;
|
||||||
env.as_mut(&mut store).init_with_instance(init_args);
|
env.as_mut(&mut store).init_with_instance(init_args);
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
memory_manager,
|
|
||||||
ecs,
|
ecs,
|
||||||
memory: instance
|
memory: instance
|
||||||
.exports
|
.exports
|
||||||
@ -219,7 +212,7 @@ fn execute_raw(
|
|||||||
// This write into memory `bytes` using allocation if necessary returning a
|
// This write into memory `bytes` using allocation if necessary returning a
|
||||||
// pointer and a length
|
// 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(),
|
&mut module.store.as_store_mut(),
|
||||||
&module.memory,
|
&module.memory,
|
||||||
&module.allocator,
|
&module.allocator,
|
||||||
|
@ -5,7 +5,7 @@ use wasmer::{ExportError, Instance, Memory, Store, StoreMut, StoreRef, TypedFunc
|
|||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
errors::PluginModuleError,
|
errors::PluginModuleError,
|
||||||
memory_manager::{self, EcsAccessManager, MemoryManager},
|
memory_manager::{self, EcsAccessManager},
|
||||||
MemoryModel,
|
MemoryModel,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,8 +18,6 @@ pub struct HostFunctionEnvironment {
|
|||||||
pub allocator: Option<
|
pub allocator: Option<
|
||||||
TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
|
TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
|
||||||
>, /* Linked to: wasm_prepare_buffer */
|
>, /* 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,13 +27,8 @@ pub struct HostFunctionEnvironmentInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl HostFunctionEnvironment {
|
impl HostFunctionEnvironment {
|
||||||
pub fn new(
|
pub fn new(name: String, ecs: Arc<EcsAccessManager>) -> Self {
|
||||||
name: String,
|
|
||||||
ecs: Arc<EcsAccessManager>,
|
|
||||||
memory_manager: Arc<MemoryManager>,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
Self {
|
||||||
memory_manager,
|
|
||||||
ecs,
|
ecs,
|
||||||
allocator: Default::default(),
|
allocator: Default::default(),
|
||||||
memory: Default::default(),
|
memory: Default::default(),
|
||||||
@ -56,9 +49,6 @@ impl HostFunctionEnvironment {
|
|||||||
self.allocator.as_ref().unwrap()
|
self.allocator.as_ref().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn memory_manager(&self) -> &Arc<MemoryManager> { &self.memory_manager }
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn name(&self) -> &str { &self.name }
|
pub fn name(&self) -> &str { &self.name }
|
||||||
|
|
||||||
@ -75,8 +65,7 @@ impl HostFunctionEnvironment {
|
|||||||
),
|
),
|
||||||
PluginModuleError,
|
PluginModuleError,
|
||||||
> {
|
> {
|
||||||
self.memory_manager
|
memory_manager::write_data(store, self.memory(), self.allocator(), object)
|
||||||
.write_data(store, self.memory(), self.allocator(), object)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is a safe interface to WASM memory that writes data to the
|
/// This function is a safe interface to WASM memory that writes data to the
|
||||||
@ -86,8 +75,7 @@ impl HostFunctionEnvironment {
|
|||||||
store: &mut StoreMut,
|
store: &mut StoreMut,
|
||||||
object: &T,
|
object: &T,
|
||||||
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
) -> Result<WasmPtr<u8, MemoryModel>, PluginModuleError> {
|
||||||
self.memory_manager
|
memory_manager::write_data_as_pointer(store, self.memory(), self.allocator(), object)
|
||||||
.write_data_as_pointer(store, self.memory(), self.allocator(), object)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is a safe interface to WASM memory that reads memory from
|
/// This function is a safe interface to WASM memory that reads memory from
|
||||||
|
Loading…
x
Reference in New Issue
Block a user