Apply clang-format for core/shared and product-mini files (#785)

Apply clang-format for core/shared and product-mini files
This commit is contained in:
Wenyong Huang
2021-10-14 09:12:07 +08:00
committed by GitHub
parent fb4afc7ca4
commit 17f62ad472
107 changed files with 3436 additions and 2898 deletions

View File

@ -5,8 +5,9 @@
#include "bh_assert.h"
void bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
void
bh_assert_internal(int v, const char *file_name, int line_number,
const char *expr_string)
{
if (v)
return;
@ -17,9 +18,8 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
if (!expr_string)
expr_string = "NULL EXPR_STRING";
os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n",
expr_string, file_name, line_number);
os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", expr_string,
file_name, line_number);
abort();
}

View File

@ -13,10 +13,11 @@ extern "C" {
#endif
#if BH_DEBUG != 0
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)(uintptr_t)(expr), \
__FILE__, __LINE__, #expr)
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)(uintptr_t)(expr), __FILE__, __LINE__, #expr)
#else
#define bh_assert(expr) (void)0
#endif /* end of BH_DEBUG */
@ -26,4 +27,3 @@ void bh_assert_internal(int v, const char *file_name, int line_number,
#endif
#endif /* end of _BH_ASSERT_H */

View File

@ -12,70 +12,67 @@
#define RSIZE_MAX 0x7FFFFFFF
int
b_memcpy_s(void * s1, unsigned int s1max,
const void * s2, unsigned int n)
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) {
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;
}
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_memmove_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;
}
memmove(dest, src, n);
return 0;
}
int
b_strcat_s(char * s1, unsigned int s1max, const char * s2)
b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
{
if (NULL == s1 || NULL == s2
|| s1max < (strlen(s1) + strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}
char *dest = (char *)s1;
char *src = (char *)s2;
if (n == 0) {
return 0;
}
memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
return 0;
if (s1 == NULL || s1max > RSIZE_MAX) {
return -1;
}
if (s2 == NULL || n > s1max) {
memset(dest, 0, s1max);
return -1;
}
memmove(dest, src, n);
return 0;
}
int
b_strcpy_s(char * s1, unsigned int s1max, const char * s2)
b_strcat_s(char *s1, unsigned int s1max, const char *s2)
{
if (NULL == s1 || NULL == s2
|| s1max < (strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}
if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}
memcpy(s1, s2, strlen(s2) + 1);
return 0;
memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
return 0;
}
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
{
if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)
|| s1max > RSIZE_MAX) {
return -1;
}
memcpy(s1, s2, strlen(s2) + 1);
return 0;
}
char *
@ -105,4 +102,3 @@ wa_strdup(const char *s)
}
return s1;
}

View File

@ -12,40 +12,50 @@
extern "C" {
#endif
#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_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_memmove_s(dest, dlen, src, slen) do { \
int _ret = slen == 0 ? 0 : b_memmove_s (dest, dlen, src, slen); \
(void)_ret; \
bh_assert (_ret == 0); \
} while (0)
#define bh_memmove_s(dest, dlen, src, slen) \
do { \
int _ret = slen == 0 ? 0 : b_memmove_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_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)
#define bh_strcpy_s(dest, dlen, src) \
do { \
int _ret = b_strcpy_s(dest, dlen, src); \
(void)_ret; \
bh_assert(_ret == 0); \
} while (0)
int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
int b_memmove_s(void * s1, unsigned int s1max, const void * s2, unsigned int n);
int b_strcat_s(char * s1, unsigned int s1max, const char * s2);
int b_strcpy_s(char * s1, unsigned int s1max, const char * s2);
int
b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
int
b_strcat_s(char *s1, unsigned int s1max, const char *s2);
int
b_strcpy_s(char *s1, unsigned int s1max, const char *s2);
/* strdup with string allocated by BH_MALLOC */
char *bh_strdup(const char *s);
char *
bh_strdup(const char *s);
/* strdup with string allocated by WA_MALLOC */
char *wa_strdup(const char *s);
char *
wa_strdup(const char *s);
#ifdef __cplusplus
}

View File

