Re-org memory allocation interfaces, add --stack-size and --heap-size option (#193)
This commit is contained in:
@ -5,6 +5,9 @@ NAME := iwasm
|
||||
IWASM_ROOT := wamr/core/iwasm
|
||||
SHARED_ROOT := wamr/core/shared
|
||||
|
||||
GLOBAL_DEFINES += BH_MALLOC=wasm_runtime_malloc
|
||||
GLOBAL_DEFINES += BH_FREE=wasm_runtime_free
|
||||
|
||||
# Change it to THUMBV7M if you want to build for developerkit
|
||||
WAMR_BUILD_TARGET := X86_32
|
||||
|
||||
@ -76,7 +79,6 @@ $(NAME)_SOURCES := ${SHARED_ROOT}/platform/alios/bh_assert.c \
|
||||
${SHARED_ROOT}/platform/alios/bh_platform_log.c \
|
||||
${SHARED_ROOT}/platform/alios/bh_thread.c \
|
||||
${SHARED_ROOT}/platform/alios/bh_time.c \
|
||||
${SHARED_ROOT}/mem-alloc/bh_memory.c \
|
||||
${SHARED_ROOT}/mem-alloc/mem_alloc.c \
|
||||
${SHARED_ROOT}/mem-alloc/ems/ems_kfc.c \
|
||||
${SHARED_ROOT}/mem-alloc/ems/ems_alloc.c \
|
||||
@ -91,6 +93,7 @@ $(NAME)_SOURCES := ${SHARED_ROOT}/platform/alios/bh_assert.c \
|
||||
${IWASM_ROOT}/common/wasm_runtime_common.c \
|
||||
${IWASM_ROOT}/common/wasm_native.c \
|
||||
${IWASM_ROOT}/common/wasm_exec_env.c \
|
||||
${IWASM_ROOT}/common/wasm_memory.c \
|
||||
${IWASM_ROOT}/common/arch/${INVOKE_NATIVE} \
|
||||
src/main.c
|
||||
|
||||
|
||||
@ -9,7 +9,6 @@
|
||||
#include "bh_log.h"
|
||||
#include "bh_platform_log.h"
|
||||
#include "wasm_export.h"
|
||||
#include "bh_memory.h"
|
||||
#include "test_wasm.h"
|
||||
|
||||
static int app_argc;
|
||||
@ -48,6 +47,7 @@ void iwasm_main(void *arg1)
|
||||
uint32 wasm_file_size;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128];
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
int log_verbose_level = 2;
|
||||
@ -55,15 +55,17 @@ void iwasm_main(void *arg1)
|
||||
|
||||
(void) arg1;
|
||||
|
||||
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
|
||||
!= 0) {
|
||||
bh_printf("Init global heap failed.\n");
|
||||
return;
|
||||
}
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_init())
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
bh_printf("Init runtime environment failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
bh_log_set_verbose_level(log_verbose_level);
|
||||
@ -77,14 +79,17 @@ void iwasm_main(void *arg1)
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail2;
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module, 8 * 1024,
|
||||
8 * 1024, error_buf, sizeof(error_buf)))) {
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
|
||||
8 * 1024,
|
||||
8 * 1024,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
app_instance_main(wasm_module_inst);
|
||||
@ -92,15 +97,13 @@ void iwasm_main(void *arg1)
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail3:
|
||||
fail2:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail2:
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
|
||||
fail1: bh_memory_destroy();
|
||||
}
|
||||
|
||||
#define DEFAULT_THREAD_STACKSIZE (6 * 1024)
|
||||
|
||||
@ -75,27 +75,25 @@ static unsigned char wasm_test_file[] = { 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
|
||||
char error_buf[128] = {0};
|
||||
|
||||
void *(*malloc_func)(size_t) = &malloc;
|
||||
void (*free_func)(void *) = &free;
|
||||
LOGI("bh_memory_init_with_allocator");
|
||||
if (bh_memory_init_with_allocator((void *) malloc_func, (void *) free_func)) {
|
||||
LOGI("Init memory with memory allocator failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
|
||||
RuntimeInitArgs init_args;
|
||||
uint wasm_file_size = 0;
|
||||
uint8_t *wasm_file_buf = NULL;
|
||||
char error_buf[128] = {0};
|
||||
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Allocator;
|
||||
init_args.mem_alloc_option.allocator.malloc_func = (void*)malloc;
|
||||
init_args.mem_alloc_option.allocator.realloc_func = (void*)realloc;
|
||||
init_args.mem_alloc_option.allocator.free_func = (void*)free;
|
||||
|
||||
LOGI("wasm_runtime_full_init");
|
||||
/* initialize runtime environment */
|
||||
LOGI("wasm_runtime_init");
|
||||
if (!wasm_runtime_init()) {
|
||||
LOGI("goto fail1\n");
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
LOGI("Init runtime failed.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// set log level to INFO
|
||||
@ -112,8 +110,8 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
LOGI("in wasm_runtime_load %s\n", error_buf);
|
||||
LOGI("goto fail3\n");
|
||||
goto fail3;
|
||||
LOGI("goto fail1\n");
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
@ -124,8 +122,8 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
LOGI("%s\n", error_buf);
|
||||
LOGI("goto fail4\n");
|
||||
goto fail4;
|
||||
LOGI("goto fail2\n");
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
LOGI("run main() of the application");
|
||||
@ -135,23 +133,18 @@ Java_com_intel_wasm_api_Runtime_run(JNIEnv *env, jclass thiz) {
|
||||
LOGI("wasm_runtime_deinstantiate");
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail4:
|
||||
fail2:
|
||||
/* unload the module */
|
||||
LOGI("wasm_runtime_unload");
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail3:
|
||||
fail1:
|
||||
// in our case, we don't need a free, but it is not a typical one
|
||||
/* free the file buffer */
|
||||
//bh_free((void *) wasm_file_buf);
|
||||
|
||||
fail2:
|
||||
/* destroy runtime environment */
|
||||
LOGI("wasm_runtime_destroy");
|
||||
wasm_runtime_destroy();
|
||||
|
||||
fail1:
|
||||
LOGI("bh_memory_destroy");
|
||||
bh_memory_destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
static int app_argc;
|
||||
@ -24,9 +23,11 @@ static int print_help()
|
||||
bh_printf(" -f|--function name Specify function name to run in module\n"
|
||||
" rather than main\n");
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
bh_printf(" -v=X Set log verbose level (0 to 5, default is 2),\n"
|
||||
bh_printf(" -v=n Set log verbose level (0 to 5, default is 2),\n"
|
||||
" larger level with more log\n");
|
||||
#endif
|
||||
bh_printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
||||
" that runs commands in the form of `FUNC ARG...`\n");
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
@ -153,8 +154,10 @@ int main(int argc, char *argv[])
|
||||
const char *func_name = NULL;
|
||||
uint8 *wasm_file_buf = NULL;
|
||||
uint32 wasm_file_size;
|
||||
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128] = { 0 };
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
int log_verbose_level = 2;
|
||||
@ -186,6 +189,16 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
else if (!strcmp(argv[0], "--repl"))
|
||||
is_repl_mode = true;
|
||||
else if (!strncmp(argv[0], "--stack-size=", 13)) {
|
||||
if (argv[0][13] == '\0')
|
||||
return print_help();
|
||||
stack_size = atoi(argv[0] + 13);
|
||||
}
|
||||
else if (!strncmp(argv[0], "--heap-size=", 12)) {
|
||||
if (argv[0][12] == '\0')
|
||||
return print_help();
|
||||
heap_size = atoi(argv[0] + 12);
|
||||
}
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
else if (!strncmp(argv[0], "--dir=", 6)) {
|
||||
if (argv[0][6] == '\0')
|
||||
@ -228,35 +241,37 @@ int main(int argc, char *argv[])
|
||||
app_argc = argc;
|
||||
app_argv = argv;
|
||||
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
#if USE_GLOBAL_HEAP_BUF != 0
|
||||
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
|
||||
!= 0) {
|
||||
bh_printf("Init memory with global heap buffer failed.\n");
|
||||
return -1;
|
||||
}
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
#else
|
||||
if (bh_memory_init_with_allocator(malloc, free)) {
|
||||
bh_printf("Init memory with memory allocator failed.\n");
|
||||
return -1;
|
||||
}
|
||||
init_args.mem_alloc_type = Alloc_With_Allocator;
|
||||
init_args.mem_alloc_option.allocator.malloc_func = malloc;
|
||||
init_args.mem_alloc_option.allocator.realloc_func = realloc;
|
||||
init_args.mem_alloc_option.allocator.free_func = free;
|
||||
#endif
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_init())
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
bh_printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bh_log_set_verbose_level(log_verbose_level);
|
||||
|
||||
/* load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file,
|
||||
&wasm_file_size)))
|
||||
goto fail2;
|
||||
goto fail1;
|
||||
|
||||
/* load WASM module */
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
@ -269,12 +284,12 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* instantiate the module */
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
|
||||
64 * 1024, /* stack size */
|
||||
64 * 1024, /* heap size */
|
||||
stack_size,
|
||||
heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail4;
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
if (is_repl_mode)
|
||||
@ -287,20 +302,17 @@ int main(int argc, char *argv[])
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail4:
|
||||
fail3:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail3:
|
||||
/* free the file buffer */
|
||||
bh_free(wasm_file_buf);
|
||||
|
||||
fail2:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
/* free the file buffer */
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
|
||||
fail1:
|
||||
bh_memory_destroy();
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -7,7 +7,6 @@
|
||||
#include <string.h>
|
||||
#include "Enclave_t.h"
|
||||
#include "test_wasm.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
static char global_heap_buf[2* 1024 * 1024] = { 0 };
|
||||
@ -50,17 +49,21 @@ void ecall_iwasm_main()
|
||||
int wasm_file_size;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128];
|
||||
|
||||
if (bh_memory_init_with_pool(global_heap_buf,
|
||||
sizeof(global_heap_buf)) != 0) {
|
||||
ocall_print("Init global heap failed.\n");
|
||||
return;
|
||||
}
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_init())
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
ocall_print("Init runtime environment failed.");
|
||||
ocall_print("\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* load WASM byte buffer from byte buffer of include file */
|
||||
wasm_file_buf = (uint8_t*) wasm_test_file;
|
||||
@ -71,7 +74,7 @@ void ecall_iwasm_main()
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
ocall_print(error_buf);
|
||||
ocall_print("\n");
|
||||
goto fail2;
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
@ -82,7 +85,7 @@ void ecall_iwasm_main()
|
||||
sizeof(error_buf)))) {
|
||||
ocall_print(error_buf);
|
||||
ocall_print("\n");
|
||||
goto fail3;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
/* execute the main function of wasm app */
|
||||
@ -91,15 +94,12 @@ void ecall_iwasm_main()
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail3:
|
||||
fail2:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail2:
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
|
||||
fail1:
|
||||
bh_memory_destroy();
|
||||
}
|
||||
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
static int app_argc;
|
||||
@ -24,9 +23,11 @@ static int print_help()
|
||||
bh_printf(" -f|--function name Specify function name to run in module\n"
|
||||
" rather than main\n");
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
bh_printf(" -v=X Set log verbose level (0 to 5, default is 2),\n"
|
||||
bh_printf(" -v=n Set log verbose level (0 to 5, default is 2),\n"
|
||||
" larger level with more log\n");
|
||||
#endif
|
||||
bh_printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
||||
" that runs commands in the form of `FUNC ARG...`\n");
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
@ -153,6 +154,7 @@ int main(int argc, char *argv[])
|
||||
const char *func_name = NULL;
|
||||
uint8 *wasm_file_buf = NULL;
|
||||
uint32 wasm_file_size;
|
||||
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
@ -187,6 +189,16 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
else if (!strcmp(argv[0], "--repl"))
|
||||
is_repl_mode = true;
|
||||
else if (!strncmp(argv[0], "--stack-size=", 13)) {
|
||||
if (argv[0][13] == '\0')
|
||||
return print_help();
|
||||
stack_size = atoi(argv[0] + 13);
|
||||
}
|
||||
else if (!strncmp(argv[0], "--heap-size=", 12)) {
|
||||
if (argv[0][12] == '\0')
|
||||
return print_help();
|
||||
heap_size = atoi(argv[0] + 12);
|
||||
}
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
else if (!strncmp(argv[0], "--dir=", 6)) {
|
||||
if (argv[0][6] == '\0')
|
||||
@ -233,15 +245,16 @@ int main(int argc, char *argv[])
|
||||
|
||||
#if USE_GLOBAL_HEAP_BUF != 0
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc.pool.heap_size = sizeof(global_heap_buf);
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
#else
|
||||
init_args.mem_alloc_type = Alloc_With_Allocator;
|
||||
init_args.mem_alloc.allocator.malloc_func = malloc;
|
||||
init_args.mem_alloc.allocator.realloc_func = realloc;
|
||||
init_args.mem_alloc.allocator.free_func = free;
|
||||
init_args.mem_alloc_option.allocator.malloc_func = malloc;
|
||||
init_args.mem_alloc_option.allocator.realloc_func = realloc;
|
||||
init_args.mem_alloc_option.allocator.free_func = free;
|
||||
#endif
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
bh_printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
@ -271,8 +284,8 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* instantiate the module */
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
|
||||
48 * 1024, /* stack size */
|
||||
16 * 1024, /* heap size */
|
||||
stack_size,
|
||||
heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
@ -295,11 +308,11 @@ fail3:
|
||||
|
||||
fail2:
|
||||
/* free the file buffer */
|
||||
bh_free(wasm_file_buf);
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_full_destroy();
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -11,7 +11,6 @@
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_export.h"
|
||||
|
||||
static int app_argc;
|
||||
@ -24,9 +23,11 @@ static int print_help()
|
||||
bh_printf(" -f|--function name Specify function name to run in module\n"
|
||||
" rather than main\n");
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
bh_printf(" -v=X Set log verbose level (0 to 5, default is 2),\n"
|
||||
bh_printf(" -v=n Set log verbose level (0 to 5, default is 2),\n"
|
||||
" larger level with more log\n");
|
||||
#endif
|
||||
bh_printf(" --stack-size=n Set maximum stack size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --heap-size=n Set maximum heap size in bytes, default is 16 KB\n");
|
||||
bh_printf(" --repl Start a very simple REPL (read-eval-print-loop) mode\n"
|
||||
" that runs commands in the form of `FUNC ARG...`\n");
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
@ -153,8 +154,10 @@ int main(int argc, char *argv[])
|
||||
const char *func_name = NULL;
|
||||
uint8 *wasm_file_buf = NULL;
|
||||
uint32 wasm_file_size;
|
||||
uint32 stack_size = 16 * 1024, heap_size = 16 * 1024;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128] = { 0 };
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
int log_verbose_level = 2;
|
||||
@ -186,6 +189,16 @@ int main(int argc, char *argv[])
|
||||
#endif
|
||||
else if (!strcmp(argv[0], "--repl"))
|
||||
is_repl_mode = true;
|
||||
else if (!strncmp(argv[0], "--stack-size=", 13)) {
|
||||
if (argv[0][13] == '\0')
|
||||
return print_help();
|
||||
stack_size = atoi(argv[0] + 13);
|
||||
}
|
||||
else if (!strncmp(argv[0], "--heap-size=", 12)) {
|
||||
if (argv[0][12] == '\0')
|
||||
return print_help();
|
||||
heap_size = atoi(argv[0] + 12);
|
||||
}
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
else if (!strncmp(argv[0], "--dir=", 6)) {
|
||||
if (argv[0][6] == '\0')
|
||||
@ -228,35 +241,37 @@ int main(int argc, char *argv[])
|
||||
app_argc = argc;
|
||||
app_argv = argv;
|
||||
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
#if USE_GLOBAL_HEAP_BUF != 0
|
||||
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
|
||||
!= 0) {
|
||||
bh_printf("Init memory with global heap buffer failed.\n");
|
||||
return -1;
|
||||
}
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
#else
|
||||
if (bh_memory_init_with_allocator(malloc, free)) {
|
||||
bh_printf("Init memory with memory allocator failed.\n");
|
||||
return -1;
|
||||
}
|
||||
init_args.mem_alloc_type = Alloc_With_Allocator;
|
||||
init_args.mem_alloc_option.allocator.malloc_func = malloc;
|
||||
init_args.mem_alloc_option.allocator.realloc_func = realloc;
|
||||
init_args.mem_alloc_option.allocator.free_func = free;
|
||||
#endif
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_init())
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
bh_printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
bh_log_set_verbose_level(log_verbose_level);
|
||||
|
||||
/* load WASM byte buffer from WASM bin file */
|
||||
if (!(wasm_file_buf = (uint8*) bh_read_file_to_buffer(wasm_file,
|
||||
&wasm_file_size)))
|
||||
goto fail2;
|
||||
goto fail1;
|
||||
|
||||
/* load WASM module */
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LIBC_WASI != 0
|
||||
@ -269,12 +284,12 @@ int main(int argc, char *argv[])
|
||||
|
||||
/* instantiate the module */
|
||||
if (!(wasm_module_inst = wasm_runtime_instantiate(wasm_module,
|
||||
64 * 1024, /* stack size */
|
||||
64 * 1024, /* heap size */
|
||||
stack_size,
|
||||
heap_size,
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail4;
|
||||
goto fail3;
|
||||
}
|
||||
|
||||
if (is_repl_mode)
|
||||
@ -287,20 +302,17 @@ int main(int argc, char *argv[])
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail4:
|
||||
fail3:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail3:
|
||||
/* free the file buffer */
|
||||
bh_free(wasm_file_buf);
|
||||
|
||||
fail2:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
/* free the file buffer */
|
||||
wasm_runtime_free(wasm_file_buf);
|
||||
|
||||
fail1:
|
||||
bh_memory_destroy();
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@ -8,7 +8,6 @@
|
||||
#include "bh_platform.h"
|
||||
#include "bh_assert.h"
|
||||
#include "bh_log.h"
|
||||
#include "bh_memory.h"
|
||||
#include "wasm_export.h"
|
||||
#include "test_wasm.h"
|
||||
|
||||
@ -63,6 +62,7 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
|
||||
uint32 wasm_file_size;
|
||||
wasm_module_t wasm_module = NULL;
|
||||
wasm_module_inst_t wasm_module_inst = NULL;
|
||||
RuntimeInitArgs init_args;
|
||||
char error_buf[128];
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
int log_verbose_level = 2;
|
||||
@ -72,15 +72,17 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
|
||||
(void) arg2;
|
||||
(void) arg3;
|
||||
|
||||
if (bh_memory_init_with_pool(global_heap_buf, sizeof(global_heap_buf))
|
||||
!= 0) {
|
||||
bh_printf("Init global heap failed.\n");
|
||||
return;
|
||||
}
|
||||
memset(&init_args, 0, sizeof(RuntimeInitArgs));
|
||||
|
||||
init_args.mem_alloc_type = Alloc_With_Pool;
|
||||
init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
|
||||
init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
|
||||
|
||||
/* initialize runtime environment */
|
||||
if (!wasm_runtime_init())
|
||||
goto fail1;
|
||||
if (!wasm_runtime_full_init(&init_args)) {
|
||||
bh_printf("Init runtime environment failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if WASM_ENABLE_LOG != 0
|
||||
bh_log_set_verbose_level(log_verbose_level);
|
||||
@ -94,7 +96,7 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
|
||||
if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
|
||||
error_buf, sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail2;
|
||||
goto fail1;
|
||||
}
|
||||
|
||||
/* instantiate the module */
|
||||
@ -104,7 +106,7 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
|
||||
error_buf,
|
||||
sizeof(error_buf)))) {
|
||||
bh_printf("%s\n", error_buf);
|
||||
goto fail3;
|
||||
goto fail2;
|
||||
}
|
||||
|
||||
/* invoke the main function */
|
||||
@ -113,16 +115,14 @@ void iwasm_main(void *arg1, void *arg2, void *arg3)
|
||||
/* destroy the module instance */
|
||||
wasm_runtime_deinstantiate(wasm_module_inst);
|
||||
|
||||
fail3:
|
||||
fail2:
|
||||
/* unload the module */
|
||||
wasm_runtime_unload(wasm_module);
|
||||
|
||||
fail2:
|
||||
fail1:
|
||||
/* destroy runtime environment */
|
||||
wasm_runtime_destroy();
|
||||
|
||||
fail1: bh_memory_destroy();
|
||||
|
||||
end = k_uptime_get_32();
|
||||
|
||||
printf("elpase: %d\n", (end - start));
|
||||
|
||||
Reference in New Issue
Block a user