WebAssembly Micro Runtime first version

This commit is contained in:
Wang Xin
2019-05-07 10:18:18 +08:00
parent 15aa50914b
commit a75a5f0f41
252 changed files with 33487 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_COMMON_H
#define _BH_COMMON_H
#include "bh_assert.h"
#include "bh_definition.h"
#include "bh_platform.h"
#include "bh_log.h"
#include "bh_list.h"
#define bh_memcpy_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memcpy_s (dest, dlen, src, slen); \
(void)_ret; \
bh_assert (_ret == 0); \
} while (0)
#define bh_strcat_s(dest, dlen, src) do { \
int _ret = b_strcat_s (dest, dlen, src); \
(void)_ret; \
bh_assert (_ret == 0); \
} while (0)
#define bh_strcpy_s(dest, dlen, src) do { \
int _ret = b_strcpy_s (dest, dlen, src); \
(void)_ret; \
bh_assert (_ret == 0); \
} while (0)
#endif

View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_LIST_H
#define _BH_LIST_H
#ifdef __cplusplus
extern "C" {
#endif
#include "korp_types.h" /*For bool type*/
#include "bh_platform.h"
/* List user should embedded bh_list_link into list elem data structure
* definition. And bh_list_link data field should be the first field.
* For example, if we would like to use bh_list for our own data type A,
* A must be defined as a structure like below:
* struct A {
* bh_list_link l;
* ...
* };
*
* bh_list_link is defined as a structure (not typedef void*).
* It will make extend list into bi-direction easy.
*/
typedef struct _bh_list_link {
struct _bh_list_link *next;
} bh_list_link;
typedef struct _bh_list {
bh_list_link head;
uint32 len;
} bh_list;
/* Beihai list operation return value */
typedef enum _bh_list_status {
BH_LIST_SUCCESS = 0, BH_LIST_ERROR = -1
} bh_list_status;
/**
* Initialize a list.
*
* @param list pointer to list.
* @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if list pointer is NULL.
*/
bh_list_status bh_list_init(bh_list *list);
/**
* Insert an elem pointer into list. The list node memory is maintained by list while
* elem memory is the responsibility of list user.
*
* @param list pointer to list.
* @param elem pointer to elem that will be inserted into list.
* @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if input is invalid or no memory available.
*/
extern bh_list_status _bh_list_insert(bh_list *list, void *elem);
#ifdef _INSTRUMENT_TEST_ENABLED
extern bh_list_status bh_list_insert_instr(bh_list *list, void *elem, const char*func_name);
#define bh_list_insert(list, elem) bh_list_insert_instr(list, elem, __FUNCTION__)
#else
#define bh_list_insert _bh_list_insert
#endif
/**
* Remove an elem pointer from list. The list node memory is maintained by list while
* elem memory is the responsibility of list user.
*
* @param list pointer to list.
* @param elem pointer to elem that will be inserted into list.
* @return <code>BH_LIST_ERROR</code> if OK;
* <code>BH_LIST_ERROR</code> if element does not exist in given list.
*/
bh_list_status bh_list_remove(bh_list *list, void *elem);
/**
* Get the list length.
*
* @param list pointer to list.
* @return the length of the list.
*/
uint32 bh_list_length(bh_list *list);
/**
* Get the first elem in the list.
*
* @param list pointer to list.
* @return pointer to the first node.
*/
void* bh_list_first_elem(bh_list *list);
/**
* Get the next elem of given list input elem.
*
* @param node pointer to list node.
* @return pointer to next list node.
*/
void* bh_list_elem_next(void *node);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_LIST_H */

View File

@ -0,0 +1,155 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file bh_log.h
* @date Tue Nov 8 18:19:10 2011
*
* @brief This log system supports wrapping multiple outputs into one
* log message. This is useful for outputting variable-length logs
* without additional memory overhead (the buffer for concatenating
* the message), e.g. exception stack trace, which cannot be printed
* by a single log calling without the help of an additional buffer.
* Avoiding additional memory buffer is useful for resource-constraint
* systems. It can minimize the impact of log system on applications
* and logs can be printed even when no enough memory is available.
* Functions with prefix "_" are private functions. Only macros that
* are not start with "_" are exposed and can be used.
*/
#ifndef _BH_LOG_H
#define _BH_LOG_H
#include <stdarg.h>
#include "korp_types.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* The following functions are the primitive operations of this log
* system. A normal usage of the log system is to call bh_log_printf
* or bh_log_vprintf one or multiple times and then call bh_log_commit
* to wrap (mark) the previous outputs into one log message. The
* bh_log and macros LOG_ERROR etc. can be used to output log messages
* that can be wrapped into one log calling.
*/
int _bh_log_init(void);
void _bh_log_set_verbose_level(int level);
void _bh_log_printf(const char *fmt, ...);
void _bh_log_vprintf(const char *fmt, va_list ap);
void _bh_log_commit(void);
#if BEIHAI_ENABLE_LOG != 0
# define bh_log_init() _bh_log_init ()
# define bh_log_set_verbose_level(l) _bh_log_set_verbose_level (l)
# define bh_log_printf(...) _bh_log_printf (__VA_ARGS__)
# define bh_log_vprintf(...) _bh_log_vprintf (__VA_ARGS__)
# define bh_log_commit() _bh_log_commit ()
#else /* BEIHAI_ENABLE_LOG != 0 */
# define bh_log_init() 0
# define bh_log_set_verbose_level(l) (void)0
# define bh_log_printf(...) (void)0
# define bh_log_vprintf(...) (void)0
# define bh_log_commit() (void)0
#endif /* BEIHAI_ENABLE_LOG != 0 */
void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...);
/* Always print fatal message */
# define LOG_FATAL(...) _bh_log ("V0.", NULL, 0, __VA_ARGS__)
#if BEIHAI_ENABLE_LOG != 0
# define LOG_ERROR(...) _bh_log ("V1.", NULL, 0, __VA_ARGS__)
# define LOG_WARNING(...) _bh_log ("V2.", NULL, 0, __VA_ARGS__)
# define LOG_INFO_RELEASE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
# define LOG_INFO_APP_DEV(...) _bh_log ("V4.", NULL, 0, __VA_ARGS__)
# define LOG_VERBOSE(...) _bh_log ("V5.", NULL, 0, __VA_ARGS__)
# if BEIHAI_ENABLE_MEMORY_PROFILING != 0
# define LOG_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
# else
# define LOG_PROFILE(...) (void)0
#endif
# if BEIHAI_ENABLE_QUEUE_PROFILING != 0
# define LOG_QUEUE_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
# else
# define LOG_QUEUE_PROFILE(...) (void)0
#endif
# if BEIHAI_ENABLE_GC_STAT_PROFILING != 0
# define LOG_GC_STAT_PROFILE(...) _bh_log ("V3.", NULL, 0, __VA_ARGS__)
# else
# define LOG_GC_STAT_PROFILE(...) (void)0
#endif
#else /* BEIHAI_ENABLE_LOG != 0 */
# define LOG_ERROR(...) (void)0
# define LOG_WARNING(...) (void)0
# define LOG_INFO_APP_DEV(...) (void)0
# define LOG_INFO_RELEASE(...) (void)0
# define LOG_VERBOSE(...) (void)0
# define LOG_PROFILE(...) (void)0
# define LOG_QUEUE_PROFILE(...) (void)0
# define LOG_GC_STAT_PROFILE(...) (void)0
#endif /* BEIHAI_ENABLE_LOG != 0 */
#define LOG_PROFILE_INSTANCE_HEAP_CREATED(heap) \
LOG_PROFILE ("PROF.INSTANCE.HEAP_CREATED: HEAP=%08X", heap)
#define LOG_PROFILE_INSTANCE_THREAD_STARTED(heap) \
LOG_PROFILE ("PROF.INSTANCE.THREAD_STARTED: HEAP=%08X", heap)
#define LOG_PROFILE_HEAP_ALLOC(heap, size) \
LOG_PROFILE ("PROF.HEAP.ALLOC: HEAP=%08X SIZE=%d", heap, size)
#define LOG_PROFILE_HEAP_FREE(heap, size) \
LOG_PROFILE ("PROF.HEAP.FREE: HEAP=%08X SIZE=%d", heap, size)
#define LOG_PROFILE_HEAP_NEW(heap, size) \
LOG_PROFILE ("PROF.HEAP.NEW: HEAP=%08X SIZE=%d", heap, size)
#define LOG_PROFILE_HEAP_GC(heap, size) \
LOG_PROFILE ("PROF.HEAP.GC: HEAP=%08X SIZE=%d", heap, size)
#define LOG_PROFILE_STACK_PUSH(used) \
LOG_PROFILE ("PROF.STACK.PUSH: USED=%d", used)
#define LOG_PROFILE_STACK_POP() \
LOG_PROFILE ("PROF.STACK.POP")
/* Please add your component ahead of LOG_COM_MAX */
enum {
LOG_COM_APP_MANAGER = 0,
LOG_COM_GC,
LOG_COM_HMC,
LOG_COM_UTILS,
LOG_COM_VERIFIER_JEFF,
LOG_COM_VMCORE_JEFF,
LOG_COM_MAX
};
#if defined(BH_DEBUG)
void log_parse_coms(const char *coms);
int bh_log_dcom_is_enabled(int component);
#define LOG_DEBUG(component, ...) do { \
if (bh_log_dcom_is_enabled (component)) \
_bh_log ("V6: ", __FILE__, __LINE__, __VA_ARGS__); \
} while (0)
#else /* defined(BH_DEBUG) */
#define LOG_DEBUG(component, ...) (void)0
#endif /* defined(BH_DEBUG) */
#ifdef __cplusplus
}
#endif
#endif /* _BH_LOG_H */

View File

@ -0,0 +1,76 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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();
/**
* 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);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_MEMORY_H */

View File

@ -0,0 +1,99 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_QUEUE_H
#define _BH_QUEUE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "korp_types.h" /*For bool type*/
#include "bh_platform.h"
struct _bh_queue_node;
typedef struct _bh_queue_node * bh_message_t;
struct bh_queue;
typedef struct bh_queue bh_queue;
typedef void (*bh_queue_handle_msg_callback)(void *message);
#define bh_queue_malloc bh_malloc
#define bh_queue_free bh_free
#define bh_queue_mutex korp_mutex
#define bh_queue_sem korp_sem
#define bh_queue_cond korp_cond
#define bh_queue_mutex_init vm_mutex_init
#define bh_queue_mutex_destroy vm_mutex_destroy
#define bh_queue_mutex_lock vm_mutex_lock
#define bh_queue_mutex_unlock vm_mutex_unlock
#define bh_queue_sem_init vm_sem_init
#define bh_queue_sem_destroy vm_sem_destroy
#define bh_queue_sem_wait vm_sem_wait
#define bh_queue_sem_reltimedwait vm_sem_reltimedwait
#define bh_queue_sem_post vm_sem_post
#define bh_queue_cond_init vm_cond_init
#define bh_queue_cond_destroy vm_cond_destroy
#define bh_queue_cond_wait vm_cond_wait
#define bh_queue_cond_timedwait vm_cond_reltimedwait
#define bh_queue_cond_signal vm_cond_signal
#define bh_queue_cond_broadcast vm_cond_broadcast
typedef void (*bh_msg_cleaner)(void *msg);
bh_queue *
bh_queue_create();
void
bh_queue_destroy(bh_queue *queue);
char * bh_message_payload(bh_message_t message);
int bh_message_payload_len(bh_message_t message);
int bh_message_type(bh_message_t message);
bh_message_t bh_new_msg(unsigned short tag, void *body, unsigned int len,
void * handler);
void bh_free_msg(bh_message_t msg);
bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
unsigned int len);
bool bh_post_msg2(bh_queue *queue, bh_message_t msg);
bh_message_t bh_get_msg(bh_queue *queue, int timeout);
unsigned
bh_queue_get_message_count(bh_queue *queue);
void
bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb);
void
bh_queue_enter_loop_run1(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb);
void
bh_queue_exit_loop_run(bh_queue *queue);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_QUEUE_H */

View File

@ -0,0 +1,90 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _CONFIG_H_
/* Memory allocator ems */
#define MEM_ALLOCATOR_EMS 0
/* Memory allocator tlsf */
#define MEM_ALLOCATOR_TLSF 1
/* Default memory allocator */
#define DEFAULT_MEM_ALLOCATOR MEM_ALLOCATOR_EMS
/* Beihai log system */
#define BEIHAI_ENABLE_LOG 1
/* Beihai debugger support */
#define BEIHAI_ENABLE_TOOL_AGENT 1
/* Beihai debug monitoring server, must define
BEIHAI_ENABLE_TOOL_AGENT firstly */
#define BEIHAI_ENABLE_TOOL_AGENT_BDMS 1
/* enable no signature on sdv since verify doesn't work as lacking public key */
#ifdef CONFIG_SDV
#define BEIHAI_ENABLE_NO_SIGNATURE 1
#else
#define BEIHAI_ENABLE_NO_SIGNATURE 0
#endif
/* WASM VM log system */
#define WASM_ENABLE_LOG 1
/* WASM Interpreter labels-as-values feature */
#define WASM_ENABLE_LABELS_AS_VALUES 1
/* Heap and stack profiling */
#define BEIHAI_ENABLE_MEMORY_PROFILING 0
/* Max app number of all modules */
#define MAX_APP_INSTALLATIONS 3
/* Default timer number in one app */
#define DEFAULT_TIMERS_PER_APP 20
/* Max timer number in one app */
#define MAX_TIMERS_PER_APP 30
/* Max resource registration number in one app */
#define RESOURCE_REGISTRATION_NUM_MAX 16
/* Max length of resource/event url */
#define RESOUCE_EVENT_URL_LEN_MAX 256
/* Default length of queue */
#define DEFAULT_QUEUE_LENGTH 50
/* Default watchdog interval in ms */
#define DEFAULT_WATCHDOG_INTERVAL (3 * 60 * 1000)
/* Workflow heap size */
/*
#define WORKING_FLOW_HEAP_SIZE 0
*/
/* Default/min/max heap size of each app */
#define APP_HEAP_SIZE_DEFAULT (48 * 1024)
#define APP_HEAP_SIZE_MIN (2 * 1024)
#define APP_HEAP_SIZE_MAX (1024 * 1024)
/* Default/min/max stack size of each app thread */
#define APP_THREAD_STACK_SIZE_DEFAULT (20 * 1024)
#define APP_THREAD_STACK_SIZE_MIN (16 * 1024)
#define APP_THREAD_STACK_SIZE_MAX (256 * 1024)
#endif

View File

@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file errcode.h
* @date Wed Feb 29 18:58:30 2012
*
* @brief Host-visible error code definition
*/
#ifndef BEIHAI_ERRCODE_H
#define BEIHAI_ERRCODE_H
/**
* Responses to all remote requests from host to Beihai runtime has a
* return error code, which is used to indicate the processing result:
* successful or any error occurs. The following definitions include
* all those error codes that may be returned to host.
*/
enum {
BHE_SUCCESS = 0x000, /* Successful */
/* General errors: 0x100 */
BHE_OUT_OF_MEMORY = 0x101, /* Out of memory */
BHE_BAD_PARAMETER = 0x102, /* Bad parameters to native */
BHE_INSUFFICIENT_BUFFER = 0x103,
BHE_MUTEX_INIT_FAIL = 0x104,
BHE_COND_INIT_FAIL = 0x105, /* Cond init fail is not return to
* host now, it may be used later.
*/
BHE_WD_TIMEOUT = 0x106, /* Watchdog time out */
/* Communication: 0x200 */
BHE_MAILBOX_NOT_FOUND = 0x201, /* Mailbox not found */
BHE_MSG_QUEUE_IS_FULL = 0x202, /* Message queue is full */
BHE_MAILBOX_DENIED = 0x203, /* Mailbox is denied by firewall */
/* Applet manager: 0x300 */
BHE_LOAD_JEFF_FAIL = 0x303, /* JEFF file load fail, OOM or file
* format error not distinct by
* current JEFF loading
* process (bool jeff_loader_load).
*/
BHE_PACKAGE_NOT_FOUND = 0x304, /* Request operation on a package,
* but it does not exist.
*/
BHE_EXIST_LIVE_SESSION = 0x305, /* Uninstall package fail because of
* live session exist.
*/
BHE_VM_INSTANCE_INIT_FAIL = 0x306, /* VM instance init fail when create
* session.
*/
BHE_QUERY_PROP_NOT_SUPPORT = 0x307, /* Query applet property that Beihai
* does not support.
*/
BHE_INVALID_BPK_FILE = 0x308, /* Incorrect Beihai package format */
BHE_VM_INSTNACE_NOT_FOUND = 0x312, /* VM instance not found */
BHE_STARTING_JDWP_FAIL = 0x313, /* JDWP agent starting fail */
BHE_GROUP_CHECK_FAIL = 0x314, /* Group access checking fail*/
/* Applet instance: 0x400 */
BHE_UNCAUGHT_EXCEPTION = 0x401, /* uncaught exception */
BHE_APPLET_BAD_PARAMETER = 0x402, /* Bad parameters to applet */
BHE_APPLET_SMALL_BUFFER = 0x403, /* Small response buffer */
/*TODO: Should be removed these UI error code when integrate with ME 9 */
/* UI: 0x500 */
BHE_UI_EXCEPTION = 0x501,
BHE_UI_ILLEGAL_USE = 0x502,
BHE_UI_ILLEGAL_PARAMETER = 0x503,
BHE_UI_NOT_INITIALIZED = 0x504,
BHE_UI_NOT_SUPPORTED = 0x505,
BHE_UI_OUT_OF_RESOURCES = 0x506
};
#endif

View File

@ -0,0 +1,160 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _KORP_TYPES_H
#define _KORP_TYPES_H
#include "bh_platform.h"
/* all types used in kORP should be explicit sized */
typedef struct _korp_object korp_object;
typedef unsigned int obj_info;
typedef korp_object* ref;
#define BYTES_OF_OBJ_INFO 4
#define BYTES_OF_REF 4
/* don't change the number, it's hardcoded in kORP */
enum _korp_array_type {
ARRAY_UINT8 = 0, /* bytes_of_uint8 = 1 << ARRAY_UINT8 */
ARRAY_UINT16 = 1, /* bytes_of_uint16 = 1 << ARRAY_UINT16 */
ARRAY_UINT32 = 2, /* bytes_of_uint32 = 1 << ARRAY_UINT32 */
ARRAY_UINT64 = 3, /* bytes_of_uint64 = 1 << ARRAY_UINT64 */
ARRAY_BOOLEAN = 4,
ARRAY_CHAR = 5,
ARRAY_FLOAT = 6,
ARRAY_DOUBLE = 7,
ARRAY_BYTE = 8,
ARRAY_SHORT = 9,
ARRAY_INT = 10,
ARRAY_LONG = 11,
ARRAY_REF = 12 /* for calculation */
};
enum _korp_java_type {
JAVA_TYPE_WRONG = 0,
JAVA_TYPE_BYTE = 'B',
JAVA_TYPE_CHAR = 'C',
JAVA_TYPE_DOUBLE = 'D',
JAVA_TYPE_FLOAT = 'F',
JAVA_TYPE_INT = 'I',
JAVA_TYPE_LONG = 'J',
JAVA_TYPE_SHORT = 'S',
JAVA_TYPE_BOOLEAN = 'Z',
JAVA_TYPE_CLASS = 'L',
JAVA_TYPE_ARRAY = '[',
JAVA_TYPE_VOID = 'V',
JAVA_TYPE_STRING = '$' /* for TAG_String const value */
};
enum korp_modifier_type {
MOD_PUBLIC = 0x0001, /* Class Field Method */
MOD_PRIVATE = 0x0002, /* Field Method */
MOD_PROTECTED = 0x0004, /* Field Method */
MOD_STATIC = 0x0008, /* Field Method */
MOD_FINAL = 0x0010, /* Class Field Method */
MOD_SUPER = 0x0020, /* Class */
MOD_SYNCHRONIZED = 0x0020, /* Method */
MOD_VOLATILE = 0x0040, /* Field */
MOD_TRANSIENT = 0x0080, /* Field */
MOD_NATIVE = 0x0100, /* Method */
MOD_INTERFACE = 0x0200, /* Class */
MOD_ABSTRACT = 0x0400, /* Class Method */
MOD_STRICT = 0x0800 /* Method */
};
/* object header, used to access object info */
struct _korp_object {
obj_info header; /* object header (I) */
};
#define HASH_TABLE_SIZE 359
#ifndef NULL
#define NULL (void*)0
#endif
#define KORP_ERROR (-1)
#ifndef __cplusplus
#define true 1
#define false 0
#define inline __inline
#endif
/* forwarded declarations */
typedef struct _korp_string_pool korp_string_pool;
typedef struct _korp_class_table korp_class_table;
typedef enum _korp_loader_exception {
LD_OK = 0,
LD_NoClassDefFoundError,
LD_ClassFormatError,
LD_ClassCircularityError,
LD_IncompatibleClassChangeError,
LD_AbstractMethodError, /* occurs during preparation */
LD_IllegalAccessError,
LD_InstantiationError,
LD_NoSuchFieldError,
LD_NoSuchMethodError,
LD_UnsatisfiedLinkError,
LD_VerifyError
} korp_loader_exception;
typedef enum _korp_java_type korp_java_type;
typedef enum _korp_array_type korp_array_type;
/* typedef struct _korp_thread korp_thread; */
typedef struct _korp_method korp_method;
typedef struct _korp_field korp_field;
typedef struct _korp_class korp_class;
typedef struct _korp_string korp_string;
typedef struct _korp_package korp_package;
typedef struct _korp_class_loader korp_class_loader;
typedef struct _korp_ref_array korp_ref_array;
typedef struct _korp_entry korp_entry;
typedef struct _korp_preloaded korp_preloaded;
typedef struct _korp_env korp_env;
typedef struct _korp_java_array korp_java_array;
typedef struct _korp_uint8_array korp_uint8_array;
typedef struct _korp_vm_thread_list korp_vm_thread_list;
#define korp_uint8 korp_uint32
#define korp_uint16 korp_uint32
#define korp_boolean korp_uint32
#define korp_char korp_uint32
#define korp_short korp_uint32
#define korp_int korp_uint32
#define korp_float korp_uint32
#define korp_long korp_uint64
#define korp_double korp_uint64
#define korp_boolean_array korp_uint8_array
#define korp_char_array korp_uint8_array
#define korp_short_array korp_uint16_array
#define korp_int_array korp_uint32_array
#define korp_float_array korp_uint32_array
#define korp_double_array korp_uint64_array
#define korp_long_array korp_uint64_array
#define korp_code korp_uint8_array
#endif /* #ifndef _KORP_TYPES_H */

