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

@ -1,20 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
include_directories (./include ../include ./${WAMR_BUILD_PLATFORM})
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE)
file (GLOB_RECURSE source_all ${WAMR_BUILD_PLATFORM}/*.c)
add_library (supportlib ${source_all})
target_link_libraries (supportlib -pthread -lrt)
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
add_library (supportlib_ut ${source_all})
set_target_properties (supportlib_ut PROPERTIES COMPILE_DEFINITIONS BH_TEST=1)
target_link_libraries (supportlib_ut -pthread -lrt)
endif ()

View File

@ -1,4 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
obj-y += zephyr/

View File

@ -4,7 +4,6 @@
*/
#include "bh_platform.h"
#include "bh_memory.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
@ -15,16 +14,33 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return NULL;
}
void *
os_realloc(void *ptr, unsigned size)
{
return NULL;
}
void
os_free(void *ptr)
{
}
void *
bh_mmap(void *hint, unsigned int size, int prot, int flags)
{
return bh_malloc(size);
return BH_MALLOC(size);
}
void
bh_munmap(void *addr, uint32 size)
{
return bh_free(addr);
return BH_FREE(addr);
}
int

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <aos/kernel.h>
#include <inttypes.h>
#include <stdbool.h>
@ -39,10 +38,6 @@
typedef uint64_t uint64;
typedef int64_t int64;
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
typedef aos_task_t korp_thread;
typedef korp_thread *korp_tid;
typedef aos_task_t *aos_tid_t;
@ -58,6 +53,10 @@ typedef struct korp_cond {
typedef void* (*thread_start_routine_t)(void*);
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
@ -85,10 +84,6 @@ int snprintf(char *buffer, size_t count, const char *format, ...);
extern void bh_assert_internal(int v, const char *file_name, int line_number, const char *expr_string);
#define bh_assert(expr) bh_assert_internal((int)(expr), __FILE__, __LINE__, # expr)
extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
extern int b_strcat_s(char * s1, size_t s1max, const char * s2);
extern int b_strcpy_s(char * s1, size_t s1max, const char * s2);
/* math functions */
double sqrt(double x);
double floor(double x);

View File

@ -6,7 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
@ -102,7 +101,7 @@ vm_thread_cleanup(void)
wait_node_sem = &thread_data->wait_node.sem;
/* Free thread data firstly */
bh_free(thread_data);
BH_FREE(thread_data);
aos_mutex_lock(wait_list_lock, AOS_WAIT_FOREVER);
if (thread_wait_list) {
@ -152,7 +151,7 @@ _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
return BHT_ERROR;
/* Create and initialize thread data */
if (!(thread_data = bh_malloc(sizeof(bh_thread_data))))
if (!(thread_data = BH_MALLOC(sizeof(bh_thread_data))))
return BHT_ERROR;
memset(thread_data, 0, sizeof(bh_thread_data));
@ -184,7 +183,7 @@ fail3:
fail2:
aos_sem_free(&thread_data->wait_node.sem);
fail1:
bh_free(thread_data);
BH_FREE(thread_data);
return BHT_ERROR;
}

View File

@ -18,6 +18,24 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return malloc(size);
}
void *
os_realloc(void *ptr, unsigned size)
{
return realloc(ptr, size);
}
void
os_free(void *ptr)
{
free(ptr);
}
char*
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
@ -46,7 +64,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
if (!(buffer = BH_MALLOC(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
@ -57,7 +75,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
if (read_size < file_size) {
printf("Read file to buffer failed: read file content failed.\n");
bh_free(buffer);
BH_FREE(buffer);
return NULL;
}

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
@ -36,16 +35,10 @@ extern "C" {
typedef uint64_t uint64;
typedef int64_t int64;
extern void DEBUGME(void);
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
#ifndef BH_PLATFORM_ANDROID
#define BH_PLATFORM_ANDROID
#endif
/* NEED qsort */
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */
@ -67,13 +60,12 @@ typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf(...) (__android_log_print(ANDROID_LOG_INFO, "wasm_runtime::", __VA_ARGS__))
int snprintf(char *buffer, size_t count, const char *format, ...);
double fmod(double x, double y);
float fmodf(float x, float y);
@ -99,15 +91,8 @@ double sqrt(double x);
#define bh_assert assert
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);
int bh_platform_init();
/* MMAP mode */

View File

@ -6,7 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
@ -67,7 +66,7 @@ static void *vm_thread_wrapper(void *arg)
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
BH_FREE(targ);
_vm_tls_put(1, NULL);
return NULL;
}
@ -94,7 +93,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_ERROR;
}
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ));
if (!targ) {
pthread_attr_destroy(&tattr);
return BHT_ERROR;
@ -106,7 +105,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
pthread_attr_destroy(&tattr);
bh_free(targ);
BH_FREE(targ);
return BHT_ERROR;
}
@ -128,7 +127,7 @@ korp_tid _vm_self_thread()
void vm_thread_exit(void * code)
{
bh_free(_vm_tls_get(1));
BH_FREE(_vm_tls_get(1));
_vm_tls_put(1, NULL);
pthread_exit(code);
}

