re-org platform APIs, simplify porting process (#201)

Co-authored-by: Xu Jun <jun1.xu@intel.com>
This commit is contained in:
Xu Jun
2020-03-16 16:43:57 +08:00
committed by GitHub
parent ef5ceffe71
commit f1a0e75ab7
177 changed files with 2954 additions and 7904 deletions

View File

@ -6,7 +6,6 @@
#ifndef _WASM_EXEC_ENV_H
#define _WASM_EXEC_ENV_H
#include "bh_thread.h"
#include "bh_assert.h"
#if WASM_ENABLE_INTERP != 0
#include "../interpreter/wasm.h"

View File

@ -6,7 +6,6 @@
#include "wasm_runtime_common.h"
#include "bh_platform.h"
#include "mem_alloc.h"
#include "bh_thread.h"
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
@ -60,12 +59,12 @@ wasm_memory_init_with_pool(void *mem, unsigned int bytes)
memory_mode = MEMORY_MODE_POOL;
pool_allocator = _allocator;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
os_mutex_init(&profile_lock);
#endif
global_pool_size = bytes;
return true;
}
bh_printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
LOG_ERROR("Init memory with pool (%p, %u) failed.\n", mem, bytes);
return false;
}
@ -80,11 +79,11 @@ wasm_memory_init_with_allocator(void *_malloc_func,
realloc_func = _realloc_func;
free_func = _free_func;
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_init(&profile_lock);
os_mutex_init(&profile_lock);
#endif
return true;
}
bh_printf("Init memory with allocator (%p, %p, %p) failed.\n",
LOG_ERROR("Init memory with allocator (%p, %p, %p) failed.\n",
_malloc_func, _realloc_func, _free_func);
return false;
}
@ -110,7 +109,7 @@ void
wasm_runtime_memory_destroy()
{
#if BEIHAI_ENABLE_MEMORY_PROFILING != 0
vm_mutex_destroy(&profile_lock);
os_mutex_destroy(&profile_lock);
#endif
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
@ -130,7 +129,7 @@ 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");
LOG_WARNING("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);
@ -143,7 +142,7 @@ 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");
LOG_WARNING("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);
@ -159,7 +158,7 @@ void
wasm_runtime_free(void *ptr)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
bh_printf("wasm_runtime_free failed: memory hasn't been initialize.\n");
LOG_WARNING("wasm_runtime_free failed: memory hasn't been initialize.\n");
} else if (memory_mode == MEMORY_MODE_POOL) {
mem_allocator_free(pool_allocator, ptr);
} else {
@ -177,7 +176,7 @@ wasm_runtime_malloc_profile(const char *file, int line,
if (p) {
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
os_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
@ -194,7 +193,7 @@ wasm_runtime_malloc_profile(const char *file, int line,
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
os_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
return (char *)p + 8;
}
@ -209,7 +208,7 @@ wasm_runtime_malloc_profile(const char *file, int line,
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
os_mutex_unlock(&profile_lock);
bh_memcpy_s(p, size + 8, &size, sizeof(size));
memory_in_use += size;
@ -234,7 +233,7 @@ wasm_runtime_free_profile(const char *file, int line,
if (memory_in_use >= size)
memory_in_use -= size;
vm_mutex_lock(&profile_lock);
os_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
@ -251,7 +250,7 @@ wasm_runtime_free_profile(const char *file, int line,
} else {
profile = wasm_runtime_malloc(sizeof(memory_profile_t));
if (!profile) {
vm_mutex_unlock(&profile_lock);
os_mutex_unlock(&profile_lock);
return;
}
@ -265,7 +264,7 @@ wasm_runtime_free_profile(const char *file, int line,
memory_profiles_list = profile;
}
vm_mutex_unlock(&profile_lock);
os_mutex_unlock(&profile_lock);
}
/**
@ -277,11 +276,11 @@ void memory_usage_summarize()
{
memory_profile_t *profile;
vm_mutex_lock(&profile_lock);
os_mutex_lock(&profile_lock);
profile = memory_profiles_list;
while (profile) {
bh_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
os_printf("malloc:%d:malloc_num:%d:free:%d:free_num:%d:%s\n",
profile->total_malloc,
profile->malloc_num,
profile->total_free,
@ -290,14 +289,14 @@ void memory_usage_summarize()
profile = profile->next;
}
vm_mutex_unlock(&profile_lock);
os_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",
os_printf("location:%s@%d:used:%d:contribution:%d\n",
func, line, memory_in_use, alloc);
}

View File

@ -3,7 +3,6 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/
#include "config.h"
#include "bh_platform.h"
#include "bh_common.h"
#include "bh_assert.h"
@ -30,14 +29,10 @@ wasm_runtime_env_init()
if (bh_platform_init() != 0)
return false;
if (bh_log_init() != 0)
return false;
if (vm_thread_sys_init() != 0)
return false;
if (wasm_native_init() == false)
if (wasm_native_init() == false) {
bh_platform_destroy();
return false;
}
return true;
}
@ -60,7 +55,7 @@ void
wasm_runtime_destroy()
{
wasm_native_destroy();
vm_thread_sys_destroy();
bh_platform_destroy();
wasm_runtime_memory_destroy();
}
@ -273,7 +268,7 @@ wasm_runtime_call_wasm(WASMExecEnv *exec_env,
return false;
}
exec_env->handle = vm_self_thread();
exec_env->handle = os_self_thread();
#if WASM_ENABLE_INTERP != 0
if (exec_env->module_inst->module_type == Wasm_Module_Bytecode)
@ -1375,7 +1370,7 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
/* print return value */
switch (type->types[type->param_count]) {
case VALUE_TYPE_I32:
bh_printf("0x%x:i32", argv1[0]);
os_printf("0x%x:i32", argv1[0]);
break;
case VALUE_TYPE_I64:
{
@ -1387,22 +1382,22 @@ wasm_application_execute_func(WASMModuleInstanceCommon *module_inst,
snprintf(buf, sizeof(buf), "%s", "0x%llx:i64");
else
snprintf(buf, sizeof(buf), "%s", "0x%lx:i64");
bh_printf(buf, u.val);
os_printf(buf, u.val);
break;
}
case VALUE_TYPE_F32:
bh_printf("%.7g:f32", *(float32*)argv1);
os_printf("%.7g:f32", *(float32*)argv1);
break;
case VALUE_TYPE_F64:
{
union { float64 val; uint32 parts[2]; } u;
u.parts[0] = argv1[0];
u.parts[1] = argv1[1];
bh_printf("%.7g:f64", u.val);
os_printf("%.7g:f64", u.val);
break;
}
}
bh_printf("\n");
os_printf("\n");
wasm_runtime_free(argv1);
return true;
@ -1413,7 +1408,7 @@ fail:
exception = wasm_runtime_get_exception(module_inst);
bh_assert(exception);
bh_printf("%s\n", exception);
os_printf("%s\n", exception);
return false;
}

View File

@ -8,7 +8,6 @@
#include "bh_platform.h"
#include "bh_common.h"
#include "bh_thread.h"
#include "wasm_exec_env.h"
#include "wasm_native.h"
#include "../include/wasm_export.h"