Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)

This commit is contained in:
wenyongh
2020-03-10 19:54:44 +08:00
committed by GitHub
parent 381859d530
commit 0fdd49ea31
110 changed files with 1264 additions and 2125 deletions

View File

@ -5,7 +5,6 @@
#include "aot_runtime.h"
#include "bh_common.h"
#include "bh_memory.h"
#include "bh_log.h"
#include "aot_reloc.h"
#include "../common/wasm_runtime_common.h"
@ -155,7 +154,7 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
char* error_buf, uint32 error_buf_size)
{
HashMap *set = module->const_str_set;
char *c_str = wasm_malloc((uint32)len + 1), *value;
char *c_str = wasm_runtime_malloc((uint32)len + 1), *value;
if (!c_str) {
set_error_buf(error_buf, error_buf_size,
@ -168,7 +167,7 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
c_str[len] = '\0';
if ((value = bh_hash_map_find(set, c_str))) {
wasm_free(c_str);
wasm_runtime_free(c_str);
return value;
}
@ -176,7 +175,7 @@ const_str_set_insert(const uint8 *str, int32 len, AOTModule *module,
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"insert string to hash map failed.");
wasm_free(c_str);
wasm_runtime_free(c_str);
return NULL;
}
@ -330,8 +329,8 @@ destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count,
uint32 i;
for (i = 0; i < count; i++)
if (data_list[i])
wasm_free(data_list[i]);
wasm_free(data_list);
wasm_runtime_free(data_list[i]);
wasm_runtime_free(data_list);
}
}
@ -349,7 +348,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTMemInitData *) * (uint64)module->mem_init_data_count;
if (size >= UINT32_MAX
|| !(module->mem_init_data_list =
data_list = wasm_malloc((uint32)size))) {
data_list = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -367,7 +366,7 @@ load_mem_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
read_uint32(buf, buf_end, byte_count);
size = offsetof(AOTMemInitData, bytes) + (uint64)byte_count;
if (size >= UINT32_MAX
|| !(data_list[i] = wasm_malloc((uint32)size))) {
|| !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -419,8 +418,8 @@ destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count,
uint32 i;
for (i = 0; i < count; i++)
if (data_list[i])
wasm_free(data_list[i]);
wasm_free(data_list);
wasm_runtime_free(data_list[i]);
wasm_runtime_free(data_list);
}
}
@ -438,7 +437,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTTableInitData *) * (uint64)module->table_init_data_count;
if (size >= UINT32_MAX
|| !(module->table_init_data_list =
data_list = wasm_malloc((uint32)size))) {
data_list = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -459,7 +458,7 @@ load_table_init_data_list(const uint8 **p_buf, const uint8 *buf_end,
size1 = sizeof(uint32) * (uint64)func_index_count;
size = offsetof(AOTTableInitData, func_indexes) + size1;
if (size >= UINT32_MAX
|| !(data_list[i] = wasm_malloc((uint32)size))) {
|| !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -507,8 +506,8 @@ destroy_func_types(AOTFuncType **func_types, uint32 count, bool is_jit_mode)
uint32 i;
for (i = 0; i < count; i++)
if (func_types[i])
wasm_free(func_types[i]);
wasm_free(func_types);
wasm_runtime_free(func_types[i]);
wasm_runtime_free(func_types);
}
}
@ -525,7 +524,7 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end,
/* Allocate memory */
size = sizeof(AOTFuncType *) * (uint64)module->func_type_count;
if (size >= UINT32_MAX
|| !(module->func_types = func_types = wasm_malloc((uint32)size))) {
|| !(module->func_types = func_types = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -545,7 +544,7 @@ load_func_types(const uint8 **p_buf, const uint8 *buf_end,
size1 = (uint64)param_count + (uint64)result_count;
size = offsetof(AOTFuncType, types) + size1;
if (size >= UINT32_MAX
|| !(func_types[i] = wasm_malloc((uint32)size))) {
|| !(func_types[i] = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -587,7 +586,7 @@ static void
destroy_import_globals(AOTImportGlobal *import_globals, bool is_jit_mode)
{
if (!is_jit_mode)
wasm_free(import_globals);
wasm_runtime_free(import_globals);
}
static bool
@ -604,7 +603,7 @@ load_import_globals(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
if (size >= UINT32_MAX
|| !(module->import_globals =
import_globals = wasm_malloc((uint32)size))) {
import_globals = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -657,7 +656,7 @@ static void
destroy_globals(AOTGlobal *globals, bool is_jit_mode)
{
if (!is_jit_mode)
wasm_free(globals);
wasm_runtime_free(globals);
}
static bool
@ -674,7 +673,7 @@ load_globals(const uint8 **p_buf, const uint8 *buf_end,
/* Allocate memory */
size = sizeof(AOTGlobal) * (uint64)module->global_count;
if (size >= UINT32_MAX
|| !(module->globals = globals = wasm_malloc((uint32)size))) {
|| !(module->globals = globals = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -739,7 +738,7 @@ destroy_import_funcs(AOTImportFunc *import_funcs,
bool is_jit_mode)
{
if (!is_jit_mode)
wasm_free(import_funcs);
wasm_runtime_free(import_funcs);
}
static bool
@ -757,7 +756,7 @@ load_import_funcs(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTImportFunc) * (uint64)module->import_func_count;
if (size >= UINT32_MAX
|| !(module->import_funcs =
import_funcs = wasm_malloc((uint32)size))) {
import_funcs = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -831,7 +830,7 @@ destroy_object_data_sections(AOTObjectDataSection *data_sections,
for (i = 0; i < data_section_count; i++, data_section++)
if (data_section->data)
bh_munmap(data_section->data, data_section->size);
wasm_free(data_sections);
wasm_runtime_free(data_sections);
}
static bool
@ -848,7 +847,7 @@ load_object_data_sections(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTObjectDataSection) * (uint64)module->data_section_count;
if (size >= UINT32_MAX
|| !(module->data_sections =
data_sections = wasm_malloc((uint32)size))) {
data_sections = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1002,7 +1001,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
size = sizeof(void*) * (uint64)module->func_count;
if (size >= UINT32_MAX
|| !(module->func_ptrs = wasm_malloc((uint32)size))) {
|| !(module->func_ptrs = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: allocate memory failed.");
return false;
@ -1042,7 +1041,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
size = sizeof(uint32) * (uint64)module->func_count;
if (size >= UINT32_MAX
|| !(module->func_type_indexes = wasm_malloc((uint32)size))) {
|| !(module->func_type_indexes = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: allocate memory failed.");
return false;
@ -1074,7 +1073,7 @@ static void
destroy_export_funcs(AOTExportFunc *export_funcs, bool is_jit_mode)
{
if (!is_jit_mode)
wasm_free(export_funcs);
wasm_runtime_free(export_funcs);
}
static bool
@ -1091,7 +1090,7 @@ load_export_funcs(const uint8 **p_buf, const uint8 *buf_end,
size = sizeof(AOTExportFunc) * (uint64)module->export_func_count;
if (size >= UINT32_MAX
|| !(module->export_funcs =
export_funcs = wasm_malloc((uint32)size))) {
export_funcs = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1204,7 +1203,7 @@ do_text_relocation(AOTModule *module,
if (symbol_len + 1 <= sizeof(symbol_buf))
symbol = symbol_buf;
else {
if (!(symbol = wasm_malloc(symbol_len + 1))) {
if (!(symbol = wasm_runtime_malloc(symbol_len + 1))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1254,7 +1253,7 @@ do_text_relocation(AOTModule *module,
}
if (symbol != symbol_buf)
wasm_free(symbol);
wasm_runtime_free(symbol);
if (!apply_relocation(module,
aot_text, aot_text_size,
@ -1270,7 +1269,7 @@ do_text_relocation(AOTModule *module,
check_symbol_fail:
if (symbol != symbol_buf)
wasm_free(symbol);
wasm_runtime_free(symbol);
return false;
}
@ -1399,7 +1398,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
/* Allocate memory for relocation groups */
size = sizeof(AOTRelocationGroup) * (uint64)group_count;
if (size >= UINT32_MAX || !(groups = wasm_malloc((uint32)size))) {
if (size >= UINT32_MAX || !(groups = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1442,7 +1441,7 @@ load_relocation_section(const uint8 *buf, const uint8 *buf_end,
size = sizeof(AOTRelocation) * (uint64)group->relocation_count;
if (size >= UINT32_MAX
|| !(group->relocations = relocation =
wasm_malloc((uint32)size))) {
wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1522,8 +1521,8 @@ fail:
if (groups) {
for (i = 0, group = groups; i < group_count; i++, group++)
if (group->relocations)
wasm_free(group->relocations);
wasm_free(groups);
wasm_runtime_free(group->relocations);
wasm_runtime_free(groups);
}
return ret;
@ -1603,16 +1602,16 @@ load_from_sections(AOTModule *module, AOTSection *sections,
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
static void aot_free(void *ptr)
{
wasm_free(ptr);
wasm_runtime_free(ptr);
}
#else
#define aot_free wasm_free
#define aot_free wasm_runtime_free
#endif
static AOTModule*
create_module(char *error_buf, uint32 error_buf_size)
{
AOTModule *module = wasm_malloc(sizeof(AOTModule));
AOTModule *module = wasm_runtime_malloc(sizeof(AOTModule));
if (!module) {
set_error_buf(error_buf, error_buf_size,
@ -1634,7 +1633,7 @@ create_module(char *error_buf, uint32 error_buf_size)
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"create const string set failed.");
wasm_free(module);
wasm_runtime_free(module);
return NULL;
}
@ -1670,7 +1669,7 @@ destroy_sections(AOTSection *section_list, bool destroy_aot_text)
&& section->section_type == AOT_SECTION_TYPE_TEXT
&& section->section_body)
bh_munmap((uint8*)section->section_body, section->section_body_size);
wasm_free(section);
wasm_runtime_free(section);
section = next;
}
}
@ -1694,7 +1693,7 @@ create_sections(const uint8 *buf, uint32 size,
read_uint32(p, p_end, section_size);
CHECK_BUF(p, p_end, section_size);
if (!(section = wasm_malloc(sizeof(AOTSection)))) {
if (!(section = wasm_runtime_malloc(sizeof(AOTSection)))) {
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"allocate memory failed.");
@ -1722,7 +1721,7 @@ create_sections(const uint8 *buf, uint32 size,
if (total_size >= UINT32_MAX
|| !(aot_text = bh_mmap(NULL, (uint32)total_size,
map_prot, map_flags))) {
wasm_free(section);
wasm_runtime_free(section);
set_error_buf(error_buf, error_buf_size,
"AOT module load failed: "
"mmap memory failed.");
@ -1849,7 +1848,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
AOTModule *module;
/* Allocate memory for module */
if (!(module = wasm_malloc(sizeof(AOTModule)))) {
if (!(module = wasm_runtime_malloc(sizeof(AOTModule)))) {
set_error_buf(error_buf, error_buf_size,
"Allocate memory for AOT module failed.");
return NULL;
@ -1891,7 +1890,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
/* Allocate memory for function pointers */
size = (uint64)module->func_count * sizeof(void *);
if (size >= UINT32_MAX
|| !(module->func_ptrs = wasm_malloc((uint32)size))) {
|| !(module->func_ptrs = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size, "Create func ptrs fail.");
goto fail1;
}
@ -1913,7 +1912,7 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
/* Allocation memory for function type indexes */
size = (uint64)module->func_count * sizeof(uint32);
if (size >= UINT32_MAX
|| !(module->func_type_indexes = wasm_malloc((uint32)size))) {
|| !(module->func_type_indexes = wasm_runtime_malloc((uint32)size))) {
set_error_buf(error_buf, error_buf_size, "Create func type indexes fail.");
goto fail2;
}
@ -1965,9 +1964,9 @@ aot_load_from_comp_data(AOTCompData *comp_data, AOTCompContext *comp_ctx,
return module;
fail2:
wasm_free(module->func_ptrs);
wasm_runtime_free(module->func_ptrs);
fail1:
wasm_free(module);
wasm_runtime_free(module);
return NULL;
}
@ -2067,10 +2066,10 @@ aot_unload(AOTModule *module)
module->is_jit_mode);
if (module->func_type_indexes)
wasm_free(module->func_type_indexes);
wasm_runtime_free(module->func_type_indexes);
if (module->func_ptrs)
wasm_free(module->func_ptrs);
wasm_runtime_free(module->func_ptrs);
if (module->const_str_set)
bh_hash_map_destroy(module->const_str_set);
@ -2082,7 +2081,7 @@ aot_unload(AOTModule *module)
destroy_object_data_sections(module->data_sections,
module->data_section_count);
wasm_free(module);
wasm_runtime_free(module);
}
uint32

View File

@ -4,7 +4,6 @@
*/
#include "aot_runtime.h"
#include "bh_memory.h"
#include "bh_log.h"
#include "mem_alloc.h"
@ -116,7 +115,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
/* Allocate memory */
if (total_size >= UINT32_MAX
|| !(module_inst->memory_data.ptr = wasm_malloc((uint32)total_size))) {
|| !(module_inst->memory_data.ptr = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module instantiate failed: allocate memory failed.");
return false;
@ -166,7 +165,7 @@ memory_instantiate(AOTModuleInstance *module_inst, AOTModule *module,
if (length > 0
&& (base_offset >= module_inst->memory_data_size
|| base_offset + length > module_inst->memory_data_size)) {
wasm_free(module_inst->memory_data.ptr);
wasm_runtime_free(module_inst->memory_data.ptr);
module_inst->memory_data.ptr = NULL;
set_error_buf(error_buf, error_buf_size,
"AOT module instantiate failed: data segment out of range.");
@ -193,7 +192,7 @@ init_func_ptrs(AOTModuleInstance *module_inst, AOTModule *module,
/* Allocate memory */
if (total_size >= UINT32_MAX
|| !(module_inst->func_ptrs.ptr = wasm_malloc((uint32)total_size))) {
|| !(module_inst->func_ptrs.ptr = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module instantiate failed: allocate memory failed.");
return false;
@ -222,7 +221,8 @@ init_func_type_indexes(AOTModuleInstance *module_inst, AOTModule *module,
/* Allocate memory */
if (total_size >= UINT32_MAX
|| !(module_inst->func_type_indexes.ptr = wasm_malloc((uint32)total_size))) {
|| !(module_inst->func_type_indexes.ptr =
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module instantiate failed: allocate memory failed.");
return false;
@ -304,7 +304,7 @@ aot_instantiate(AOTModule *module,
/* Allocate module instance, global data, table data and heap data */
if (total_size >= UINT32_MAX
|| !(module_inst = wasm_malloc((uint32)total_size))) {
|| !(module_inst = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"AOT module instantiate failed: allocate memory failed.");
return NULL;
@ -407,18 +407,18 @@ aot_deinstantiate(AOTModuleInstance *module_inst)
#endif
if (module_inst->memory_data.ptr)
wasm_free(module_inst->memory_data.ptr);
wasm_runtime_free(module_inst->memory_data.ptr);
if (module_inst->heap_handle.ptr)
mem_allocator_destroy(module_inst->heap_handle.ptr);
if (module_inst->func_ptrs.ptr)
wasm_free(module_inst->func_ptrs.ptr);
wasm_runtime_free(module_inst->func_ptrs.ptr);
if (module_inst->func_type_indexes.ptr)
wasm_free(module_inst->func_type_indexes.ptr);
wasm_runtime_free(module_inst->func_type_indexes.ptr);
wasm_free(module_inst);
wasm_runtime_free(module_inst);
}
AOTFunctionInstance*
@ -768,8 +768,8 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
return false;
}
if (!(mem_data_new = wasm_realloc(mem_data_old, (uint32)total_size))) {
if (!(mem_data_new = wasm_malloc((uint32)total_size))) {
if (!(mem_data_new = wasm_runtime_realloc(mem_data_old, (uint32)total_size))) {
if (!(mem_data_new = wasm_runtime_malloc((uint32)total_size))) {
aot_set_exception(module_inst, "fail to enlarge memory.");
return false;
}
@ -778,7 +778,7 @@ aot_enlarge_memory(AOTModuleInstance *module_inst, uint32 inc_page_count)
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);
wasm_runtime_free(mem_data_old);
}
module_inst->mem_cur_page_count = total_page_count;

View File

@ -5,6 +5,9 @@ set (IWASM_COMMON_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories (${IWASM_COMMON_DIR})
add_definitions(-DBH_MALLOC=wasm_runtime_malloc)
add_definitions(-DBH_FREE=wasm_runtime_free)
file (GLOB c_source_all ${IWASM_COMMON_DIR}/*.c)
if (${WAMR_BUILD_TARGET} STREQUAL "X86_64" OR ${WAMR_BUILD_TARGET} STREQUAL "AMD_64")

View File

@ -4,7 +4,6 @@
*/
#include "wasm_exec_env.h"
#include "bh_memory.h"
#include "wasm_runtime_common.h"
WASMExecEnv *
@ -16,14 +15,14 @@ wasm_exec_env_create(struct WASMModuleInstanceCommon *module_inst,
WASMExecEnv *exec_env;
if (total_size >= UINT32_MAX
|| !(exec_env = wasm_malloc((uint32)total_size)))
|| !(exec_env = wasm_runtime_malloc((uint32)total_size)))
return NULL;
memset(exec_env, 0, (uint32)total_size);
#if WASM_ENABLE_AOT != 0
if (!(exec_env->argv_buf = wasm_malloc(sizeof(uint32) * 64))) {
wasm_free(exec_env);
if (!(exec_env->argv_buf = wasm_runtime_malloc(sizeof(uint32) * 64))) {
wasm_runtime_free(exec_env);
return NULL;
}
#endif
@ -40,9 +39,9 @@ void
wasm_exec_env_destroy(WASMExecEnv *exec_env)
{
#if WASM_ENABLE_AOT != 0
wasm_free(exec_env->argv_buf);
wasm_runtime_free(exec_env->argv_buf);
#endif
wasm_free(exec_env);
wasm_runtime_free(exec_env);
}
WASMModuleInstanceCommon *

View File

@ -0,0 +1,372 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "wasm_runtime_common.h"
#include "bh_platform.h"
#include "mem_alloc.h"
#include "bh_thread.h"
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
/* Memory profile data of a function */
typedef struct memory_profile {
struct memory_profile *next;
const char *function_name;
const char *file_name;
int line_in_file;
int malloc_num;
int free_num;
int total_malloc;
int total_free;
} memory_profile_t;
/* Memory in use which grows when BH_MALLOC was called
* and decreases when bh_free was called */
static unsigned int memory_in_use = 0;
/* Memory profile data list */
static memory_profile_t *memory_profiles_list = NULL;
/* Lock of the memory profile list */
static korp_mutex profile_lock;
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#ifndef MALLOC_MEMORY_FROM_SYSTEM
typedef enum Memory_Mode {
MEMORY_MODE_UNKNOWN = 0,
MEMORY_MODE_POOL,
MEMORY_MODE_ALLOCATOR
} Memory_Mode;
static Memory_Mode memory_mode = MEMORY_MODE_UNKNOWN;
static mem_allocator_t pool_allocator = NULL;
static void *(*malloc_func)(unsigned int size) = NULL;
static void *(*realloc_func)(void *ptr, unsigned int size) = NULL;
static void (*free_func)(void *ptr) = NULL;
static unsigned int global_pool_size;
static bool
wasm_memory_init_with_pool(void *mem, unsigned int bytes)
{
mem_allocator_t _allocator = mem_allocator_create(mem, bytes);
if (_allocator) {
memory_mode = MEMORY_MODE_POOL;
pool_allocator = _allocator;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
global_pool_size = bytes;
return true;
}
bh_printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
return false;
}
static bool
wasm_memory_init_with_allocator(void *_malloc_func,
void *_realloc_func,
void *_free_func)
{
if (_malloc_func && _free_func && _malloc_func != _free_func) {
memory_mode = MEMORY_MODE_ALLOCATOR;
malloc_func = _malloc_func;
realloc_func = _realloc_func;
free_func = _free_func;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
#endif
return true;
}
bh_printf("Init memory with allocator (%p, %p, %p) failed.\n",
_malloc_func, _realloc_func, _free_func);
return false;
}
bool
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
const MemAllocOption *alloc_option)
{
if (mem_alloc_type == Alloc_With_Pool)
return wasm_memory_init_with_pool(alloc_option->pool.heap_buf,
alloc_option->pool.heap_size);
else if (mem_alloc_type == Alloc_With_Allocator)
return wasm_memory_init_with_allocator(alloc_option->allocator.malloc_func,
alloc_option->allocator.realloc_func,
alloc_option->allocator.free_func);
else if (mem_alloc_type == Alloc_With_System_Allocator)
return wasm_memory_init_with_allocator(os_malloc, os_realloc, os_free);
else
return false;
}
void
wasm_runtime_memory_destroy()
{
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_destroy(&profile_lock);
#endif
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
memory_mode = MEMORY_MODE_UNKNOWN;
}
unsigned
wasm_runtime_memory_pool_size()
{
if (memory_mode == MEMORY_MODE_POOL)
return global_pool_size;
else
return 1 * BH_GB;
}
void *
wasm_runtime_malloc(unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_malloc failed: memory hasn't been initialize.\n");
return NULL;
} else if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_malloc(pool_allocator, size);
} else {
return malloc_func(size);
}
}
void *
wasm_runtime_realloc(void *ptr, unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_realloc failed: memory hasn't been initialize.\n");
return NULL;
} else if (memory_mode == MEMORY_MODE_POOL) {
return mem_allocator_realloc(pool_allocator, ptr, size);
} else {
if (realloc_func)
return realloc_func(ptr, size);
else
return NULL;
}
}
void
wasm_runtime_free(void *ptr)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_free failed: memory hasn't been initialize.\n");
} else if (memory_mode == MEMORY_MODE_POOL) {
mem_allocator_free(pool_allocator, ptr);
} else {
free_func(ptr);
}
}
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
{
void *p = wasm_rutime_malloc(size + 8);
if (p) {
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_malloc += size;/* TODO: overflow check */
profile->malloc_num++;
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
return (char *)p + 8;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->malloc_num = 1;
profile->total_malloc = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
memory_in_use += size;
memory_profile_print(file, line, func, size);
return (char *)p + 8;
}
return NULL;
}
void
wasm_runtime_free_profile(const char *file, int line,
const char *func, void *ptr)
{
unsigned int size = *(unsigned int *)((char *)ptr - 8);
memory_profile_t *profile;
wasm_runtime_free((char *)ptr - 8);
if (memory_in_use >= size)
memory_in_use -= size;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
if (strcmp(profile->function_name, func) == 0
&& strcmp(profile->file_name, file) == 0) {
break;
}
profile = profile->next;
}
if (profile) {
profile->total_free += size;/* TODO: overflow check */
profile->free_num++;
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
return;
}
memset(profile, 0, sizeof(memory_profile_t));
profile->file_name = file;
profile->line_in_file = line;
profile->function_name = func;
profile->free_num = 1;
profile->total_free = size;
profile->next = memory_profiles_list;
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
}
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize()
{
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
bh_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
profile->total_malloc,
profile->malloc_num,
profile->total_free,
profile->free_num,
profile->function_name);
profile = profile->next;
}
vm_mutex_unlock(&profile_lock);
}
void
memory_profile_print(const char *file, int line,
const char *func, int alloc)
{
bh_printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#else /* else of MALLOC_MEMORY_FROM_SYSTEM */
void *
wasm_runtime_malloc(unsigned int size)
{
return malloc(size);
}
void *
wasm_runtime_realloc(void *ptr, unsigned int size)
{
return realloc(ptr, size);
}
void
wasm_runtime_free(void *ptr)
{
if (ptr)
free(ptr);
}
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
void *
wasm_runtime_malloc_profile(const char *file, int line,
const char *func, unsigned int size)
{
(void)file;
(void)line;
(void)func;
(void)memory_profiles_list;
(void)profile_lock;
(void)memory_in_use;
return malloc(size);
}
void *
wasm_runtime_realloc_profile(const char *file, int line,
const char *func, void *ptr, unsigned int size)
{
(void)file;
(void)line;
(void)func;
(void)memory_profiles_list;
(void)profile_lock;
(void)memory_in_use;
return realloc(ptr, size);
}
void
wasm_runtime_free_profile(const char *file, int line,
const char *func, void *ptr)
{
(void)file;
(void)line;
(void)func;
if (ptr)
free(ptr);
}
#endif /* end of BEIHAI_ENABLE_MEMORY_PROFILING */
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/

View File

@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _WASM_MEMORY_H
#define _WASM_MEMORY_H
#include "bh_common.h"
#include "../include/wasm_export.h"
#ifdef __cplusplus
extern "C" {
#endif
bool
wasm_runtime_memory_init(mem_alloc_type_t mem_alloc_type,
const MemAllocOption *alloc_option);
void
wasm_runtime_memory_destroy();
#ifdef __cplusplus
}
#endif
#endif /* end of _WASM_MEMORY_H */

View File

@ -184,7 +184,7 @@ wasm_native_register_natives(const char *module_name,
{
NativeSymbolsNode *node;
if (!(node = bh_malloc(sizeof(NativeSymbolsNode))))
if (!(node = wasm_runtime_malloc(sizeof(NativeSymbolsNode))))
return false;
node->module_name = module_name;
@ -256,7 +256,7 @@ wasm_native_destroy()
node = g_native_symbols_list;
while (node) {
node_next = node->next;
bh_free(node);
wasm_runtime_free(node);
node = node_next;
}

View File

@ -9,6 +9,7 @@
#include "bh_assert.h"
#include "bh_log.h"
#include "wasm_runtime_common.h"
#include "wasm_memory.h"
#if WASM_ENABLE_INTERP != 0
#include "../interpreter/wasm_runtime.h"
#endif
@ -23,8 +24,8 @@ set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
snprintf(error_buf, error_buf_size, "%s", string);
}
bool
wasm_runtime_init()
static bool
wasm_runtime_env_init()
{
if (bh_platform_init() != 0)
return false;
@ -35,8 +36,20 @@ wasm_runtime_init()
if (vm_thread_sys_init() != 0)
return false;
if (wasm_native_init() == false) {
wasm_runtime_destroy();
if (wasm_native_init() == false)
return false;
return true;
}
bool
wasm_runtime_init()
{
if (!wasm_runtime_memory_init(Alloc_With_System_Allocator, NULL))
return false;
if (!wasm_runtime_env_init()) {
wasm_runtime_memory_destroy();
return false;
}
@ -48,56 +61,30 @@ wasm_runtime_destroy()
{
wasm_native_destroy();
vm_thread_sys_destroy();
wasm_runtime_memory_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)
{
if (init_args->mem_alloc_type == Alloc_With_Pool) {
void *heap_buf = init_args->mem_alloc.pool.heap_buf;
uint32 heap_size = init_args->mem_alloc.pool.heap_size;
if (bh_memory_init_with_pool(heap_buf, heap_size) != 0)
return false;
}
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_internal(malloc_func,
realloc_func,
free_func) != 0)
return false;
}
else
if (!wasm_runtime_memory_init(init_args->mem_alloc_type,
&init_args->mem_alloc_option))
return false;
if (!wasm_runtime_init())
goto fail1;
if (!wasm_runtime_env_init()) {
wasm_runtime_memory_destroy();
return false;
}
if (init_args->n_native_symbols > 0
&& !wasm_runtime_register_natives(init_args->native_module_name,
init_args->native_symbols,
init_args->n_native_symbols))
goto fail2;
init_args->n_native_symbols)) {
wasm_runtime_destroy();
return false;
}
return true;
fail2:
wasm_runtime_destroy();
fail1:
bh_memory_destroy();
return false;
}
void
wasm_runtime_full_destroy()
{
wasm_runtime_destroy();
bh_memory_destroy();
}
PackageType
@ -723,7 +710,7 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
uint64 total_size;
uint32 i;
if (!(wasi_ctx = wasm_malloc(sizeof(WASIContext)))) {
if (!(wasi_ctx = wasm_runtime_malloc(sizeof(WASIContext)))) {
set_error_buf(error_buf, error_buf_size,
"Init wasi environment failed: allocate memory failed.");
return false;
@ -952,7 +939,7 @@ wasm_runtime_destroy_wasi(WASMModuleInstanceCommon *module_inst)
fd_table_destroy(wasi_ctx->curfds);
if (wasi_ctx->prestats)
fd_prestats_destroy(wasi_ctx->prestats);
bh_free(wasi_ctx);
wasm_runtime_free(wasi_ctx);
}
}
@ -1272,7 +1259,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
total_size = sizeof(uint32) * (uint64)(argc1 > 2 ? argc1 : 2);
if (total_size >= UINT32_MAX
|| (!(argv1 = wasm_malloc((uint32)total_size)))) {
|| (!(argv1 = wasm_runtime_malloc((uint32)total_size)))) {
wasm_runtime_set_exception(module_inst, "allocate memory failed.");
goto fail;
}
@ -1417,12 +1404,12 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
}
bh_printf("\n");
wasm_free(argv1);
wasm_runtime_free(argv1);
return true;
fail:
if (argv1)
wasm_free(argv1);
wasm_runtime_free(argv1);
exception = wasm_runtime_get_exception(module_inst);
bh_assert(exception);
@ -1551,7 +1538,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
size = sizeof(uint32) * (uint32)argc1;
if (size >= UINT32_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1679,7 +1666,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}
#endif /* end of defined(BUILD_TARGET_ARM_VFP) || defined(BUILD_TARGET_THUMB_VFP) */
@ -1726,7 +1713,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint32)) {
size = sizeof(uint32) * (uint64)argc1;
if (size >= UINT_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1819,7 +1806,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}
@ -1874,7 +1861,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
if (argc1 > sizeof(argv_buf) / sizeof(uint64)) {
size = sizeof(uint64) * (uint64)argc1;
if (size >= UINT32_MAX
|| !(argv1 = wasm_malloc((uint32)size))) {
|| !(argv1 = wasm_runtime_malloc((uint32)size))) {
wasm_runtime_set_exception(exec_env->module_inst,
"allocate memory failed.");
return false;
@ -1976,7 +1963,7 @@ wasm_runtime_invoke_native(WASMExecEnv *exec_env, void *func_ptr,
ret = true;
fail:
if (argv1 != argv_buf)
wasm_free(argv1);
wasm_runtime_free(argv1);
return ret;
}

View File

@ -22,9 +22,6 @@
extern "C" {
#endif
#define wasm_malloc bh_malloc
#define wasm_realloc bh_realloc
#define wasm_free bh_free
typedef struct WASMModuleCommon {
/* Module type, for module loaded from WASM bytecode binary,
@ -63,17 +60,13 @@ typedef wasm_section_t WASMSection, AOTSection;
bool
wasm_runtime_init();
/* See wasm_export.h for description */
void
wasm_runtime_destroy();
/* See wasm_export.h for description */
bool
wasm_runtime_full_init(RuntimeInitArgs *init_args);
/* See wasm_export.h for description */
void
wasm_runtime_full_destroy();
wasm_runtime_destroy();
/* See wasm_export.h for description */
PackageType

View File

@ -4,7 +4,6 @@
*/
#include "aot.h"
#include "bh_memory.h"
static char aot_error[128];
@ -30,8 +29,8 @@ aot_destroy_mem_init_data_list(AOTMemInitData **data_list, uint32 count)
uint32 i;
for (i = 0; i < count; i++)
if (data_list[i])
wasm_free(data_list[i]);
wasm_free(data_list);
wasm_runtime_free(data_list[i]);
wasm_runtime_free(data_list);
}
static AOTMemInitData **
@ -44,7 +43,7 @@ aot_create_mem_init_data_list(const WASMModule *module)
/* Allocate memory */
size = sizeof(AOTMemInitData *) * (uint64)module->data_seg_count;
if (size >= UINT32_MAX
|| !(data_list = wasm_malloc((uint32)size))) {
|| !(data_list = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -56,7 +55,7 @@ aot_create_mem_init_data_list(const WASMModule *module)
size = offsetof(AOTMemInitData, bytes) +
(uint64)module->data_segments[i]->data_length;
if (size >= UINT32_MAX
|| !(data_list[i] = wasm_malloc((uint32)size))) {
|| !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
@ -80,8 +79,8 @@ aot_destroy_table_init_data_list(AOTTableInitData **data_list, uint32 count)
uint32 i;
for (i = 0; i < count; i++)
if (data_list[i])
wasm_free(data_list[i]);
wasm_free(data_list);
wasm_runtime_free(data_list[i]);
wasm_runtime_free(data_list);
}
static AOTTableInitData **
@ -94,7 +93,7 @@ aot_create_table_init_data_list(const WASMModule *module)
/* Allocate memory */
size = sizeof(AOTTableInitData *) * (uint64)module->table_seg_count;
if (size >= UINT32_MAX
|| !(data_list = wasm_malloc((uint32)size))) {
|| !(data_list = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -106,7 +105,7 @@ aot_create_table_init_data_list(const WASMModule *module)
size = offsetof(AOTTableInitData, func_indexes) +
sizeof(uint32) * (uint64)module->table_segments[i].function_count;
if (size >= UINT32_MAX
|| !(data_list[i] = wasm_malloc((uint32)size))) {
|| !(data_list[i] = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
@ -135,7 +134,7 @@ aot_create_import_globals(const WASMModule *module,
/* Allocate memory */
size = sizeof(AOTImportGlobal) * (uint64)module->import_global_count;
if (size >= UINT32_MAX
|| !(import_globals = wasm_malloc((uint32)size))) {
|| !(import_globals = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -172,7 +171,7 @@ aot_create_globals(const WASMModule *module,
/* Allocate memory */
size = sizeof(AOTGlobal) * (uint64)module->global_count;
if (size >= UINT32_MAX
|| !(globals = wasm_malloc((uint32)size))) {
|| !(globals = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -202,8 +201,8 @@ aot_destroy_func_types(AOTFuncType **func_types, uint32 count)
uint32 i;
for (i = 0; i < count; i++)
if (func_types[i])
wasm_free(func_types[i]);
wasm_free(func_types);
wasm_runtime_free(func_types[i]);
wasm_runtime_free(func_types);
}
static AOTFuncType **
@ -216,7 +215,7 @@ aot_create_func_types(const WASMModule *module)
/* Allocate memory */
size = sizeof(AOTFuncType*) * (uint64)module->type_count;
if (size >= UINT32_MAX
|| !(func_types = wasm_malloc((uint32)size))) {
|| !(func_types = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -229,7 +228,7 @@ aot_create_func_types(const WASMModule *module)
(uint64)module->types[i]->param_count +
(uint64)module->types[i]->result_count;
if (size >= UINT32_MAX
|| !(func_types[i] = wasm_malloc((uint32)size))) {
|| !(func_types[i] = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
@ -253,7 +252,7 @@ aot_create_import_funcs(const WASMModule *module)
/* Allocate memory */
size = sizeof(AOTImportFunc) * (uint64)module->import_function_count;
if (size >= UINT32_MAX
|| !(import_funcs = wasm_malloc((uint32)size))) {
|| !(import_funcs = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -284,8 +283,8 @@ aot_destroy_funcs(AOTFunc **funcs, uint32 count)
for (i = 0; i < count; i++)
if (funcs[i])
wasm_free(funcs[i]);
wasm_free(funcs);
wasm_runtime_free(funcs[i]);
wasm_runtime_free(funcs);
}
static AOTFunc **
@ -298,7 +297,7 @@ aot_create_funcs(const WASMModule *module)
/* Allocate memory */
size = sizeof(AOTFunc*) * (uint64)module->function_count;
if (size >= UINT32_MAX
|| !(funcs = wasm_malloc((uint32)size))) {
|| !(funcs = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -309,7 +308,7 @@ aot_create_funcs(const WASMModule *module)
for (i = 0; i < module->function_count; i++) {
WASMFunction *func = module->functions[i];
size = sizeof (AOTFunc);
if (!(funcs[i] = wasm_malloc((uint32)size))) {
if (!(funcs[i] = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
@ -348,7 +347,7 @@ aot_create_export_funcs(const WASMModule *module,
/* Allocate memory */
size = sizeof(AOTExportFunc) * (uint64)export_func_count;
if (size >= UINT32_MAX
|| !(export_funcs = wasm_malloc((uint32)size))) {
|| !(export_funcs = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -376,7 +375,7 @@ aot_create_comp_data(WASMModule *module)
uint32 import_global_data_size = 0, global_data_size = 0, i;
/* Allocate memory */
if (!(comp_data = wasm_malloc(sizeof(AOTCompData)))) {
if (!(comp_data = wasm_runtime_malloc(sizeof(AOTCompData)))) {
aot_set_last_error("create compile data failed.\n");
return NULL;
}
@ -492,24 +491,24 @@ aot_destroy_comp_data(AOTCompData *comp_data)
comp_data->table_init_data_count);
if (comp_data->import_globals)
wasm_free(comp_data->import_globals);
wasm_runtime_free(comp_data->import_globals);
if (comp_data->globals)
wasm_free(comp_data->globals);
wasm_runtime_free(comp_data->globals);
if (comp_data->func_types)
aot_destroy_func_types(comp_data->func_types,
comp_data->func_type_count);
if (comp_data->import_funcs)
wasm_free(comp_data->import_funcs);
wasm_runtime_free(comp_data->import_funcs);
if (comp_data->funcs)
aot_destroy_funcs(comp_data->funcs, comp_data->func_count);
if (comp_data->export_funcs)
wasm_free(comp_data->export_funcs);
wasm_runtime_free(comp_data->export_funcs);
wasm_free(comp_data);
wasm_runtime_free(comp_data);
}

View File

@ -14,7 +14,6 @@
#include "aot_emit_control.h"
#include "aot_emit_function.h"
#include "aot_emit_parametric.h"
#include "bh_memory.h"
#include "../aot/aot_runtime.h"
#include "../interpreter/wasm_opcode.h"
#include <errno.h>
@ -152,7 +151,8 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
case WASM_OP_BR_TABLE:
read_leb_uint32(frame_ip, frame_ip_end, br_count);
if (!(br_depths = wasm_malloc((uint32)sizeof(uint32) * (br_count + 1)))) {
if (!(br_depths =
wasm_runtime_malloc((uint32)sizeof(uint32) * (br_count + 1)))) {
aot_set_last_error("allocate memory failed.");
goto fail;
}
@ -161,11 +161,11 @@ aot_compile_func(AOTCompContext *comp_ctx, uint32 func_index)
if (!aot_compile_op_br_table(comp_ctx, func_ctx,
br_depths, br_count, &frame_ip)) {
wasm_free(br_depths);
wasm_runtime_free(br_depths);
return false;
}
wasm_free(br_depths);
wasm_runtime_free(br_depths);
break;
case WASM_OP_RETURN:

View File

@ -8,7 +8,6 @@
#include "aot.h"
#include "aot_llvm.h"
#include "bh_memory.h"
#ifdef __cplusplus
extern "C" {
@ -103,7 +102,7 @@ typedef enum FloatArithmetic {
&& (aot_value->type != VALUE_TYPE_I32 \
&& aot_value->type != VALUE_TYPE_I1))) { \
aot_set_last_error("invalid WASM stack data type."); \
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
goto fail; \
} \
if (aot_value->type == value_type) \
@ -113,11 +112,11 @@ typedef enum FloatArithmetic {
if (!(llvm_value = LLVMBuildZExt(comp_ctx->builder, \
aot_value->value, I32_TYPE, "i1toi32"))) { \
aot_set_last_error("invalid WASM stack data type.");\
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
goto fail; \
} \
} \
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
} while (0)
#define POP_I32(v) POP(v, VALUE_TYPE_I32)
@ -133,7 +132,7 @@ typedef enum FloatArithmetic {
if (aot_value->type != VALUE_TYPE_I1 \
&& aot_value->type != VALUE_TYPE_I32) { \
aot_set_last_error("invalid WASM stack data type."); \
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
goto fail; \
} \
if (aot_value->type == VALUE_TYPE_I1) \
@ -143,11 +142,11 @@ typedef enum FloatArithmetic {
LLVMIntNE, aot_value->value, I32_ZERO, \
"i1_cond"))){ \
aot_set_last_error("llvm build trunc failed."); \
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
goto fail; \
} \
} \
wasm_free(aot_value); \
wasm_runtime_free(aot_value); \
} while (0)
#define PUSH(llvm_value, value_type) do { \
@ -156,7 +155,7 @@ typedef enum FloatArithmetic {
aot_set_last_error("WASM block stack underflow."); \
goto fail; \
} \
aot_value = wasm_malloc(sizeof(AOTValue)); \
aot_value = wasm_runtime_malloc(sizeof(AOTValue)); \
memset(aot_value, 0, sizeof(AOTValue)); \
if (!aot_value) { \
aot_set_last_error("allocate memory failed."); \

View File

@ -509,7 +509,7 @@ get_relocation_symbol_index(const char *symbol_name,
}
/* Not found in symbol_list, add it */
sym = bh_malloc(sizeof(AOTSymbolNode));
sym = wasm_runtime_malloc(sizeof(AOTSymbolNode));
if (!sym) {
return (uint32)-1;
}
@ -1492,7 +1492,7 @@ aot_resolve_object_data_sections(AOTObjectData *obj_data)
if (sections_count > 0) {
size = (uint32)sizeof(AOTObjectDataSection) * sections_count;
if (!(data_section = obj_data->data_sections = bh_malloc(size))) {
if (!(data_section = obj_data->data_sections = wasm_runtime_malloc(size))) {
aot_set_last_error("allocate memory for data sections failed.");
return false;
}
@ -1531,7 +1531,7 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
/* allocate memory for aot function */
obj_data->func_count = comp_ctx->comp_data->func_count;
if (!(obj_data->funcs
= bh_malloc((uint32)sizeof(AOTObjectFunc) * obj_data->func_count))) {
= wasm_runtime_malloc((uint32)sizeof(AOTObjectFunc) * obj_data->func_count))) {
aot_set_last_error("allocate memory for functions failed.");
return false;
}
@ -1602,7 +1602,7 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
return false;
}
size = (uint32)sizeof(AOTRelocation) * group->relocation_count;
if (!(relocation = group->relocations = bh_malloc(size))) {
if (!(relocation = group->relocations = wasm_runtime_malloc(size))) {
aot_set_last_error("allocate memory for relocations failed.");
return false;
}
@ -1754,7 +1754,7 @@ aot_resolve_object_relocation_groups(AOTObjectData *obj_data)
return true;
size = (uint32)sizeof(AOTRelocationGroup) * group_count;
if (!(relocation_group = obj_data->relocation_groups = bh_malloc(size))) {
if (!(relocation_group = obj_data->relocation_groups = wasm_runtime_malloc(size))) {
aot_set_last_error("allocate memory for relocation groups failed.");
return false;
}
@ -1795,8 +1795,8 @@ destroy_relocation_groups(AOTRelocationGroup *relocation_groups,
for (i = 0; i < relocation_group_count; i++, relocation_group++)
if (relocation_group->relocations)
bh_free(relocation_group->relocations);
bh_free(relocation_groups);
wasm_runtime_free(relocation_group->relocations);
wasm_runtime_free(relocation_groups);
}
static void
@ -1807,7 +1807,7 @@ destroy_relocation_symbol_list(AOTSymbolList *symbol_list)
elem = symbol_list->head;
while (elem) {
AOTSymbolNode *next = elem->next;
bh_free(elem);
wasm_runtime_free(elem);
elem = next;
}
}
@ -1820,15 +1820,15 @@ aot_obj_data_destroy(AOTObjectData *obj_data)
if (obj_data->mem_buf)
LLVMDisposeMemoryBuffer(obj_data->mem_buf);
if (obj_data->funcs)
bh_free(obj_data->funcs);
wasm_runtime_free(obj_data->funcs);
if (obj_data->data_sections)
bh_free(obj_data->data_sections);
wasm_runtime_free(obj_data->data_sections);
if (obj_data->relocation_groups)
destroy_relocation_groups(obj_data->relocation_groups,
obj_data->relocation_group_count);
if (obj_data->symbol_list.len)
destroy_relocation_symbol_list(&obj_data->symbol_list);
bh_free(obj_data);
wasm_runtime_free(obj_data);
}
static AOTObjectData *
@ -1837,7 +1837,7 @@ aot_obj_data_create(AOTCompContext *comp_ctx)
char *err = NULL;
AOTObjectData *obj_data;
if (!(obj_data = bh_malloc(sizeof(AOTObjectData)))) {
if (!(obj_data = wasm_runtime_malloc(sizeof(AOTObjectData)))) {
aot_set_last_error("allocate memory failed.");
return false;
}
@ -1896,7 +1896,7 @@ aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
aot_file_size = get_aot_file_size(comp_data, obj_data);
if (!(buf = aot_file_buf = bh_malloc(aot_file_size))) {
if (!(buf = aot_file_buf = wasm_runtime_malloc(aot_file_size))) {
aot_set_last_error("allocate memory failed.");
goto fail1;
}
@ -1938,7 +1938,7 @@ fail3:
fclose(file);
fail2:
bh_free(aot_file_buf);
wasm_runtime_free(aot_file_buf);
fail1:
aot_obj_data_destroy(obj_data);

View File

@ -7,7 +7,6 @@
#include "aot_emit_exception.h"
#include "../aot/aot_runtime.h"
#include "../interpreter/wasm_loader.h"
#include "bh_memory.h"
static char *block_name_prefix[] = { "block", "loop", "if" };
static char *block_name_suffix[] = { "begin", "else", "end" };
@ -208,7 +207,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
}
/* Allocate memory */
if (!(block = wasm_malloc(sizeof(AOTBlock)))) {
if (!(block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
aot_set_last_error("allocate memory failed.");
return false;
}
@ -309,7 +308,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
goto fail;
}
/* skip the block */
wasm_free(block);
wasm_runtime_free(block);
*p_frame_ip = end_addr + 1;
}
}
@ -322,7 +321,7 @@ aot_compile_op_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return true;
fail:
wasm_free(block);
wasm_runtime_free(block);
return false;
}

View File

@ -247,7 +247,7 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
param_count = (int32)func_type->param_count;
total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
if (total_size >= UINT32_MAX
|| !(param_values = wasm_malloc((uint32)total_size))) {
|| !(param_values = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
return false;
}
@ -268,7 +268,7 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
/* Initialize parameter types of the LLVM function */
total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
if (total_size >= UINT32_MAX
|| !(param_types = wasm_malloc((uint32)total_size))) {
|| !(param_types = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
goto fail;
}
@ -321,9 +321,9 @@ aot_compile_op_call(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
ret = true;
fail:
if (param_types)
wasm_free(param_types);
wasm_runtime_free(param_types);
if (param_values)
wasm_free(param_values);
wasm_runtime_free(param_values);
return ret;
}
@ -548,7 +548,7 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
param_count = (int32)func_type->param_count;
total_size = sizeof(LLVMTypeRef) * (uint64)(param_count + 1);
if (total_size >= UINT32_MAX
|| !(param_types = wasm_malloc((uint32)total_size))) {
|| !(param_types = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
goto fail;
}
@ -571,7 +571,7 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
/* Allocate memory for parameters */
total_size = sizeof(LLVMValueRef) * (uint64)(param_count + 1);
if (total_size >= UINT32_MAX
|| !(param_values = wasm_malloc((uint32)total_size))) {
|| !(param_values = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("Allocate memory failed.");
goto fail;
}
@ -601,9 +601,9 @@ aot_compile_op_call_indirect(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
fail:
if (param_values)
wasm_free(param_values);
wasm_runtime_free(param_values);
if (param_types)
wasm_free(param_types);
wasm_runtime_free(param_types);
return ret;
}

View File

@ -186,7 +186,7 @@ call_llvm_intrinsic(AOTCompContext *comp_ctx,
/* Create param values */
total_size = sizeof(LLVMValueRef) * (uint64)param_count;
if (total_size >= UINT32_MAX
|| !(param_values = wasm_malloc((uint32)total_size))) {
|| !(param_values = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("allocate memory for param values failed.");
return false;
}
@ -201,7 +201,7 @@ call_llvm_intrinsic(AOTCompContext *comp_ctx,
param_types, param_count,
param_values);
wasm_free(param_values);
wasm_runtime_free(param_values);
return ret;
}
@ -221,7 +221,7 @@ call_llvm_intrinsic_v(AOTCompContext *comp_ctx,
/* Create param values */
total_size = sizeof(LLVMValueRef) * (uint64)param_count;
if (total_size >= UINT32_MAX
|| !(param_values = wasm_malloc((uint32)total_size))) {
|| !(param_values = wasm_runtime_malloc((uint32)total_size))) {
aot_set_last_error("allocate memory for param values failed.");
return false;
}
@ -234,7 +234,7 @@ call_llvm_intrinsic_v(AOTCompContext *comp_ctx,
param_types, param_count,
param_values);
wasm_free(param_values);
wasm_runtime_free(param_values);
return ret;
}

View File

@ -43,7 +43,7 @@ pop_value_from_wasm_stack(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
*p_value = aot_value->value;
}
wasm_free(aot_value);
wasm_runtime_free(aot_value);
if ((is_32
&& (type != VALUE_TYPE_I32 && type != VALUE_TYPE_F32))

View File

@ -4,7 +4,6 @@
*/
#include "aot_llvm.h"
#include "bh_memory.h"
#include "aot_compiler.h"
#include "../aot/aot_runtime.h"
@ -47,7 +46,7 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, AOTFuncType *aot_func_type,
/* Initialize parameter types of the LLVM function */
size = sizeof(LLVMTypeRef) * ((uint64)param_count);
if (size >= UINT32_MAX
|| !(param_types = wasm_malloc((uint32)size))) {
|| !(param_types = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -88,7 +87,7 @@ aot_add_llvm_func(AOTCompContext *comp_ctx, AOTFuncType *aot_func_type,
}
fail:
wasm_free(param_types);
wasm_runtime_free(param_types);
return func;
}
@ -102,7 +101,7 @@ aot_create_func_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
AOTBlock *aot_block;
/* Allocate memory */
if (!(aot_block = wasm_malloc(sizeof(AOTBlock)))) {
if (!(aot_block = wasm_runtime_malloc(sizeof(AOTBlock)))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -129,7 +128,7 @@ aot_create_func_block(AOTCompContext *comp_ctx, AOTFuncContext *func_ctx,
return aot_block;
fail:
wasm_free(aot_block);
wasm_runtime_free(aot_block);
return NULL;
}
@ -137,7 +136,7 @@ static bool
create_exception_blocks(AOTFuncContext *func_ctx)
{
if (!(func_ctx->exception_blocks =
wasm_malloc(sizeof(LLVMBasicBlockRef) * EXCE_NUM))) {
wasm_runtime_malloc(sizeof(LLVMBasicBlockRef) * EXCE_NUM))) {
aot_set_last_error("allocate memory failed.");
return false;;
}
@ -451,7 +450,7 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
size = offsetof(AOTFuncContext, locals) + sizeof(LLVMValueRef) *
((uint64)aot_func_type->param_count + func->local_count);
if (size >= UINT32_MAX
|| !(func_ctx = wasm_malloc((uint32)size))) {
|| !(func_ctx = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -605,9 +604,9 @@ aot_create_func_context(AOTCompData *comp_data, AOTCompContext *comp_ctx,
fail:
if (func_ctx->exception_blocks)
wasm_free(func_ctx->exception_blocks);
wasm_runtime_free(func_ctx->exception_blocks);
aot_block_stack_destroy(&func_ctx->block_stack);
wasm_free(func_ctx);
wasm_runtime_free(func_ctx);
return NULL;
}
@ -619,11 +618,11 @@ aot_destroy_func_contexts(AOTFuncContext **func_ctxes, uint32 count)
for (i = 0; i < count; i++)
if (func_ctxes[i]) {
if (func_ctxes[i]->exception_blocks)
wasm_free(func_ctxes[i]->exception_blocks);
wasm_runtime_free(func_ctxes[i]->exception_blocks);
aot_block_stack_destroy(&func_ctxes[i]->block_stack);
wasm_free(func_ctxes[i]);
wasm_runtime_free(func_ctxes[i]);
}
wasm_free(func_ctxes);
wasm_runtime_free(func_ctxes);
}
/**
@ -639,7 +638,7 @@ aot_create_func_contexts(AOTCompData *comp_data, AOTCompContext *comp_ctx)
/* Allocate memory */
size = sizeof(AOTFuncContext*) * (uint64)comp_data->func_count;
if (size >= UINT32_MAX
|| !(func_ctxes = wasm_malloc((uint32)size))) {
|| !(func_ctxes = wasm_runtime_malloc((uint32)size))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -877,7 +876,7 @@ aot_create_comp_context(AOTCompData *comp_data,
LLVMLinkInMCJIT();
/* Allocate memory */
if (!(comp_ctx = wasm_malloc(sizeof(AOTCompContext)))) {
if (!(comp_ctx = wasm_runtime_malloc(sizeof(AOTCompContext)))) {
aot_set_last_error("allocate memory failed.");
return NULL;
}
@ -1176,7 +1175,7 @@ aot_destroy_comp_context(AOTCompContext *comp_ctx)
if (comp_ctx->func_ctxes)
aot_destroy_func_contexts(comp_ctx->func_ctxes, comp_ctx->func_ctx_count);
wasm_free(comp_ctx);
wasm_runtime_free(comp_ctx);
}
void
@ -1216,7 +1215,7 @@ aot_value_stack_destroy(AOTValueStack *stack)
while (value) {
p = value->next;
wasm_free(value);
wasm_runtime_free(value);
value = p;
}
}
@ -1259,7 +1258,7 @@ aot_block_stack_destroy(AOTBlockStack *stack)
while (block) {
p = block->next;
aot_value_stack_destroy(&block->value_stack);
wasm_free(block);
wasm_runtime_free(block);
block = p;
}
}
@ -1268,5 +1267,5 @@ void
aot_block_destroy(AOTBlock *block)
{
aot_value_stack_destroy(&block->value_stack);
wasm_free(block);
wasm_runtime_free(block);
}

View File

@ -1,99 +0,0 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#ifndef _BH_MEMORY_H
#define _BH_MEMORY_H
#ifdef __cplusplus
extern "C" {
#endif
#define BH_KB (1024)
#define BH_MB ((BH_KB)*1024)
#define BH_GB ((BH_MB)*1024)
/**
* Initialize memory allocator with a pool, the bh_malloc/bh_free function
* will malloc/free memory from the pool
*
* @param mem the pool buffer
* @param bytes the size bytes of the buffer
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_pool(void *mem, unsigned int bytes);
/**
* Initialize memory allocator with memory allocator, the bh_malloc/bh_free
* function will malloc/free memory with the allocator passed
*
* @param malloc_func the malloc function
* @param free_func the free function
*
* @return 0 if success, -1 otherwise
*/
int bh_memory_init_with_allocator(void *malloc_func, void *free_func);
/**
* Destroy memory
*/
void bh_memory_destroy();
/**
* Get the pool size of memory, if memory is initialized with allocator,
* return 1GB by default.
*/
unsigned bh_memory_pool_size();
#if BEIHAI_ENABLE_MEMORY_PROFILING == 0
/**
* This function allocates a memory chunk from system
*
* @param size bytes need allocate
*
* @return the pointer to memory allocated
*/
void* bh_malloc(unsigned int size);
/**
* This function frees memory chunk
*
* @param ptr the pointer to memory need free
*/
void bh_free(void *ptr);
#else
void* bh_malloc_profile(const char *file, int line, const char *func, unsigned int size);
void bh_free_profile(const char *file, int line, const char *func, void *ptr);
#define bh_malloc(size) bh_malloc_profile(__FILE__, __LINE__, __func__, size)
#define bh_free(ptr) bh_free_profile(__FILE__, __LINE__, __func__, ptr)
/**
* Print current memory profiling data
*
* @param file file name of the caller
* @param line line of the file of the caller
* @param func function name of the caller
*/
void memory_profile_print(const char *file, int line, const char *func, int alloc);
/**
* Summarize memory usage and print it out
* Can use awk to analyze the output like below:
* awk -F: '{print $2,$4,$6,$8,$9}' OFS="\t" ./out.txt | sort -n -r -k 1
*/
void memory_usage_summarize();
#endif
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_MEMORY_H */

View File

@ -9,7 +9,6 @@
#include <inttypes.h>
#include <stdbool.h>
#include "lib_export.h"
#include "bh_memory.h"
#ifdef __cplusplus
@ -53,24 +52,33 @@ typedef enum {
/* Memory allocator type */
typedef enum {
/* pool mode, allocate memory from user defined heap buffer */
Alloc_With_Pool = 0,
Alloc_With_Allocator
/* user allocator mode, allocate memory from user defined
malloc function */
Alloc_With_Allocator,
/* system allocator mode, allocate memory from system allocator,
or, platform's os_malloc function */
Alloc_With_System_Allocator,
} mem_alloc_type_t;
/* Memory allocator option */
typedef union MemAllocOption {
struct {
void *heap_buf;
uint32_t heap_size;
} pool;
struct {
void *malloc_func;
void *realloc_func;
void *free_func;
} allocator;
} MemAllocOption;
/* WASM runtime initialize arguments */
typedef struct RuntimeInitArgs {
mem_alloc_type_t mem_alloc_type;
union {
struct {
void *heap_buf;
uint32_t heap_size;
} pool;
struct {
void *malloc_func;
void *realloc_func;
void *free_func;
} allocator;
} mem_alloc;
MemAllocOption mem_alloc_option;
const char *native_module_name;
NativeSymbol *native_symbols;
@ -78,19 +86,15 @@ typedef struct RuntimeInitArgs {
} RuntimeInitArgs;
/**
* Initialize the WASM runtime environment.
* Initialize the WASM runtime environment, and also initialize
* the memory allocator with system allocator, which calls os_malloc
* to allocate memory
*
* @return true if success, false otherwise
*/
bool
wasm_runtime_init();
/**
* Destroy the WASM runtime environment.
*/
void
wasm_runtime_destroy();
/**
* Initialize the WASM runtime environment, and also initialize
* the memory allocator and register native symbols, which are specified
@ -104,11 +108,36 @@ bool
wasm_runtime_full_init(RuntimeInitArgs *init_args);
/**
* Destroy the wasm runtime environment, and also destroy
* the memory allocator and registered native symbols
* Destroy the WASM runtime environment.
*/
void
wasm_runtime_full_destroy();
wasm_runtime_destroy();
/**
* Allocate memory from runtime memory environment.
*
* @param size bytes need to allocate
*
* @return the pointer to memory allocated
*/
void *
wasm_runtime_malloc(unsigned int size);
/**
* Reallocate memory from runtime memory environment
*
* @param ptr the original memory
* @param size bytes need to reallocate
*
* @return the pointer to memory reallocated
*/
void *
wasm_runtime_realloc(void *ptr, unsigned int size);
/*
* Free memory to runtime memory environment.
*/
void wasm_runtime_free(void *ptr);
/**
* Get the package type of a buffer.

View File

@ -4,7 +4,6 @@
*/
#include "wasm_interp.h"
#include "bh_memory.h"
#include "bh_log.h"
#include "wasm_runtime.h"
#include "wasm_opcode.h"
@ -948,7 +947,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
else {
uint64 total_size = sizeof(uint32) * (uint64)count;
if (total_size >= UINT32_MAX
|| !(depths = wasm_malloc((uint32)total_size))) {
|| !(depths = wasm_runtime_malloc((uint32)total_size))) {
wasm_set_exception(module,
"WASM interp failed: allocate memory failed.");
goto got_exception;
@ -963,7 +962,7 @@ wasm_interp_call_func_bytecode(WASMModuleInstance *module,
depth = depths[didx];
}
if (depths != depth_buf) {
wasm_free(depths);
wasm_runtime_free(depths);
depths = NULL;
}
POP_CSP_N(depth);

View File

@ -4,7 +4,6 @@
*/
#include "wasm_interp.h"
#include "bh_memory.h"
#include "bh_log.h"
#include "wasm_runtime.h"
#include "wasm_opcode.h"

View File

@ -5,7 +5,6 @@
#include "wasm_loader.h"
#include "bh_common.h"
#include "bh_memory.h"
#include "bh_log.h"
#include "wasm.h"
#include "wasm_opcode.h"
@ -295,7 +294,7 @@ const_str_list_insert(const uint8 *str, uint32 len, WASMModule *module,
if (node)
return node->str;
if (!(node = wasm_malloc(sizeof(StringNode) + len + 1))) {
if (!(node = wasm_runtime_malloc(sizeof(StringNode) + len + 1))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: "
"allocate memory failed.");
@ -394,7 +393,7 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->type_count = type_count;
total_size = sizeof(WASMType*) * (uint64)type_count;
if (total_size >= UINT32_MAX
|| !(module->types = wasm_malloc((uint32)total_size))) {
|| !(module->types = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load type section failed: allocate memory failed.");
return false;
@ -429,7 +428,8 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
total_size = offsetof(WASMType, types) +
sizeof(uint8) * (uint64)(param_count + result_count);
if (total_size >= UINT32_MAX
|| !(type = module->types[i] = wasm_malloc((uint32)total_size))) {
|| !(type = module->types[i] =
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load type section failed: allocate memory failed.");
return false;
@ -482,13 +482,16 @@ load_table_import(const uint8 **p_buf, const uint8 *buf_end,
return true;
}
unsigned
wasm_runtime_memory_pool_size();
static bool
load_memory_import(const uint8 **p_buf, const uint8 *buf_end,
WASMMemoryImport *memory,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = bh_memory_pool_size();
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
@ -535,7 +538,7 @@ load_memory(const uint8 **p_buf, const uint8 *buf_end, WASMMemory *memory,
char *error_buf, uint32 error_buf_size)
{
const uint8 *p = *p_buf, *p_end = buf_end;
uint32 pool_size = bh_memory_pool_size();
uint32 pool_size = wasm_runtime_memory_pool_size();
uint32 max_page_count = pool_size * APP_MEMORY_MAX_GLOBAL_HEAP_PERCENT
/ DEFAULT_NUM_BYTES_PER_PAGE;
@ -575,7 +578,7 @@ load_import_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->import_count = import_count;
total_size = sizeof(WASMImport) * (uint64)import_count;
if (total_size >= UINT32_MAX
|| !(module->imports = wasm_malloc((uint32)total_size))) {
|| !(module->imports = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load import section failed: allocate memory failed.");
return false;
@ -817,7 +820,7 @@ init_function_local_offsets(WASMFunction *func,
uint64 total_size = sizeof(uint16) * ((uint64)param_count + local_count);
if (total_size >= UINT32_MAX
|| !(func->local_offsets = wasm_malloc((uint32)total_size))) {
|| !(func->local_offsets = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: allocate memory failed.");
return false;
@ -868,7 +871,7 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
module->function_count = func_count;
total_size = sizeof(WASMFunction*) * (uint64)func_count;
if (total_size >= UINT32_MAX
|| !(module->functions = wasm_malloc((uint32)total_size))) {
|| !(module->functions = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: allocate memory failed.");
return false;
@ -921,7 +924,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
total_size = sizeof(WASMFunction) + (uint64)local_count;
if (total_size >= UINT32_MAX
|| !(func = module->functions[i] = wasm_malloc((uint32)total_size))) {
|| !(func = module->functions[i] =
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load function section failed: "
"allocate memory failed.");
@ -1006,7 +1010,7 @@ load_table_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->table_count = table_count;
total_size = sizeof(WASMTable) * (uint64)table_count;
if (total_size >= UINT32_MAX
|| !(module->tables = wasm_malloc((uint32)total_size))) {
|| !(module->tables = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load table section failed: allocate memory failed.");
return false;
@ -1052,7 +1056,7 @@ load_memory_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->memory_count = memory_count;
total_size = sizeof(WASMMemory) * (uint64)memory_count;
if (total_size >= UINT32_MAX
|| !(module->memories = wasm_malloc((uint32)total_size))) {
|| !(module->memories = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load memory section failed: allocate memory failed.");
return false;
@ -1093,7 +1097,7 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->global_count = global_count;
total_size = sizeof(WASMGlobal) * (uint64)global_count;
if (total_size >= UINT32_MAX
|| !(module->globals = wasm_malloc((uint32)total_size))) {
|| !(module->globals = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load global section failed: "
"allocate memory failed.");
@ -1148,7 +1152,7 @@ load_export_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
module->export_count = export_count;
total_size = sizeof(WASMExport) * (uint64)export_count;
if (total_size >= UINT32_MAX
|| !(module->exports = wasm_malloc((uint32)total_size))) {
|| !(module->exports = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load export section failed: "
"allocate memory failed.");
@ -1242,7 +1246,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m
module->table_seg_count = table_segment_count;
total_size = sizeof(WASMTableSeg) * (uint64)table_segment_count;
if (total_size >= UINT32_MAX
|| !(module->table_segments = wasm_malloc((uint32)total_size))) {
|| !(module->table_segments = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load table segment section failed: "
"allocate memory failed.");
@ -1272,7 +1276,7 @@ load_table_segment_section(const uint8 *buf, const uint8 *buf_end, WASMModule *m
total_size = sizeof(uint32) * (uint64)function_count;
if (total_size >= UINT32_MAX
|| !(table_segment->func_indexes = (uint32 *)
wasm_malloc((uint32)total_size))) {
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load table segment section failed: "
"allocate memory failed.");
@ -1312,7 +1316,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
module->data_seg_count = data_seg_count;
total_size = sizeof(WASMDataSeg*) * (uint64)data_seg_count;
if (total_size >= UINT32_MAX
|| !(module->data_segments = wasm_malloc((uint32)total_size))) {
|| !(module->data_segments = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Load data segment section failed: "
"allocate memory failed.");
@ -1330,7 +1334,7 @@ load_data_segment_section(const uint8 *buf, const uint8 *buf_end,
read_leb_uint32(p, p_end, data_seg_len);
if (!(dataseg = module->data_segments[i] =
wasm_malloc((uint32)sizeof(WASMDataSeg)))) {
wasm_runtime_malloc((uint32)sizeof(WASMDataSeg)))) {
set_error_buf(error_buf, error_buf_size,
"Load data segment section failed: "
"allocate memory failed.");
@ -1564,7 +1568,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
total_size = sizeof(BlockAddr) * (uint64)BLOCK_ADDR_CACHE_SIZE * BLOCK_ADDR_CONFLICT_SIZE;
if (total_size >= UINT32_MAX
|| !(block_addr_cache = wasm_malloc((uint32)total_size))) {
|| !(block_addr_cache = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: allocate memory failed");
return false;
@ -1576,7 +1580,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
if (!wasm_loader_prepare_bytecode(module, func, block_addr_cache, error_buf, error_buf_size))
return false;
}
wasm_free(block_addr_cache);
wasm_runtime_free(block_addr_cache);
/* Resolve llvm auxiliary data/stack/heap info and reset memory info */
if (!module->possible_memory_grow) {
@ -1693,7 +1697,7 @@ load_from_sections(WASMModule *module, WASMSection *sections,
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
static void wasm_loader_free(void *ptr)
{
wasm_free(ptr);
wasm_runtime_free(ptr);
}
#else
#define wasm_loader_free wasm_free
@ -1702,7 +1706,7 @@ static void wasm_loader_free(void *ptr)
static WASMModule*
create_module(char *error_buf, uint32 error_buf_size)
{
WASMModule *module = wasm_malloc(sizeof(WASMModule));
WASMModule *module = wasm_runtime_malloc(sizeof(WASMModule));
if (!module) {
set_error_buf(error_buf, error_buf_size,
@ -1744,7 +1748,7 @@ destroy_sections(WASMSection *section_list)
WASMSection *section = section_list, *next;
while (section) {
next = section->next;
wasm_free(section);
wasm_runtime_free(section);
section = next;
}
}
@ -1783,7 +1787,7 @@ create_sections(const uint8 *buf, uint32 size,
read_leb_uint32(p, p_end, section_size);
CHECK_BUF1(p, p_end, section_size);
if (!(section = wasm_malloc(sizeof(WASMSection)))) {
if (!(section = wasm_runtime_malloc(sizeof(WASMSection)))) {
set_error_buf(error_buf, error_buf_size,
"WASM module load failed: "
"allocate memory failed.");
@ -1877,7 +1881,7 @@ load(const uint8 *buf, uint32 size, WASMModule *module,
WASMModule*
wasm_loader_load(const uint8 *buf, uint32 size, char *error_buf, uint32 error_buf_size)
{
WASMModule *module = wasm_malloc(sizeof(WASMModule));
WASMModule *module = wasm_runtime_malloc(sizeof(WASMModule));
if (!module) {
set_error_buf(error_buf, error_buf_size,
@ -1914,69 +1918,69 @@ wasm_loader_unload(WASMModule *module)
if (module->types) {
for (i = 0; i < module->type_count; i++) {
if (module->types[i])
wasm_free(module->types[i]);
wasm_runtime_free(module->types[i]);
}
wasm_free(module->types);
wasm_runtime_free(module->types);
}
if (module->imports)
wasm_free(module->imports);
wasm_runtime_free(module->imports);
if (module->functions) {
for (i = 0; i < module->function_count; i++) {
if (module->functions[i]) {
if (module->functions[i]->local_offsets)
wasm_free(module->functions[i]->local_offsets);
wasm_runtime_free(module->functions[i]->local_offsets);
#if WASM_ENABLE_FAST_INTERP != 0
if (module->functions[i]->code_compiled)
wasm_free(module->functions[i]->code_compiled);
wasm_runtime_free(module->functions[i]->code_compiled);
if (module->functions[i]->consts)
wasm_free(module->functions[i]->consts);
wasm_runtime_free(module->functions[i]->consts);
#endif
wasm_free(module->functions[i]);
wasm_runtime_free(module->functions[i]);
}
}
wasm_free(module->functions);
wasm_runtime_free(module->functions);
}
if (module->tables)
wasm_free(module->tables);
wasm_runtime_free(module->tables);
if (module->memories)
wasm_free(module->memories);
wasm_runtime_free(module->memories);
if (module->globals)
wasm_free(module->globals);
wasm_runtime_free(module->globals);
if (module->exports)
wasm_free(module->exports);
wasm_runtime_free(module->exports);
if (module->table_segments) {
for (i = 0; i < module->table_seg_count; i++) {
if (module->table_segments[i].func_indexes)
wasm_free(module->table_segments[i].func_indexes);
wasm_runtime_free(module->table_segments[i].func_indexes);
}
wasm_free(module->table_segments);
wasm_runtime_free(module->table_segments);
}
if (module->data_segments) {
for (i = 0; i < module->data_seg_count; i++) {
if (module->data_segments[i])
wasm_free(module->data_segments[i]);
wasm_runtime_free(module->data_segments[i]);
}
wasm_free(module->data_segments);
wasm_runtime_free(module->data_segments);
}
if (module->const_str_list) {
StringNode *node = module->const_str_list, *node_next;
while (node) {
node_next = node->next;
wasm_free(node);
wasm_runtime_free(node);
node = node_next;
}
}
wasm_free(module);
wasm_runtime_free(module);
}
bool
@ -2388,10 +2392,10 @@ memory_realloc(void *mem_old, uint32 size_old, uint32 size_new,
{
uint8 *mem_new;
bh_assert(size_new > size_old);
if ((mem_new = wasm_malloc(size_new))) {
if ((mem_new = wasm_runtime_malloc(size_new))) {
bh_memcpy_s(mem_new, size_new, mem_old, size_old);
memset(mem_new + size_old, 0, size_new - size_old);
wasm_free(mem_old);
wasm_runtime_free(mem_old);
}
else {
set_error_buf(error_buf, error_buf_size,
@ -2454,7 +2458,7 @@ static void free_label_patch_list(BranchBlock *frame_csp)
BranchBlockPatch *next;
while (label_patch != NULL) {
next = label_patch->next;
wasm_free(label_patch);
wasm_runtime_free(label_patch);
label_patch = next;
}
frame_csp->patch_list = NULL;
@ -2524,20 +2528,20 @@ static void wasm_loader_ctx_destroy(WASMLoaderContext *ctx)
{
if (ctx) {
if (ctx->frame_ref_bottom)
wasm_free(ctx->frame_ref_bottom);
wasm_runtime_free(ctx->frame_ref_bottom);
if (ctx->frame_csp_bottom) {
#if WASM_ENABLE_FAST_INTERP != 0
free_all_label_patch_lists(ctx->frame_csp_bottom, ctx->csp_num);
#endif
wasm_free(ctx->frame_csp_bottom);
wasm_runtime_free(ctx->frame_csp_bottom);
}
#if WASM_ENABLE_FAST_INTERP != 0
if (ctx->frame_offset_bottom)
wasm_free(ctx->frame_offset_bottom);
wasm_runtime_free(ctx->frame_offset_bottom);
if (ctx->const_buf)
wasm_free(ctx->const_buf);
wasm_runtime_free(ctx->const_buf);
#endif
wasm_free(ctx);
wasm_runtime_free(ctx);
}
}
@ -2545,14 +2549,14 @@ static WASMLoaderContext*
wasm_loader_ctx_init(WASMFunction *func)
{
WASMLoaderContext *loader_ctx =
wasm_malloc(sizeof(WASMLoaderContext));
wasm_runtime_malloc(sizeof(WASMLoaderContext));
if (!loader_ctx)
return false;
memset(loader_ctx, 0, sizeof(WASMLoaderContext));
loader_ctx->frame_ref_size = 32;
if (!(loader_ctx->frame_ref_bottom = loader_ctx->frame_ref =
wasm_malloc(loader_ctx->frame_ref_size)))
wasm_runtime_malloc(loader_ctx->frame_ref_size)))
goto fail;
memset(loader_ctx->frame_ref_bottom, 0, loader_ctx->frame_ref_size);
loader_ctx->frame_ref_boundary = loader_ctx->frame_ref_bottom +
@ -2560,7 +2564,7 @@ wasm_loader_ctx_init(WASMFunction *func)
loader_ctx->frame_csp_size = sizeof(BranchBlock) * 8;
if (!(loader_ctx->frame_csp_bottom = loader_ctx->frame_csp =
wasm_malloc(loader_ctx->frame_csp_size)))
wasm_runtime_malloc(loader_ctx->frame_csp_size)))
goto fail;
memset(loader_ctx->frame_csp_bottom, 0, loader_ctx->frame_csp_size);
loader_ctx->frame_csp_boundary = loader_ctx->frame_csp_bottom + 8;
@ -2568,7 +2572,7 @@ wasm_loader_ctx_init(WASMFunction *func)
#if WASM_ENABLE_FAST_INTERP != 0
loader_ctx->frame_offset_size = sizeof(int16) * 32;
if (!(loader_ctx->frame_offset_bottom = loader_ctx->frame_offset =
wasm_malloc(loader_ctx->frame_offset_size)))
wasm_runtime_malloc(loader_ctx->frame_offset_size)))
goto fail;
memset(loader_ctx->frame_offset_bottom, 0,
loader_ctx->frame_offset_size);
@ -2576,7 +2580,7 @@ wasm_loader_ctx_init(WASMFunction *func)
loader_ctx->num_const = 0;
loader_ctx->const_buf_size = sizeof(Const) * 8;
if (!(loader_ctx->const_buf = wasm_malloc(loader_ctx->const_buf_size)))
if (!(loader_ctx->const_buf = wasm_runtime_malloc(loader_ctx->const_buf_size)))
goto fail;
memset(loader_ctx->const_buf, 0, loader_ctx->const_buf_size);
@ -2715,7 +2719,7 @@ wasm_loader_check_br(WASMLoaderContext *ctx, uint32 depth,
static bool
wasm_loader_ctx_reinit(WASMLoaderContext *ctx)
{
if (!(ctx->p_code_compiled = wasm_malloc(ctx->code_compiled_size)))
if (!(ctx->p_code_compiled = wasm_runtime_malloc(ctx->code_compiled_size)))
return false;
memset(ctx->p_code_compiled, 0, ctx->code_compiled_size);
ctx->p_code_compiled_end = ctx->p_code_compiled +
@ -2804,7 +2808,7 @@ add_label_patch_to_list(BranchBlock *frame_csp,
uint8 patch_type, uint8 *p_code_compiled,
char *error_buf, uint32 error_buf_size)
{
BranchBlockPatch *patch = wasm_malloc(sizeof(BranchBlockPatch));
BranchBlockPatch *patch = wasm_runtime_malloc(sizeof(BranchBlockPatch));
if (!patch) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: "
@ -2845,7 +2849,7 @@ apply_label_patch(WASMLoaderContext *ctx, uint8 depth,
else {
node_prev->next = node_next;
}
wasm_free(node);
wasm_runtime_free(node);
}
else {
node_prev = node;
@ -4589,7 +4593,8 @@ handle_next_reachable_block:
goto re_scan;
func->const_cell_num = loader_ctx->const_cell_num;
if (!(func->consts = func_const = wasm_malloc(func->const_cell_num * 4))) {
if (!(func->consts = func_const =
wasm_runtime_malloc(func->const_cell_num * 4))) {
set_error_buf(error_buf, error_buf_size,
"WASM loader prepare bytecode failed: "
"allocate memory failed");

View File

@ -51,10 +51,10 @@ memories_deinstantiate(WASMMemoryInstance **memories, uint32 count)
if (memories[i]) {
if (memories[i]->heap_handle)
mem_allocator_destroy(memories[i]->heap_handle);
wasm_free(memories[i]->heap_data);
wasm_free(memories[i]);
wasm_runtime_free(memories[i]->heap_data);
wasm_runtime_free(memories[i]);
}
wasm_free(memories);
wasm_runtime_free(memories);
}
}
@ -72,7 +72,7 @@ memory_instantiate(uint32 num_bytes_per_page,
/* Allocate memory space, addr data and global data */
if (total_size >= UINT32_MAX
|| !(memory = wasm_malloc((uint32)total_size))) {
|| !(memory = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate memory failed: allocate memory failed.");
return NULL;
@ -92,7 +92,7 @@ memory_instantiate(uint32 num_bytes_per_page,
memory->end_addr = memory->global_data + global_data_size;
/* Allocate heap space */
if (!(memory->heap_data = wasm_malloc(heap_size))) {
if (!(memory->heap_data = wasm_runtime_malloc(heap_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate memory failed: allocate memory failed.");
goto fail1;
@ -114,10 +114,10 @@ memory_instantiate(uint32 num_bytes_per_page,
return memory;
fail2:
wasm_free(memory->heap_data);
wasm_runtime_free(memory->heap_data);
fail1:
wasm_free(memory);
wasm_runtime_free(memory);
return NULL;
}
@ -141,7 +141,7 @@ memories_instantiate(const WASMModule *module,
total_size = sizeof(WASMMemoryInstance*) * (uint64)memory_count;
if (total_size >= UINT32_MAX
|| !(memories = wasm_malloc((uint32)total_size))) {
|| !(memories = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate memory failed: "
"allocate memory failed.");
@ -210,8 +210,8 @@ tables_deinstantiate(WASMTableInstance **tables, uint32 count)
if (tables) {
for (i = 0; i < count; i++)
if (tables[i])
wasm_free(tables[i]);
wasm_free(tables);
wasm_runtime_free(tables[i]);
wasm_runtime_free(tables);
}
}
@ -229,7 +229,7 @@ tables_instantiate(const WASMModule *module,
WASMTableInstance **tables, *table;
if (total_size >= UINT32_MAX
|| !(tables = wasm_malloc((uint32)total_size))) {
|| !(tables = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate table failed: "
"allocate memory failed.");
@ -244,7 +244,8 @@ tables_instantiate(const WASMModule *module,
total_size = offsetof(WASMTableInstance, base_addr) +
sizeof(uint32) * (uint64)import->u.table.init_size;
if (total_size >= UINT32_MAX
|| !(table = tables[table_index++] = wasm_malloc((uint32)total_size))) {
|| !(table = tables[table_index++] =
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate table failed: "
"allocate memory failed.");
@ -264,7 +265,8 @@ tables_instantiate(const WASMModule *module,
total_size = offsetof(WASMTableInstance, base_addr) +
sizeof(uint32) * (uint64)module->tables[i].init_size;
if (total_size >= UINT32_MAX
|| !(table = tables[table_index++] = wasm_malloc((uint32)total_size))) {
|| !(table = tables[table_index++] =
wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate table failed: "
"allocate memory failed.");
@ -290,7 +292,7 @@ static void
functions_deinstantiate(WASMFunctionInstance *functions, uint32 count)
{
if (functions) {
wasm_free(functions);
wasm_runtime_free(functions);
}
}
@ -308,7 +310,7 @@ functions_instantiate(const WASMModule *module,
WASMFunctionInstance *functions, *function;
if (total_size >= UINT32_MAX
|| !(functions = wasm_malloc((uint32)total_size))) {
|| !(functions = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate function failed: "
"allocate memory failed.");
@ -373,7 +375,7 @@ static void
globals_deinstantiate(WASMGlobalInstance *globals)
{
if (globals)
wasm_free(globals);
wasm_runtime_free(globals);
}
/**
@ -392,7 +394,7 @@ globals_instantiate(const WASMModule *module,
WASMGlobalInstance *globals, *global;
if (total_size >= UINT32_MAX
|| !(globals = wasm_malloc((uint32)total_size))) {
|| !(globals = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate global failed: "
"allocate memory failed.");
@ -505,7 +507,7 @@ static void
export_functions_deinstantiate(WASMExportFuncInstance *functions)
{
if (functions)
wasm_free(functions);
wasm_runtime_free(functions);
}
/**
@ -523,7 +525,7 @@ export_functions_instantiate(const WASMModule *module,
uint64 total_size = sizeof(WASMExportFuncInstance) * (uint64)export_func_count;
if (total_size >= UINT32_MAX
|| !(export_func = export_funcs = wasm_malloc((uint32)total_size))) {
|| !(export_func = export_funcs = wasm_runtime_malloc((uint32)total_size))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate export function failed: "
"allocate memory failed.");
@ -623,7 +625,7 @@ wasm_instantiate(WASMModule *module,
return NULL;
/* Allocate the memory */
if (!(module_inst = wasm_malloc((uint32)sizeof(WASMModuleInstance)))) {
if (!(module_inst = wasm_runtime_malloc((uint32)sizeof(WASMModuleInstance)))) {
set_error_buf(error_buf, error_buf_size,
"Instantiate module failed: allocate memory failed.");
globals_deinstantiate(globals);
@ -850,7 +852,7 @@ wasm_deinstantiate(WASMModuleInstance *module_inst)
globals_deinstantiate(module_inst->globals);
export_functions_deinstantiate(module_inst->export_functions);
wasm_free(module_inst);
wasm_runtime_free(module_inst);
}
WASMFunctionInstance*
@ -1128,8 +1130,8 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
return false;
}
if (!(new_memory = wasm_realloc(memory, (uint32)total_size))) {
if (!(new_memory = wasm_malloc((uint32)total_size))) {
if (!(new_memory = wasm_runtime_realloc(memory, (uint32)total_size))) {
if (!(new_memory = wasm_runtime_malloc((uint32)total_size))) {
wasm_set_exception(module, "fail to enlarge memory.");
return false;
}
@ -1138,7 +1140,7 @@ wasm_enlarge_memory(WASMModuleInstance *module, uint32 inc_page_count)
(uint8*)memory, total_size_old);
memset((uint8*)new_memory + total_size_old,
0, (uint32)total_size - total_size_old);
wasm_free(memory);
wasm_runtime_free(memory);
}
new_memory->cur_page_count = total_page_count;

View File

@ -78,12 +78,12 @@ wasi_args_get(wasm_exec_env_t exec_env, int32 *argv_offsets, char *argv_buf)
total_size = sizeof(char*) * ((uint64)argc + 1);
if (total_size >= UINT32_MAX
|| !(argv = bh_malloc((uint32)total_size)))
|| !(argv = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_args_get(wasi_ctx->argv_environ, argv, argv_buf);
if (err) {
bh_free(argv);
wasm_runtime_free(argv);
return err;
}
@ -91,7 +91,7 @@ wasi_args_get(wasm_exec_env_t exec_env, int32 *argv_offsets, char *argv_buf)
argv_offsets[i] = addr_native_to_app(argv[i]);
argv_offsets[argc] = 0;
bh_free(argv);
wasm_runtime_free(argv);
return 0;
}
@ -171,12 +171,12 @@ wasi_environ_get(wasm_exec_env_t exec_env,
total_size = sizeof(char*) * (((uint64)environ_count + 1));
if (total_size >= UINT32_MAX
|| !(environs = bh_malloc((uint32)total_size)))
|| !(environs = wasm_runtime_malloc((uint32)total_size)))
return (wasi_errno_t)-1;
err = wasmtime_ssp_environ_get(wasi_ctx->argv_environ, environs, environ_buf);
if (err) {
bh_free(environs);
wasm_runtime_free(environs);
return err;
}
@ -184,7 +184,7 @@ wasi_environ_get(wasm_exec_env_t exec_env,
environ_offsets[i] = addr_native_to_app(environs[i]);
environ_offsets[environ_count] = 0;
bh_free(environs);
wasm_runtime_free(environs);
return 0;
}

View File

@ -47,7 +47,6 @@
#include "str.h"
#include "bh_common.h"
#include "bh_memory.h"
#if 0 /* TODO: -std=gnu99 causes compile error, comment them first */
// struct iovec must have the same layout as __wasi_iovec_t.
@ -278,7 +277,7 @@ static bool fd_prestats_grow(
size *= 2;
// Grow the file descriptor table's allocation.
struct fd_prestat *prestats = bh_malloc((uint32)(sizeof(*prestats) * size));
struct fd_prestat *prestats = wasm_runtime_malloc((uint32)(sizeof(*prestats) * size));
if (prestats == NULL)
return false;
@ -288,7 +287,7 @@ static bool fd_prestats_grow(
}
if (pt->prestats)
bh_free(pt->prestats);
wasm_runtime_free(pt->prestats);
// Mark all new file descriptors as unused.
for (size_t i = pt->size; i < size; ++i)
@ -408,7 +407,7 @@ static bool fd_table_grow(
size *= 2;
// Grow the file descriptor table's allocation.
struct fd_entry *entries = bh_malloc((uint32)(sizeof(*entries) * size));
struct fd_entry *entries = wasm_runtime_malloc((uint32)(sizeof(*entries) * size));
if (entries == NULL)
return false;
@ -418,7 +417,7 @@ static bool fd_table_grow(
}
if (ft->entries)
bh_free(ft->entries);
wasm_runtime_free(ft->entries);
// Mark all new file descriptors as unused.
for (size_t i = ft->size; i < size; ++i)
@ -434,7 +433,7 @@ static __wasi_errno_t fd_object_new(
__wasi_filetype_t type,
struct fd_object **fo
) TRYLOCKS_SHARED(0, (*fo)->refcount) {
*fo = bh_malloc(sizeof(**fo));
*fo = wasm_runtime_malloc(sizeof(**fo));
if (*fo == NULL)
return __WASI_ENOMEM;
refcount_init(&(*fo)->refcount, 1);
@ -585,7 +584,7 @@ static void fd_object_release(
CLOSE_NON_STD_FD(fd_number(fo));
break;
}
bh_free(fo);
wasm_runtime_free(fo);
}
}
@ -878,7 +877,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(
size_t totalsize = 0;
for (size_t i = 0; i < iovcnt; ++i)
totalsize += iov[i].buf_len;
char *buf = bh_malloc(totalsize);
char *buf = wasm_runtime_malloc(totalsize);
if (buf == NULL) {
fd_object_release(fo);
return __WASI_ENOMEM;
@ -888,7 +887,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(
ssize_t len = pread(fd_number(fo), buf, totalsize, offset);
fd_object_release(fo);
if (len < 0) {
bh_free(buf);
wasm_runtime_free(buf);
return convert_errno(errno);
}
@ -903,7 +902,7 @@ __wasi_errno_t wasmtime_ssp_fd_pread(
break;
}
}
bh_free(buf);
wasm_runtime_free(buf);
*nread = len;
return 0;
}
@ -940,7 +939,7 @@ __wasi_errno_t wasmtime_ssp_fd_pwrite(
size_t totalsize = 0;
for (size_t i = 0; i < iovcnt; ++i)
totalsize += iov[i].buf_len;
char *buf = bh_malloc(totalsize);
char *buf = wasm_runtime_malloc(totalsize);
if (buf == NULL) {
fd_object_release(fo);
return __WASI_ENOMEM;
@ -954,7 +953,7 @@ __wasi_errno_t wasmtime_ssp_fd_pwrite(
// Perform a single write operation.
len = pwrite(fd_number(fo), buf, totalsize, offset);
bh_free(buf);
wasm_runtime_free(buf);
}
#endif
fd_object_release(fo);
@ -1377,23 +1376,23 @@ static char *readlinkat_dup(
size_t len_org = len;
for (;;) {
char *newbuf = bh_malloc((uint32)len);
char *newbuf = wasm_runtime_malloc((uint32)len);
if (newbuf == NULL) {
if (buf)
bh_free(buf);
wasm_runtime_free(buf);
return NULL;
}
if (buf != NULL) {
bh_memcpy_s(newbuf, (uint32)len, buf, (uint32)len_org);
bh_free(buf);
wasm_runtime_free(buf);
}
buf = newbuf;
ssize_t ret = readlinkat(fd, path, buf, len);
if (ret < 0) {
bh_free(buf);
wasm_runtime_free(buf);
return NULL;
}
if ((size_t)ret + 1 < len) {
@ -1444,7 +1443,7 @@ static __wasi_errno_t path_get(
__wasi_errno_t error =
fd_object_get(curfds, &fo, fd, rights_base, rights_inheriting);
if (error != 0) {
bh_free(path);
wasm_runtime_free(path);
return error;
}
@ -1585,7 +1584,7 @@ static __wasi_errno_t path_get(
// when called on paths like ".", "a/..", but also if the path
// had trailing slashes and the caller is not interested in the
// name of the pathname component.
bh_free(paths_start[0]);
wasm_runtime_free(paths_start[0]);
pa->path = ".";
pa->path_start = NULL;
goto success;
@ -1593,7 +1592,7 @@ static __wasi_errno_t path_get(
// Finished expanding symlink. Continue processing along the
// original path.
bh_free(paths_start[curpath--]);
wasm_runtime_free(paths_start[curpath--]);
}
continue;
@ -1601,7 +1600,7 @@ static __wasi_errno_t path_get(
// Prevent infinite loops by placing an upper limit on the number of
// symlink expansions.
if (++expansions == 128) {
bh_free(symlink);
wasm_runtime_free(symlink);
error = __WASI_ELOOP;
goto fail;
}
@ -1609,10 +1608,10 @@ static __wasi_errno_t path_get(
if (*paths[curpath] == '\0') {
// The original path already finished processing. Replace it by
// this symlink entirely.
bh_free(paths_start[curpath]);
wasm_runtime_free(paths_start[curpath]);
} else if (curpath + 1 == sizeof(paths) / sizeof(paths[0])) {
// Too many nested symlinks. Stop processing.
bh_free(symlink);
wasm_runtime_free(symlink);
error = __WASI_ELOOP;
goto fail;
} else {
@ -1644,7 +1643,7 @@ fail:
for (size_t i = 1; i <= curfd; ++i)
close(fds[i]);
for (size_t i = 0; i <= curpath; ++i)
bh_free(paths_start[i]);
wasm_runtime_free(paths_start[i]);
fd_object_release(fo);
return error;
#endif
@ -1668,7 +1667,7 @@ static __wasi_errno_t path_get_nofollow(
static void path_put(
struct path_access *pa
) UNLOCKS(pa->fd_object->refcount) {
bh_free(pa->path_start);
wasm_runtime_free(pa->path_start);
if (fd_number(pa->fd_object) != pa->fd)
close(pa->fd);
fd_object_release(pa->fd_object);
@ -1770,12 +1769,12 @@ __wasi_errno_t wasmtime_ssp_path_link(
rwlock_rdlock(&prestats->lock);
if (!validate_path(target, prestats)) {
rwlock_unlock(&prestats->lock);
bh_free(target);
wasm_runtime_free(target);
return __WASI_EBADF;
}
rwlock_unlock(&prestats->lock);
ret = symlinkat(target, new_pa.fd, new_pa.path);
bh_free(target);
wasm_runtime_free(target);
}
}
path_put(&old_pa);
@ -2312,21 +2311,21 @@ __wasi_errno_t wasmtime_ssp_path_symlink(
__wasi_errno_t error = path_get_nofollow(curfds,
&pa, fd, new_path, new_path_len, __WASI_RIGHT_PATH_SYMLINK, 0, true);
if (error != 0) {
bh_free(target);
wasm_runtime_free(target);
return error;
}
rwlock_rdlock(&prestats->lock);
if (!validate_path(target, prestats)) {
rwlock_unlock(&prestats->lock);
bh_free(target);
wasm_runtime_free(target);
return __WASI_EBADF;
}
rwlock_unlock(&prestats->lock);
int ret = symlinkat(target, pa.fd, pa.path);
path_put(&pa);
bh_free(target);
wasm_runtime_free(target);
if (ret < 0)
return convert_errno(errno);
return 0;
@ -2479,12 +2478,12 @@ __wasi_errno_t wasmtime_ssp_poll_oneoff(
// __WASI_EVENTTYPE_FD_WRITE entries. There may be up to one
// __WASI_EVENTTYPE_CLOCK entry to act as a timeout. These are also
// the subscriptions generate by cloudlibc's poll() and select().
struct fd_object **fos = bh_malloc((uint32)(nsubscriptions * sizeof(*fos)));
struct fd_object **fos = wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*fos)));
if (fos == NULL)
return __WASI_ENOMEM;
struct pollfd *pfds = bh_malloc((uint32)(nsubscriptions * sizeof(*pfds)));
struct pollfd *pfds = wasm_runtime_malloc((uint32)(nsubscriptions * sizeof(*pfds)));
if (pfds == NULL) {
bh_free(fos);
wasm_runtime_free(fos);
return __WASI_ENOMEM;
}
@ -2623,8 +2622,8 @@ __wasi_errno_t wasmtime_ssp_poll_oneoff(
for (size_t i = 0; i < nsubscriptions; ++i)
if (fos[i] != NULL)
fd_object_release(fos[i]);
bh_free(fos);
bh_free(pfds);
wasm_runtime_free(fos);
wasm_runtime_free(pfds);
return error;
}
@ -2866,12 +2865,12 @@ bool argv_environ_init(struct argv_environ_values *argv_environ,
total_size = sizeof(char *) * (uint64)argv_offsets_len;
if (total_size >= UINT32_MAX
|| !(argv_environ->argv = bh_malloc((uint32)total_size)))
|| !(argv_environ->argv = wasm_runtime_malloc((uint32)total_size)))
return false;
if (argv_buf_len >= UINT32_MAX
|| !(argv_environ->argv_buf = bh_malloc((uint32)argv_buf_len)))
|| !(argv_environ->argv_buf = wasm_runtime_malloc((uint32)argv_buf_len)))
goto fail1;
for (i = 0; i < argv_offsets_len; ++i) {
@ -2885,11 +2884,11 @@ bool argv_environ_init(struct argv_environ_values *argv_environ,
total_size = sizeof(char *) * (uint64)environ_offsets_len;
if (total_size >= UINT32_MAX
|| !(argv_environ->environ = bh_malloc((uint32)total_size)))
|| !(argv_environ->environ = wasm_runtime_malloc((uint32)total_size)))
goto fail2;
if (environ_buf_len >= UINT32_MAX
|| !(argv_environ->environ_buf = bh_malloc((uint32)environ_buf_len)))
|| !(argv_environ->environ_buf = wasm_runtime_malloc((uint32)environ_buf_len)))
goto fail3;
for (i = 0; i < environ_offsets_len; ++i) {
@ -2901,11 +2900,11 @@ bool argv_environ_init(struct argv_environ_values *argv_environ,
return true;
fail3:
bh_free(argv_environ->environ);
wasm_runtime_free(argv_environ->environ);
fail2:
bh_free(argv_environ->argv_buf);
wasm_runtime_free(argv_environ->argv_buf);
fail1:
bh_free(argv_environ->argv);
wasm_runtime_free(argv_environ->argv);
memset(argv_environ, 0, sizeof(struct argv_environ_values));
return false;
@ -2914,13 +2913,13 @@ fail1:
void argv_environ_destroy(struct argv_environ_values *argv_environ)
{
if (argv_environ->argv_buf)
bh_free(argv_environ->argv_buf);
wasm_runtime_free(argv_environ->argv_buf);
if (argv_environ->argv)
bh_free(argv_environ->argv);
wasm_runtime_free(argv_environ->argv);
if (argv_environ->environ_buf)
bh_free(argv_environ->environ_buf);
wasm_runtime_free(argv_environ->environ_buf);
if (argv_environ->environ)
bh_free(argv_environ->environ);
wasm_runtime_free(argv_environ->environ);
}
void fd_table_destroy(struct fd_table *ft)
@ -2931,7 +2930,7 @@ void fd_table_destroy(struct fd_table *ft)
fd_object_release(ft->entries[i].object);
}
}
bh_free(ft->entries);
wasm_runtime_free(ft->entries);
}
}
@ -2940,9 +2939,9 @@ void fd_prestats_destroy(struct fd_prestats *pt)
if (pt->prestats) {
for (uint32 i = 0; i < pt->size; i++) {
if (pt->prestats[i].dir != NULL) {
bh_free((void*)pt->prestats[i].dir);
wasm_runtime_free((void*)pt->prestats[i].dir);
}
}
bh_free(pt->prestats);
wasm_runtime_free(pt->prestats);
}
}