View File

@ -18,6 +18,24 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return malloc(size);
}
void *
os_realloc(void *ptr, unsigned size)
{
return realloc(ptr, size);
}
void
os_free(void *ptr)
{
free(ptr);
}
char*
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
@ -46,7 +64,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
if (!(buffer = BH_MALLOC(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
@ -57,7 +75,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
if (read_size < file_size) {
printf("Read file to buffer failed: read file content failed.\n");
bh_free(buffer);
BH_FREE(buffer);
return NULL;
}

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
@ -34,16 +33,10 @@ extern "C" {
typedef uint64_t uint64;
typedef int64_t int64;
extern void DEBUGME(void);
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
#ifndef BH_PLATFORM_DARWIN
#define BH_PLATFORM_DARWIN
#endif
/* NEED qsort */
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */
@ -65,9 +58,9 @@ typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf printf
@ -96,15 +89,8 @@ double sqrt(double x);
#define bh_assert assert
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);
int bh_platform_init();
/* MMAP mode */

View File

@ -6,7 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
@ -67,7 +66,7 @@ static void *vm_thread_wrapper(void *arg)
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
BH_FREE(targ);
_vm_tls_put(1, NULL);
return NULL;
}
@ -93,7 +92,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_ERROR;
}
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ));
if (!targ) {
pthread_attr_destroy(&tattr);
return BHT_ERROR;
@ -105,7 +104,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
pthread_attr_destroy(&tattr);
bh_free(targ);
BH_FREE(targ);
return BHT_ERROR;
}
@ -127,7 +126,7 @@ korp_tid _vm_self_thread()
void vm_thread_exit(void * code)
{
bh_free(_vm_tls_get(1));
BH_FREE(_vm_tls_get(1));
_vm_tls_put(1, NULL);
pthread_exit(code);
}

View File

@ -16,5 +16,38 @@
#include "config.h"
#define BH_KB (1024)
#define BH_MB ((BH_KB)*1024)
#define BH_GB ((BH_MB)*1024)
#ifndef BH_MALLOC
#define BH_MALLOC os_malloc
#endif
#ifndef BH_FREE
#define BH_FREE os_free
#endif
#ifndef WA_MALLOC
#include <stdlib.h>
#define WA_MALLOC malloc
#endif
#ifndef WA_FREE
#include <stdlib.h>
#define WA_FREE free
#endif
#ifdef __cplusplus
extern "C" {
#endif
void *wasm_runtime_malloc(unsigned int size);
void wasm_runtime_free(void *ptr);
#ifdef __cplusplus
}
#endif
#endif /* end of BH_CONFIG */

View File