View File

@ -0,0 +1,45 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __MEM_ALLOC_H
#define __MEM_ALLOC_H
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void *mem_allocator_t;
mem_allocator_t
mem_allocator_create(void *mem, uint32_t size);
void
mem_allocator_destroy(mem_allocator_t allocator);
void *
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size);
void
mem_allocator_free(mem_allocator_t allocator, void *ptr);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef __MEM_ALLOC_H */

View File

@ -0,0 +1,105 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_memory.h"
#include "mem_alloc.h"
#include <stdio.h>
#include <stdlib.h>
#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 (*free_func)(void *ptr) = NULL;
int bh_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;
return 0;
}
printf("Init memory with pool (%p, %u) failed.\n", mem, bytes);
return -1;
}
int bh_memory_init_with_allocator(void *_malloc_func, void *_free_func)
{
if (_malloc_func && _free_func && _malloc_func != _free_func) {
memory_mode = MEMORY_MODE_ALLOCATOR;
malloc_func = _malloc_func;
free_func = _free_func;
return 0;
}
printf("Init memory with allocator (%p, %p) failed.\n", _malloc_func,
_free_func);
return -1;
}
void bh_memory_destroy()
{
if (memory_mode == MEMORY_MODE_POOL)
mem_allocator_destroy(pool_allocator);
memory_mode = MEMORY_MODE_UNKNOWN;
}
void* bh_malloc(unsigned int size)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
printf("bh_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 bh_free(void *ptr)
{
if (memory_mode == MEMORY_MODE_UNKNOWN) {
printf("bh_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);
}
}
#else /* else of MALLOC_MEMORY_FROM_SYSTEM */
void* bh_malloc(unsigned int size)
{
return malloc(size);
}
void bh_free(void *ptr)
{
if (ptr)
free(ptr);
}
#endif /* end of MALLOC_MEMORY_FROM_SYSTEM*/

View File

@ -0,0 +1,590 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ems_gc_internal.h"
#if !defined(NVALGRIND)
#include <valgrind/memcheck.h>
#endif
static int hmu_is_in_heap(gc_heap_t* heap, hmu_t* hmu)
{
return heap && hmu && (gc_uint8*) hmu >= heap->base_addr
&& (gc_uint8*) hmu < heap->base_addr + heap->current_size;
}
/* Remove a node from the tree it belongs to*/
/* @p can not be NULL*/
/* @p can not be the ROOT node*/
/* Node @p will be removed from the tree and left,right,parent pointers of node @p will be*/
/* set to be NULL. Other fields will not be touched.*/
/* The tree will be re-organized so that the order conditions are still satisified.*/
BH_STATIC void remove_tree_node(hmu_tree_node_t *p)
{
hmu_tree_node_t *q = NULL, **slot = NULL;
bh_assert(p);
bh_assert(p->parent); /* @p can not be the ROOT node*/
/* get the slot which holds pointer to node p*/
if (p == p->parent->right) {
slot = &p->parent->right;
} else {
bh_assert(p == p->parent->left); /* @p should be a child of its parent*/
slot = &p->parent->left;
}
/* algorithms used to remove node p*/
/* case 1: if p has no left child, replace p with its right child*/
/* case 2: if p has no right child, replace p with its left child*/
/* case 3: otherwise, find p's predecessor, remove it from the tree and replace p with it.*/
/* use predecessor can keep the left <= root < right condition.*/
if (!p->left) {
/* move right child up*/
*slot = p->right;
if (p->right)
p->right->parent = p->parent;
p->left = p->right = p->parent = NULL;
return;
}
if (!p->right) {
/* move left child up*/
*slot = p->left;
p->left->parent = p->parent; /* p->left can never be NULL.*/
p->left = p->right = p->parent = NULL;
return;
}
/* both left & right exist, find p's predecessor at first*/
q = p->left;
while (q->right)
q = q->right;
remove_tree_node(q); /* remove from the tree*/
*slot = q;
q->parent = p->parent;
q->left = p->left;
q->right = p->right;
if (q->left)
q->left->parent = q;
if (q->right)
q->right->parent = q;
p->left = p->right = p->parent = NULL;
}
static void unlink_hmu(gc_heap_t *heap, hmu_t *hmu)
{
gc_size_t size;
bh_assert(gci_is_heap_valid(heap));
bh_assert(
hmu && (gc_uint8*) hmu >= heap->base_addr
&& (gc_uint8*) hmu < heap->base_addr + heap->current_size);
bh_assert(hmu_get_ut(hmu) == HMU_FC);
size = hmu_get_size(hmu);
if (HMU_IS_FC_NORMAL(size)) {
int node_idx = size >> 3;
hmu_normal_node_t* node = heap->kfc_normal_list[node_idx].next;
hmu_normal_node_t** p = &(heap->kfc_normal_list[node_idx].next);
while (node) {
if ((hmu_t*) node == hmu) {
*p = node->next;
break;
}
p = &(node->next);
node = node->next;
}
if (!node) {
printf("[GC_ERROR]couldn't find the node in the normal list");
}
} else {
remove_tree_node((hmu_tree_node_t *) hmu);
}
}
static void hmu_set_free_size(hmu_t *hmu)
{
gc_size_t size;
bh_assert(hmu && hmu_get_ut(hmu) == HMU_FC);
size = hmu_get_size(hmu);
*((int*) ((char*) hmu + size) - 1) = size;
}
/* Add free chunk back to KFC*/
/* @heap should not be NULL and it should be a valid heap*/
/* @hmu should not be NULL and it should be a HMU of length @size inside @heap*/
/* @hmu should be aligned to 8*/
/* @size should be positive and multiple of 8*/
/* @hmu with size @size will be added into KFC as a new FC.*/
void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size)
{
hmu_normal_node_t *np = NULL;
hmu_tree_node_t *root = NULL, *tp = NULL, *node = NULL;
int node_idx;
bh_assert(gci_is_heap_valid(heap));
bh_assert(
hmu && (gc_uint8*) hmu >= heap->base_addr
&& (gc_uint8*) hmu < heap->base_addr + heap->current_size);
bh_assert(((gc_uint32) hmu_to_obj(hmu) & 7) == 0);
bh_assert(
size > 0
&& ((gc_uint8*) hmu) + size
<= heap->base_addr + heap->current_size);
bh_assert(!(size & 7));
hmu_set_ut(hmu, HMU_FC);
hmu_set_size(hmu, size);
hmu_set_free_size(hmu);
if (HMU_IS_FC_NORMAL(size)) {
np = (hmu_normal_node_t*) hmu;
node_idx = size >> 3;
np->next = heap->kfc_normal_list[node_idx].next;
heap->kfc_normal_list[node_idx].next = np;
return;
}
/* big block*/
node = (hmu_tree_node_t*) hmu;
node->size = size;
node->left = node->right = node->parent = NULL;
/* find proper node to link this new node to*/
root = &heap->kfc_tree_root;
tp = root;
bh_assert(tp->size < size);
while (1) {
if (tp->size < size) {
if (!tp->right) {
tp->right = node;
node->parent = tp;
break;
}
tp = tp->right;
} else /* tp->size >= size*/
{
if (!tp->left) {
tp->left = node;
node->parent = tp;
break;
}
tp = tp->left;
}
}
}
/* Find a proper hmu for required memory size*/
/* @heap should not be NULL and it should be a valid heap*/
/* @size should cover the header and it should be 8 bytes aligned*/
/* GC will not be performed here.*/
/* Heap extension will not be performed here.*/
/* A proper HMU will be returned. This HMU can include the header and given size. The returned HMU will be aligned to 8 bytes.*/
/* NULL will be returned if there are no proper HMU.*/
BH_STATIC hmu_t *alloc_hmu(gc_heap_t *heap, gc_size_t size)
{
hmu_normal_node_t *node = NULL, *p = NULL;
int node_idx = 0, init_node_idx = 0;
hmu_tree_node_t *root = NULL, *tp = NULL, *last_tp = NULL;
hmu_t *next, *rest;
bh_assert(gci_is_heap_valid(heap));
bh_assert(size > 0 && !(size & 7));
if (size < GC_SMALLEST_SIZE)
size = GC_SMALLEST_SIZE;
/* check normal list at first*/
if (HMU_IS_FC_NORMAL(size)) {
/* find a non-empty slot in normal_node_list with good size*/
init_node_idx = (int) (size >> 3);
for (node_idx = init_node_idx; node_idx < HMU_NORMAL_NODE_CNT;
node_idx++) {
node = heap->kfc_normal_list + node_idx;
if (node->next)
break;
node = NULL;
}
/* not found in normal list*/
if (node) {
bh_assert(node_idx >= init_node_idx);
p = node->next;
node->next = p->next;
bh_assert(((gc_int32) hmu_to_obj(p) & 7) == 0);
if ((gc_size_t) node_idx
!= init_node_idx&& ((gc_size_t)node_idx << 3) >= size + GC_SMALLEST_SIZE) { /* with bigger size*/
rest = (hmu_t*) (((char *) p) + size);
gci_add_fc(heap, rest, (node_idx << 3) - size);
hmu_mark_pinuse(rest);
} else {
size = node_idx << 3;
next = (hmu_t*) ((char*) p + size);
if (hmu_is_in_heap(heap, next))
hmu_mark_pinuse(next);
}
#if GC_STAT_DATA != 0
heap->total_free_size -= size;
if ((heap->current_size - heap->total_free_size)
> heap->highmark_size)
heap->highmark_size = heap->current_size
- heap->total_free_size;
#endif
hmu_set_size((hmu_t* ) p, size);
return (hmu_t*) p;
}
}
/* need to find a node in tree*/
root = &heap->kfc_tree_root;
/* find the best node*/
bh_assert(root);
tp = root->right;
while (tp) {
if (tp->size < size) {
tp = tp->right;
continue;
}
/* record the last node with size equal to or bigger than given size*/
last_tp = tp;
tp = tp->left;
}
if (last_tp) {
bh_assert(last_tp->size >= size);
/* alloc in last_p*/
/* remove node last_p from tree*/
remove_tree_node(last_tp);
if (last_tp->size >= size + GC_SMALLEST_SIZE) {
rest = (hmu_t*) ((char*) last_tp + size);
gci_add_fc(heap, rest, last_tp->size - size);
hmu_mark_pinuse(rest);
} else {
size = last_tp->size;
next = (hmu_t*) ((char*) last_tp + size);
if (hmu_is_in_heap(heap, next))
hmu_mark_pinuse(next);
}
#if GC_STAT_DATA != 0
heap->total_free_size -= size;
if ((heap->current_size - heap->total_free_size) > heap->highmark_size)
heap->highmark_size = heap->current_size - heap->total_free_size;
#endif
hmu_set_size((hmu_t* ) last_tp, size);
return (hmu_t*) last_tp;
}
return NULL;
}
/* Find a proper HMU for given size*/
/* @heap should not be NULL and it should be a valid heap*/
/* @size should cover the header and it should be 8 bytes aligned*/
/* This function will try several ways to satisfy the allocation request.*/
/* 1. Find a proper on available HMUs.*/
/* 2. GC will be triggered if 1 failed.*/
/* 3. Find a proper on available HMUS.*/
/* 4. Return NULL if 3 failed*/
/* A proper HMU will be returned. This HMU can include the header and given size. The returned HMU will be aligned to 8 bytes.*/
/* NULL will be returned if there are no proper HMU.*/
BH_STATIC hmu_t* alloc_hmu_ex(gc_heap_t *heap, gc_size_t size)
{
hmu_t *ret = NULL;
bh_assert(gci_is_heap_valid(heap));
bh_assert(size > 0 && !(size & 7));
#ifdef GC_IN_EVERY_ALLOCATION
gci_gc_heap(heap);
ret = alloc_hmu(heap, size);
#else
# if GC_STAT_DATA != 0
if (heap->gc_threshold < heap->total_free_size)
ret = alloc_hmu(heap, size);
# else
ret = alloc_hmu(heap, size);
# endif
if (ret)
return ret;
/*gci_gc_heap(heap);*//* disable gc claim currently */
ret = alloc_hmu(heap, size);
#endif
return ret;
}
unsigned long g_total_malloc = 0;
unsigned long g_total_free = 0;
gc_object_t _gc_alloc_vo_i_heap(void *vheap,
gc_size_t size ALLOC_EXTRA_PARAMETERS)
{
gc_heap_t* heap = (gc_heap_t*) vheap;
hmu_t *hmu = NULL;
gc_object_t ret = (gc_object_t) NULL;
gc_size_t tot_size = 0;
/* align size*/
tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
if (tot_size < size)
return NULL;
gct_vm_mutex_lock(&heap->lock);
hmu = alloc_hmu_ex(heap, tot_size);
if (!hmu)
goto FINISH;
g_total_malloc += tot_size;
hmu_set_ut(hmu, HMU_VO);
hmu_unfree_vo(hmu);
#if defined(GC_VERIFY)
hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
#endif
ret = hmu_to_obj(hmu);
#if BH_ENABLE_MEMORY_PROFILING != 0
printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
#endif
FINISH:
gct_vm_mutex_unlock(&heap->lock);
return ret;
}
/* see ems_gc.h for description*/
gc_object_t _gc_alloc_jo_i_heap(void *vheap,
gc_size_t size ALLOC_EXTRA_PARAMETERS)
{
gc_heap_t* heap = (gc_heap_t*) vheap;
gc_object_t ret = (gc_object_t) NULL;
hmu_t *hmu = NULL;
gc_size_t tot_size = 0;
bh_assert(gci_is_heap_valid(heap));
/* align size*/
tot_size = GC_ALIGN_8(size + HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE); /* hmu header, prefix, suffix*/
if (tot_size < size)
return NULL;
hmu = alloc_hmu_ex(heap, tot_size);
if (!hmu)
goto FINISH;
/* reset all fields*/
memset((char*) hmu + sizeof(*hmu), 0, tot_size - sizeof(*hmu));
/* hmu->header = 0; */
hmu_set_ut(hmu, HMU_JO);
hmu_unmark_jo(hmu);
#if defined(GC_VERIFY)
hmu_init_prefix_and_suffix(hmu, tot_size, file_name, line_number);
#endif
ret = hmu_to_obj(hmu);
#if BH_ENABLE_MEMORY_PROFILING != 0
printf("HEAP.ALLOC: heap: %p, size: %u", heap, size);
#endif
FINISH:
return ret;
}
/* Do some checking to see if given pointer is a possible valid heap*/
/* Return GC_TRUE if all checking passed*/
/* Return GC_FALSE otherwise*/
int gci_is_heap_valid(gc_heap_t *heap)
{
if (!heap)
return GC_FALSE;
if (heap->heap_id != (gc_handle_t) heap)
return GC_FALSE;
return GC_TRUE;
}
int gc_free_i_heap(void *vheap, gc_object_t obj ALLOC_EXTRA_PARAMETERS)
{
gc_heap_t* heap = (gc_heap_t*) vheap;
hmu_t *hmu = NULL;
hmu_t *prev = NULL;
hmu_t *next = NULL;
gc_size_t size = 0;
hmu_type_t ut;
int ret = GC_SUCCESS;
if (!obj) {
return GC_SUCCESS;
}
hmu = obj_to_hmu(obj);
gct_vm_mutex_lock(&heap->lock);
if ((gc_uint8 *) hmu >= heap->base_addr
&& (gc_uint8 *) hmu < heap->base_addr + heap->current_size) {
#ifdef GC_VERIFY
hmu_verify(hmu);
#endif
ut = hmu_get_ut(hmu);
if (ut == HMU_VO) {
if (hmu_is_vo_freed(hmu)) {
bh_assert(0);
ret = GC_ERROR;
goto out;
}
size = hmu_get_size(hmu);
g_total_free += size;
#if GC_STAT_DATA != 0
heap->total_free_size += size;
#endif
#if BH_ENABLE_MEMORY_PROFILING != 0
printf("HEAP.FREE, heap: %p, size: %u\n",heap, size);
#endif
if (!hmu_get_pinuse(hmu)) {
prev = (hmu_t*) ((char*) hmu - *((int*) hmu - 1));
if (hmu_is_in_heap(heap, prev) && hmu_get_ut(prev) == HMU_FC) {
size += hmu_get_size(prev);
hmu = prev;
unlink_hmu(heap, prev);
}
}
next = (hmu_t*) ((char*) hmu + size);
if (hmu_is_in_heap(heap, next)) {
if (hmu_get_ut(next) == HMU_FC) {
size += hmu_get_size(next);
unlink_hmu(heap, next);
next = (hmu_t*) ((char*) hmu + size);
}
}
gci_add_fc(heap, hmu, size);
if (hmu_is_in_heap(heap, next)) {
hmu_unmark_pinuse(next);
}
} else {
ret = GC_ERROR;
goto out;
}
ret = GC_SUCCESS;
goto out;
}
out:
gct_vm_mutex_unlock(&heap->lock);
return ret;
}
void gc_dump_heap_stats(gc_heap_t *heap)
{
printf("heap: %p, heap start: %p\n", heap, heap->base_addr);
printf(
"total malloc: totalfree: %u, current: %u, highmark: %u, gc cnt: %u\n",
heap->total_free_size, heap->current_size, heap->highmark_size,
heap->total_gc_count);
printf("g_total_malloc=%lu, g_total_free=%lu, occupied=%lu\n",
g_total_malloc, g_total_free, g_total_malloc - g_total_free);
}
#ifdef GC_TEST
void gci_dump(char* buf, gc_heap_t *heap)
{
hmu_t *cur = NULL, *end = NULL;
hmu_type_t ut;
gc_size_t size;
int i = 0;
int p;
char inuse;
int mark;
cur = (hmu_t*)heap->base_addr;
end = (hmu_t*)((char*)heap->base_addr + heap->current_size);
while(cur < end)
{
ut = hmu_get_ut(cur);
size = hmu_get_size(cur);
p = hmu_get_pinuse(cur);
mark = hmu_is_jo_marked (cur);
if(ut == HMU_VO)
inuse = 'V';
else if(ut == HMU_JO)
inuse = hmu_is_jo_marked(cur) ? 'J' : 'j';
else if(ut == HMU_FC)
inuse = 'F';
bh_assert(size > 0);
buf += sprintf(buf, "#%d %08x %x %x %d %c %d\n", i, (char*) cur - (char*) heap->base_addr, ut, p, mark, inuse, hmu_obj_size(size));
cur = (hmu_t*)((char *)cur + size);
i++;
}
bh_assert(cur == end);
}
#endif

View File

@ -0,0 +1,341 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file ems_gc.h
* @date Wed Aug 3 10:46:38 2011
*
* @brief This file defines GC modules types and interfaces.
*
*
*/
#ifndef _EMS_GC_H
#define _EMS_GC_H
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
/*Pre-compile configuration can be done here or on Makefiles*/
/*#define GC_EMBEDDED or GC_STANDALONE*/
/*#define GC_DEBUG*/
/*#define GC_TEST // TEST mode is a sub-mode of STANDALONE*/
/* #define GC_ALLOC_TRACE */
/* #define GC_STAT */
#ifndef GC_STAT_DATA
#define GC_STAT_DATA 1
#endif
#define GC_HEAD_PADDING 4
/* Standalone GC is used for testing.*/
#ifndef GC_EMBEDDED
# ifndef GC_STANDALONE
# define GC_STANDALONE
# endif
#endif
#if defined(GC_EMBEDDED) && defined(GC_STANDALONE)
# error "Can not define GC_EMBEDDED and GC_STANDALONE at the same time"
#endif
#ifdef BH_TEST
# ifndef GC_TEST
# define GC_TEST
# endif
#endif
#ifdef BH_DEBUG
/*instrument mode ignore GC_DEBUG feature, for instrument testing gc_alloc_vo_i_heap only has func_name parameter*/
#if !defined INSTRUMENT_TEST_ENABLED && !defined GC_DEBUG
# define GC_DEBUG
#endif
#endif
#if defined(GC_EMBEDDED) && defined(GC_TEST)
# error "Can not defined GC_EMBEDDED and GC_TEST at the same time"
#endif
typedef void *gc_handle_t;
typedef void *gc_object_t;
#define NULL_REF ((gc_object_t)NULL)
#define GC_SUCCESS (0)
#define GC_ERROR (-1)
#define GC_TRUE (1)
#define GC_FALSE (0)
#define GC_MAX_HEAP_SIZE (256 * BH_KB)
typedef int64 gc_int64;
typedef unsigned int gc_uint32;
typedef signed int gc_int32;
typedef unsigned short gc_uint16;
typedef signed short gc_int16;
typedef unsigned char gc_uint8;
typedef signed char gc_int8;
typedef gc_uint32 gc_size_t;
typedef enum {
MMT_SHARED = 0,
MMT_INSTANCE = 1,
MMT_APPMANAGER = MMT_SHARED,
MMT_VERIFIER = MMT_SHARED,
MMT_JHI = MMT_SHARED,
MMT_LOADER = MMT_SHARED,
MMT_APPLET = MMT_INSTANCE,
MMT_INTERPRETER = MMT_INSTANCE
} gc_mm_t;
#ifdef GC_STAT
#define GC_HEAP_STAT_SIZE (128 / 4)
typedef struct {
int usage;
int usage_block;
int vo_usage;
int jo_usage;
int free;
int free_block;
int vo_free;
int jo_free;
int usage_sizes[GC_HEAP_STAT_SIZE];
int free_sizes[GC_HEAP_STAT_SIZE];
}gc_stat_t;
extern void gc_heap_stat(void* heap, gc_stat_t* gc_stat);
extern void __gc_print_stat(void *heap, int verbose);
#define gc_print_stat __gc_print_stat
#else
#define gc_print_stat(heap, verbose)
#endif
#if GC_STAT_DATA != 0
typedef enum {
GC_STAT_TOTAL = 0,
GC_STAT_FREE,
GC_STAT_HIGHMARK,
GC_STAT_COUNT,
GC_STAT_TIME,
GC_STAT_MAX_1,
GC_STAT_MAX_2,
GC_STAT_MAX_3,
GC_STAT_MAX
} GC_STAT_INDEX;
#endif
/*////////////// Exported APIs*/
/**
* GC initialization from a buffer
*
* @param buf the buffer to be initialized to a heap
* @param buf_size the size of buffer
*
* @return gc handle if success, NULL otherwise
*/
extern gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size);
/**
* Destroy heap which is initilized from a buffer
*
* @param handle handle to heap needed destory
*
* @return GC_SUCCESS if success
* GC_ERROR for bad parameters or failed system resource freeing.
*/
extern int gc_destroy_with_pool(gc_handle_t handle);
#if GC_STAT_DATA != 0
/**
* Get Heap Stats
*
* @param stats [out] integer array to save heap stats
* @param size [in] the size of stats
* @param mmt [in] type of heap, MMT_SHARED or MMT_INSTANCE
*/
extern void* gc_heap_stats(void *heap, int* stats, int size, gc_mm_t mmt);
/**
* Set GC threshold factor
*
* @param heap [in] the heap to set
* @param factor [in] the threshold size is free_size * factor / 1000
*
* @return GC_SUCCESS if success.
*/
extern int gc_set_threshold_factor(void *heap, unsigned int factor);
#endif
/*////// Allocate heap object*/
/* There are two versions of allocate functions. The functions with _i suffix should be only used*/
/* internally. Functions without _i suffix are just wrappers with the corresponded functions with*/
/* _i suffix. Allocation operation code position are record under DEBUG model for debugging.*/
#ifdef GC_DEBUG
# define ALLOC_EXTRA_PARAMETERS ,const char*file_name,int line_number
# define ALLOC_EXTRA_ARGUMENTS , __FILE__, __LINE__
# define ALLOC_PASSDOWN_EXTRA_ARGUMENTS , file_name, line_number
# define gc_alloc_vo_h(heap, size) gc_alloc_vo_i_heap(heap, size, __FILE__, __LINE__)
# define gc_free_h(heap, obj) gc_free_i_heap(heap, obj, __FILE__, __LINE__)
#else
# define ALLOC_EXTRA_PARAMETERS
# define ALLOC_EXTRA_ARGUMENTS
# define ALLOC_PASSDOWN_EXTRA_ARGUMENTS
# define gc_alloc_vo_h gc_alloc_vo_i_heap
# define gc_free_h gc_free_i_heap
#endif
/**
* Invoke a GC
*
* @param heap
*
* @return GC_SUCCESS if success
*/
extern int gci_gc_heap(void *heap);
/**
* Allocate VM Object in specific heap.
*
* @param heap heap to allocate.
* @param size bytes to allocate.
*
* @return pointer to VM object allocated
* NULL if failed.
*/
extern gc_object_t _gc_alloc_vo_i_heap(void *heap,
gc_size_t size ALLOC_EXTRA_PARAMETERS);
extern gc_object_t _gc_alloc_jo_i_heap(void *heap,
gc_size_t size ALLOC_EXTRA_PARAMETERS);
#ifdef INSTRUMENT_TEST_ENABLED
extern gc_object_t gc_alloc_vo_i_heap_instr(void *heap, gc_size_t size, const char* func_name );
extern gc_object_t gc_alloc_jo_i_heap_instr(void *heap, gc_size_t size, const char* func_name);
# define gc_alloc_vo_i_heap(heap, size) gc_alloc_vo_i_heap_instr(heap, size, __FUNCTION__)
# define gc_alloc_jo_i_heap(heap, size) gc_alloc_jo_i_heap_instr(heap, size, __FUNCTION__)
#else
# define gc_alloc_vo_i_heap _gc_alloc_vo_i_heap
# define gc_alloc_jo_i_heap _gc_alloc_jo_i_heap
#endif
/**
* Allocate Java object in specific heap.
*
* @param heap heap to allocate.
* @param size bytes to allocate.
*
* @return pointer to Java object allocated
* NULL if failed.
*/
extern gc_object_t _gc_alloc_jo_i_heap(void *heap,
gc_size_t size ALLOC_EXTRA_PARAMETERS);
/**
* Free VM object
*
* @param heap heap to free.
* @param obj pointer to object need free.
*
* @return GC_SUCCESS if success
*/
extern int gc_free_i_heap(void *heap, gc_object_t obj ALLOC_EXTRA_PARAMETERS);
/**
* Add ref to rootset of gc for current instance.
*
* @param obj pointer to real load of a valid Java object managed by gc for current instance.
*
* @return GC_SUCCESS if success.
* GC_ERROR for invalid parameters.
*/
extern int gc_add_root(void* heap, gc_object_t obj);
/*////////////// Imported APIs which should be implemented in other components*/
/*////// Java object layout related APIs*/
/**
* Get Java object size from corresponding VM module
*
* @param obj pointer to the real load of a Java object.
*
* @return size of java object.
*/
extern gc_size_t vm_get_java_object_size(gc_object_t obj);
/**
* Get reference list of this object
*
* @param obj [in] pointer to java object.
* @param is_compact_mode [in] indicate the java object mode. GC_TRUE or GC_FALSE.
* @param ref_num [out] the size of ref_list.
* @param ref_list [out] if is_compact_mode is GC_FALSE, this parameter will be set to a list of offset.
* @param ref_start_offset [out] If is_compact_mode is GC_TRUE, this parameter will be set to the start offset of the references in this object.
*
* @return GC_SUCCESS if success.
* GC_ERROR when error occurs.
*/
extern int vm_get_java_object_ref_list(gc_object_t obj, int *is_compact_mode,
gc_size_t *ref_num, gc_uint16 **ref_list, gc_uint32 *ref_start_offset);
/**
* Get gc handle for current instance
*
*
* @return instance heap handle.
*/
extern gc_handle_t app_manager_get_cur_applet_heap(void);
/**
* Begin current instance heap rootset enumeration
*
*
* @return GC_SUCCESS if success.
* GC_ERROR when error occurs.
*/
extern int vm_begin_rootset_enumeration(void *heap);
#ifdef _INSTRUMENT_TEST_ENABLED
extern int vm_begin_rootset_enumeration_instr(void *heap, const char*func_name);
#define vm_begin_rootset_enumeration(heap) vm_begin_rootset_enumeration_instr(heap, __FUNCTION__)
#else
#define vm_begin_rootset_enumeration _vm_begin_rootset_enumeration
#endif /* INSTUMENT_TEST_ENABLED*/
#ifndef offsetof
#define offsetof(Type, field) ((size_t)(&((Type *)0)->field))
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,282 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _EMS_GC_INTERNAL_H
#define _EMS_GC_INTERNAL_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_thread.h"
#include "bh_memory.h"
#include "bh_assert.h"
#include "ems_gc.h"
/* basic block managed by EMS gc is the so-called HMU (heap memory unit)*/
typedef enum _hmu_type_enum
{
HMU_TYPE_MIN = 0,
HMU_TYPE_MAX = 3,
HMU_JO = 3,
HMU_VO = 2,
HMU_FC = 1,
HMU_FM = 0
}hmu_type_t;
typedef struct _hmu_struct
{
gc_uint32 header;
}hmu_t;
#if defined(GC_VERIFY)
#define GC_OBJECT_PREFIX_PADDING_CNT 3
#define GC_OBJECT_SUFFIX_PADDING_CNT 4
#define GC_OBJECT_PADDING_VALUE (0x12345678)
typedef struct _gc_object_prefix
{
const char *file_name;
gc_int32 line_no;
gc_int32 size;
gc_uint32 padding[GC_OBJECT_PREFIX_PADDING_CNT];
}gc_object_prefix_t;
#define OBJ_PREFIX_SIZE (sizeof(gc_object_prefix_t))
typedef struct _gc_object_suffix
{
gc_uint32 padding[GC_OBJECT_SUFFIX_PADDING_CNT];
}gc_object_suffix_t;
#define OBJ_SUFFIX_SIZE (sizeof(gc_object_suffix_t))
extern void hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size, const char *file_name, int line_no);
extern void hmu_verify(hmu_t *hmu);
#define SKIP_OBJ_PREFIX(p) ((void*)((gc_uint8*)(p) + OBJ_PREFIX_SIZE))
#define SKIP_OBJ_SUFFIX(p) ((void*)((gc_uint8*)(p) + OBJ_SUFFIX_SIZE))
#define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE)
#else
#define OBJ_PREFIX_SIZE 0
#define OBJ_SUFFIX_SIZE 0
#define SKIP_OBJ_PREFIX(p) ((void*)((gc_uint8*)(p) + OBJ_PREFIX_SIZE))
#define SKIP_OBJ_SUFFIX(p) ((void*)((gc_uint8*)(p) + OBJ_SUFFIX_SIZE))
#define OBJ_EXTRA_SIZE (HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE)
#endif /* GC_DEBUG*/
#define hmu_obj_size(s) ((s)-OBJ_EXTRA_SIZE)
#define GC_ALIGN_8(s) (((int)(s) + 7) & ~7)
#define GC_SMALLEST_SIZE GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + 8)
#define GC_GET_REAL_SIZE(x) GC_ALIGN_8(HMU_SIZE + OBJ_PREFIX_SIZE + OBJ_SUFFIX_SIZE + (((x) > 8) ? (x): 8))
/*////// functions for bit operation*/
#define SETBIT(v, offset) (v) |= (1 << (offset))
#define GETBIT(v, offset) ((v) & (1 << (offset)) ? 1 : 0)
#define CLRBIT(v, offset) (v) &= ~(1 << (offset))
#define SETBITS(v, offset, size, value) do { \
(v) &= ~(((1 << size) - 1) << offset); \
(v) |= value << offset; \
} while(0)
#define CLRBITS(v, offset, size) (v) &= ~(((1 << size) - 1) << offset)
#define GETBITS(v, offset, size) (((v) & (((1 << size) - 1) << offset)) >> offset)
/*////// gc object layout definition*/
#define HMU_SIZE (sizeof(hmu_t))
#define hmu_to_obj(hmu) (gc_object_t)(SKIP_OBJ_PREFIX((hmu_t*) (hmu) + 1))
#define obj_to_hmu(obj) ((hmu_t *)((gc_uint8*)(obj) - OBJ_PREFIX_SIZE) - 1)
#define HMU_UT_SIZE 2
#define HMU_UT_OFFSET 30
#define hmu_get_ut(hmu) GETBITS ((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE)
#define hmu_set_ut(hmu, type) SETBITS ((hmu)->header, HMU_UT_OFFSET, HMU_UT_SIZE, type)
#define hmu_is_ut_valid(tp) (tp >= HMU_TYPE_MIN && tp <= HMU_TYPE_MAX)
/* P in use bit means the previous chunk is in use */
#define HMU_P_OFFSET 29
#define hmu_mark_pinuse(hmu) SETBIT ((hmu)->header, HMU_P_OFFSET)
#define hmu_unmark_pinuse(hmu) CLRBIT ((hmu)->header, HMU_P_OFFSET)
#define hmu_get_pinuse(hmu) GETBIT ((hmu)->header, HMU_P_OFFSET)
#define HMU_JO_VT_SIZE 27
#define HMU_JO_VT_OFFSET 0
#define HMU_JO_MB_OFFSET 28
#define hmu_mark_jo(hmu) SETBIT ((hmu)->header, HMU_JO_MB_OFFSET)
#define hmu_unmark_jo(hmu) CLRBIT ((hmu)->header, HMU_JO_MB_OFFSET)
#define hmu_is_jo_marked(hmu) GETBIT ((hmu)->header, HMU_JO_MB_OFFSET)
#define HMU_SIZE_SIZE 27
#define HMU_SIZE_OFFSET 0
#define HMU_VO_FB_OFFSET 28
#define hmu_is_vo_freed(hmu) GETBIT ((hmu)->header, HMU_VO_FB_OFFSET)
#define hmu_unfree_vo(hmu) CLRBIT ((hmu)->header, HMU_VO_FB_OFFSET)
#define hmu_get_size(hmu) GETBITS ((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE)
#define hmu_set_size(hmu, size) SETBITS ((hmu)->header, HMU_SIZE_OFFSET, HMU_SIZE_SIZE, size)
/*////// HMU free chunk management*/
#define HMU_NORMAL_NODE_CNT 32
#define HMU_FC_NORMAL_MAX_SIZE ((HMU_NORMAL_NODE_CNT - 1) << 3)
#define HMU_IS_FC_NORMAL(size) ((size) < HMU_FC_NORMAL_MAX_SIZE)
#if HMU_FC_NORMAL_MAX_SIZE >= GC_MAX_HEAP_SIZE
# error "Too small GC_MAX_HEAP_SIZE"
#endif
typedef struct _hmu_normal_node
{
hmu_t hmu_header;
struct _hmu_normal_node *next;
}hmu_normal_node_t;
typedef struct _hmu_tree_node
{
hmu_t hmu_header;
gc_size_t size;
struct _hmu_tree_node *left;
struct _hmu_tree_node *right;
struct _hmu_tree_node *parent;
}hmu_tree_node_t;
typedef struct _gc_heap_struct
{
gc_handle_t heap_id; /* for double checking*/
gc_uint8 *base_addr;
gc_size_t current_size;
gc_size_t max_size;
korp_mutex lock;
hmu_normal_node_t kfc_normal_list[HMU_NORMAL_NODE_CNT];
/* order in kfc_tree is: size[left] <= size[cur] < size[right]*/
hmu_tree_node_t kfc_tree_root;
/* for rootset enumeration of private heap*/
void *root_set;
/* whether the fast mode of marking process that requires
additional memory fails. When the fast mode fails, the
marking process can still be done in the slow mode, which
doesn't need additional memory (by walking through all
blocks and marking sucessors of marked nodes until no new
node is marked). TODO: slow mode is not implemented. */
unsigned is_fast_marking_failed : 1;
#if GC_STAT_DATA != 0
gc_size_t highmark_size;
gc_size_t init_size;
gc_size_t total_gc_count;
gc_size_t total_free_size;
gc_size_t gc_threshold;
gc_size_t gc_threshold_factor;
gc_int64 total_gc_time;
#endif
}gc_heap_t;
/*////// MISC internal used APIs*/
extern void gci_add_fc(gc_heap_t *heap, hmu_t *hmu, gc_size_t size);
extern int gci_is_heap_valid(gc_heap_t *heap);
#ifdef GC_DEBUG
extern void gci_verify_heap(gc_heap_t *heap);
extern void gci_dump(char* buf, gc_heap_t *heap);
#endif
#if GC_STAT_DATA != 0
/* the default GC threshold size is free_size * GC_DEFAULT_THRESHOLD_FACTOR / 1000 */
#define GC_DEFAULT_THRESHOLD_FACTOR 400
static inline void gc_update_threshold(gc_heap_t *heap)
{
heap->gc_threshold = heap->total_free_size * heap->gc_threshold_factor / 1000;
}
#endif
/*////// MISC data structures*/
#define MARK_NODE_OBJ_CNT 256
/* mark node is used for gc marker*/
typedef struct _mark_node_struct
{
/* number of to-expand objects can be saved in this node*/
gc_size_t cnt;
/* the first unused index*/
int idx;
/* next node on the node list*/
struct _mark_node_struct *next;
/* the actual to-expand objects list*/
gc_object_t set[MARK_NODE_OBJ_CNT];
}mark_node_t;
/*////// Imported APIs wrappers under TEST mode*/
#ifdef GC_TEST
extern int (*gct_vm_get_java_object_ref_list)(
gc_object_t obj,
int *is_compact_mode, /* can be set to GC_TRUE, or GC_FALSE */
gc_size_t *ref_num,
gc_uint16 **ref_list,
gc_uint32 *ref_start_offset);
extern int (*gct_vm_mutex_init)(korp_mutex *mutex);
extern int (*gct_vm_mutex_destroy)(korp_mutex *mutex);
extern int (*gct_vm_mutex_lock)(korp_mutex *mutex);
extern int (*gct_vm_mutex_unlock)(korp_mutex *mutex);
extern gc_handle_t (*gct_vm_get_gc_handle_for_current_instance)(void);
extern int (*gct_vm_begin_rootset_enumeration)(void* heap);
extern int (*gct_vm_gc_finished)(void);
#else
#define gct_vm_get_java_object_ref_list bh_get_java_object_ref_list
#define gct_vm_mutex_init vm_mutex_init
#define gct_vm_mutex_destroy vm_mutex_destroy
#define gct_vm_mutex_lock vm_mutex_lock
#define gct_vm_mutex_unlock vm_mutex_unlock
#define gct_vm_get_gc_handle_for_current_instance app_manager_get_cur_applet_heap
#define gct_vm_begin_rootset_enumeration vm_begin_rootset_enumeration
#define gct_vm_gc_finished jeff_runtime_gc_finished
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,98 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ems_gc_internal.h"
#if defined(GC_VERIFY)
/* Set default value to prefix and suffix*/
/* @hmu should not be NULL and it should have been correctly initilized (except for prefix and suffix part)*/
/* @tot_size is offered here because hmu_get_size can not be used till now. @tot_size should not be smaller than OBJ_EXTRA_SIZE.*/
/* For VO, @tot_size should be equal to object total size.*/
void hmu_init_prefix_and_suffix(hmu_t *hmu, gc_size_t tot_size, const char *file_name, int line_no)
{
gc_object_prefix_t *prefix = NULL;
gc_object_suffix_t *suffix = NULL;
gc_uint32 i = 0;
bh_assert(hmu);
bh_assert(hmu_get_ut(hmu) == HMU_JO || hmu_get_ut(hmu) == HMU_VO);
bh_assert(tot_size >= OBJ_EXTRA_SIZE);
bh_assert(!(tot_size & 7));
bh_assert(hmu_get_ut(hmu) != HMU_VO || hmu_get_size(hmu) >= tot_size);
prefix = (gc_object_prefix_t *)(hmu + 1);
suffix = (gc_object_suffix_t *)((gc_uint8*)hmu + tot_size - OBJ_SUFFIX_SIZE);
prefix->file_name = file_name;
prefix->line_no = line_no;
prefix->size = tot_size;
for(i = 0;i < GC_OBJECT_PREFIX_PADDING_CNT;i++)
{
prefix->padding[i] = GC_OBJECT_PADDING_VALUE;
}
for(i = 0;i < GC_OBJECT_SUFFIX_PADDING_CNT;i++)
{
suffix->padding[i] = GC_OBJECT_PADDING_VALUE;
}
}
void hmu_verify(hmu_t *hmu)
{
gc_object_prefix_t *prefix = NULL;
gc_object_suffix_t *suffix = NULL;
gc_uint32 i = 0;
hmu_type_t ut;
gc_size_t size = 0;
int is_padding_ok = 1;
bh_assert(hmu);
ut = hmu_get_ut(hmu);
bh_assert(hmu_is_ut_valid(ut));
prefix = (gc_object_prefix_t *)(hmu + 1);
size = prefix->size;
suffix = (gc_object_suffix_t *)((gc_uint8*)hmu + size - OBJ_SUFFIX_SIZE);
if(ut == HMU_VO || ut == HMU_JO)
{
/* check padding*/
for(i = 0;i < GC_OBJECT_PREFIX_PADDING_CNT;i++)
{
if(prefix->padding[i] != GC_OBJECT_PADDING_VALUE)
{
is_padding_ok = 0;
break;
}
}
for(i = 0;i < GC_OBJECT_SUFFIX_PADDING_CNT;i++)
{
if(suffix->padding[i] != GC_OBJECT_PADDING_VALUE)
{
is_padding_ok = 0;
break;
}
}
if(!is_padding_ok)
{
printf("Invalid padding for object created at %s:%d",
(prefix->file_name ? prefix->file_name : ""), prefix->line_no);
}
bh_assert(is_padding_ok);
}
}
#endif

