mirror of
https://github.com/fluencelabs/wasmer
synced 2024-12-13 22:25:40 +00:00
Add Memory Grow C API
This commit is contained in:
parent
a8dcc0ee87
commit
a0288c87ac
@ -146,6 +146,22 @@ pub unsafe extern "C" fn wasmer_memory_new(
|
|||||||
wasmer_memory_result_t::WASMER_MEMORY_OK
|
wasmer_memory_result_t::WASMER_MEMORY_OK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasmer_memory_grow(
|
||||||
|
memory: *mut wasmer_memory_t,
|
||||||
|
delta: uint32_t,
|
||||||
|
) -> wasmer_memory_result_t {
|
||||||
|
let memory = unsafe { Box::from_raw(memory as *mut Memory) };
|
||||||
|
let maybe_delta = memory.grow(Pages(delta));
|
||||||
|
Box::into_raw(memory);
|
||||||
|
if let Some(_delta) = maybe_delta {
|
||||||
|
wasmer_memory_result_t::WASMER_MEMORY_OK
|
||||||
|
} else {
|
||||||
|
wasmer_memory_result_t::WASMER_MEMORY_ERROR
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::cast_ptr_alignment)]
|
#[allow(clippy::cast_ptr_alignment)]
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t {
|
pub extern "C" fn wasmer_memory_length(memory: *mut wasmer_memory_t) -> uint32_t {
|
||||||
|
@ -8,7 +8,7 @@ int main()
|
|||||||
wasmer_memory_t *memory = NULL;
|
wasmer_memory_t *memory = NULL;
|
||||||
wasmer_limits_t descriptor;
|
wasmer_limits_t descriptor;
|
||||||
descriptor.min = 10;
|
descriptor.min = 10;
|
||||||
descriptor.max = 10;
|
descriptor.max = 15;
|
||||||
wasmer_memory_result_t memory_result = wasmer_memory_new(&memory, descriptor);
|
wasmer_memory_result_t memory_result = wasmer_memory_new(&memory, descriptor);
|
||||||
printf("Memory result: %d\n", memory_result);
|
printf("Memory result: %d\n", memory_result);
|
||||||
assert(memory_result == WASMER_MEMORY_OK);
|
assert(memory_result == WASMER_MEMORY_OK);
|
||||||
@ -17,6 +17,17 @@ int main()
|
|||||||
printf("Memory pages length: %d\n", len);
|
printf("Memory pages length: %d\n", len);
|
||||||
assert(len == 10);
|
assert(len == 10);
|
||||||
|
|
||||||
|
wasmer_memory_result_t grow_result = wasmer_memory_grow(memory, 2);
|
||||||
|
assert(grow_result == WASMER_MEMORY_OK);
|
||||||
|
|
||||||
|
uint32_t new_len = wasmer_memory_length(memory);
|
||||||
|
printf("Memory pages length: %d\n", new_len);
|
||||||
|
assert(new_len == 12);
|
||||||
|
|
||||||
|
// Err, grow beyond max
|
||||||
|
wasmer_memory_result_t grow_result2 = wasmer_memory_grow(memory, 10);
|
||||||
|
assert(grow_result2 == WASMER_MEMORY_ERROR);
|
||||||
|
|
||||||
printf("Destroy memory\n");
|
printf("Destroy memory\n");
|
||||||
wasmer_memory_destroy(memory);
|
wasmer_memory_destroy(memory);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -112,6 +112,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
|
|||||||
|
|
||||||
void wasmer_memory_destroy(wasmer_memory_t *memory);
|
void wasmer_memory_destroy(wasmer_memory_t *memory);
|
||||||
|
|
||||||
|
wasmer_memory_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta);
|
||||||
|
|
||||||
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
|
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
|
||||||
|
|
||||||
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
|
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
|
||||||
|
@ -112,6 +112,8 @@ wasmer_compile_result_t wasmer_instantiate(wasmer_instance_t **instance,
|
|||||||
|
|
||||||
void wasmer_memory_destroy(wasmer_memory_t *memory);
|
void wasmer_memory_destroy(wasmer_memory_t *memory);
|
||||||
|
|
||||||
|
wasmer_memory_result_t wasmer_memory_grow(wasmer_memory_t *memory, uint32_t delta);
|
||||||
|
|
||||||
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
|
uint32_t wasmer_memory_length(wasmer_memory_t *memory);
|
||||||
|
|
||||||
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
|
wasmer_memory_result_t wasmer_memory_new(wasmer_memory_t **memory, wasmer_limits_t limits);
|
||||||
|
Loading…
Reference in New Issue
Block a user