@ -19,6 +19,24 @@ int bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return malloc(size);
}
void *
os_realloc(void *ptr, unsigned size)
{
return realloc(ptr, size);
}
void
os_free(void *ptr)
{
free(ptr);
}
int putchar(int c)
{
return 0;

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
@ -27,14 +26,15 @@
extern "C" {
#endif
typedef void (*bh_print_function_t)(const char* message);
void bh_set_print_function(bh_print_function_t pf);
extern int bh_printf_sgx(const char *message, ...);
extern int bh_vprintf_sgx(const char * format, va_list arg);
typedef uint64_t uint64;
typedef int64_t int64;
#define DIE do{bh_debug("Die here\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); DEBUGME(void); while(1);}while(0)
#ifndef BH_PLATFORM_LINUX_SGX
#define BH_PLATFORM_LINUX_SGX
#endif
@ -62,9 +62,9 @@ typedef sgx_thread_t korp_tid;
typedef sgx_thread_t korp_thread;
typedef sgx_thread_cond_t korp_cond;
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf bh_printf_sgx
@ -94,13 +94,6 @@ double sqrt(double x);
#define bh_assert assert
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_strdup(const char *s);
int bh_platform_init();
/* MMAP mode */

View File

@ -5,7 +5,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sgx_thread.h>

View File

@ -18,6 +18,24 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return malloc(size);
}
void *
os_realloc(void *ptr, unsigned size)
{
return realloc(ptr, size);
}
void
os_free(void *ptr)
{
free(ptr);
}
char*
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
@ -46,7 +64,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
if (!(buffer = BH_MALLOC(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
@ -57,7 +75,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
if (read_size < file_size) {
printf("Read file to buffer failed: read file content failed.\n");
bh_free(buffer);
BH_FREE(buffer);
return NULL;
}

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
@ -66,9 +65,9 @@ typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf printf
@ -97,15 +96,8 @@ double sqrt(double x);
#define bh_assert assert
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);
int bh_platform_init();
/* MMAP mode */

View File

@ -6,7 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
@ -67,7 +66,7 @@ static void *vm_thread_wrapper(void *arg)
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
BH_FREE(targ);
_vm_tls_put(1, NULL);
return NULL;
}
@ -93,7 +92,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_ERROR;
}
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ));
if (!targ) {
pthread_attr_destroy(&tattr);
return BHT_ERROR;
@ -105,7 +104,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
pthread_attr_destroy(&tattr);
bh_free(targ);
BH_FREE(targ);
return BHT_ERROR;
}
@ -127,7 +126,7 @@ korp_tid _vm_self_thread()
void vm_thread_exit(void * code)
{
bh_free(_vm_tls_get(1));
BH_FREE(_vm_tls_get(1));
_vm_tls_put(1, NULL);
pthread_exit(code);
}

View File

@ -22,6 +22,24 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return malloc(size);
}
void *
os_realloc(void *ptr, unsigned size)
{
return realloc(ptr, size);
}
void
os_free(void *ptr)
{
free(ptr);
}
char*
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
@ -50,7 +68,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
file_size = (uint32)stat_buf.st_size;
if (!(buffer = bh_malloc(file_size))) {
if (!(buffer = BH_MALLOC(file_size))) {
printf("Read file to buffer failed: alloc memory failed.\n");
close(file);
return NULL;
@ -61,7 +79,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
if (read_size < file_size) {
printf("Read file to buffer failed: read file content failed.\n");
bh_free(buffer);
BH_FREE(buffer);
return NULL;
}

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
@ -42,8 +41,6 @@ extern void DEBUGME(void);
#define BH_PLATFORM_VXWORKS
#endif
/* NEED qsort */
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet threads's native part. */
@ -65,9 +62,9 @@ typedef pthread_cond_t korp_cond;
typedef pthread_t korp_thread;
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf printf
@ -96,15 +93,8 @@ double sqrt(double x);
#define bh_assert assert
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
char *bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
char *bh_strdup(const char *s);
int bh_platform_init();
/* MMAP mode */

View File

@ -6,7 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
@ -67,7 +66,7 @@ static void *vm_thread_wrapper(void *arg)
targ->stack = (void *)((uintptr_t)(&arg) & (uintptr_t)~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
BH_FREE(targ);
_vm_tls_put(1, NULL);
return NULL;
}
@ -93,7 +92,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
return BHT_ERROR;
}
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
targ = (thread_wrapper_arg*) BH_MALLOC(sizeof(*targ));
if (!targ) {
pthread_attr_destroy(&tattr);
return BHT_ERROR;
@ -105,7 +104,7 @@ int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
pthread_attr_destroy(&tattr);
bh_free(targ);
BH_FREE(targ);
return BHT_ERROR;
}
@ -127,7 +126,7 @@ korp_tid _vm_self_thread()
void vm_thread_exit(void * code)
{
bh_free(_vm_tls_get(1));
BH_FREE(_vm_tls_get(1));
_vm_tls_put(1, NULL);
pthread_exit(code);
}

View File

@ -1,4 +0,0 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
obj-y += bh_assert.o bh_definition.o bh_memory.o bh_platform_log.o bh_thread.o bh_time.o

View File

@ -4,7 +4,6 @@
*/
#include "bh_platform.h"
#include "bh_memory.h"
#include "bh_common.h"
#include <stdlib.h>
#include <string.h>
@ -58,16 +57,33 @@ bh_platform_init()
return 0;
}
void *
os_malloc(unsigned size)
{
return NULL;
}
void *
os_realloc(void *ptr, unsigned size)
{
return NULL;
}
void
os_free(void *ptr)
{
}
void *
bh_mmap(void *hint, unsigned int size, int prot, int flags)
{
return bh_malloc(size);
return BH_MALLOC(size);
}
void
bh_munmap(void *addr, uint32 size)
{
return bh_free(addr);
return BH_FREE(addr);
}
int