@ -25,11 +25,9 @@ struct HashMap {
HashMapElem *elements[1];
};
HashMap*
bh_hash_map_create(uint32 size, bool use_lock,
HashFunc hash_func,
KeyEqualFunc key_equal_func,
KeyDestroyFunc key_destroy_func,
HashMap *
bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
ValueDestroyFunc value_destroy_func)
{
HashMap *map;
@ -42,16 +40,15 @@ bh_hash_map_create(uint32 size, bool use_lock,
if (!hash_func || !key_equal_func) {
LOG_ERROR("HashMap create failed: hash function or key equal function "
" is NULL.\n");
" is NULL.\n");
return NULL;
}
total_size = offsetof(HashMap, elements) +
sizeof(HashMapElem *) * (uint64)size +
(use_lock ? sizeof(korp_mutex) : 0);
total_size = offsetof(HashMap, elements)
+ sizeof(HashMapElem *) * (uint64)size
+ (use_lock ? sizeof(korp_mutex) : 0);
if (total_size >= UINT32_MAX
|| !(map = BH_MALLOC((uint32)total_size))) {
if (total_size >= UINT32_MAX || !(map = BH_MALLOC((uint32)total_size))) {
LOG_ERROR("HashMap create failed: alloc memory failed.\n");
return NULL;
}
@ -59,9 +56,8 @@ bh_hash_map_create(uint32 size, bool use_lock,
memset(map, 0, (uint32)total_size);
if (use_lock) {
map->lock = (korp_mutex*)
((uint8*)map + offsetof(HashMap, elements)
+ sizeof(HashMapElem *) * size);
map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements)
+ sizeof(HashMapElem *) * size);
if (os_mutex_init(map->lock)) {
LOG_ERROR("HashMap create failed: init map lock failed.\n");
BH_FREE(map);
@ -124,7 +120,7 @@ fail:
return false;
}
void*
void *
bh_hash_map_find(HashMap *map, void *key)
{
uint32 index;
@ -161,8 +157,7 @@ bh_hash_map_find(HashMap *map, void *key)
}
bool
bh_hash_map_update(HashMap *map, void *key, void *value,
void **p_old_value)
bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
{
uint32 index;
HashMapElem *elem;
@ -199,8 +194,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value,
}
bool
bh_hash_map_remove(HashMap *map, void *key,
void **p_old_key, void **p_old_value)
bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
void **p_old_value)
{
uint32 index;
HashMapElem *elem, *prev;

View File

@ -51,11 +51,9 @@ typedef void (*TraverseCallbackFunc)(void *key, void *value, void *user_data);
*
* @return the hash map created, NULL if failed
*/
HashMap*
bh_hash_map_create(uint32 size, bool use_lock,
HashFunc hash_func,
KeyEqualFunc key_equal_func,
KeyDestroyFunc key_destroy_func,
HashMap *
bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
ValueDestroyFunc value_destroy_func);
/**
@ -79,7 +77,7 @@ bh_hash_map_insert(HashMap *map, void *key, void *value);
*
* @return the value of the found element if success, NULL otherwise
*/
void*
void *
bh_hash_map_find(HashMap *map, void *key);
/**
@ -95,8 +93,7 @@ bh_hash_map_find(HashMap *map, void *key);
* it will be copied to p_old_value for user to process.
*/
bool
bh_hash_map_update(HashMap *map, void *key, void *value,
void **p_old_value);
bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value);
/**
* Remove an element from the hash map
@ -112,8 +109,8 @@ bh_hash_map_update(HashMap *map, void *key, void *value,
* p_old_key and p_old_value for user to process.
*/
bool
bh_hash_map_remove(HashMap *map, void *key,
void **p_old_key, void **p_old_value);
bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
void **p_old_value);
/**
* Destroy the hashmap
@ -166,4 +163,3 @@ bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
#endif
#endif /* endof WASM_HASHMAP_H */

View File

