Add realloc func argument for memory allocator (#191)
This commit is contained in:
@ -750,8 +750,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
uint32 cur_page_count = module_inst->mem_cur_page_count;
|
||||
uint32 max_page_count = module_inst->mem_max_page_count;
|
||||
uint32 total_page_count = cur_page_count + inc_page_count;
|
||||
uint32 old_size = num_bytes_per_page * cur_page_count;
|
||||
uint64 total_size = (uint64)num_bytes_per_page * total_page_count;
|
||||
uint32 total_size_old;
|
||||
|
||||
if (inc_page_count <= 0)
|
||||
/* No need to enlarge memory */
|
||||
@ -763,20 +763,29 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(mem_data_new = wasm_malloc((uint32)total_size))) {
|
||||
if (total_size >= UINT32_MAX) {
|
||||
aot_set_exception(module_inst, "fail to enlarge memory.");
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(mem_data_new, mem_data_old, old_size);
|
||||
memset(mem_data_new + old_size, 0, (uint32)total_size - old_size);
|
||||
if (!(mem_data_new = wasm_realloc(mem_data_old, (uint32)total_size))) {
|
||||
if (!(mem_data_new = wasm_malloc((uint32)total_size))) {
|
||||
aot_set_exception(module_inst, "fail to enlarge memory.");
|
||||
return false;
|
||||
}
|
||||
total_size_old = module_inst->memory_data_size;
|
||||
bh_memcpy_s(mem_data_new, (uint32)total_size,
|
||||
mem_data_old, total_size_old);
|
||||
memset(mem_data_new + total_size_old,
|
||||
0, (uint32)total_size - total_size_old);
|
||||
wasm_free(mem_data_old);
|
||||
}
|
||||
|
||||
module_inst->mem_cur_page_count = total_page_count;
|
||||
module_inst->memory_data_size = (uint32)total_size;
|
||||
module_inst->memory_data.ptr = mem_data_new;
|
||||
module_inst->memory_data_end.ptr = mem_data_new + (uint32)total_size;
|
||||
|
||||
wasm_free(mem_data_old);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@ -50,6 +50,10 @@ wasm_runtime_destroy()
|
||||
vm_thread_sys_destroy();
|
||||
}
|
||||
|
||||
int bh_memory_init_with_allocator_internal(void *_malloc_func,
|
||||
void *_realloc_func,
|
||||
void *_free_func);
|
||||
|
||||
bool
|
||||
wasm_runtime_full_init(RuntimeInitArgs *init_args)
|
||||
{
|
||||
@ -61,8 +65,11 @@ wasm_runtime_full_init(RuntimeInitArgs *init_args)
|
||||
}
|
||||
else if (init_args->mem_alloc_type == Alloc_With_Allocator) {
|
||||
void *malloc_func = init_args->mem_alloc.allocator.malloc_func;
|
||||
void *realloc_func = init_args->mem_alloc.allocator.realloc_func;
|
||||
void *free_func = init_args->mem_alloc.allocator.free_func;
|
||||
if (bh_memory_init_with_allocator(malloc_func, free_func) != 0)
|
||||
if (bh_memory_init_with_allocator_internal(malloc_func,
|
||||
realloc_func,
|
||||
free_func) != 0)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
|
||||
@ -23,6 +23,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#define wasm_malloc bh_malloc
|
||||
#define wasm_realloc bh_realloc
|
||||
#define wasm_free bh_free
|
||||
|
||||
typedef struct WASMModuleCommon {
|
||||
|
||||
@ -67,6 +67,7 @@ typedef struct RuntimeInitArgs {
|
||||
} pool;
|
||||
struct {
|
||||
void *malloc_func;
|
||||
void *realloc_func;
|
||||
void *free_func;
|
||||
} allocator;
|
||||
} mem_alloc;
|
||||
|
||||
@ -1105,12 +1105,13 @@ bool
|
||||
wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
|
||||
{
|
||||
#if WASM_ENABLE_MEMORY_GROW != 0
|
||||
WASMMemoryInstance *memory = module->default_memory;
|
||||
WASMMemoryInstance *new_memory;
|
||||
WASMMemoryInstance *memory = module->default_memory, *new_memory;
|
||||
uint32 old_page_count = memory->cur_page_count, total_size_old;
|
||||
uint32 total_page_count = inc_page_count + memory->cur_page_count;
|
||||
uint64 total_size = offsetof(WASMMemoryInstance, base_addr) +
|
||||
memory->num_bytes_per_page * (uint64)total_page_count +
|
||||
memory->global_data_size;
|
||||
uint8 *global_data_old;
|
||||
|
||||
if (inc_page_count <= 0)
|
||||
/* No need to enlarge memory */
|
||||
@ -1122,43 +1123,39 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (total_size >= UINT32_MAX
|
||||
|| !(new_memory = wasm_malloc((uint32)total_size))) {
|
||||
if (total_size >= UINT32_MAX) {
|
||||
wasm_set_exception(module, "fail to enlarge memory.");
|
||||
return false;
|
||||
}
|
||||
|
||||
new_memory->num_bytes_per_page = memory->num_bytes_per_page;
|
||||
if (!(new_memory = wasm_realloc(memory, (uint32)total_size))) {
|
||||
if (!(new_memory = wasm_malloc((uint32)total_size))) {
|
||||
wasm_set_exception(module, "fail to enlarge memory.");
|
||||
return false;
|
||||
}
|
||||
total_size_old = memory->end_addr - (uint8*)memory;
|
||||
bh_memcpy_s((uint8*)new_memory, (uint32)total_size,
|
||||
(uint8*)memory, total_size_old);
|
||||
memset((uint8*)new_memory + total_size_old,
|
||||
0, (uint32)total_size - total_size_old);
|
||||
wasm_free(memory);
|
||||
}
|
||||
|
||||
new_memory->cur_page_count = total_page_count;
|
||||
new_memory->max_page_count = memory->max_page_count;
|
||||
|
||||
new_memory->memory_data = new_memory->base_addr;
|
||||
|
||||
new_memory->global_data = new_memory->memory_data +
|
||||
memory->num_bytes_per_page * total_page_count;
|
||||
new_memory->global_data_size = memory->global_data_size;
|
||||
new_memory->num_bytes_per_page * total_page_count;
|
||||
new_memory->end_addr = new_memory->global_data + new_memory->global_data_size;
|
||||
|
||||
new_memory->end_addr = new_memory->global_data + memory->global_data_size;
|
||||
global_data_old = new_memory->memory_data +
|
||||
new_memory->num_bytes_per_page * old_page_count;
|
||||
|
||||
/* Copy memory data */
|
||||
bh_memcpy_s(new_memory->memory_data,
|
||||
(uint32)(memory->global_data - memory->memory_data),
|
||||
memory->memory_data,
|
||||
(uint32)(memory->global_data - memory->memory_data));
|
||||
/* Copy global data */
|
||||
bh_memcpy_s(new_memory->global_data, new_memory->global_data_size,
|
||||
memory->global_data, memory->global_data_size);
|
||||
/* Init free space of new memory */
|
||||
memset(new_memory->memory_data + memory->num_bytes_per_page * memory->cur_page_count,
|
||||
0, memory->num_bytes_per_page * (total_page_count - memory->cur_page_count));
|
||||
|
||||
new_memory->heap_data = memory->heap_data;
|
||||
new_memory->heap_data_end = memory->heap_data_end;
|
||||
new_memory->heap_handle = memory->heap_handle;
|
||||
new_memory->heap_base_offset = memory->heap_base_offset;
|
||||
global_data_old, new_memory->global_data_size);
|
||||
memset(global_data_old, 0, new_memory->global_data_size);
|
||||
|
||||
module->memories[0] = module->default_memory = new_memory;
|
||||
wasm_free(memory);
|
||||
return true;
|
||||
#else /* else of WASM_ENABLE_MEMORY_GROW */
|
||||
wasm_set_exception(module, "unsupported operation: enlarge memory.");
|
||||
|
||||
Reference in New Issue
Block a user