View File

@ -8,7 +8,6 @@
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <autoconf.h>
#include <zephyr.h>
#include <kernel.h>
@ -55,10 +54,6 @@ typedef korp_thread *korp_tid;
typedef struct k_mutex korp_mutex;
typedef struct k_sem korp_sem;
#define wa_malloc bh_malloc
#define wa_free bh_free
#define wa_strdup bh_strdup
struct bh_thread_wait_node;
typedef struct bh_thread_wait_node *bh_thread_wait_list;
typedef struct korp_cond {
@ -68,8 +63,9 @@ typedef struct korp_cond {
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
void *os_malloc(unsigned size);
void *os_realloc(void *ptr, unsigned size);
void os_free(void *ptr);
#define bh_printf printf
@ -102,11 +98,6 @@ int snprintf(char *buffer, size_t count, const char *format, ...);
} \
} while (0)
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
int b_strcat_s(char * s1, size_t s1max, const char * s2);
int b_strcpy_s(char * s1, size_t s1max, const char * s2);
/* math functions */
double sqrt(double x);
double floor(double x);

View File

@ -6,9 +6,6 @@
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct bh_thread_wait_node {
struct k_sem sem;
@ -140,11 +137,11 @@ static void thread_obj_list_reclaim()
if (p->to_be_freed) {
if (p_prev == NULL) { /* p is the head of list */
thread_obj_list = p->next;
bh_free(p);
BH_FREE(p);
p = thread_obj_list;
} else { /* p is not the head of list */
p_prev->next = p->next;
bh_free(p);
BH_FREE(p);
p = p_prev->next;
}
} else {
@ -210,7 +207,7 @@ static void vm_thread_cleanup(void)
/* Set flag to true for the next thread creating to
free the thread object */
((bh_thread_obj*) thread_data->tid)->to_be_freed = true;
bh_free(thread_data);
BH_FREE(thread_data);
}
static void vm_thread_wrapper(void *start, void *arg, void *thread_data)
@ -244,15 +241,15 @@ int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
thread_obj_list_reclaim();
/* Create and initialize thread object */
if (!(tid = bh_malloc(sizeof(bh_thread_obj))))
if (!(tid = BH_MALLOC(sizeof(bh_thread_obj))))
return BHT_ERROR;
memset(tid, 0, sizeof(bh_thread_obj));
/* Create and initialize thread data */
thread_data_size = offsetof(bh_thread_data, stack) + stack_size;
if (!(thread_data = bh_malloc(thread_data_size))) {
bh_free(tid);
if (!(thread_data = BH_MALLOC(thread_data_size))) {
BH_FREE(tid);
return BHT_ERROR;
}
@ -265,8 +262,8 @@ int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
if (!((tid = k_thread_create(tid, (k_thread_stack_t *) thread_data->stack,
stack_size, vm_thread_wrapper, start, arg, thread_data, prio, 0,
K_NO_WAIT)))) {
bh_free(tid);
bh_free(thread_data);
BH_FREE(tid);
BH_FREE(thread_data);
return BHT_ERROR;
}
@ -305,7 +302,7 @@ int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
bh_thread_wait_node *node;
/* Create wait node and append it to wait list */
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
if (!(node = BH_MALLOC(sizeof(bh_thread_wait_node))))
return BHT_ERROR;
k_sem_init(&node->sem, 0, 1);
@ -334,7 +331,7 @@ int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
k_sleep(100);
/* Destroy resource */
bh_free(node);
BH_FREE(node);
return BHT_OK;
}
@ -449,7 +446,7 @@ static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
bh_thread_wait_node *node;
/* Create wait node and append it to wait list */
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
if (!(node = BH_MALLOC(sizeof(bh_thread_wait_node))))
return BHT_ERROR;
k_sem_init(&node->sem, 0, 1);
@ -483,7 +480,7 @@ static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
p = p->next;
p->next = node->next;
}
bh_free(node);
BH_FREE(node);
k_mutex_unlock(&cond->wait_list_lock);
return BHT_OK;