2023-04-17 16:13:07 +00:00
|
|
|
/************************************************************//**
|
|
|
|
*
|
2023-08-09 11:06:32 +00:00
|
|
|
* @file: runtime_memory.c
|
2023-04-17 16:13:07 +00:00
|
|
|
* @author: Martin Fouilleul
|
|
|
|
* @date: 17/04/2023
|
|
|
|
*
|
|
|
|
*****************************************************************/
|
|
|
|
|
2023-08-09 11:06:32 +00:00
|
|
|
#include"runtime.h"
|
2023-04-17 16:13:07 +00:00
|
|
|
|
|
|
|
void* wasm_memory_resize_callback(void* p, unsigned long size, void* userData)
|
|
|
|
{
|
|
|
|
wasm_memory* memory = (wasm_memory*)userData;
|
|
|
|
|
|
|
|
if(memory->committed >= size)
|
|
|
|
{
|
|
|
|
return(memory->ptr);
|
|
|
|
}
|
|
|
|
else if(memory->committed < memory->reserved)
|
|
|
|
{
|
|
|
|
u32 commitSize = size - memory->committed;
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
oc_base_allocator* allocator = oc_base_allocator_default();
|
|
|
|
oc_base_commit(allocator, memory->ptr + memory->committed, commitSize);
|
2023-04-17 16:13:07 +00:00
|
|
|
memory->committed += commitSize;
|
|
|
|
return(memory->ptr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-14 08:26:11 +00:00
|
|
|
OC_ABORT("Out of memory");
|
2023-04-17 16:13:07 +00:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wasm_memory_free_callback(void* p, void* userData)
|
|
|
|
{
|
|
|
|
wasm_memory* memory = (wasm_memory*)userData;
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
oc_base_allocator* allocator = oc_base_allocator_default();
|
|
|
|
oc_base_release(allocator, memory->ptr, memory->reserved);
|
2023-04-17 16:13:07 +00:00
|
|
|
memset(memory, 0, sizeof(wasm_memory));
|
|
|
|
}
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
extern u32 oc_mem_grow(u64 size)
|
2023-04-17 16:13:07 +00:00
|
|
|
{
|
2023-08-14 08:26:11 +00:00
|
|
|
oc_runtime_env* runtime = oc_runtime_env_get();
|
2023-04-17 16:13:07 +00:00
|
|
|
wasm_memory* memory = &runtime->wasmMemory;
|
|
|
|
|
2023-08-14 08:26:11 +00:00
|
|
|
size = oc_align_up_pow2(size, d_m3MemPageSize);
|
2023-04-17 16:13:07 +00:00
|
|
|
u64 totalSize = size + m3_GetMemorySize(runtime->m3Runtime);
|
|
|
|
|
|
|
|
u32 addr = memory->committed;
|
|
|
|
|
|
|
|
//NOTE: call resize memory, which will call our custom resize callback... this is a bit involved because
|
|
|
|
// wasm3 doesn't allow resizing the memory directly
|
|
|
|
M3Result res = ResizeMemory(runtime->m3Runtime, totalSize/d_m3MemPageSize);
|
|
|
|
|
|
|
|
return(addr);
|
|
|
|
}
|
2023-07-14 04:38:32 +00:00
|
|
|
|
|
|
|
void* wasm_memory_offset_to_ptr(wasm_memory* memory, u32 offset)
|
|
|
|
{
|
|
|
|
M3MemoryHeader* header = (M3MemoryHeader*)(memory->ptr);
|
2023-08-14 08:26:11 +00:00
|
|
|
OC_DEBUG_ASSERT(offset < header->length, "Wasm offset exceeds memory length");
|
2023-07-14 04:38:32 +00:00
|
|
|
return memory->ptr + sizeof(M3MemoryHeader) + offset;
|
2023-08-09 11:06:32 +00:00
|
|
|
}
|