View File

@ -0,0 +1,196 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ems_gc_internal.h"
#if !defined(NVALGRIND)
#include <valgrind/memcheck.h>
#endif
#define HEAP_INC_FACTOR 1
/* Check if current platform is compatible with current GC design*/
/* Return GC_ERROR if not;*/
/* Return GC_SUCCESS otherwise.*/
int gci_check_platform()
{
#define CHECK(x, y) do { \
if((x) != (y)) { \
printf("Platform checking failed on LINE %d at FILE %s.", \
__LINE__, __FILE__); \
return GC_ERROR; \
} \
} while(0)
CHECK(8, sizeof(gc_int64));
CHECK(4, sizeof(gc_uint32));
CHECK(4, sizeof(gc_int32));
CHECK(2, sizeof(gc_uint16));
CHECK(2, sizeof(gc_int16));
CHECK(1, sizeof(gc_int8));
CHECK(1, sizeof(gc_uint8));
CHECK(4, sizeof(gc_size_t));
CHECK(4, sizeof(void *));
return GC_SUCCESS;
}
gc_handle_t gc_init_with_pool(char *buf, gc_size_t buf_size)
{
char *buf_end = buf + buf_size;
char *buf_aligned = (char*) (((uintptr_t) buf + 7) & ~7);
char *base_addr = buf_aligned + sizeof(gc_heap_t);
gc_heap_t *heap = (gc_heap_t*) buf_aligned;
gc_size_t heap_max_size;
hmu_normal_node_t *p = NULL;
hmu_tree_node_t *root = NULL, *q = NULL;
int i = 0, ret;
/* check system compatibility*/
if (gci_check_platform() == GC_ERROR) {
printf("Check platform compatibility failed");
return NULL;
}
if (buf_size < 1024) {
printf("[GC_ERROR]heap_init_size(%d) < 1024", buf_size);
return NULL;
}
base_addr = (char*) (((uintptr_t) base_addr + 7) & ~7) + GC_HEAD_PADDING;
heap_max_size = (buf_end - base_addr) & ~7;
memset(heap, 0, sizeof *heap);
memset(base_addr, 0, heap_max_size);
ret = gct_vm_mutex_init(&heap->lock);
if (ret != BHT_OK) {
printf("[GC_ERROR]failed to init lock ");
return NULL;
}
#ifdef BH_FOOTPRINT
printf("\nINIT HEAP 0x%08x %d\n", base_addr, heap_max_size);
#endif
/* init all data structures*/
heap->max_size = heap_max_size;
heap->current_size = heap_max_size;
heap->base_addr = (gc_uint8*) base_addr;
heap->heap_id = (gc_handle_t) heap;
#if GC_STAT_DATA != 0
heap->total_free_size = heap->current_size;
heap->highmark_size = 0;
heap->total_gc_count = 0;
heap->total_gc_time = 0;
heap->gc_threshold_factor = GC_DEFAULT_THRESHOLD_FACTOR;
gc_update_threshold(heap);
#endif
for (i = 0; i < HMU_NORMAL_NODE_CNT; i++) {
/* make normal node look like a FC*/
p = &heap->kfc_normal_list[i];
memset(p, 0, sizeof *p);
hmu_set_ut(&p->hmu_header, HMU_FC);
hmu_set_size(&p->hmu_header, sizeof *p);
}
root = &heap->kfc_tree_root;
memset(root, 0, sizeof *root);
root->size = sizeof *root;
hmu_set_ut(&root->hmu_header, HMU_FC);
hmu_set_size(&root->hmu_header, sizeof *root);
q = (hmu_tree_node_t *) heap->base_addr;
memset(q, 0, sizeof *q);
hmu_set_ut(&q->hmu_header, HMU_FC);
hmu_set_size(&q->hmu_header, heap->current_size);
hmu_mark_pinuse(&q->hmu_header);
root->right = q;
q->parent = root;
q->size = heap->current_size;
bh_assert(
root->size <= HMU_FC_NORMAL_MAX_SIZE
&& HMU_FC_NORMAL_MAX_SIZE < q->size); /*@NOTIFY*/
#if BH_ENABLE_MEMORY_PROFILING != 0
printf("heap is successfully initialized with max_size=%u.",
heap_max_size);
#endif
return heap;
}
int gc_destroy_with_pool(gc_handle_t handle)
{
gc_heap_t *heap = (gc_heap_t *) handle;
gct_vm_mutex_destroy(&heap->lock);
memset(heap->base_addr, 0, heap->max_size);
memset(heap, 0, sizeof(gc_heap_t));
return GC_SUCCESS;
}
#if defined(GC_VERIFY)
/* Verify heap integrity*/
/* @heap should not be NULL and it should be a valid heap*/
void gci_verify_heap(gc_heap_t *heap)
{
hmu_t *cur = NULL, *end = NULL;
bh_assert(heap && gci_is_heap_valid(heap));
cur = (hmu_t *)heap->base_addr;
end = (hmu_t *)(heap->base_addr + heap->current_size);
while(cur < end)
{
hmu_verify(cur);
cur = (hmu_t *)((gc_uint8*)cur + hmu_get_size(cur));
}
bh_assert(cur == end);
}
#endif
void* gc_heap_stats(void *heap_arg, int* stats, int size, gc_mm_t mmt)
{
(void) mmt;
int i;
gc_heap_t *heap = (gc_heap_t *) heap_arg;
for (i = 0; i < size; i++) {
switch (i) {
case GC_STAT_TOTAL:
stats[i] = heap->current_size;
break;
case GC_STAT_FREE:
stats[i] = heap->total_free_size;
break;
case GC_STAT_HIGHMARK:
stats[i] = heap->highmark_size;
break;
case GC_STAT_COUNT:
stats[i] = heap->total_gc_count;
break;
case GC_STAT_TIME:
stats[i] = (int) heap->total_gc_time;
break;
default:
break;
}
}
return heap;
}

