mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 22:25:40 +00:00
feat(runtime-c-api) Extract the memory
module.
This commit is contained in:
parent
168aa8031f
commit
55c010688c
@ -2,7 +2,6 @@ extern crate wasmer_runtime;
|
|||||||
extern crate wasmer_runtime_core;
|
extern crate wasmer_runtime_core;
|
||||||
|
|
||||||
use libc::{c_char, c_int, uint32_t, uint8_t};
|
use libc::{c_char, c_int, uint32_t, uint8_t};
|
||||||
use std::cell::Cell;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
@ -12,14 +11,15 @@ use wasmer_runtime::{Ctx, Global, ImportObject, Instance, Memory, Module, Table,
|
|||||||
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
|
use wasmer_runtime_core::export::{Context, Export, FuncPointer};
|
||||||
use wasmer_runtime_core::import::Namespace;
|
use wasmer_runtime_core::import::Namespace;
|
||||||
use wasmer_runtime_core::module::{ExportIndex, ImportName};
|
use wasmer_runtime_core::module::{ExportIndex, ImportName};
|
||||||
use wasmer_runtime_core::types::{ElementType, FuncSig, MemoryDescriptor, TableDescriptor, Type};
|
use wasmer_runtime_core::types::{ElementType, FuncSig, TableDescriptor, Type};
|
||||||
use wasmer_runtime_core::units::{Bytes, Pages};
|
|
||||||
|
|
||||||
pub mod error;
|
pub mod error;
|
||||||
|
pub mod memory;
|
||||||
pub mod module;
|
pub mod module;
|
||||||
pub mod value;
|
pub mod value;
|
||||||
|
|
||||||
use error::{update_last_error, CApiError};
|
use error::{update_last_error, CApiError};
|
||||||
|
use memory::wasmer_memory_t;
|
||||||
use module::wasmer_module_t;
|
use module::wasmer_module_t;
|
||||||
use value::{wasmer_value, wasmer_value_t, wasmer_value_tag};
|
use value::{wasmer_value, wasmer_value_t, wasmer_value_tag};
|
||||||
|
|
||||||
@ -43,10 +43,6 @@ pub struct wasmer_global_descriptor_t {
|
|||||||
kind: wasmer_value_tag,
|
kind: wasmer_value_tag,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct wasmer_memory_t;
|
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct wasmer_table_t;
|
pub struct wasmer_table_t;
|
||||||
@ -147,74 +143,6 @@ pub unsafe extern "C" fn wasmer_validate(
|
|||||||
wasmer_runtime_core::validate(bytes)
|
wasmer_runtime_core::validate(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new Memory for the given descriptor and initializes the given
|
|
||||||
/// pointer to pointer to a pointer to the new memory.
|
|
||||||
///
|
|
||||||
/// The caller owns the object and should call `wasmer_memory_destroy` to free it.
|
|
||||||
///
|
|
||||||
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
||||||
///
|
|
||||||
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
||||||
/// and `wasmer_last_error_message` to get an error message.
|
|
||||||
#[no_mangle]
|
|
||||||
pub unsafe extern "C" fn wasmer_memory_new(
|
|
||||||
memory: *mut *mut wasmer_memory_t,
|
|
||||||
limits: wasmer_limits_t,
|
|
||||||
) -> wasmer_result_t {
|
|
||||||
let max = if limits.max.has_some {
|
|
||||||
Some(Pages(limits.max.some))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
let desc = MemoryDescriptor {
|
|
||||||
minimum: Pages(limits.min),
|
|
||||||
maximum: max,
|
|
||||||
shared: false,
|
|
||||||
};
|
|
||||||
let result = Memory::new(desc);
|
|
||||||
let new_memory = match result {
|
|
||||||
Ok(memory) => memory,
|
|
||||||
Err(error) => {
|
|
||||||
update_last_error(error);
|
|
||||||
return wasmer_result_t::WASMER_ERROR;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
*memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t;
|
|
||||||
wasmer_result_t::WASMER_OK
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Grows a Memory by the given number of pages.
|
|
||||||
///
|
|
||||||
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
||||||
///
|
|
||||||
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
||||||
/// and `wasmer_last_error_message` to get an error message.
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wasmer_memory_grow(
|
|
||||||
memory: *mut wasmer_memory_t,
|
|
||||||
delta: uint32_t,
|
|
||||||
) -> wasmer_result_t {
|
|
||||||
let memory = unsafe { &*(memory as *mut Memory) };
|
|
||||||
let delta_result = memory.grow(Pages(delta));
|
|
||||||
match delta_result {
|
|
||||||
Ok(_) => wasmer_result_t::WASMER_OK,
|
|
||||||
Err(grow_error) => {
|
|
||||||
update_last_error(grow_error);
|
|
||||||
wasmer_result_t::WASMER_ERROR
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the current length in pages of the given memory
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wasmer_memory_length(memory: *const wasmer_memory_t) -> uint32_t {
|
|
||||||
let memory = unsafe { &*(memory as *const Memory) };
|
|
||||||
let Pages(len) = memory.size();
|
|
||||||
len
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new Table for the given descriptor and initializes the given
|
/// Creates a new Table for the given descriptor and initializes the given
|
||||||
/// pointer to pointer to a pointer to the new Table.
|
/// pointer to pointer to a pointer to the new Table.
|
||||||
///
|
///
|
||||||
@ -346,15 +274,6 @@ pub extern "C" fn wasmer_global_destroy(global: *mut wasmer_global_t) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Frees memory for the given Memory
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wasmer_memory_destroy(memory: *mut wasmer_memory_t) {
|
|
||||||
if !memory.is_null() {
|
|
||||||
unsafe { Box::from_raw(memory as *mut Memory) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets export descriptors for the given module
|
/// Gets export descriptors for the given module
|
||||||
///
|
///
|
||||||
/// The caller owns the object and should call `wasmer_export_descriptors_destroy` to free it.
|
/// The caller owns the object and should call `wasmer_export_descriptors_destroy` to free it.
|
||||||
@ -1256,23 +1175,6 @@ pub extern "C" fn wasmer_instance_context_data_get(
|
|||||||
ctx.data
|
ctx.data
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the start pointer to the bytes within a Memory
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wasmer_memory_data(mem: *const wasmer_memory_t) -> *mut uint8_t {
|
|
||||||
let memory = unsafe { &*(mem as *const Memory) };
|
|
||||||
memory.view::<u8>()[..].as_ptr() as *mut Cell<u8> as *mut u8
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the size in bytes of a Memory
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern "C" fn wasmer_memory_data_length(mem: *mut wasmer_memory_t) -> uint32_t {
|
|
||||||
let memory = mem as *mut Memory;
|
|
||||||
let Bytes(len) = unsafe { (*memory).size().bytes() };
|
|
||||||
len as uint32_t
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Frees memory for the given Instance
|
/// Frees memory for the given Instance
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
108
lib/runtime-c-api/src/memory.rs
Normal file
108
lib/runtime-c-api/src/memory.rs
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
//! Wasm memory.o
|
||||||
|
|
||||||
|
use crate::{error::update_last_error, wasmer_limits_t, wasmer_result_t};
|
||||||
|
use libc::{uint32_t, uint8_t};
|
||||||
|
use std::cell::Cell;
|
||||||
|
use wasmer_runtime::Memory;
|
||||||
|
use wasmer_runtime_core::{
|
||||||
|
types::MemoryDescriptor,
|
||||||
|
units::{Bytes, Pages},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct wasmer_memory_t;
|
||||||
|
|
||||||
|
/// Creates a new Memory for the given descriptor and initializes the given
|
||||||
|
/// pointer to pointer to a pointer to the new memory.
|
||||||
|
///
|
||||||
|
/// The caller owns the object and should call `wasmer_memory_destroy` to free it.
|
||||||
|
///
|
||||||
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
||||||
|
///
|
||||||
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
||||||
|
/// and `wasmer_last_error_message` to get an error message.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn wasmer_memory_new(
|
||||||
|
memory: *mut *mut wasmer_memory_t,
|
||||||
|
limits: wasmer_limits_t,
|
||||||
|
) -> wasmer_result_t {
|
||||||
|
let max = if limits.max.has_some {
|
||||||
|
Some(Pages(limits.max.some))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
let desc = MemoryDescriptor {
|
||||||
|
minimum: Pages(limits.min),
|
||||||
|
maximum: max,
|
||||||
|
shared: false,
|
||||||
|
};
|
||||||
|
let result = Memory::new(desc);
|
||||||
|
let new_memory = match result {
|
||||||
|
Ok(memory) => memory,
|
||||||
|
Err(error) => {
|
||||||
|
update_last_error(error);
|
||||||
|
return wasmer_result_t::WASMER_ERROR;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*memory = Box::into_raw(Box::new(new_memory)) as *mut wasmer_memory_t;
|
||||||
|
wasmer_result_t::WASMER_OK
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Grows a Memory by the given number of pages.
|
||||||
|
///
|
||||||
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
||||||
|
///
|
||||||
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
||||||
|
/// and `wasmer_last_error_message` to get an error message.
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_grow(
|
||||||
|
memory: *mut wasmer_memory_t,
|
||||||
|
delta: uint32_t,
|
||||||
|
) -> wasmer_result_t {
|
||||||
|
let memory = unsafe { &*(memory as *mut Memory) };
|
||||||
|
let delta_result = memory.grow(Pages(delta));
|
||||||
|
match delta_result {
|
||||||
|
Ok(_) => wasmer_result_t::WASMER_OK,
|
||||||
|
Err(grow_error) => {
|
||||||
|
update_last_error(grow_error);
|
||||||
|
wasmer_result_t::WASMER_ERROR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the current length in pages of the given memory
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_length(memory: *const wasmer_memory_t) -> uint32_t {
|
||||||
|
let memory = unsafe { &*(memory as *const Memory) };
|
||||||
|
let Pages(len) = memory.size();
|
||||||
|
len
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the start pointer to the bytes within a Memory
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_data(mem: *const wasmer_memory_t) -> *mut uint8_t {
|
||||||
|
let memory = unsafe { &*(mem as *const Memory) };
|
||||||
|
memory.view::<u8>()[..].as_ptr() as *mut Cell<u8> as *mut u8
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the size in bytes of a Memory
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_data_length(mem: *mut wasmer_memory_t) -> uint32_t {
|
||||||
|
let memory = mem as *mut Memory;
|
||||||
|
let Bytes(len) = unsafe { (*memory).size().bytes() };
|
||||||
|
len as uint32_t
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Frees memory for the given Memory
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_destroy(memory: *mut wasmer_memory_t) {
|
||||||
|
if !memory.is_null() {
|
||||||
|
unsafe { Box::from_raw(memory as *mut Memory) };
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user