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

@ -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;
}