View File

@ -0,0 +1,136 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mem_alloc.h"
#include "config.h"
#if DEFAULT_MEM_ALLOCATOR == MEM_ALLOCATOR_EMS
#include "ems/ems_gc.h"
mem_allocator_t mem_allocator_create(void *mem, uint32_t size)
{
return gc_init_with_pool((char *) mem, size);
}
void mem_allocator_destroy(mem_allocator_t allocator)
{
gc_destroy_with_pool((gc_handle_t) allocator);
}
void *
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
{
return gc_alloc_vo_h((gc_handle_t) allocator, size);
}
void mem_allocator_free(mem_allocator_t allocator, void *ptr)
{
if (ptr)
gc_free_h((gc_handle_t) allocator, ptr);
}
#else /* else of DEFAULT_MEM_ALLOCATOR */
#include "tlsf/tlsf.h"
#include "bh_thread.h"
typedef struct mem_allocator_tlsf {
tlsf_t tlsf;
korp_mutex lock;
}mem_allocator_tlsf;
mem_allocator_t
mem_allocator_create(void *mem, uint32_t size)
{
mem_allocator_tlsf *allocator_tlsf;
tlsf_t tlsf;
char *mem_aligned = (char*)(((uintptr_t)mem + 3) & ~3);
if (size < 1024) {
printf("Create mem allocator failed: pool size must be "
"at least 1024 bytes.\n");
return NULL;
}
size -= mem_aligned - (char*)mem;
mem = (void*)mem_aligned;
tlsf = tlsf_create_with_pool(mem, size);
if (!tlsf) {
printf("Create mem allocator failed: tlsf_create_with_pool failed.\n");
return NULL;
}
allocator_tlsf = tlsf_malloc(tlsf, sizeof(mem_allocator_tlsf));
if (!allocator_tlsf) {
printf("Create mem allocator failed: tlsf_malloc failed.\n");
tlsf_destroy(tlsf);
return NULL;
}
allocator_tlsf->tlsf = tlsf;
if (vm_mutex_init(&allocator_tlsf->lock)) {
printf("Create mem allocator failed: tlsf_malloc failed.\n");
tlsf_free(tlsf, allocator_tlsf);
tlsf_destroy(tlsf);
return NULL;
}
return allocator_tlsf;
}
void
mem_allocator_destroy(mem_allocator_t allocator)
{
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
tlsf_t tlsf = allocator_tlsf->tlsf;
vm_mutex_destroy(&allocator_tlsf->lock);
tlsf_free(tlsf, allocator_tlsf);
tlsf_destroy(tlsf);
}
void *
mem_allocator_malloc(mem_allocator_t allocator, uint32_t size)
{
void *ret;
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
if (size == 0)
/* tlsf doesn't allow to allocate 0 byte */
size = 1;
vm_mutex_lock(&allocator_tlsf->lock);
ret = tlsf_malloc(allocator_tlsf->tlsf, size);
vm_mutex_unlock(&allocator_tlsf->lock);
return ret;
}
void
mem_allocator_free(mem_allocator_t allocator, void *ptr)
{
if (ptr) {
mem_allocator_tlsf *allocator_tlsf = (mem_allocator_tlsf *)allocator;
vm_mutex_lock(&allocator_tlsf->lock);
tlsf_free(allocator_tlsf->tlsf, ptr);
vm_mutex_unlock(&allocator_tlsf->lock);
}
}
#endif /* end of DEFAULT_MEM_ALLOCATOR */

View File

@ -0,0 +1,27 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set (MEM_ALLOC_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${MEM_ALLOC_DIR})
file (GLOB_RECURSE source_all
${MEM_ALLOC_DIR}/ems/*.c
${MEM_ALLOC_DIR}/tlsf/*.c
${MEM_ALLOC_DIR}/mem_alloc.c
${MEM_ALLOC_DIR}/bh_memory.c)
set (MEM_ALLOC_SHARED_SOURCE ${source_all})

View File

@ -0,0 +1,31 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include_directories (./include ../include ./${PLATFORM})
add_definitions (-D__POSIX__ -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=199309L -D_BSD_SOURCE)
file (GLOB_RECURSE source_all ${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

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
obj-y += zephyr/

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include "bh_assert.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef BH_TEST
#include <setjmp.h>
#endif
#ifdef BH_TEST
/* for exception throwing */
jmp_buf bh_test_jb;
#endif
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
if (!file_name)
file_name = "NULL FILENAME";
if (!expr_string)
expr_string = "NULL EXPR_STRING";
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
file_name, line_number);
#ifdef BH_TEST
longjmp(bh_test_jb, 1);
#endif
abort();
}
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
...)
{
#ifndef JEFF_TEST_VERIFIER
va_list args;
va_start(args, fmt);
bh_assert(file_name);
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
vprintf(fmt, args);
va_end(args);
printf("\n");
#endif
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_definition.h"
#include "bh_platform.h"
int bh_return(int ret)
{
return ret;
}
#define RSIZE_MAX 0x7FFFFFFF
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
{
char *dest = (char*) s1;
char *src = (char*) s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}
int b_strcat_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcat(s1, s2);
return 0;
}
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcpy(s1, s2);
return 0;
}
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
{
if (NULL == pFile || NULL == filename || NULL == mode) {
return -1;
}
*pFile = fopen(filename, mode);
if (NULL == *pFile)
return -1;
return 0;
}

View File

@ -0,0 +1,117 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_PLATFORM_H
#define _BH_PLATFORM_H
#include "bh_config.h"
#include "bh_types.h"
#include <inttypes.h>
#include <stdbool.h>
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)
#define BH_PLATFORM "Linux"
/* NEED qsort */
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <semaphore.h>
#include <errno.h>
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet manager thread. */
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of HMC thread. */
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of watchdog thread. */
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of remote invoke listen thread. */
#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of remote post listen thread. */
#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Maximal recursion depth of interpreter. */
#define BH_MAX_INTERP_RECURSION_DEPTH 8
#define BH_ROUTINE_MODIFIER
#define BHT_TIMEDOUT ETIMEDOUT
#define INVALID_THREAD_ID 0xFFffFFff
#define INVALID_SEM_ID SEM_FAILED
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef sem_t korp_sem;
typedef pthread_cond_t korp_cond;
typedef void* (*thread_start_routine_t)(void*);
#include <time.h>
#include <string.h>
/* The following operations declared in string.h may be defined as
macros on Linux, so don't declare them as functions here. */
/* memset */
/* memcpy */
/* memmove */
/* #include <stdio.h> */
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
/* #include <math.h> */
double fmod(double x, double y);
float fmodf(float x, float y);
/* Definitions for applet debugging */
#define APPLET_DEBUG_LISTEN_PORT 8000
#define BH_SOCKET_INVALID_SOCK -1
#define BH_WAIT_FOREVER 0xFFFFFFFF
typedef int bh_socket_t;
#ifndef NULL
# define NULL ((void*) 0)
#endif
extern int b_memcpy_s(void * s1, unsigned int s1max, const void * s2,
unsigned int n);
extern int strcat_s(char * s1, size_t s1max, const char * s2);
extern int strcpy_s(char * s1, size_t s1max, const char * s2);
#include <stdio.h>
extern int fopen_s(FILE ** pFile, const char *filename, const char *mode);
#endif

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include <stdio.h>
void bh_log_emit(const char *fmt, va_list ap)
{
vprintf(fmt, ap);
fflush(stdout);
}
int bh_fprintf(FILE *stream, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vfprintf(stream ? stream : stdout, fmt, ap);
va_end(ap);
return ret;
}
int bh_fflush(void *stream)
{
return fflush(stream ? stream : stdout);
}

View File

@ -0,0 +1,150 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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>
static korp_mutex thread_list_lock;
static pthread_key_t thread_local_storage_key[BH_MAX_TLS_NUM];
int _vm_thread_sys_init()
{
unsigned i;
for (i = 0; i < BH_MAX_TLS_NUM; i++)
pthread_key_create(&thread_local_storage_key[i], NULL);
return vm_mutex_init(&thread_list_lock);
}
korp_tid _vm_self_thread()
{
return (korp_tid) pthread_self();
}
void *_vm_tls_get(unsigned idx)
{
bh_assert(idx < BH_MAX_TLS_NUM);
return pthread_getspecific(thread_local_storage_key[idx]);
}
int _vm_tls_put(unsigned idx, void * tls)
{
bh_assert(idx < BH_MAX_TLS_NUM);
pthread_setspecific(thread_local_storage_key[idx], tls);
return BHT_OK;
}
int _vm_mutex_init(korp_mutex *mutex)
{
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_recursive_mutex_init(korp_mutex *mutex)
{
int ret;
pthread_mutexattr_t mattr;
bh_assert(mutex);
ret = pthread_mutexattr_init(&mattr);
if (ret)
return BHT_ERROR;
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
ret = pthread_mutex_init(mutex, &mattr);
pthread_mutexattr_destroy(&mattr);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_mutex_destroy(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_destroy(mutex);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
locking the mutex indicates some logic error present in
the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_lock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_lock(mutex);
if (0 != ret) {
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", ret);
exit(-1);
}
}
int vm_mutex_trylock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_trylock(mutex);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
/* Returned error (EINVAL, EAGAIN and EPERM) from
unlocking the mutex indicates some logic error present
in the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_unlock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_unlock(mutex);
if (0 != ret) {
LOG_FATAL("vm mutex unlock failed (ret=%d)!\n", ret);
exit(-1);
}
}
int _vm_cond_init(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_init(cond, NULL) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_destroy(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_destroy(cond) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}

View File

@ -0,0 +1,71 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_time.h"
#include <unistd.h>
#include <stdio.h>
//#include <sys/timeb.h>
#include <time.h>
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
}
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
uint64 _bh_time_get_boot_millisecond()
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
}
/*
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
* @return milliseconds since from 1970.1.1.
*/
uint64 _bh_time_get_millisecond_from_1970()
{
struct timeval tv;
gettimeofday(&tv, NULL);
uint64 millisecondsSinceEpoch = (uint64_t)(tv.tv_sec) * 1000
+ (uint64_t)(tv.tv_usec) / 1000;
return millisecondsSinceEpoch;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
struct tm *ltp;
ltp = localtime(&time_sec);
if (ltp == NULL) {
return 0;
}
return strftime(s, max, format, ltp);
}

View File

@ -0,0 +1,84 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_ASSERT_H
#define _BH_ASSERT_H
#include "bh_config.h"
#include "bh_platform.h"
#ifdef BH_TEST
# ifndef BH_DEBUG
# error "BH_TEST should be defined under BH_DEBUG"
# endif
#endif
#ifdef BH_TEST
# if defined(WIN32) || defined(__linux__)
# else
# error "Test case can not run on the current platform"
# endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BH_DEBUG
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 void bh_debug_internal(const char *file_name, int line_number, const char *fmt, ...);
#if defined(WIN32) || defined(EMU)
# define bh_debug(fmt, ...) bh_debug_internal(__FILE__, __LINE__, fmt, __VA_ARGS__)
#elif defined(__linux__)
/*# define bh_debug(...) bh_debug_internal(__FILE__, __LINE__, ## __VA_ARGS__)*/
# define bh_debug bh_debug_internal(__FILE__, __LINE__, "");printf
#elif defined(PLATFORM_SEC)
# define bh_debug(fmt, ...) bh_debug_internal(__FILE__, __LINE__, fmt, __VA_ARGS__)
#else
# error "Unsupported platform"
#endif
#else
#define bh_debug if(0)printf
#endif
#define bh_assert_abort(x) do { \
if (!x) \
abort(); \
} while (0)
#ifdef BH_TEST
# define BH_STATIC
#else
# define BH_STATIC static
#endif
#ifdef __cplusplus
}
#endif
#endif
/* Local Variables: */
/* mode:c */
/* c-basic-offset: 4 */
/* indent-tabs-mode: nil */
/* End: */

View File