@ -14,10 +14,12 @@
* @return <code>true</code> if the pointer has been in the list;
* <code>false</code> otherwise.
*/
static bool bh_list_is_elem_exist(bh_list *list, void *elem);
static bool
bh_list_is_elem_exist(bh_list *list, void *elem);
#endif
bh_list_status bh_list_init(bh_list *list)
bh_list_status
bh_list_init(bh_list *list)
{
if (!list)
return BH_LIST_ERROR;
@ -27,23 +29,25 @@ bh_list_status bh_list_init(bh_list *list)
return BH_LIST_SUCCESS;
}
bh_list_status bh_list_insert(bh_list *list, void *elem)
bh_list_status
bh_list_insert(bh_list *list, void *elem)
{
bh_list_link *p = NULL;
if (!list || !elem)
return BH_LIST_ERROR;
#if BH_DEBUG != 0
bh_assert (!bh_list_is_elem_exist(list, elem));
bh_assert(!bh_list_is_elem_exist(list, elem));
#endif
p = (bh_list_link *) elem;
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_status
bh_list_remove(bh_list *list, void *elem)
{
bh_list_link *cur = NULL;
bh_list_link *prev = NULL;
@ -71,32 +75,37 @@ bh_list_status bh_list_remove(bh_list *list, void *elem)
return BH_LIST_ERROR;
}
uint32 bh_list_length(bh_list *list)
uint32
bh_list_length(bh_list *list)
{
return (list ? list->len : 0);
}
void* bh_list_first_elem(bh_list *list)
void *
bh_list_first_elem(bh_list *list)
{
return (list ? (list->head).next : NULL);
}
void* bh_list_elem_next(void *node)
void *
bh_list_elem_next(void *node)
{
return (node ? ((bh_list_link *) node)->next : NULL);
return (node ? ((bh_list_link *)node)->next : NULL);
}
#if BH_DEBUG != 0
static bool bh_list_is_elem_exist(bh_list *list, void *elem)
static bool
bh_list_is_elem_exist(bh_list *list, void *elem)
{
bh_list_link *p = NULL;
if (!list || !elem) return false;
if (!list || !elem)
return false;
p = (list->head).next;
while (p && p != elem) p = p->next;
while (p && p != elem)
p = p->next;
return (p != NULL);
}
#endif

View File

@ -46,29 +46,34 @@ typedef enum bh_list_status {
* @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);
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.
* 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.
* <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);
extern bh_list_status
bh_list_insert(bh_list *list, void *elem);
/**
* Remove an elem pointer from list. The list node memory is maintained by list while
* elem memory is the responsibility of list user.
* 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.
* <code>BH_LIST_ERROR</code> if element does not exist in given
* list.
*/
bh_list_status bh_list_remove(bh_list *list, void *elem);
bh_list_status
bh_list_remove(bh_list *list, void *elem);
/**
* Get the list length.
@ -76,7 +81,8 @@ bh_list_status bh_list_remove(bh_list *list, void *elem);
* @param list pointer to list.
* @return the length of the list.
*/
uint32 bh_list_length(bh_list *list);
uint32
bh_list_length(bh_list *list);
/**
* Get the first elem in the list.
@ -84,7 +90,8 @@ uint32 bh_list_length(bh_list *list);
* @param list pointer to list.
* @return pointer to the first node.
*/
void* bh_list_first_elem(bh_list *list);
void *
bh_list_first_elem(bh_list *list);
/**
* Get the next elem of given list input elem.
@ -92,11 +99,11 @@ void* bh_list_first_elem(bh_list *list);
* @param node pointer to list node.
* @return pointer to next list node.
*/
void* bh_list_elem_next(void *node);
void *
bh_list_elem_next(void *node);
#ifdef __cplusplus
}
#endif
#endif /* #ifndef _BH_LIST_H */

View File

@ -71,9 +71,8 @@ bh_print_time(const char *prompt)
total_time_ms += curr_time_ms - last_time_ms;
os_printf("%-48s time of last stage: %u ms, total time: %u ms\n",
prompt, curr_time_ms - last_time_ms, total_time_ms);
os_printf("%-48s time of last stage: %u ms, total time: %u ms\n", prompt,
curr_time_ms - last_time_ms, total_time_ms);
last_time_ms = curr_time_ms;
}

View File