@ -0,0 +1,31 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file bh_config.h
* @date Tue Sep 13 14:53:17 2011
*
* @brief Configurations for different platforms and targets. Make
* sure all source files in Beihai project include this header file
* directly or indirectly.
*/
#ifndef BH_CONFIG
#include "config.h"
#endif /* end of BH_CONFIG */

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_DEFINITION_H
#define _BH_DEFINITION_H
#include "bh_config.h"
typedef enum {
BH_FAILED = -100,
BH_UNKOWN = -99,
BH_MAGIC_UNMATCH = -12,
BH_UNIMPLEMENTED = -11,
BH_INTR = -10,
BH_CLOSED = -9,
BH_BUFFER_OVERFLOW = -8, /* TODO: no used error, should remove*/
BH_NOT_SUPPORTED = -7,
BH_WEAR_OUT_VIOLATION = -6,
BH_NOT_FOUND = -5,
BH_INVALID_PARAMS = -4,
BH_ACCESS_DENIED = -3,
BH_OUT_OF_MEMORY = -2,
BH_INVALID = -1,
BH_SUCCESS = 0,
BH_TIMEOUT = 2
} bh_status;
#endif

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BH_PLATFORM_LOG
#define BH_PLATFORM_LOG
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
void bh_log_emit(const char *fmt, va_list ap);
int bh_fprintf(void *stream, const char *fmt, ...);
int bh_fflush(void *stream);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,409 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_THREAD_H
#define _BH_THREAD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_config.h"
#include "bh_platform.h"
#define BH_MAX_THREAD 32
#define BH_MAX_TLS_NUM 2
#define BHT_ERROR (-1)
#define BHT_TIMED_OUT (1)
#define BHT_OK (0)
#define BHT_NO_WAIT 0x00000000
#define BHT_WAIT_FOREVER 0xFFFFFFFF
/**
* vm_thread_sys_init
* initiation function for beihai thread system. Invoked at the beginning of beihai intiation.
*
* @return BH_SUCCESS if succuess.
*/
int _vm_thread_sys_init(void);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_sys_init_instr(const char*func_name);
#define vm_thread_sys_init(void) vm_thread_sys_init_instr(__FUNCTION__)
#else
#define vm_thread_sys_init _vm_thread_sys_init
#endif
void vm_thread_sys_destroy(void);
/**
* This function creates a thread
*
* @param p_tid [OUTPUT] the pointer of tid
* @param start main routine of the thread
* @param arg argument passed to main routine
* @param stack_size bytes of stack size
*
* @return BH_SUCCESS if success.
*/
int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_create_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, const char*func_name);
#define vm_thread_create(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, __FUNCTION__)
#else
#define vm_thread_create _vm_thread_create
#endif
/**
* This function creates a thread
*
* @param p_tid [OUTPUT] the pointer of tid
* @param start main routine of the thread
* @param arg argument passed to main routine
* @param stack_size bytes of stack size
* @param prio the priority
*
* @return BH_SUCCESS if success.
*/
int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_create_with_prio_instr(korp_tid *p_tid, thread_start_routine_t start, void *arg, unsigned int stack_size, int prio, const char*func_name);
#define vm_thread_create_with_prio(p_tid, start, arg, stack_size) vm_thread_create_instr(p_tid, start, arg, stack_size, prio, __FUNCTION__)
#else
#define vm_thread_create_with_prio _vm_thread_create_with_prio
#endif
/**
* This function never returns.
*
* @param code not used
*/
void vm_thread_exit(void *code);
/**
* This function gets current thread id
*
* @return current thread id
*/
korp_tid _vm_self_thread(void);
#ifdef _INSTRUMENT_TEST_ENABLED
korp_tid vm_self_thread_instr(const char*func_name);
#define vm_self_thread(void) vm_self_thread_instr(__FUNCTION__)
#else
#define vm_self_thread _vm_self_thread
#endif
/**
* This function saves a pointer in thread local storage. One thread can only save one pointer.
*
* @param idx tls array index
* @param ptr pointer need save as TLS
*
* @return BH_SUCCESS if success
*/
int _vm_tls_put(unsigned idx, void *ptr);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_tls_put_instr(unsigned idx, void *ptr, const char*func_name);
#define vm_tls_put(idx, ptr) vm_tls_put_instr(idx, ptr, __FUNCTION__)
#else
#define vm_tls_put _vm_tls_put
#endif
/**
* This function gets a pointer saved in TLS.
*
* @param idx tls array index
*
* @return the pointer saved in TLS.
*/
void *_vm_tls_get(unsigned idx);
#ifdef _INSTRUMENT_TEST_ENABLED
void *vm_tls_get_instr(unsigned idx, const char*func_name);
#define vm_tls_get(idx) vm_tls_get_instr(idx, __FUNCTION__)
#else
#define vm_tls_get _vm_tls_get
#endif
#define vm_thread_testcancel(void)
/**
* This function creates a non-recursive mutex
*
* @param mutex [OUTPUT] pointer to mutex initialized.
*
* @return BH_SUCCESS if success
*/
int _vm_mutex_init(korp_mutex *mutex);
#ifdef INSTRUMENT_TEST_ENABLED
int vm_mutex_init_instr(korp_mutex *mutex, const char*func_name);
#define vm_mutex_init(mutex) vm_mutex_init_instr(mutex, __FUNCTION__)
#else
#define vm_mutex_init _vm_mutex_init
#endif
/**
* This function creates a recursive mutex
*
* @param mutex [OUTPUT] pointer to mutex initialized.
*
* @return BH_SUCCESS if success
*/
int _vm_recursive_mutex_init(korp_mutex *mutex);
#ifdef INSTRUMENT_TEST_ENABLED
int vm_recursive_mutex_init_instr(korp_mutex *mutex, const char*func_name);
#define vm_recursive_mutex_init(mutex) vm_recursive_mutex_init_instr(mutex, __FUNCTION__)
#else
#define vm_recursive_mutex_init _vm_recursive_mutex_init
#endif
/**
* This function destorys a mutex
*
* @param mutex pointer to mutex need destory
*
* @return BH_SUCCESS if success
*/
int _vm_mutex_destroy(korp_mutex *mutex);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_mutex_destroy_instr(korp_mutex *mutex, const char*func_name);
#define vm_mutex_destroy(mutex) vm_mutex_destroy_instr(mutex, __FUNCTION__)
#else
#define vm_mutex_destroy _vm_mutex_destroy
#endif
/**
* This function locks the mutex
*
* @param mutex pointer to mutex need lock
*
* @return Void
*/
void vm_mutex_lock(korp_mutex *mutex);
/**
* This function locks the mutex without waiting
*
* @param mutex pointer to mutex need lock
*
* @return BH_SUCCESS if success
*/
int vm_mutex_trylock(korp_mutex *mutex);
/**
* This function unlocks the mutex
*
* @param mutex pointer to mutex need unlock
*
* @return Void
*/
void vm_mutex_unlock(korp_mutex *mutex);
/**
* This function creates a semaphone
*
* @param sem [OUTPUT] pointer to semaphone
* @param c counter of semaphone
*
* @return BH_SUCCESS if success
*/
int _vm_sem_init(korp_sem *sem, unsigned int c);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_sem_init_instr(korp_sem *sem, unsigned int c, const char*func_name);
#define vm_sem_init(sem, c) vm_sem_init_instr(sem, c, __FUNCTION__)
#else
#define vm_sem_init _vm_sem_init
#endif
/**
* This function destroys a semaphone
*
* @param sem pointer to semaphone need destroy
*
* @return BH_SUCCESS if success
*/
int _vm_sem_destroy(korp_sem *sem);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_sem_destroy_instr(korp_sem *sem, const char*func_name);
#define vm_sem_destroy(sem) vm_sem_destroy_instr(sem, __FUNCTION__)
#else
#define vm_sem_destroy _vm_sem_destroy
#endif
/**
* This function performs wait operation on semaphone
*
* @param sem pointer to semaphone need perform wait operation
*
* @return BH_SUCCESS if success
*/
int _vm_sem_wait(korp_sem *sem);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_sem_wait_instr(korp_sem *sem, const char*func_name);
#define vm_sem_wait(sem) vm_sem_wait_instr(sem, __FUNCTION__)
#else
#define vm_sem_wait _vm_sem_wait
#endif
/**
* This function performs wait operation on semaphone with a timeout
*
* @param sem pointer to semaphone need perform wait operation
* @param mills wait milliseconds to return
*
* @return BH_SUCCESS if success
* @return BH_TIMEOUT if time out
*/
int _vm_sem_reltimedwait(korp_sem *sem, int mills);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_sem_reltimedwait_instr(korp_sem *sem, int mills, const char*func_name);
#define vm_sem_reltimedwait(sem, mills) vm_sem_reltimedwait_instr(sem, mills, __FUNCTION__)
#else
#define vm_sem_reltimedwait _vm_sem_reltimedwait
#endif
/**
* This function performs post operation on semaphone
*
* @param sem pointer to semaphone need perform post operation
*
* @return BH_SUCCESS if success
*/
int _vm_sem_post(korp_sem *sem);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_sem_post_instr(korp_sem *sem, const char*func_name);
#define vm_sem_post(sem) vm_sem_post_instr(sem, __FUNCTION__)
#else
#define vm_sem_post _vm_sem_post
#endif
/**
* This function creates a condition variable
*
* @param cond [OUTPUT] pointer to condition variable
*
* @return BH_SUCCESS if success
*/
int _vm_cond_init(korp_cond *cond);
#ifdef INSTRUMENT_TEST_ENABLED
int vm_cond_init_instr(korp_cond *cond, const char*func_name);
#define vm_cond_init(cond) vm_cond_init_instr(cond, __FUNCTION__)
#else
#define vm_cond_init _vm_cond_init
#endif
/**
* This function destorys condition variable
*
* @param cond pointer to condition variable
*
* @return BH_SUCCESS if success
*/
int _vm_cond_destroy(korp_cond *cond);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_cond_destroy_instr(korp_cond *cond, const char*func_name);
#define vm_cond_destroy(cond) vm_cond_destroy_instr(cond, __FUNCTION__)
#else
#define vm_cond_destroy _vm_cond_destroy
#endif
/**
* This function will block on a condition varible.
*
* @param cond pointer to condition variable
* @param mutex pointer to mutex to protect the condition variable
*
* @return BH_SUCCESS if success
*/
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_cond_wait_instr(korp_cond *cond, korp_mutex *mutex, const char*func_name);
#define vm_cond_wait(cond, mutex) vm_cond_wait_instr(cond, mutex, __FUNCTION__)
#else
#define vm_cond_wait _vm_cond_wait
#endif
/**
* This function will block on a condition varible or return if time specified passes.
*
* @param cond pointer to condition variable
* @param mutex pointer to mutex to protect the condition variable
* @param mills milliseconds to wait
*
* @return BH_SUCCESS if success
*/
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_cond_reltimedwait_instr(korp_cond *cond, korp_mutex *mutex, int mills, const char*func_name);
#define vm_cond_reltimedwait(cond, mutex, mills) vm_cond_reltimedwait_instr(cond, mutex, mills, __FUNCTION__)
#else
#define vm_cond_reltimedwait _vm_cond_reltimedwait
#endif
/**
* This function signals the condition variable
*
* @param cond condition variable
*
* @return BH_SUCCESS if success
*/
int _vm_cond_signal(korp_cond *cond);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_cond_signal_instr(korp_cond *cond, const char*func_name);
#define vm_cond_signal(cond) vm_cond_signal_instr(cond, __FUNCTION__)
#else
#define vm_cond_signal _vm_cond_signal
#endif
int _vm_cond_broadcast(korp_cond *cond);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_cond_broadcast_instr(korp_cond *cond, const char*func_name);
#define vm_cond_broadcast(cond) vm_cond_broadcast_instr(cond, __FUNCTION__)
#else
#define vm_cond_broadcast _vm_cond_broadcast
#endif
int _vm_thread_cancel(korp_tid thread);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_cancel_instr(korp_tid thread, const char*func_name);
#define vm_thread_cancel(thread) vm_thread_cancel_instr(thread, __FUNCTION__)
#else
#define vm_thread_cancel _vm_thread_cancel
#endif
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_join_instr(korp_tid thread, void **value_ptr, int mills, const char*func_name);
#define vm_thread_join(thread, value_ptr, mills) vm_thread_join_instr(thread, value_ptr, mills, __FUNCTION__)
#else
#define vm_thread_join _vm_thread_join
#endif
int _vm_thread_detach(korp_tid thread);
#ifdef _INSTRUMENT_TEST_ENABLED
int vm_thread_detach_instr(korp_tid thread, const char*func_name);
#define vm_thread_detach(thread) vm_thread_detach_instr(thread, __FUNCTION__)
#else
#define vm_thread_detach _vm_thread_detach
#endif
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_THREAD_H */

View File

@ -0,0 +1,98 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_TIME_H
#define _BH_TIME_H
#ifdef __cplusplus
extern "C" {
#endif
#include "bh_config.h"
#include "bh_definition.h"
#include "bh_types.h"
#include "bh_platform.h"
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
extern uint64 _bh_time_get_tick_millisecond(void);
#ifdef _INSTRUMENT_TEST_ENABLED
extern uint64 bh_time_get_tick_millisecond_instr(const char*func_name);
#define bh_time_get_tick_millisecond() bh_time_get_tick_millisecond_instr(__FUNCTION__)
#else
#define bh_time_get_tick_millisecond _bh_time_get_tick_millisecond
#endif
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
extern uint64 _bh_time_get_boot_millisecond(void);
#ifdef _INSTRUMENT_TEST_ENABLED
extern uint64 bh_time_get_boot_millisecond_instr(const char*func_name);
#define bh_time_get_boot_millisecond() bh_time_get_boot_millisecond_instr(__FUNCTION__)
#else
#define bh_time_get_boot_millisecond _bh_time_get_boot_millisecond
#endif
extern uint32 bh_get_tick_sec();
#define bh_get_tick_ms _bh_time_get_boot_millisecond
/*
* This function returns GMT milliseconds since from 1970.1.1, AKA UNIX time.
* @return milliseconds since from 1970.1.1.
*/
extern uint64 _bh_time_get_millisecond_from_1970(void);
#ifdef _INSTRUMENT_TEST_ENABLED
extern uint64 bh_time_get_millisecond_from_1970_instr(const char*func_name);
#define bh_time_get_millisecond_from_1970() bh_time_get_millisecond_from_1970_instr(__FUNCTION__)
#else
#define bh_time_get_millisecond_from_1970 _bh_time_get_millisecond_from_1970
#endif
/**
* This function sets timezone with specific hours.
*
* @param hours represents the deviation (in hours) of the local time from GMT (can be a positive or a negative number)
* @param half_hour if true, adds half an hour to the local time calculation. For example, if hours=(+5) then the time will be GMT +5:30; if hours=(-5) then the time will be GMT -4:30.
* @param daylight_save if true, applies the daylight saving scheme when calculating the local time (adds one hour to the local time calculation)
*/
extern void bh_set_timezone(int hours, int half_hour, int daylight_save);
/**
* This functions returns the offset in seconds which needs to be added GMT to get the local time.
*
*
* @return offset in secords which needs to be added GMT to get the local time.
*/
extern int bh_get_timezone_offset(void);
size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time);
#ifdef _INSTRUMENT_TEST_ENABLED
size_t bh_time_strftime_instr(char *s, size_t max, const char *format, int64 time, const char*func_name);
#define bh_time_strftime(s, max, format, time) bh_time_strftime_instr(s, max, format, time, __FUNCTION__)
#else
#define bh_time_strftime _bh_time_strftime
#endif
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,51 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_TYPES_H
#define _BH_TYPES_H
#include "bh_config.h"
typedef unsigned char uint8;
typedef char int8;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned int uint32;
typedef int int32;
#define BYTES_OF_UINT8 1
#define BYTES_OF_UINT16 2
#define BYTES_OF_UINT32 4
#define BYTES_OF_UINT64 8
#define BYTES_OF_BOOLEAN 1
#define BYTES_OF_BYTE 1
#define BYTES_OF_CHAR 2
#define BYTES_OF_SHORT 2
#define BYTES_OF_INT 4
#define BYTES_OF_FLOAT 4
#define BYTES_OF_LONG 8
#define BYTES_OF_DOUBLE 8
#include "bh_platform.h"
#ifndef __cplusplus
#define true 1
#define false 0
#endif
#endif

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include "bh_assert.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef BH_TEST
#include <setjmp.h>
#endif
#ifdef BH_TEST
/* for exception throwing */
jmp_buf bh_test_jb;
#endif
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
if (!file_name)
file_name = "NULL FILENAME";
if (!expr_string)
expr_string = "NULL EXPR_STRING";
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
file_name, line_number);
#ifdef BH_TEST
longjmp(bh_test_jb, 1);
#endif
abort();
}
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
...)
{
#ifndef JEFF_TEST_VERIFIER
va_list args;
va_start(args, fmt);
bh_assert(file_name);
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
vprintf(fmt, args);
va_end(args);
printf("\n");
#endif
}

View File

@ -0,0 +1,81 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_definition.h"
#include "bh_platform.h"
int bh_return(int ret)
{
return ret;
}
#define RSIZE_MAX 0x7FFFFFFF
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
{
char *dest = (char*) s1;
char *src = (char*) s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}
int b_strcat_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcat(s1, s2);
return 0;
}
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcpy(s1, s2);
return 0;
}
int fopen_s(FILE ** pFile, const char *filename, const char *mode)
{
if (NULL == pFile || NULL == filename || NULL == mode) {
return -1;
}
*pFile = fopen(filename, mode);
if (NULL == *pFile)
return -1;
return 0;
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
return s1;
}

View File

@ -0,0 +1,129 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_PLATFORM_H
#define _BH_PLATFORM_H
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <inttypes.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
#ifdef __cplusplus
extern "C" {
#endif
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)
#define BH_PLATFORM "Linux"
/* NEED qsort */
#include <stdarg.h>
#include <ctype.h>
#include <pthread.h>
#include <limits.h>
#include <semaphore.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet manager thread. */
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of HMC thread. */
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of watchdog thread. */
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of remote invoke listen thread. */
#define BH_REMOTE_INVOKE_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of remote post listen thread. */
#define BH_REMOTE_POST_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Maximal recursion depth of interpreter. */
#define BH_MAX_INTERP_RECURSION_DEPTH 8
/* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 0
#define BH_ROUTINE_MODIFIER
#define BHT_TIMEDOUT ETIMEDOUT
#define INVALID_THREAD_ID 0xFFffFFff
#define INVALID_SEM_ID SEM_FAILED
typedef pthread_t korp_tid;
typedef pthread_mutex_t korp_mutex;
typedef sem_t korp_sem;
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
double fmod(double x, double y);
float fmodf(float x, float y);
/* Definitions for applet debugging */
#define APPLET_DEBUG_LISTEN_PORT 8000
#define BH_SOCKET_INVALID_SOCK -1
#define BH_WAIT_FOREVER 0xFFFFFFFF
typedef int bh_socket_t;
#ifndef NULL
# define NULL ((void*) 0)
#endif
#define bh_assert assert
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);
extern int fopen_s(FILE ** pFile, const char *filename, const char *mode);
extern char *bh_strdup(const char *s);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include <stdio.h>
void bh_log_emit(const char *fmt, va_list ap)
{
vprintf(fmt, ap);
fflush(stdout);
}
int bh_fprintf(FILE *stream, const char *fmt, ...)
{
va_list ap;
int ret;
va_start(ap, fmt);
ret = vfprintf(stream ? stream : stdout, fmt, ap);
va_end(ap);
return ret;
}
int bh_fflush(void *stream)
{
return fflush(stream ? stream : stdout);
}

View File

@ -0,0 +1,404 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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>
static bool is_thread_sys_inited = false;
static korp_mutex thread_list_lock;
static pthread_key_t thread_local_storage_key[BH_MAX_TLS_NUM];
int _vm_thread_sys_init()
{
unsigned i, j;
int ret;
if (is_thread_sys_inited)
return 0;
for (i = 0; i < BH_MAX_TLS_NUM; i++) {
ret = pthread_key_create(&thread_local_storage_key[i], NULL);
if (ret)
goto fail;
}
ret = vm_mutex_init(&thread_list_lock);
if (ret)
goto fail;
is_thread_sys_inited = true;
return 0;
fail: for (j = 0; j < i; j++)
pthread_key_delete(thread_local_storage_key[j]);
return -1;
}
void vm_thread_sys_destroy(void)
{
if (is_thread_sys_inited) {
unsigned i;
for (i = 0; i < BH_MAX_TLS_NUM; i++)
pthread_key_delete(thread_local_storage_key[i]);
vm_mutex_destroy(&thread_list_lock);
is_thread_sys_inited = false;
}
}
typedef struct {
thread_start_routine_t start;
void* stack;
int stack_size;
void* arg;
} thread_wrapper_arg;
static void *vm_thread_wrapper(void *arg)
{
thread_wrapper_arg * targ = arg;
LOG_VERBOSE("THREAD CREATE 0x%08x\n", &targ);
targ->stack = (void *) ((unsigned int) (&arg) & ~0xfff);
_vm_tls_put(1, targ);
targ->start(targ->arg);
bh_free(targ);
_vm_tls_put(1, NULL);
return NULL;
}
int _vm_thread_create_with_prio(korp_tid *tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{
pthread_attr_t tattr;
thread_wrapper_arg *targ;
bh_assert(stack_size > 0);
bh_assert(tid);
bh_assert(start);
*tid = INVALID_THREAD_ID;
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
if (pthread_attr_setstacksize(&tattr, stack_size) != 0) {
bh_debug("Invalid thread stack size %u. Min stack size on Linux = %u",
stack_size, PTHREAD_STACK_MIN);
pthread_attr_destroy(&tattr);
return BHT_ERROR;
}
targ = (thread_wrapper_arg*) bh_malloc(sizeof(*targ));
if (!targ) {
pthread_attr_destroy(&tattr);
return BHT_ERROR;
}
targ->start = start;
targ->arg = arg;
targ->stack_size = stack_size;
if (pthread_create(tid, &tattr, vm_thread_wrapper, targ) != 0) {
pthread_attr_destroy(&tattr);
bh_free(targ);
return BHT_ERROR;
}
pthread_attr_destroy(&tattr);
return BHT_OK;
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{
return _vm_thread_create_with_prio(tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
}
korp_tid _vm_self_thread()
{
return (korp_tid) pthread_self();
}
void vm_thread_exit(void * code)
{
bh_free(_vm_tls_get(1));
_vm_tls_put(1, NULL);
pthread_exit(code);
}
void *_vm_tls_get(unsigned idx)
{
bh_assert(idx < BH_MAX_TLS_NUM);
return pthread_getspecific(thread_local_storage_key[idx]);
}
int _vm_tls_put(unsigned idx, void * tls)
{
bh_assert(idx < BH_MAX_TLS_NUM);
pthread_setspecific(thread_local_storage_key[idx], tls);
return BHT_OK;
}
int _vm_mutex_init(korp_mutex *mutex)
{
return pthread_mutex_init(mutex, NULL) == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_recursive_mutex_init(korp_mutex *mutex)
{
int ret;
pthread_mutexattr_t mattr;
bh_assert(mutex);
ret = pthread_mutexattr_init(&mattr);
if (ret)
return BHT_ERROR;
pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE_NP);
ret = pthread_mutex_init(mutex, &mattr);
pthread_mutexattr_destroy(&mattr);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_mutex_destroy(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_destroy(mutex);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
/* Returned error (EINVAL, EAGAIN and EDEADLK) from
locking the mutex indicates some logic error present in
the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_lock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_lock(mutex);
if (0 != ret) {
printf("vm mutex lock failed (ret=%d)!\n", ret);
exit(-1);
}
}
int vm_mutex_trylock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_trylock(mutex);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
/* Returned error (EINVAL, EAGAIN and EPERM) from
unlocking the mutex indicates some logic error present
in the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_unlock(korp_mutex *mutex)
{
int ret;
bh_assert(mutex);
ret = pthread_mutex_unlock(mutex);
if (0 != ret) {
printf("vm mutex unlock failed (ret=%d)!\n", ret);
exit(-1);
}
}
int _vm_sem_init(korp_sem* sem, unsigned int c)
{
int ret;
bh_assert(sem);
ret = sem_init(sem, 0, c);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_sem_destroy(korp_sem *sem)
{
int ret;
bh_assert(sem);
ret = sem_destroy(sem);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_sem_wait(korp_sem *sem)
{
int ret;
bh_assert(sem);
ret = sem_wait(sem);
return ret == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
{
int ret = BHT_OK;
struct timespec timeout;
const int mills_per_sec = 1000;
const int mills_to_nsec = 1E6;
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER) {
ret = sem_wait(sem);
} else {
timeout.tv_sec = mills / mills_per_sec;
timeout.tv_nsec = (mills % mills_per_sec) * mills_to_nsec;
timeout.tv_sec += time(NULL);
ret = sem_timedwait(sem, &timeout);
}
if (ret != BHT_OK) {
if (errno == BHT_TIMEDOUT) {
ret = BHT_TIMEDOUT;
errno = 0;
} else {
bh_debug("Faliure happens when timed wait is called");
bh_assert(0);
}
}
return ret;
}
int _vm_sem_post(korp_sem *sem)
{
bh_assert(sem);
return sem_post(sem) == 0 ? BHT_OK : BHT_ERROR;
}
int _vm_cond_init(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_init(cond, NULL) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_destroy(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_destroy(cond) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
{
bh_assert(cond);
bh_assert(mutex);
if (pthread_cond_wait(cond, mutex) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
static void msec_nsec_to_abstime(struct timespec *ts, int64 msec, int32 nsec)
{
struct timeval tv;
gettimeofday(&tv, NULL);
ts->tv_sec = tv.tv_sec + msec / 1000;
ts->tv_nsec = tv.tv_usec * 1000 + (msec % 1000) * 1000000 + nsec;
if (ts->tv_nsec >= 1000000000L) {
ts->tv_sec++;
ts->tv_nsec -= 1000000000L;
}
}
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
{
int ret;
struct timespec abstime;
if (mills == BHT_WAIT_FOREVER)
ret = pthread_cond_wait(cond, mutex);
else {
msec_nsec_to_abstime(&abstime, mills, 0);
ret = pthread_cond_timedwait(cond, mutex, &abstime);
}
if (ret != BHT_OK && ret != BHT_TIMEDOUT)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_signal(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_signal(cond) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_broadcast(korp_cond *cond)
{
bh_assert(cond);
if (pthread_cond_broadcast(cond) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_thread_cancel(korp_tid thread)
{
return pthread_cancel(thread);
}
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
{
return pthread_join(thread, value_ptr);
}
int _vm_thread_detach(korp_tid thread)
{
return pthread_detach(thread);
}

View File

@ -0,0 +1,80 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_time.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/timeb.h>
#include <time.h>
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
uint64 _bh_time_get_tick_millisecond()
{
return sysconf(_SC_CLK_TCK);
}
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
uint64 _bh_time_get_boot_millisecond()
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
return 0;
}
return ((uint64) ts.tv_sec) * 1000 + ts.tv_nsec / (1000 * 1000);
}
uint32 bh_get_tick_sec()
{
return _bh_time_get_boot_millisecond() / 1000;
}
/*
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
* @return milliseconds since from 1970.1.1.
*/
uint64 _bh_time_get_millisecond_from_1970()
{
struct timeb tp;
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
}
size_t _bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
struct timeb tp;
struct tm *ltp;
ftime(&tp);
time_sec -= tp.timezone * 60;
ltp = localtime(&time_sec);
if (ltp == NULL) {
return 0;
}
return strftime(s, max, format, ltp);
}

View File

@ -0,0 +1,24 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all})

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include "bh_assert.h"
#include <stdarg.h>
#ifdef BH_TEST
#include <setjmp.h>
#endif
#ifdef BH_TEST
/* for exception throwing */
jmp_buf bh_test_jb;
#endif
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
if (!file_name)
file_name = "NULL FILENAME";
if (!expr_string)
expr_string = "NULL EXPR_STRING";
printf("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
file_name, line_number);
#ifdef BH_TEST
longjmp(bh_test_jb, 1);
#endif
abort();
}
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
...)
{
#ifndef JEFF_TEST_VERIFIER
va_list args;
va_start(args, fmt);
bh_assert(file_name);
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
vprintf(fmt, args);
va_end(args);
printf("\n");
#endif
}

View File

@ -0,0 +1,22 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_definition.h"
int bh_return(int ret)
{
return ret;
}

View File

@ -0,0 +1,93 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_PLATFORM_H
#define _BH_PLATFORM_H
#include "bh_config.h"
#include "bh_types.h"
#ifndef NVALGRIND
#define NVALGRIND
#endif
/* Reserve bytes on applet stack for native functions called from
* Java methods to avoid undetectable stack overflow.
*/
#ifndef APPLET_PRESERVED_STACK_SIZE
#define APPLET_PRESERVED_STACK_SIZE (16 * BH_KB)
#endif
typedef unsigned __int64 uint64;
typedef __int64 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_INVALID_HANDLE
#define BH_INVALID_HANDLE NULL
#endif
#define BH_PLATFORM "AMULET"
#include <stdarg.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define _STACK_SIZE_ADJUSTMENT (32 * 1024)
/* Stack size of applet manager thread. */
#define BH_APPLET_MANAGER_THREAD_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of HMC thread. */
#define BH_HMC_THREAD_STACK_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of watchdog thread. */
#define BH_WATCHDOG_THREAD_SIZE (4 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Stack size of applet threads's native part. */
#define BH_APPLET_PRESERVED_STACK_SIZE (8 * 1024 + _STACK_SIZE_ADJUSTMENT)
/* Maximal recursion depth of interpreter. */
#define BH_MAX_INTERP_RECURSION_DEPTH 8
#define wa_malloc bh_malloc
#define wa_free bh_free
#define snprintf _snprintf
#define BH_ROUTINE_MODIFIER __stdcall
typedef void *korp_tid;
#define INVALID_THREAD_ID 0
typedef void *korp_mutex;
typedef void *korp_sem;
typedef struct {
korp_sem s;
unsigned waiting_count;
} korp_cond;
typedef void* (BH_ROUTINE_MODIFIER *thread_start_routine_t)(void*);
#endif

View File

@ -0,0 +1,39 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "bh_platform.h"
void bh_log_emit(const char *fmt, va_list ap)
{
vprintf(fmt, ap);
fflush(stdout);
}
int bh_fprintf(FILE *stream, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vfprintf(stream ? stream : stdout, fmt, ap);
va_end(ap);
return ret;
}
int bh_fflush(void *stream)
{
return fflush(stream ? stream : stdout);
}

View File

@ -0,0 +1,342 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_thread.h"
#include "bh_assert.h"
#include "bh_log.h"
#include "bh_memory.h"
#include <windows.h>
#include <process.h>
#ifdef _DEBUG
#define THREAD_STACK_ADJUSTMENT (32 * 1024)
#else
#define THREAD_STACK_ADJUSTMENT 0
#endif
static korp_mutex thread_list_lock;
static DWORD tls_indexes[BH_MAX_TLS_NUM];
typedef struct {
int zero_padding;
thread_start_routine_t start;
void* stack;
void* args;
int stack_size;
} vm_thread_block;
static DWORD tb_index;
int _vm_thread_sys_init()
{
unsigned int i;
for (i = 0; i < BH_MAX_TLS_NUM; i++) {
tls_indexes[i] = TlsAlloc();
if (tls_indexes[i] == TLS_OUT_OF_INDEXES)
return BHT_ERROR;
}
tb_index = TlsAlloc();
if (tb_index == TLS_OUT_OF_INDEXES)
return BHT_ERROR;
return vm_mutex_init(&thread_list_lock);
}
static unsigned int BH_ROUTINE_MODIFIER beihai_starter(void* arg)
{
vm_thread_block* tb = (vm_thread_block*) arg;
TlsSetValue(tb_index, tb);
tb->stack = (void *) &arg;
tb->start(tb->args);
return 0;
}
int _vm_thread_create(korp_tid *tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{
unsigned int default_stack_size = 20 * 1024;
vm_thread_block* tb;
bh_assert(tid);
bh_assert(start);
if (stack_size == 0)
stack_size = default_stack_size;
#ifdef _DEBUG
stack_size = THREAD_STACK_ADJUSTMENT + stack_size*3;
#endif
tb = (vm_thread_block*) bh_malloc(sizeof(*tb));
if (tb == NULL)
return BHT_ERROR;
memset(tb, 0, sizeof(*tb));
tb->start = start;
tb->stack_size = stack_size;
tb->args = arg;
*tid = (korp_tid) _beginthreadex(NULL, stack_size, beihai_starter,
(void*) tb, 0, NULL);
/* TODO: to deal with the handle; how to close it? */
return (*tid == INVALID_THREAD_ID) ? BHT_ERROR : BHT_OK;
}
korp_tid _vm_self_thread()
{
return (korp_tid) GetCurrentThread();
}
void vm_thread_exit(void *code)
{
vm_thread_block *tb = (vm_thread_block*) TlsGetValue(tb_index);
bh_free(tb);
_endthreadex((unsigned int) code);
}
void* vm_get_stackaddr()
{
vm_thread_block *tb = (vm_thread_block*) TlsGetValue(tb_index);
return (char *) tb->stack + THREAD_STACK_ADJUSTMENT - tb->stack_size;
}
void *_vm_tls_get(unsigned idx)
{
bh_assert(idx < BH_MAX_TLS_NUM);
return TlsGetValue(tls_indexes[idx]);
}
int _vm_tls_put(unsigned idx, void *tls)
{
BOOL r;
bh_assert(idx < BH_MAX_TLS_NUM);
r = TlsSetValue(tls_indexes[idx], tls);
return (r == FALSE) ? BHT_ERROR : BHT_OK;
}
int _vm_mutex_init(korp_mutex *mutex)
{
bh_assert(mutex);
*mutex = CreateMutex(NULL, FALSE, NULL);
return (*mutex == 0) ? BHT_ERROR : BHT_OK;
}
int _vm_mutex_destroy(korp_mutex *mutex)
{
return BHT_OK;
}
/* Returned error (e.g. ERROR_INVALID_HANDLE) from
locking the mutex indicates some logic error present in
the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_lock(korp_mutex *mutex)
{
DWORD ret;
bh_assert(mutex);
ret = WaitForSingleObject(*mutex, INFINITE);
if (WAIT_FAILED == ret) {
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", GetLastError());
exit(-1);
}
}
int vm_mutex_trylock(korp_mutex *mutex)
{
DWORD ret;
bh_assert(mutex);
ret = WaitForSingleObject(*mutex, 0);
if (WAIT_FAILED == ret) {
LOG_FATAL("vm mutex lock failed (ret=%d)!\n", GetLastError());
exit(-1);
}
return ret == WAIT_OBJECT_0 ? BHT_OK : BHT_ERROR;
}
/* Returned error (e.g. ERROR_INVALID_HANDLE) from
unlocking the mutex indicates some logic error present
in the program somewhere.
Don't try to recover error for an existing unknown error.*/
void vm_mutex_unlock(korp_mutex *mutex)
{
BOOL ret;
bh_assert(mutex);
ret = ReleaseMutex(*mutex);
if (FALSE == ret) {
LOG_FATAL("vm mutex unlock failed (ret=%d)!\n", GetLastError());
exit(-1);
}
}
#define BH_SEM_COUNT_MAX 0xFFFF
int _vm_sem_init(korp_sem *sem, unsigned int count)
{
bh_assert(sem);
bh_assert(count <= BH_SEM_COUNT_MAX);
*sem = CreateSemaphore(NULL, count, BH_SEM_COUNT_MAX, NULL);
return (*sem == NULL) ? BHT_ERROR : BHT_OK;
}
int _vm_sem_destroy(korp_sem *sem)
{
return BHT_OK;
}
int _vm_sem_P(korp_sem *sem)
{
DWORD r;
bh_assert(sem);
r = WaitForSingleObject(*sem, INFINITE);
return (r == WAIT_FAILED) ? BHT_ERROR : BHT_OK;
}
int _vm_sem_reltimedP(korp_sem *sem, int mills)
{
DWORD r;
bh_assert(sem);
if (mills == BHT_WAIT_FOREVER)
mills = INFINITE;
r = WaitForSingleObject(*sem, (unsigned int) mills);
switch (r) {
case WAIT_OBJECT_0:
return BHT_OK;
case WAIT_TIMEOUT:
return BHT_TIMEDOUT;
default:
return BHT_ERROR;
}
}
int _vm_sem_V(korp_sem *sem)
{
BOOL r;
bh_assert(sem);
r = ReleaseSemaphore(*sem, 1, NULL);
return (r == FALSE) ? BHT_ERROR : BHT_OK;
}
int _vm_cond_init(korp_cond *cond)
{
bh_assert(cond);
cond->waiting_count = 0;
return vm_sem_init(&cond->s, 0);
}
int _vm_cond_destroy(korp_cond *cond)
{
bh_assert(cond);
return vm_sem_destroy(&cond->s);
}
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
{
bh_assert(cond);
bh_assert(mutex);
cond->waiting_count++;
vm_mutex_unlock(mutex);
if (vm_sem_P(&cond->s) != BHT_OK)
return BHT_ERROR;
vm_mutex_lock(mutex);
cond->waiting_count--;
return BHT_OK;
}
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
{
int r;
bh_assert(cond);
bh_assert(mutex);
cond->waiting_count++;
vm_mutex_unlock(mutex);
r = vm_sem_reltimedP(&cond->s, mills);
if ((r != BHT_OK) && (r != BHT_TIMEDOUT))
return BHT_ERROR;
vm_mutex_lock(mutex);
cond->waiting_count--;
return r;
}
int _vm_cond_signal(korp_cond *cond)
{
bh_assert(cond);
if (cond->waiting_count == 0)
return BHT_OK;
if (vm_sem_V(&cond->s) != BHT_OK)
return BHT_ERROR;
return BHT_OK;
}
int _vm_cond_broadcast(korp_cond *cond)
{
/* FIXME: use pthread's API to implement this and above
functions. */
unsigned count = cond->waiting_count;
for (; count > 0; count--)
vm_sem_V(&cond->s);
return BHT_OK;
}
int _vm_thread_cancel(korp_tid thread)
{
/* FIXME: implement this with Windows API. */
return 0;
}
int _vm_thread_join(korp_tid thread, void **value_ptr)
{
/* FIXME: implement this with Windows API. */
return 0;
}
int _vm_thread_detach(korp_tid thread)
{
/* FIXME: implement this with Windows API. */
return 0;
}

View File

@ -0,0 +1,101 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "bh_time.h"
#include <time.h>
#include <sys/timeb.h>
/* Since GetTickCount64 is not supported on Windows XP, use a temporary method
* to solve the issue. However, GetTickCount return a DWORD value and overflow
* may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
*
* TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
*/
#if (WINVER >= 0x0600)
extern uint64 __stdcall GetTickCount64(void);
#else
extern uint32 __stdcall GetTickCount(void);
#endif
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
uint64 _bh_time_get_tick_millisecond()
{
return 5;
}
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
uint64 _bh_time_get_boot_millisecond()
{
/* Since GetTickCount64 is not supported on Windows XP, use a temporary method
* to solve the issue. However, GetTickCount return a DWORD value and overflow
* may happen (http://msdn.microsoft.com/en-us/library/ms724408(v=vs.85).aspx).
*
* TODO: Implement GetTickCount64 on Windows XP by self or check overflow issues.
*/
#if (WINVER >= 0x0600)
return GetTickCount64();
#else
return GetTickCount();
#endif
}
/*
* This function returns GMT time milliseconds since from 1970.1.1, AKA UNIX time.
* @return milliseconds since from 1970.1.1.
*/
uint64 _bh_time_get_millisecond_from_1970()
{
struct timeb tp;
ftime(&tp);
return ((uint64) tp.time) * 1000 + tp.millitm
- (tp.dstflag == 0 ? 0 : 60 * 60 * 1000) + tp.timezone * 60 * 1000;
}
size_t bh_time_strftime(char *s, size_t max, const char *format, int64 time)
{
time_t time_sec = time / 1000;
struct timeb tp;
struct tm local_time;
if (NULL == s)
return 0;
ftime(&tp);
time_sec -= tp.timezone * 60;
if (localtime_s(&local_time, &time_sec) != 0)
return 0;
return strftime(s, max, format, &local_time);
}
int bh_time_get(uint8 *timeoff_info, int16 timeoff_info_len, uint32* time)
{
return BH_UNIMPLEMENTED;
}
int bh_time_set(uint32 ntp, uint8 *timeoff_info)
{
return BH_UNIMPLEMENTED;
}

View File

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
obj-y += bh_assert.o bh_definition.o bh_memory.o bh_platform_log.o bh_thread.o bh_time.o

View File

@ -0,0 +1,69 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include "bh_assert.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef BH_TEST
#include <setjmp.h>
#endif
#ifdef BH_TEST
/* for exception throwing */
jmp_buf bh_test_jb;
#endif
extern void abort(void);
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
if (!file_name)
file_name = "NULL FILENAME";
if (!expr_string)
expr_string = "NULL EXPR_STRING";
printk("\nASSERTION FAILED: %s, at FILE=%s, LINE=%d\n", expr_string,
file_name, line_number);
#ifdef BH_TEST
longjmp(bh_test_jb, 1);
#endif
abort();
}
void bh_debug_internal(const char *file_name, int line_number, const char *fmt,
...)
{
#ifndef JEFF_TEST_VERIFIER
va_list args;
va_start(args, fmt);
bh_assert(file_name);
printf("\nDebug info FILE=%s, LINE=%d: ", file_name, line_number);
vprintf(fmt, args);
va_end(args);
printf("\n");
#endif
}

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_definition.h"
#include "bh_platform.h"
int bh_return(int ret)
{
return ret;
}
#define RSIZE_MAX 0x7FFFFFFF
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
{
char *dest = (char*) s1;
char *src = (char*) s2;
if (n == 0) {
return 0;
}
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memcpy(dest, src, n);
return 0;
}
int b_strcat_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcat(s1, s2);
return 0;
}
int b_strcpy_s(char * s1, size_t s1max, const char * s2)
{
if (NULL
== s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
return -1;
}
strcpy(s1, s2);
return 0;
}

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include <stdlib.h>
#include <string.h>
char *bh_strdup(const char *s)
{
char *s1 = NULL;
if (s && (s1 = bh_malloc(strlen(s) + 1)))
memcpy(s1, s, strlen(s) + 1);
return s1;
}