@ -52,19 +52,22 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
#endif
#if BH_DEBUG != 0
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_FATAL(...) \
bh_log(BH_LOG_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
#else
#define LOG_FATAL(...) bh_log(BH_LOG_LEVEL_FATAL, __FUNCTION__, __LINE__, __VA_ARGS__)
#define LOG_FATAL(...) \
bh_log(BH_LOG_LEVEL_FATAL, __FUNCTION__, __LINE__, __VA_ARGS__)
#endif
#define LOG_ERROR(...) bh_log(BH_LOG_LEVEL_ERROR, NULL, 0, __VA_ARGS__)
#define LOG_ERROR(...) bh_log(BH_LOG_LEVEL_ERROR, NULL, 0, __VA_ARGS__)
#define LOG_WARNING(...) bh_log(BH_LOG_LEVEL_WARNING, NULL, 0, __VA_ARGS__)
#define LOG_VERBOSE(...) bh_log(BH_LOG_LEVEL_VERBOSE, NULL, 0, __VA_ARGS__)
#if BH_DEBUG != 0
#define LOG_DEBUG(...) bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#define LOG_DEBUG(...) \
bh_log(BH_LOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
#else
#define LOG_DEBUG(...) (void)0
#define LOG_DEBUG(...) (void)0
#endif
void
@ -74,4 +77,4 @@ bh_print_time(const char *prompt);
}
#endif
#endif /* _BH_LOG_H */
#endif /* _BH_LOG_H */

View File

@ -18,8 +18,6 @@
#include "bh_vector.h"
#include "runtime_timer.h"
/**
* WA_MALLOC/WA_FREE need to be redefined for both
* runtime native and WASM app respectively.
@ -38,4 +36,3 @@
#endif
#endif /* #ifndef _BH_PLATFORM_H */

View File

@ -6,11 +6,11 @@
#include "bh_queue.h"
typedef struct bh_queue_node {
struct bh_queue_node * next;
struct bh_queue_node * prev;
struct bh_queue_node *next;
struct bh_queue_node *prev;
unsigned short tag;
unsigned int len;
void * body;
void *body;
bh_msg_cleaner msg_cleaner;
} bh_queue_node;
@ -20,23 +20,26 @@ struct bh_queue {
unsigned int cnt;
unsigned int max;
unsigned int drops;
bh_queue_node * head;
bh_queue_node * tail;
bh_queue_node *head;
bh_queue_node *tail;
bool exit_loop_run;
};
char * bh_message_payload(bh_message_t message)
char *
bh_message_payload(bh_message_t message)
{
return message->body;
}
uint32 bh_message_payload_len(bh_message_t message)
uint32
bh_message_payload_len(bh_message_t message)
{
return message->len;
}
int bh_message_type(bh_message_t message)
int
bh_message_type(bh_message_t message)
{
return message->tag;
}
@ -68,7 +71,8 @@ bh_queue_create()
return queue;
}
void bh_queue_destroy(bh_queue *queue)
void
bh_queue_destroy(bh_queue *queue)
{
bh_queue_node *node;
@ -89,7 +93,8 @@ void bh_queue_destroy(bh_queue *queue)
bh_queue_free(queue);
}
bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
bool
bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
{
if (queue->cnt >= queue->max) {
queue->drops++;
@ -107,7 +112,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
queue->cnt = 1;
bh_queue_cond_signal(&queue->queue_wait_cond);
} else {
}
else {
msg->next = NULL;
msg->prev = queue->tail;
queue->tail->next = msg;
@ -120,8 +126,8 @@ bool bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
return true;
}
bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
unsigned int len)
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) {
@ -139,23 +145,24 @@ bool bh_post_msg(bh_queue *queue, unsigned short tag, void *body,
return true;
}
bh_queue_node * bh_new_msg(unsigned short tag, void *body, unsigned int len,
void * handler)
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));
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;
msg->msg_cleaner = (bh_msg_cleaner)handler;
return msg;
}
void bh_free_msg(bh_queue_node *msg)
void
bh_free_msg(bh_queue_node *msg)
{
if (msg->msg_cleaner) {
msg->msg_cleaner(msg->body);
@ -171,7 +178,8 @@ void bh_free_msg(bh_queue_node *msg)
bh_queue_free(msg);
}
bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us)
bh_message_t
bh_get_msg(bh_queue *queue, uint64 timeout_us)
{
bh_queue_node *msg = NULL;
bh_queue_mutex_lock(&queue->queue_lock);
@ -192,13 +200,15 @@ bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us)
if (queue->cnt == 0) {
bh_assert(queue->head == NULL);
bh_assert(queue->tail == NULL);
} else if (queue->cnt == 1) {
}
else if (queue->cnt == 1) {
bh_assert(queue->head == queue->tail);
msg = queue->head;
queue->head = queue->tail = NULL;
queue->cnt = 0;
} else {
}
else {
msg = queue->head;
queue->head = queue->head->next;
queue->head->prev = NULL;
@ -210,7 +220,8 @@ bh_message_t bh_get_msg(bh_queue *queue, uint64 timeout_us)
return msg;
}
unsigned bh_queue_get_message_count(bh_queue *queue)
unsigned
bh_queue_get_message_count(bh_queue *queue)
{
if (!queue)
return 0;
@ -218,15 +229,15 @@ unsigned bh_queue_get_message_count(bh_queue *queue)
return queue->cnt;
}
void bh_queue_enter_loop_run(bh_queue *queue,
bh_queue_handle_msg_callback handle_cb,
void *arg)
void
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
void *arg)
{
if (!queue)
return;
while (!queue->exit_loop_run) {
bh_queue_node * message = bh_get_msg(queue, BHT_WAIT_FOREVER);
bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);
if (message) {
handle_cb(message, arg);
@ -235,7 +246,8 @@ void bh_queue_enter_loop_run(bh_queue *queue,
}
}
void bh_queue_exit_loop_run(bh_queue *queue)
void
bh_queue_exit_loop_run(bh_queue *queue)
{
if (queue) {
queue->exit_loop_run = true;

View File

@ -13,7 +13,7 @@ extern "C" {
#include "bh_platform.h"
struct bh_queue_node;
typedef struct bh_queue_node * bh_message_t;
typedef struct bh_queue_node *bh_message_t;
struct bh_queue;
typedef struct bh_queue bh_queue;
@ -45,25 +45,30 @@ bh_queue_create();
void
bh_queue_destroy(bh_queue *queue);
char * bh_message_payload(bh_message_t message);
uint32 bh_message_payload_len(bh_message_t message);
int bh_message_type(bh_message_t message);
char *
bh_message_payload(bh_message_t message);
uint32
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_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, uint64 timeout_us);
bh_message_t
bh_get_msg(bh_queue *queue, uint64 timeout_us);
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,
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
void *arg);
void
bh_queue_exit_loop_run(bh_queue *queue);
@ -73,4 +78,3 @@ bh_queue_exit_loop_run(bh_queue *queue);
#endif
#endif /* #ifndef _BH_QUEUE_H */

View File

@ -5,14 +5,13 @@
#include "bh_vector.h"
static uint8*
static uint8 *
alloc_vector_data(size_t length, size_t size_elem)
{
uint64 total_size = ((uint64)size_elem) * length;
uint8 *data;
if (length > UINT32_MAX
|| size_elem > UINT32_MAX
if (length > UINT32_MAX || size_elem > UINT32_MAX
|| total_size > UINT32_MAX) {
return NULL;
}
@ -82,12 +81,13 @@ bh_vector_set(Vector *vector, uint32 index, const void *elem_buf)
return false;
}
memcpy(vector->data + vector->size_elem * index,
elem_buf, vector->size_elem);
memcpy(vector->data + vector->size_elem * index, elem_buf,
vector->size_elem);
return true;
}
bool bh_vector_get(const Vector *vector, uint32 index, void *elem_buf)
bool
bh_vector_get(const Vector *vector, uint32 index, void *elem_buf)
{
if (!vector || !elem_buf) {
LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n");
@ -104,7 +104,8 @@ bool bh_vector_get(const Vector *vector, uint32 index, void *elem_buf)
return true;
}
bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
bool
bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
{
size_t i;
uint8 *p;
@ -135,7 +136,8 @@ bool bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
return true;
}
bool bh_vector_append(Vector *vector, const void *elem_buf)
bool
bh_vector_append(Vector *vector, const void *elem_buf)
{
if (!vector || !elem_buf) {
LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n");
@ -147,8 +149,8 @@ bool bh_vector_append(Vector *vector, const void *elem_buf)
return false;
}
memcpy(vector->data + vector->size_elem * vector->num_elems,
elem_buf, vector->size_elem);
memcpy(vector->data + vector->size_elem * vector->num_elems, elem_buf,
vector->size_elem);
vector->num_elems++;
return true;
}

View File

@ -122,4 +122,3 @@ bh_vector_destroy(Vector *vector);
#endif
#endif /* endof _WASM_VECTOR_H */

View File

@ -12,7 +12,7 @@
#endif
typedef struct _app_timer {
struct _app_timer * next;
struct _app_timer *next;
uint32 id;
uint32 interval;
uint64 expiry;
@ -81,13 +81,13 @@ remove_timer_from(timer_ctx_t ctx, uint32 timer_id, bool active_list)
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);
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);
PRINT("removed timer [%d] after [%d] from list %d\n", t->id,
prev->id, active_list);
}
os_mutex_unlock(&ctx->mutex);
@ -143,8 +143,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer)
else {
timer->next = t;
prev->next = timer;
PRINT("rescheduled timer [%d] after [%d]\n",
timer->id, prev->id);
PRINT("rescheduled timer [%d] after [%d]\n", timer->id,
prev->id);
}
goto out;
@ -158,8 +158,8 @@ reschedule_timer(timer_ctx_t ctx, app_timer_t *timer)
if (prev) {
/* insert to the list end */
prev->next = timer;
PRINT("rescheduled timer [%d] at end, after [%d]\n",
timer->id, prev->id);
PRINT("rescheduled timer [%d] at end, after [%d]\n", timer->id,
prev->id);
}
else {
/* insert at the begin */
@ -213,8 +213,8 @@ release_timer_list(app_timer_t **p_list)
timer_ctx_t
create_timer_ctx(timer_callback_f timer_handler,
check_timer_expiry_f expiery_checker,
int prealloc_num, unsigned int owner)
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));
@ -229,7 +229,7 @@ create_timer_ctx(timer_callback_f timer_handler,
ctx->owner = owner;
while (prealloc_num > 0) {
app_timer_t *timer = (app_timer_t*)BH_MALLOC(sizeof(app_timer_t));
app_timer_t *timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
if (timer == NULL)
goto cleanup;
@ -283,7 +283,7 @@ timer_ctx_get_owner(timer_ctx_t ctx)
}
void
add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
add_idle_timer(timer_ctx_t ctx, app_timer_t *timer)
{
os_mutex_lock(&ctx->mutex);
timer->next = ctx->idle_timers;
@ -292,8 +292,7 @@ add_idle_timer(timer_ctx_t ctx, app_timer_t * timer)
}
uint32
sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool auto_start)
sys_create_timer(timer_ctx_t ctx, int interval, bool is_period, bool auto_start)
{
app_timer_t *timer;
@ -307,7 +306,7 @@ sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
}
}
else {
timer = (app_timer_t*)BH_MALLOC(sizeof(app_timer_t));
timer = (app_timer_t *)BH_MALLOC(sizeof(app_timer_t));
if (timer == NULL)
return (uint32)-1;
}
@ -333,7 +332,7 @@ 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);
app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL)
return false;
@ -348,7 +347,7 @@ bool
sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id)
{
bool from_active;
app_timer_t * t = remove_timer(ctx, timer_id, &from_active);
app_timer_t *t = remove_timer(ctx, timer_id, &from_active);
if (t == NULL)
return false;
@ -362,7 +361,7 @@ sys_timer_destroy(timer_ctx_t ctx, uint32 timer_id)
bool
sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
{
app_timer_t * t = remove_timer(ctx, timer_id, NULL);
app_timer_t *t = remove_timer(ctx, timer_id, NULL);
if (t == NULL)
return false;
@ -376,7 +375,8 @@ sys_timer_restart(timer_ctx_t ctx, uint32 timer_id, int interval)
}
/*
* API called by the timer manager from another thread or the kernel timer handler
* API called by the timer manager from another thread or the kernel timer
* handler
*/
/**
@ -467,4 +467,3 @@ cleanup_app_timers(timer_ctx_t ctx)
os_mutex_unlock(&ctx->mutex);
}

View File

@ -12,28 +12,38 @@
extern "C" {
#endif
uint64 bh_get_tick_ms();
uint32 bh_get_elpased_ms(uint32 *last_system_clock);
uint64
bh_get_tick_ms();
uint32
bh_get_elpased_ms(uint32 *last_system_clock);
struct _timer_ctx;
typedef struct _timer_ctx * timer_ctx_t;
typedef struct _timer_ctx *timer_ctx_t;
typedef void (*timer_callback_f)(unsigned int 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);
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);
unsigned int timer_ctx_get_owner(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_destroy(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);
uint32 check_app_timers(timer_ctx_t ctx);
uint32 get_expiry_ms(timer_ctx_t ctx);
uint32
sys_create_timer(timer_ctx_t ctx, int interval, bool is_period,
bool auto_start);
bool
sys_timer_destroy(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);
uint32
check_app_timers(timer_ctx_t ctx);
uint32
get_expiry_ms(timer_ctx_t ctx);
#ifdef __cplusplus
}

View File

@ -9,19 +9,22 @@
#include <stdio.h>
#include <string.h>
char* optarg = NULL;
char *optarg = NULL;
int optind = 1;
int getopt(int argc, char *const argv[], const char *optstring)
int
getopt(int argc, char *const argv[], const char *optstring)
{
static int sp = 1;
int opt;
char *p;
if (sp == 1) {
if ((optind >= argc) || (argv[optind][0] != '-') || (argv[optind][1] == 0)){
if ((optind >= argc) || (argv[optind][0] != '-')
|| (argv[optind][1] == 0)) {
return -1;
} else if (!strcmp(argv[optind], "--")) {
}
else if (!strcmp(argv[optind], "--")) {
optind++;
return -1;
}
@ -31,24 +34,26 @@ int getopt(int argc, char *const argv[], const char *optstring)
p = strchr(optstring, opt);
if (opt == ':' || p == NULL) {
printf("illegal option : '-%c'\n", opt);
if ( argv[optind][++sp] == '\0') {
optind ++;
if (argv[optind][++sp] == '\0') {
optind++;
sp = 1;
}
return ('?');
}
if (p[1] == ':') {
if (p[1] == ':') {
if (argv[optind][sp + 1] != '\0')
optarg = &argv[optind++][sp + 1];
else if (++optind >= argc) {
printf("option '-%c' requires an argument :\n", opt);
sp = 1;
return ('?');
} else {
}
else {
optarg = argv[optind++];
}
sp = 1;
} else {
}
else {
if (argv[optind][++sp] == '\0') {
sp = 1;
optind++;

View File

@ -17,7 +17,8 @@ extern "C" {
extern char *optarg;
extern int optind;
int getopt(int argc, char *const argv[], const char *optstring);
int
getopt(int argc, char *const argv[], const char *optstring);
#ifdef __cplusplus
}

View File

@ -9,7 +9,7 @@
#endif
#if defined(_WIN32) || defined(_WIN32_)
char*
char *
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
@ -22,15 +22,13 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
return NULL;
}
if (_sopen_s(&file, filename, _O_RDONLY| _O_BINARY, _SH_DENYNO, 0)) {
printf("Read file to buffer failed: open file %s failed.\n",
filename);
if (_sopen_s(&file, filename, _O_RDONLY | _O_BINARY, _SH_DENYNO, 0)) {
printf("Read file to buffer failed: open file %s failed.\n", filename);
return NULL;
}
if (fstat(file, &stat_buf) != 0) {
printf("Read file to buffer failed: fstat file %s failed.\n",
filename);
printf("Read file to buffer failed: fstat file %s failed.\n", filename);
_close(file);
return NULL;
}
@ -61,7 +59,7 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
return buffer;
}
#else /* else of defined(_WIN32) || defined(_WIN32_) */
char*
char *
bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
{
char *buffer;
@ -75,14 +73,12 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
}
if ((file = open(filename, O_RDONLY, 0)) == -1) {
printf("Read file to buffer failed: open file %s failed.\n",
filename);
printf("Read file to buffer failed: open file %s failed.\n", filename);
return NULL;
}
if (fstat(file, &stat_buf) != 0) {
printf("Read file to buffer failed: fstat file %s failed.\n",
filename);
printf("Read file to buffer failed: fstat file %s failed.\n", filename);
close(file);
return NULL;
}

View File

@ -20,4 +20,3 @@ bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
#endif
#endif /* end of _BH_FILE_H */