View File

@ -0,0 +1,104 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _BH_PLATFORM_H
#define _BH_PLATFORM_H
#include "bh_config.h"
#include "bh_types.h"
#include "bh_memory.h"
#include <autoconf.h>
#include <zephyr.h>
#include <kernel.h>
#include <misc/printk.h>
#include <inttypes.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifndef CONFIG_NET_BUF_USER_DATA_SIZE
#define CONFIG_NET_BUF_USER_DATA_SIZE 0
#endif
#include <net/net_pkt.h>
#include <net/net_if.h>
#include <net/net_core.h>
#include <net/net_context.h>
/* Platform name */
#define BH_PLATFORM "Zephyr"
#define BH_APPLET_PRESERVED_STACK_SIZE (2 * BH_KB)
/* Default thread priority */
#define BH_THREAD_DEFAULT_PRIORITY 7
#define BH_ROUTINE_MODIFIER
/* Invalid thread tid */
#define INVALID_THREAD_ID NULL
#define INVALID_SEM_ID SEM_FAILED
#define BH_WAIT_FOREVER K_FOREVER
typedef uint64_t uint64;
typedef int64_t int64;
typedef struct k_thread korp_thread;
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 {
struct k_mutex wait_list_lock;
bh_thread_wait_list thread_wait_list;
} korp_cond;
typedef void* (*thread_start_routine_t)(void*);
#define wa_malloc bh_malloc
#define wa_free bh_free
/* Unit test framework is based on C++, where the declaration of
snprintf is different. */
#ifndef __cplusplus
int snprintf(char *buffer, size_t count, const char *format, ...);
#endif
#ifndef NULL
#define NULL ((void*)0)
#endif
#define bh_assert(x) \
do { \
if (!(x)) { \
printk("bh_assert(%s, %d)\n", __func__, __LINE__);\
} \
} while (0)
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);
#endif

View File

@ -0,0 +1,68 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_platform.h"
#include <stdio.h>
struct out_context {
int count;
};
typedef int (*out_func_t)(int c, void *ctx);
extern void *__printk_get_hook(void);
static int (*_char_out)(int) = NULL;
static int char_out(int c, struct out_context *ctx)
{
ctx->count++;
if (_char_out == NULL) {
_char_out = __printk_get_hook();
}
return _char_out(c);
}
static int bh_vprintk(const char *fmt, va_list ap)
{
struct out_context ctx = { 0 };
_vprintk((out_func_t) char_out, &ctx, fmt, ap);
return ctx.count;
}
void bh_log_emit(const char *fmt, va_list ap)
{
bh_vprintk(fmt, ap);
}
int bh_fprintf(FILE *stream, const char *fmt, ...)
{
(void) stream;
va_list ap;
int ret;
va_start(ap, fmt);
ret = bh_vprintk(fmt, ap);
va_end(ap);
return ret;
}
int bh_fflush(void *stream)
{
(void) stream;
return 0;
}

View File

@ -0,0 +1,537 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#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;
bh_thread_wait_list next;
} bh_thread_wait_node;
typedef struct bh_thread_data {
/* Next thread data */
struct bh_thread_data *next;
/* Zephyr thread handle */
korp_tid tid;
/* Jeff thread local root */
void *tlr;
/* Lock for waiting list */
struct k_mutex wait_list_lock;
/* Waiting list of other threads who are joining this thread */
bh_thread_wait_list thread_wait_list;
/* Thread stack size */
unsigned stack_size;
/* Thread stack */
char stack[1];
} bh_thread_data;
typedef struct bh_thread_obj {
struct k_thread thread;
/* Whether the thread is terminated and this thread object is to
be freed in the future. */
bool to_be_freed;
struct bh_thread_obj *next;
} bh_thread_obj;
static bool is_thread_sys_inited = false;
/* Thread data of supervisor thread */
static bh_thread_data supervisor_thread_data;
/* Lock for thread data list */
static struct k_mutex thread_data_lock;
/* Thread data list */
static bh_thread_data *thread_data_list = NULL;
/* Lock for thread object list */
static struct k_mutex thread_obj_lock;
/* Thread object list */
static bh_thread_obj *thread_obj_list = NULL;
static void thread_data_list_add(bh_thread_data *thread_data)
{
k_mutex_lock(&thread_data_lock, K_FOREVER);
if (!thread_data_list)
thread_data_list = thread_data;
else {
/* If already in list, just return */
bh_thread_data *p = thread_data_list;
while (p) {
if (p == thread_data) {
k_mutex_unlock(&thread_data_lock);
return;
}
p = p->next;
}
/* Set as head of list */
thread_data->next = thread_data_list;
thread_data_list = thread_data;
}
k_mutex_unlock(&thread_data_lock);
}
static void thread_data_list_remove(bh_thread_data *thread_data)
{
k_mutex_lock(&thread_data_lock, K_FOREVER);
if (thread_data_list) {
if (thread_data_list == thread_data)
thread_data_list = thread_data_list->next;
else {
/* Search and remove it from list */
bh_thread_data *p = thread_data_list;
while (p && p->next != thread_data)
p = p->next;
if (p && p->next == thread_data)
p->next = p->next->next;
}
}
k_mutex_unlock(&thread_data_lock);
}
static bh_thread_data *
thread_data_list_lookup(k_tid_t tid)
{
k_mutex_lock(&thread_data_lock, K_FOREVER);
if (thread_data_list) {
bh_thread_data *p = thread_data_list;
while (p) {
if (p->tid == tid) {
/* Found */
k_mutex_unlock(&thread_data_lock);
return p;
}
p = p->next;
}
}
k_mutex_unlock(&thread_data_lock);
return NULL;
}
static void thread_obj_list_add(bh_thread_obj *thread_obj)
{
k_mutex_lock(&thread_obj_lock, K_FOREVER);
if (!thread_obj_list)
thread_obj_list = thread_obj;
else {
/* Set as head of list */
thread_obj->next = thread_obj_list;
thread_obj_list = thread_obj;
}
k_mutex_unlock(&thread_obj_lock);
}
static void thread_obj_list_reclaim()
{
bh_thread_obj *p, *p_prev;
k_mutex_lock(&thread_obj_lock, K_FOREVER);
p_prev = NULL;
p = thread_obj_list;
while (p) {
if (p->to_be_freed) {
if (p_prev == NULL) { /* p is the head of list */
thread_obj_list = p->next;
bh_free(p);
p = thread_obj_list;
} else { /* p is not the head of list */
p_prev->next = p->next;
bh_free(p);
p = p_prev->next;
}
} else {
p_prev = p;
p = p->next;
}
}
k_mutex_unlock(&thread_obj_lock);
}
int _vm_thread_sys_init()
{
if (is_thread_sys_inited)
return BHT_OK;
k_mutex_init(&thread_data_lock);
k_mutex_init(&thread_obj_lock);
/* Initialize supervisor thread data */
memset(&supervisor_thread_data, 0, sizeof(supervisor_thread_data));
supervisor_thread_data.tid = k_current_get();
/* Set as head of thread data list */
thread_data_list = &supervisor_thread_data;
is_thread_sys_inited = true;
return BHT_OK;
}
void vm_thread_sys_destroy(void)
{
if (is_thread_sys_inited) {
is_thread_sys_inited = false;
}
}
static bh_thread_data *
thread_data_current()
{
k_tid_t tid = k_current_get();
return thread_data_list_lookup(tid);
}
static void vm_thread_cleanup(void)
{
bh_thread_data *thread_data = thread_data_current();
bh_assert(thread_data != NULL);
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
if (thread_data->thread_wait_list) {
/* Signal each joining thread */
bh_thread_wait_list head = thread_data->thread_wait_list;
while (head) {
bh_thread_wait_list next = head->next;
k_sem_give(&head->sem);
bh_free(head);
head = next;
}
thread_data->thread_wait_list = NULL;
}
k_mutex_unlock(&thread_data->wait_list_lock);
thread_data_list_remove(thread_data);
/* 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);
}
static void vm_thread_wrapper(void *start, void *arg, void *thread_data)
{
/* Set thread custom data */
((bh_thread_data*) thread_data)->tid = k_current_get();
thread_data_list_add(thread_data);
((thread_start_routine_t) start)(arg);
vm_thread_cleanup();
}
int _vm_thread_create(korp_tid *p_tid, thread_start_routine_t start, void *arg,
unsigned int stack_size)
{
return _vm_thread_create_with_prio(p_tid, start, arg, stack_size,
BH_THREAD_DEFAULT_PRIORITY);
}
int _vm_thread_create_with_prio(korp_tid *p_tid, thread_start_routine_t start,
void *arg, unsigned int stack_size, int prio)
{
korp_tid tid;
bh_thread_data *thread_data;
unsigned thread_data_size;
if (!p_tid || !stack_size)
return BHT_ERROR;
/* Free the thread objects of terminated threads */
thread_obj_list_reclaim();
/* Create and initialize thread object */
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);
return BHT_ERROR;
}
memset(thread_data, 0, thread_data_size);
k_mutex_init(&thread_data->wait_list_lock);
thread_data->stack_size = stack_size;
thread_data->tid = tid;
/* Create the thread */
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);
return BHT_ERROR;
}
bh_assert(tid == thread_data->tid);
/* Set thread custom data */
thread_data_list_add(thread_data);
thread_obj_list_add((bh_thread_obj*) tid);
*p_tid = tid;
return BHT_OK;
}
korp_tid _vm_self_thread()
{
return (korp_tid) k_current_get();
}
void vm_thread_exit(void * code)
{
(void) code;
korp_tid self = vm_self_thread();
vm_thread_cleanup();
k_thread_abort((k_tid_t) self);
}
int _vm_thread_cancel(korp_tid thread)
{
k_thread_abort((k_tid_t) thread);
return 0;
}
int _vm_thread_join(korp_tid thread, void **value_ptr, int mills)
{
(void) value_ptr;
bh_thread_data *thread_data;
bh_thread_wait_node *node;
/* Create wait node and append it to wait list */
if (!(node = bh_malloc(sizeof(bh_thread_wait_node))))
return BHT_ERROR;
k_sem_init(&node->sem, 0, 1);
node->next = NULL;
/* Get thread data */
thread_data = thread_data_list_lookup(thread);
bh_assert(thread_data != NULL);
k_mutex_lock(&thread_data->wait_list_lock, K_FOREVER);
if (!thread_data->thread_wait_list)
thread_data->thread_wait_list = node;
else {
/* Add to end of waiting list */
bh_thread_wait_node *p = thread_data->thread_wait_list;
while (p->next)
p = p->next;
p->next = node;
}
k_mutex_unlock(&thread_data->wait_list_lock);
/* Wait the sem */
k_sem_take(&node->sem, mills);
/* Wait some time for the thread to be actually terminated */
k_sleep(100);
return BHT_OK;
}
int _vm_thread_detach(korp_tid thread)
{
(void) thread;
return BHT_OK;
}
void *_vm_tls_get(unsigned idx)
{
(void) idx;
bh_thread_data *thread_data;
bh_assert(idx == 0);
thread_data = thread_data_current();
return thread_data ? thread_data->tlr : NULL;
}
int _vm_tls_put(unsigned idx, void * tls)
{
bh_thread_data *thread_data;
(void) idx;
bh_assert(idx == 0);
thread_data = thread_data_current();
bh_assert(thread_data != NULL);
thread_data->tlr = tls;
return BHT_OK;
}
int _vm_mutex_init(korp_mutex *mutex)
{
(void) mutex;
k_mutex_init(mutex);
return BHT_OK;
}
int _vm_recursive_mutex_init(korp_mutex *mutex)
{
k_mutex_init(mutex);
return BHT_OK;
}
int _vm_mutex_destroy(korp_mutex *mutex)
{
(void) mutex;
return BHT_OK;
}
void vm_mutex_lock(korp_mutex *mutex)
{
k_mutex_lock(mutex, K_FOREVER);
}
int vm_mutex_trylock(korp_mutex *mutex)
{
return k_mutex_lock(mutex, K_NO_WAIT);
}
void vm_mutex_unlock(korp_mutex *mutex)
{
k_mutex_unlock(mutex);
}
int _vm_sem_init(korp_sem* sem, unsigned int c)
{
k_sem_init(sem, 0, c);
return BHT_OK;
}
int _vm_sem_destroy(korp_sem *sem)
{
(void) sem;
return BHT_OK;
}
int _vm_sem_wait(korp_sem *sem)
{
return k_sem_take(sem, K_FOREVER);
}
int _vm_sem_reltimedwait(korp_sem *sem, int mills)
{
return k_sem_take(sem, mills);
}
int _vm_sem_post(korp_sem *sem)
{
k_sem_give(sem);
return BHT_OK;
}
int _vm_cond_init(korp_cond *cond)
{
k_mutex_init(&cond->wait_list_lock);
cond->thread_wait_list = NULL;
return BHT_OK;
}
int _vm_cond_destroy(korp_cond *cond)
{
(void) cond;
return BHT_OK;
}
static int _vm_cond_wait_internal(korp_cond *cond, korp_mutex *mutex,
bool timed, 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))))
return BHT_ERROR;
k_sem_init(&node->sem, 0, 1);
node->next = NULL;
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
if (!cond->thread_wait_list)
cond->thread_wait_list = node;
else {
/* Add to end of wait list */
bh_thread_wait_node *p = cond->thread_wait_list;
while (p->next)
p = p->next;
p->next = node;
}
k_mutex_unlock(&cond->wait_list_lock);
/* Unlock mutex, wait sem and lock mutex again */
k_mutex_unlock(mutex);
k_sem_take(&node->sem, timed ? mills : K_FOREVER);
k_mutex_lock(mutex, K_FOREVER);
/* Remove wait node from wait list */
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
if (cond->thread_wait_list == node)
cond->thread_wait_list = node->next;
else {
/* Remove from the wait list */
bh_thread_wait_node *p = cond->thread_wait_list;
while (p->next != node)
p = p->next;
p->next = node->next;
}
bh_free(node);
k_mutex_unlock(&cond->wait_list_lock);
return BHT_OK;
}
int _vm_cond_wait(korp_cond *cond, korp_mutex *mutex)
{
return _vm_cond_wait_internal(cond, mutex, false, 0);
}
int _vm_cond_reltimedwait(korp_cond *cond, korp_mutex *mutex, int mills)
{
return _vm_cond_wait_internal(cond, mutex, true, mills);
}
int _vm_cond_signal(korp_cond *cond)
{
/* Signal the head wait node of wait list */
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
if (cond->thread_wait_list)
k_sem_give(&cond->thread_wait_list->sem);
k_mutex_unlock(&cond->wait_list_lock);
return BHT_OK;
}
int _vm_cond_broadcast(korp_cond *cond)
{
/* Signal each wait node of wait list */
k_mutex_lock(&cond->wait_list_lock, K_FOREVER);
if (cond->thread_wait_list) {
bh_thread_wait_node *p = cond->thread_wait_list;
while (p) {
k_sem_give(&p->sem);
p = p->next;
}
}
k_mutex_unlock(&cond->wait_list_lock);
return BHT_OK;
}

View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_time.h"
/*
* This function returns milliseconds per tick.
* @return milliseconds per tick.
*/
uint64 _bh_time_get_tick_millisecond()
{
return k_uptime_get_32();
}
/*
* This function returns milliseconds after boot.
* @return milliseconds after boot.
*/
uint64 _bh_time_get_boot_millisecond()
{
return k_uptime_get_32();
}
uint64 _bh_time_get_millisecond_from_1970()
{
return k_uptime_get();
}
size_t _bh_time_strftime(char *str, size_t max, const char *format, int64 time)
{
(void) format;
(void) time;
uint32 t = k_uptime_get_32();
int h, m, s;
t = t % (24 * 60 * 60);
h = t / (60 * 60);
t = t % (60 * 60);
m = t / 60;
s = t % 60;
return snprintf(str, max, "%02d:%02d:%02d", h, m, s);
}

View File

@ -0,0 +1,24 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)
file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)
set (PLATFORM_SHARED_SOURCE ${source_all})

View File

@ -0,0 +1,17 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include (${CMAKE_CURRENT_LIST_DIR}/utils/shared_utils.cmake)
include (${CMAKE_CURRENT_LIST_DIR}/mem-alloc/mem_alloc.cmake)
include (${CMAKE_CURRENT_LIST_DIR}/platform/${PLATFORM}/shared_platform.cmake)

View File

@ -0,0 +1,21 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
include_directories (. ../include ../platform/include ../platform/${PLATFORM} coap/er-coap coap/extension)
file (GLOB_RECURSE source_all *.c )
add_library (vmutilslib ${source_all})

View File

@ -0,0 +1,16 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
obj-y += bh_list.o bh_log.o attr-container.o bh_queue.o
obj-y += coap/

View File

@ -0,0 +1,114 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_list.h"
#include "bh_assert.h"
#ifdef BH_DEBUG
/**
* Test whehter a pointer value has exist in given list.
*
* @param list pointer to list.
* @param elem pointer to elem that will be inserted into list.
* @return <code>true</code> if the pointer has been in the list;
* <code>false</code> otherwise.
*/
BH_STATIC bool bh_list_is_elem_exist(bh_list *list, void *elem);
#endif
bh_list_status bh_list_init(bh_list *list)
{
if (!list)
return BH_LIST_ERROR;
(list->head).next = NULL;
list->len = 0;
return BH_LIST_SUCCESS;
}
bh_list_status _bh_list_insert(bh_list *list, void *elem)
{
bh_list_link *p = NULL;
if (!list || !elem)
return BH_LIST_ERROR;
#ifdef BH_DEBUG
bh_assert (!bh_list_is_elem_exist(list, elem));
#endif
p = (bh_list_link *) elem;
p->next = (list->head).next;
(list->head).next = p;
list->len++;
return BH_LIST_SUCCESS;
}
bh_list_status bh_list_remove(bh_list *list, void *elem)
{
bh_list_link *cur = NULL;
bh_list_link *prev = NULL;
if (!list || !elem)
return BH_LIST_ERROR;
cur = (list->head).next;
while (cur) {
if (cur == elem) {
if (prev)
prev->next = cur->next;
else
(list->head).next = cur->next;
list->len--;
return BH_LIST_SUCCESS;
}
prev = cur;
cur = cur->next;
}
return BH_LIST_ERROR;
}
uint32 bh_list_length(bh_list *list)
{
return (list ? list->len : 0);
}
void* bh_list_first_elem(bh_list *list)
{
return (list ? (list->head).next : NULL);
}
void* bh_list_elem_next(void *node)
{
return (node ? ((bh_list_link *) node)->next : NULL);
}
#ifdef BH_DEBUG
BH_STATIC bool bh_list_is_elem_exist(bh_list *list, void *elem)
{
bh_list_link *p = NULL;
if (!list || !elem) return false;
p = (list->head).next;
while (p && p != elem) p = p->next;
return (p != NULL);
}
#endif

View File

@ -0,0 +1,213 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file bh_log.c
* @date Tue Nov 8 18:20:06 2011
*
* @brief Implementation of Beihai's log system.
*/
#include "bh_assert.h"
#include "bh_time.h"
#include "bh_thread.h"
#include "bh_log.h"
#include "bh_platform_log.h"
/**
* The verbose level of the log system. Only those verbose logs whose
* levels are less than or equal to this value are outputed.
*/
static int log_verbose_level;
/**
* The lock for protecting the global output stream of logs.
*/
static korp_mutex log_stream_lock;
/**
* The current logging thread that owns the log_stream_lock.
*/
static korp_tid cur_logging_thread = INVALID_THREAD_ID;
/**
* Whether the currently being printed log is ennabled.
*/
static bool cur_log_enabled;
int _bh_log_init()
{
log_verbose_level = 3;
return vm_mutex_init(&log_stream_lock);
}
void _bh_log_set_verbose_level(int level)
{
log_verbose_level = level;
}
void _bh_log_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_bh_log_vprintf(fmt, ap);
va_end(ap);
}
/**
* Return true if the given tag is enabled by the configuration.
*
* @param tag the tag (or prefix) of the log message.
*
* @return true if the log should be enabled.
*/
static bool is_log_enabled(const char *tag)
{
/* Print all non-verbose or verbose logs whose levels are less than
or equal to the configured verbose level. */
return tag[0] != 'V' || tag[1] - '0' <= log_verbose_level;
}
/**
* Helper function for converting "..." to va_list.
*/
static void bh_log_emit_helper(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
bh_log_emit(fmt, ap);
va_end(ap);
}
extern size_t _bh_time_strftime(char *s, size_t max, const char *format,
int64 time);
void _bh_log_vprintf(const char *fmt, va_list ap)
{
korp_tid self = vm_self_thread();
/* Try to own the log stream and start the log output. */
if (self != cur_logging_thread) {
vm_mutex_lock(&log_stream_lock);
cur_logging_thread = self;
if (fmt && (cur_log_enabled = is_log_enabled(fmt))) {
char buf[32];
bh_time_strftime(buf, 32, "%Y-%m-%d %H:%M:%S",
bh_time_get_millisecond_from_1970());
bh_log_emit_helper("\n[%s - %X]: ", buf, (int) self);
/* Strip the "Vn." prefix. */
if (fmt && strlen(fmt) >= 3 && fmt[0] == 'V' && fmt[1] && fmt[2])
fmt += 3;
}
}
if (cur_log_enabled && fmt)
bh_log_emit(fmt, ap);
}
void _bh_log_commit()
{
if (vm_self_thread() != cur_logging_thread)
/* Ignore the single commit without printing anything. */
return;
cur_logging_thread = INVALID_THREAD_ID;
vm_mutex_unlock(&log_stream_lock);
}
void _bh_log(const char *tag, const char *file, int line, const char *fmt, ...)
{
va_list ap;
if (tag)
_bh_log_printf(tag);
if (file)
_bh_log_printf("%s:%d", file, line);
va_start(ap, fmt);
_bh_log_vprintf(fmt, ap);
va_end(ap);
_bh_log_commit();
}
#if defined(BH_DEBUG)
BH_STATIC char com_switchs[LOG_COM_MAX]; /* 0: off; 1: on */
BH_STATIC char *com_names[LOG_COM_MAX] = {"app_manager", "gc", "hmc", "utils",
"verifier_jeff", "vmcore_jeff"};
BH_STATIC int com_find_name(const char **com)
{
int i;
const char *c, *name;
for (i = 0; i < LOG_COM_MAX; i++) {
c = *com;
name = com_names[i];
while (*name != '\0') {
if (*c != *name)
break;
c++;
name++;
}
if (*name == '\0') {
*com = c;
return i;
}
}
return LOG_COM_MAX;
}
void log_parse_coms(const char *coms)
{
int i;
for (i = LOG_COM_MAX; i--; )
com_switchs[i] = 0;
com_switchs[LOG_COM_UTILS] = 1; /* utils is a common part */
if (coms == NULL)
return;
while (*coms != '\0') {
i = com_find_name(&coms);
if (i == LOG_COM_MAX)
break;
com_switchs[i] = 1;
if (*coms == '\0')
return;
if (*coms != ';')
break;
/* *com == ';' */
coms++;
}
/* Log the message without aborting. */
LOG_DEBUG(LOG_COM_UTILS, "The component names for logging are not right: %s.", coms);
}
int bh_log_dcom_is_enabled(int component)
{
return com_switchs[component];
}
#endif /* defined(BH_DEBUG) */

View File

@ -0,0 +1,258 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bh_queue.h"
#include "bh_thread.h"
#include "bh_memory.h"
#include "bh_time.h"
#include "bh_common.h"
typedef struct _bh_queue_node {
struct _bh_queue_node * next;
struct _bh_queue_node * prev;
unsigned short tag;
unsigned long len;
void * body;
bh_msg_cleaner msg_cleaner;
} bh_queue_node;
struct bh_queue {
bh_queue_mutex queue_lock;
bh_queue_cond queue_wait_cond;
unsigned int cnt;
unsigned int max;
unsigned int drops;
bh_queue_node * head;
bh_queue_node * tail;
bool exit_loop_run;
};
char * bh_message_payload(bh_message_t message)
{
return message->body;
}
int bh_message_payload_len(bh_message_t message)
{
return message->len;
}
int bh_message_type(bh_message_t message)
{
return message->tag;
}
bh_queue *
bh_queue_create()
{
int ret;
bh_queue *queue = bh_queue_malloc(sizeof(bh_queue));
if (queue) {
memset(queue, 0, sizeof(bh_queue));
queue->max = DEFAULT_QUEUE_LENGTH;
ret = bh_queue_mutex_init(&queue->queue_lock);
if (ret != 0) {
bh_queue_free(queue);
return NULL;
}
ret = bh_queue_cond_init(&queue->queue_wait_cond);
if (ret != 0) {
bh_queue_mutex_destroy(&queue->queue_lock);
bh_queue_free(queue);
return NULL;
}
}
return queue;
}
void bh_queue_destroy(bh_queue *queue)
{
bh_queue_node *node;
if (!queue)
return;
bh_queue_mutex_lock(&queue->queue_lock);
while (queue->head) {
node = queue->head;
queue->head = node->next;
bh_free_msg(node);
}
bh_queue_mutex_unlock(&queue->queue_lock);
bh_queue_cond_destroy(&queue->queue_wait_cond);
bh_queue_mutex_destroy(&queue->queue_lock);
bh_queue_free(queue);
}
bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
{
if (queue->cnt >= queue->max) {
queue->drops++;
bh_free_msg(msg);
return false;
}
bh_queue_mutex_lock(&queue->queue_lock);
if (queue->cnt == 0) {
bh_assert(queue->head == NULL);
bh_assert(queue->tail == NULL);
queue->head = queue->tail = msg;
msg->next = msg->prev = NULL;
queue->cnt = 1;
bh_queue_cond_signal(&queue->queue_wait_cond);
} else {
msg->next = NULL;
msg->prev = queue->tail;
queue->tail->next = msg;
queue->tail = msg;
queue->cnt++;
}
bh_queue_mutex_unlock(&queue->queue_lock);
return true;
}
bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
unsigned int len)
{
bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
if (msg == NULL) {
queue->drops++;
if (len != 0 && body)
bh_free(body);
return false;
}
if (!bh_post_msg2(queue, msg)) {
// bh_post_msg2 already freed the msg for failure
return false;
}
return true;
}
bh_queue_node * bh_new_msg(unsigned short tag, void *body, unsigned int len,
void * handler)
{
bh_queue_node *msg = (bh_queue_node*) bh_queue_malloc(
sizeof(bh_queue_node));
if (msg == NULL)
return NULL;
memset(msg, 0, sizeof(bh_queue_node));
msg->len = len;
msg->body = body;
msg->tag = tag;
msg->msg_cleaner = (bh_msg_cleaner) handler;
return msg;
}
void bh_free_msg(bh_queue_node *msg)
{
if (msg->msg_cleaner) {
msg->msg_cleaner(msg->body);
bh_queue_free(msg);
return;
}
// note: sometime we just use the payload pointer for a integer value
// len!=0 is the only indicator about the body is an allocated buffer.
if (msg->body && msg->len)
bh_queue_free(msg->body);
bh_queue_free(msg);
}
bh_message_t bh_get_msg(bh_queue *queue, int timeout)
{
bh_queue_node *msg = NULL;
bh_queue_mutex_lock(&queue->queue_lock);
if (queue->cnt == 0) {
bh_assert(queue->head == NULL);
bh_assert(queue->tail == NULL);
if (timeout == 0) {
bh_queue_mutex_unlock(&queue->queue_lock);
return NULL;
}
bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
timeout);
}
if (queue->cnt == 0) {
bh_assert(queue->head == NULL);
bh_assert(queue->tail == NULL);
} else if (queue->cnt == 1) {
bh_assert(queue->head == queue->tail);
msg = queue->head;
queue->head = queue->tail = NULL;
queue->cnt = 0;
} else {
msg = queue->head;
queue->head = queue->head->next;
queue->head->prev = NULL;
queue->cnt--;
}
bh_queue_mutex_unlock(&queue->queue_lock);
return msg;
}
unsigned bh_queue_get_message_count(bh_queue *queue)
{
if (!queue)
return 0;
return queue->cnt;
}
void bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb)
{
if (!queue)
return;
while (!queue->exit_loop_run) {
bh_queue_node * message = bh_get_msg(queue, BH_WAIT_FOREVER);
if (message) {
handle_cb(message);
bh_free_msg(message);
}
}
}
void bh_queue_exit_loop_run(bh_queue *queue)
{
if (queue) {
queue->exit_loop_run = true;
bh_queue_cond_signal(&queue->queue_wait_cond);
}
}

View File

@ -0,0 +1,478 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "runtime_timer.h"
#include "bh_thread.h"
#include "bh_time.h"
#define PRINT(...)
//#define PRINT printf
typedef struct _app_timer {
struct _app_timer * next;
uint32 id;
unsigned int interval;
uint64 expiry;
bool is_periodic;
} app_timer_t;
struct _timer_ctx {
app_timer_t * g_app_timers;
app_timer_t * idle_timers;
app_timer_t * free_timers;
unsigned int g_max_id;
int pre_allocated;
unsigned int owner;
//add mutext and conditions
korp_cond cond;
korp_mutex mutex;
timer_callback_f timer_callback;
check_timer_expiry_f refresh_checker;
};
uint32 bh_get_elpased_ms(uint32 * last_system_clock)
{
uint32 elpased_ms;
// attention: the bh_get_tick_ms() return 64 bits integer.
// but the bh_get_elpased_ms() is designed to use 32 bits clock count.
uint32 now = (uint32) bh_get_tick_ms();
// system clock overrun
if (now < *last_system_clock) {
elpased_ms = now + (0xFFFFFFFF - *last_system_clock) + 1;
} else {
elpased_ms = now - *last_system_clock;
}
*last_system_clock = now;
return elpased_ms;
}
static app_timer_t * remove_timer_from(timer_ctx_t ctx, uint32 timer_id,
bool active_list)
{
vm_mutex_lock(&ctx->mutex);
app_timer_t ** head;
if (active_list)
head = &ctx->g_app_timers;
else
head = &ctx->idle_timers;
app_timer_t * t = *head;
app_timer_t * prev = NULL;
while (t) {
if (t->id == timer_id) {
if (prev == NULL) {
*head = t->next;
PRINT("removed timer [%d] at head from list %d\n", t->id, active_list);
} else {
prev->next = t->next;
PRINT("removed timer [%d] after [%d] from list %d\n", t->id, prev->id, active_list);
}
vm_mutex_unlock(&ctx->mutex);
if (active_list && prev == NULL && ctx->refresh_checker)
ctx->refresh_checker(ctx);
return t;
} else {
prev = t;
t = t->next;
}
}
vm_mutex_unlock(&ctx->mutex);
return NULL;
}
static app_timer_t * remove_timer(timer_ctx_t ctx, uint32 timer_id,
bool * active)
{
app_timer_t* t = remove_timer_from(ctx, timer_id, true);
if (t) {
if (active)
*active = true;
return t;
}
if (active)
*active = false;
return remove_timer_from(ctx, timer_id, false);
}
static void reschedule_timer(timer_ctx_t ctx, app_timer_t * timer)
{
vm_mutex_lock(&ctx->mutex);
app_timer_t * t = ctx->g_app_timers;
app_timer_t * prev = NULL;
timer->next = NULL;
timer->expiry = bh_get_tick_ms() + timer->interval;
while (t) {
if (timer->expiry < t->expiry) {
if (prev == NULL) {
timer->next = ctx->g_app_timers;
ctx->g_app_timers = timer;
PRINT("rescheduled timer [%d] at head\n", timer->id);
} else {
timer->next = t;
prev->next = timer;
PRINT("rescheduled timer [%d] after [%d]\n", timer->id, prev->id);
}
vm_mutex_unlock(&ctx->mutex);
// ensure the refresh_checker() is called out of the lock
if (prev == NULL && ctx->refresh_checker)
ctx->refresh_checker(ctx);
return;
} else {
prev = t;
t = t->next;
}
}
if (prev) {
// insert to the list end
prev->next = timer;
PRINT("rescheduled timer [%d] at end, after [%d]\n", timer->id, prev->id);
} else {
// insert at the begin
bh_assert(ctx->g_app_timers == NULL);
ctx->g_app_timers = timer;
PRINT("rescheduled timer [%d] as first\n", timer->id);
}
vm_mutex_unlock(&ctx->mutex);
// ensure the refresh_checker() is called out of the lock
if (prev == NULL && ctx->refresh_checker)
ctx->refresh_checker(ctx);
}
static void release_timer(timer_ctx_t ctx, app_timer_t * t)
{
if (ctx->pre_allocated) {
vm_mutex_lock(&ctx->mutex);
t->next = ctx->free_timers;
ctx->free_timers = t;
PRINT("recycle timer :%d\n", t->id);
vm_mutex_unlock(&ctx->mutex);
} else {
PRINT("destroy timer :%d\n", t->id);
bh_free(t);
}
}
void release_timer_list(app_timer_t ** p_list)
{
app_timer_t *t = *p_list;
while (t) {
app_timer_t *next = t->next;
PRINT("destroy timer list:%d\n", t->id);
bh_free(t);
t = next;
}
*p_list = NULL;
}
/*
*
* API exposed
*
*/
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f expiery_checker, int prealloc_num,
unsigned int owner)
{
timer_ctx_t ctx = (timer_ctx_t) bh_malloc(sizeof(struct _timer_ctx));
if (ctx == NULL)
return NULL;
memset(ctx, 0, sizeof(struct _timer_ctx));
ctx->timer_callback = timer_handler;
ctx->pre_allocated = prealloc_num;
ctx->refresh_checker = expiery_checker;
ctx->owner = owner;
while (prealloc_num > 0) {
app_timer_t *timer = (app_timer_t*) bh_malloc(sizeof(app_timer_t));
if (timer == NULL)
goto cleanup;
memset(timer, 0, sizeof(*timer));
timer->next = ctx->free_timers;
ctx->free_timers = timer;
prealloc_num--;
}
vm_cond_init(&ctx->cond);
vm_mutex_init(&ctx->mutex);
PRINT("timer ctx created. pre-alloc: %d\n", ctx->pre_allocated);
return ctx;
cleanup:
if (ctx) {
release_timer_list(&ctx->free_timers);
bh_free(ctx);
}
printf("timer ctx create failed\n");
return NULL;
}
void destroy_timer_ctx(timer_ctx_t ctx)
{
while (ctx->free_timers) {
void * tmp = ctx->free_timers;
ctx->free_timers = ctx->free_timers->next;
bh_free(tmp);
}
cleanup_app_timers(ctx);
vm_cond_destroy(&ctx->cond);
vm_mutex_destroy(&ctx->mutex);
bh_free(ctx);
}
void timer_ctx_set_lock(timer_ctx_t ctx, bool lock)
{
if (lock)
vm_mutex_lock(&ctx->mutex);
else
vm_mutex_unlock(&ctx->mutex);
}
void * timer_ctx_get_lock(timer_ctx_t ctx)
{
return &ctx->mutex;
}
unsigned int timer_ctx_get_owner(timer_ctx_t ctx)
{
return ctx->owner;
}
void add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
{
vm_mutex_lock(&ctx->mutex);
timer->next = ctx->idle_timers;
ctx->idle_timers = timer;
vm_mutex_unlock(&ctx->mutex);
}
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool auto_start)
{
app_timer_t *timer;
if (ctx->pre_allocated) {
if (ctx->free_timers == NULL)
return -1;
else {
timer = ctx->free_timers;
ctx->free_timers = timer->next;
}
} else {
timer = (app_timer_t*) bh_malloc(sizeof(app_timer_t));
if (timer == NULL)
return -1;
}
memset(timer, 0, sizeof(*timer));
ctx->g_max_id++;
if (ctx->g_max_id == -1)
ctx->g_max_id++;
timer->id = ctx->g_max_id;
timer->interval = interval;
timer->is_periodic = is_period;
if (auto_start)
reschedule_timer(ctx, timer);
else
add_idle_timer(ctx, timer);
return timer->id;
}
bool sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id)
{
bool from_active;
app_timer_t * t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL)
return false;
add_idle_timer(ctx, t);
PRINT("sys_timer_stop called\n");
return from_active;
}
bool sys_timer_destory(timer_ctx_t ctx, uint32 timer_id)
{
bool from_active;
app_timer_t * t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL)
return false;
release_timer(ctx, t);
PRINT("sys_timer_destory called\n");
return true;
}
bool sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
{
app_timer_t * t = remove_timer(ctx, timer_id, NULL);
if (t == NULL)
return false;
if (interval > 0)
t->interval = interval;
reschedule_timer(ctx, t);
PRINT("sys_timer_restart called\n");
return true;
}
/*
*
*
* API called by the timer manager from another thread or the kernel timer handler
*
*
*/
// lookup the app queue by the module name
//post a timeout message to the app queue
//
static void handle_expired_timers(timer_ctx_t ctx, app_timer_t * expired)
{
while (expired) {
app_timer_t * t = expired;
ctx->timer_callback(t->id, ctx->owner);
expired = expired->next;
if (t->is_periodic) {
// if it is repeating, then reschedule it;
reschedule_timer(ctx, t);
} else {
// else move it to idle list
add_idle_timer(ctx, t);
}
}
}
int get_expiry_ms(timer_ctx_t ctx)
{
int ms_to_next_expiry;
uint64 now = bh_get_tick_ms();
vm_mutex_lock(&ctx->mutex);
if (ctx->g_app_timers == NULL)
ms_to_next_expiry = 7 * 24 * 60 * 60 * 1000; // 1 week
else if (ctx->g_app_timers->expiry >= now)
ms_to_next_expiry = ctx->g_app_timers->expiry - now;
else
ms_to_next_expiry = 0;
vm_mutex_unlock(&ctx->mutex);
return ms_to_next_expiry;
}
int check_app_timers(timer_ctx_t ctx)
{
vm_mutex_lock(&ctx->mutex);
app_timer_t * t = ctx->g_app_timers;
app_timer_t * prev = NULL;
app_timer_t * expired = NULL;
uint64 now = bh_get_tick_ms();
while (t) {
if (now >= t->expiry) {
ctx->g_app_timers = t->next;
t->next = expired;
expired = t;
t = ctx->g_app_timers;
} else {
break;
}
}
vm_mutex_unlock(&ctx->mutex);
handle_expired_timers(ctx, expired);
return get_expiry_ms(ctx);
}
void cleanup_app_timers(timer_ctx_t ctx)
{
app_timer_t *t;
vm_mutex_lock(&ctx->mutex);
release_timer_list(&ctx->g_app_timers);
release_timer_list(&ctx->idle_timers);
vm_mutex_unlock(&ctx->mutex);
}
/*
*
* One reference implementation for timer manager
*
*
*/
void * thread_timer_check(void * arg)
{
timer_ctx_t ctx = (timer_ctx_t) arg;
while (1) {
int ms_to_expiry = check_app_timers(ctx);
vm_mutex_lock(&ctx->mutex);
vm_cond_reltimedwait(&ctx->cond, &ctx->mutex, ms_to_expiry);
vm_mutex_unlock(&ctx->mutex);
}
}
void wakeup_timer_thread(timer_ctx_t ctx)
{
vm_cond_signal(&ctx->cond);
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2019 Intel Corporation. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LIB_BASE_RUNTIME_TIMER_H_
#define LIB_BASE_RUNTIME_TIMER_H_
#include "bh_platform.h"
#ifdef __cplusplus
extern "C" {
#endif
uint32 bh_get_elpased_ms(uint32 * last_system_clock);
struct _timer_ctx;
typedef struct _timer_ctx * timer_ctx_t;
typedef void (*timer_callback_f)(uint32 id, unsigned int owner);
typedef void (*check_timer_expiry_f)(timer_ctx_t ctx);
timer_ctx_t create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f, int prealloc_num, unsigned int owner);
void destroy_timer_ctx(timer_ctx_t);
void timer_ctx_set_lock(timer_ctx_t ctx, bool lock);
void * timer_ctx_get_lock(timer_ctx_t ctx);
unsigned int timer_ctx_get_owner(timer_ctx_t ctx);
uint32 sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool auto_start);
bool sys_timer_destory(timer_ctx_t ctx, uint32 timer_id);
bool sys_timer_cancel(timer_ctx_t ctx, uint32 timer_id);
bool sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval);
void cleanup_app_timers(timer_ctx_t ctx);
int check_app_timers(timer_ctx_t ctx);
int get_expiry_ms(timer_ctx_t ctx);
void wakeup_timer_thread(timer_ctx_t ctx);
void * thread_timer_check(void * arg);
#ifdef __cplusplus
}
#endif
#endif /* LIB_BASE_RUNTIME_TIMER_H_ */

View File

@ -0,0 +1,24 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set (UTILS_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
include_directories(${UTILS_SHARED_DIR})
include_directories(${UTILS_SHARED_DIR}/../include)
file (GLOB_RECURSE source_all ${UTILS_SHARED_DIR}/*.c)
set (UTILS_SHARED_SOURCE ${source_all})