summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils
diff options
context:
space:
mode:
Diffstat (limited to 'fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils')
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/SConscript17
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.c25
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.h42
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.c163
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.h73
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.c337
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.h168
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.c111
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.h109
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.c107
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.h88
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_platform.h38
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.c256
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.h80
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.c279
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.h126
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.c469
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.h51
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/shared_utils.cmake12
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/SConscript32
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.c65
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.h28
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.c117
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.h22
-rw-r--r--fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/shared_uncommon.cmake11
25 files changed, 2826 insertions, 0 deletions
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/SConscript b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/SConscript
new file mode 100644
index 000000000..358f2ffca
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/SConscript
@@ -0,0 +1,17 @@
+#
+# Copyright (c) 2021, RT-Thread Development Team
+#
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+
+from building import *
+import os
+
+cwd = GetCurrentDir()
+
+src = Glob('*.c')
+CPPPATH = [cwd]
+
+group = DefineGroup('iwasm_shared_utils', src, depend = [''], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.c
new file mode 100644
index 000000000..246c55d1b
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_assert.h"
+
+void
+bh_assert_internal(int64 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";
+
+ os_printf("\nASSERTION FAILED: %s, at file %s, line %d\n", expr_string,
+ file_name, line_number);
+
+ abort();
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.h
new file mode 100644
index 000000000..b7c995af8
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_assert.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_ASSERT_H
+#define _BH_ASSERT_H
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if BH_DEBUG != 0
+void
+bh_assert_internal(int64 v, const char *file_name, int line_number,
+ const char *expr_string);
+#define bh_assert(expr) \
+ bh_assert_internal((int64)(uintptr_t)(expr), __FILE__, __LINE__, #expr)
+#else
+#define bh_assert(expr) (void)0
+#endif /* end of BH_DEBUG */
+
+#if !defined(__has_extension)
+#define __has_extension(a) 0
+#endif
+
+#if __STDC_VERSION__ >= 201112L \
+ || (defined(__GNUC__) && __GNUC__ * 0x100 + __GNUC_MINOR__ >= 0x406) \
+ || __has_extension(c_static_assert)
+
+#define bh_static_assert(expr) _Static_assert(expr, #expr)
+#else
+#define bh_static_assert(expr) /* nothing */
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _BH_ASSERT_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.c
new file mode 100644
index 000000000..aeeab26bd
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.c
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_common.h"
+
+static char *
+align_ptr(char *src, unsigned int b)
+{
+ uintptr_t v = (uintptr_t)src;
+ uintptr_t m = b - 1;
+ return (char *)((v + m) & ~m);
+}
+
+/*
+Memory copy, with word alignment
+*/
+int
+b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n)
+{
+ char *dest = (char *)s1;
+ char *src = (char *)s2;
+
+ char *pa = align_ptr(src, 4);
+ char *pb = align_ptr((src + n), 4);
+
+ unsigned int buff;
+ const char *p_byte_read;
+
+ unsigned int *p;
+ char *ps;
+
+ if (pa > src) {
+ pa -= 4;
+ }
+
+ for (p = (unsigned int *)pa; p < (unsigned int *)pb; p++) {
+ buff = *(p);
+ p_byte_read = ((char *)&buff);
+
+ /* read leading word */
+ if ((char *)p <= src) {
+ for (ps = src; ps < ((char *)p + 4); ps++) {
+ if (ps >= src + n) {
+ break;
+ }
+ p_byte_read = ((char *)&buff) + (ps - (char *)p);
+ *dest++ = *p_byte_read;
+ }
+ }
+ /* read trailing word */
+ else if ((char *)p >= pb - 4) {
+ for (ps = (char *)p; ps < src + n; ps++) {
+ *dest++ = *p_byte_read++;
+ }
+ }
+ /* read meaning word(s) */
+ else {
+ if ((char *)p + 4 >= src + n) {
+ for (ps = (char *)p; ps < src + n; ps++) {
+ *dest++ = *p_byte_read++;
+ }
+ }
+ else {
+ *(unsigned int *)dest = buff;
+ dest += 4;
+ }
+ }
+ }
+
+ return 0;
+}
+
+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) {
+ 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) {
+ 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)
+{
+ if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)) {
+ return -1;
+ }
+
+ 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)) {
+ return -1;
+ }
+
+ memcpy(s1, s2, strlen(s2) + 1);
+ return 0;
+}
+
+char *
+bh_strdup(const char *s)
+{
+ uint32 size;
+ char *s1 = NULL;
+
+ if (s) {
+ size = (uint32)(strlen(s) + 1);
+ if ((s1 = BH_MALLOC(size)))
+ bh_memcpy_s(s1, size, s, size);
+ }
+ return s1;
+}
+
+char *
+wa_strdup(const char *s)
+{
+ uint32 size;
+ char *s1 = NULL;
+
+ if (s) {
+ size = (uint32)(strlen(s) + 1);
+ if ((s1 = WA_MALLOC(size)))
+ bh_memcpy_s(s1, size, s, size);
+ }
+ return s1;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.h
new file mode 100644
index 000000000..edb962eb1
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_common.h
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_COMMON_H
+#define _BH_COMMON_H
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+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_wa(dest, dlen, src, slen) \
+ do { \
+ int _ret = slen == 0 ? 0 : b_memcpy_wa(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_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_memcpy_wa(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);
+
+/* strdup with string allocated by WA_MALLOC */
+char *
+wa_strdup(const char *s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.c
new file mode 100644
index 000000000..3502239ad
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.c
@@ -0,0 +1,337 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_hashmap.h"
+
+typedef struct HashMapElem {
+ void *key;
+ void *value;
+ struct HashMapElem *next;
+} HashMapElem;
+
+struct HashMap {
+ /* size of element array */
+ uint32 size;
+ /* lock for elements */
+ korp_mutex *lock;
+ /* hash function of key */
+ HashFunc hash_func;
+ /* key equal function */
+ KeyEqualFunc key_equal_func;
+ KeyDestroyFunc key_destroy_func;
+ ValueDestroyFunc value_destroy_func;
+ HashMapElem *elements[1];
+};
+
+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;
+ uint64 total_size;
+
+ if (size < HASH_MAP_MIN_SIZE)
+ size = HASH_MAP_MIN_SIZE;
+
+ if (size > HASH_MAP_MAX_SIZE) {
+ LOG_ERROR("HashMap create failed: size is too large.\n");
+ return NULL;
+ }
+
+ if (!hash_func || !key_equal_func) {
+ LOG_ERROR("HashMap create failed: hash function or key equal function "
+ " is NULL.\n");
+ return NULL;
+ }
+
+ 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))) {
+ LOG_ERROR("HashMap create failed: alloc memory failed.\n");
+ return NULL;
+ }
+
+ memset(map, 0, (uint32)total_size);
+
+ if (use_lock) {
+ 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);
+ return NULL;
+ }
+ }
+
+ map->size = size;
+ map->hash_func = hash_func;
+ map->key_equal_func = key_equal_func;
+ map->key_destroy_func = key_destroy_func;
+ map->value_destroy_func = value_destroy_func;
+ return map;
+}
+
+bool
+bh_hash_map_insert(HashMap *map, void *key, void *value)
+{
+ uint32 index;
+ HashMapElem *elem;
+
+ if (!map || !key) {
+ LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
+ return false;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ index = map->hash_func(key) % map->size;
+ elem = map->elements[index];
+ while (elem) {
+ if (map->key_equal_func(elem->key, key)) {
+ LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
+ goto fail;
+ }
+ elem = elem->next;
+ }
+
+ if (!(elem = BH_MALLOC(sizeof(HashMapElem)))) {
+ LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
+ goto fail;
+ }
+
+ elem->key = key;
+ elem->value = value;
+ elem->next = map->elements[index];
+ map->elements[index] = elem;
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return true;
+
+fail:
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return false;
+}
+
+void *
+bh_hash_map_find(HashMap *map, void *key)
+{
+ uint32 index;
+ HashMapElem *elem;
+ void *value;
+
+ if (!map || !key) {
+ LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
+ return NULL;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ index = map->hash_func(key) % map->size;
+ elem = map->elements[index];
+
+ while (elem) {
+ if (map->key_equal_func(elem->key, key)) {
+ value = elem->value;
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return value;
+ }
+ elem = elem->next;
+ }
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return NULL;
+}
+
+bool
+bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
+{
+ uint32 index;
+ HashMapElem *elem;
+
+ if (!map || !key) {
+ LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
+ return false;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ index = map->hash_func(key) % map->size;
+ elem = map->elements[index];
+
+ while (elem) {
+ if (map->key_equal_func(elem->key, key)) {
+ if (p_old_value)
+ *p_old_value = elem->value;
+ elem->value = value;
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return true;
+ }
+ elem = elem->next;
+ }
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return false;
+}
+
+bool
+bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
+ void **p_old_value)
+{
+ uint32 index;
+ HashMapElem *elem, *prev;
+
+ if (!map || !key) {
+ LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
+ return false;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ index = map->hash_func(key) % map->size;
+ prev = elem = map->elements[index];
+
+ while (elem) {
+ if (map->key_equal_func(elem->key, key)) {
+ if (p_old_key)
+ *p_old_key = elem->key;
+ if (p_old_value)
+ *p_old_value = elem->value;
+
+ if (elem == map->elements[index])
+ map->elements[index] = elem->next;
+ else
+ prev->next = elem->next;
+
+ BH_FREE(elem);
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return true;
+ }
+
+ prev = elem;
+ elem = elem->next;
+ }
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+ return false;
+}
+
+bool
+bh_hash_map_destroy(HashMap *map)
+{
+ uint32 index;
+ HashMapElem *elem, *next;
+
+ if (!map) {
+ LOG_ERROR("HashMap destroy failed: map is NULL.\n");
+ return false;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ for (index = 0; index < map->size; index++) {
+ elem = map->elements[index];
+ while (elem) {
+ next = elem->next;
+
+ if (map->key_destroy_func) {
+ map->key_destroy_func(elem->key);
+ }
+ if (map->value_destroy_func) {
+ map->value_destroy_func(elem->value);
+ }
+ BH_FREE(elem);
+
+ elem = next;
+ }
+ }
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ os_mutex_destroy(map->lock);
+ }
+ BH_FREE(map);
+ return true;
+}
+
+uint32
+bh_hash_map_get_struct_size(HashMap *hashmap)
+{
+ uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
+ + (uint32)sizeof(HashMapElem *) * hashmap->size;
+
+ if (hashmap->lock) {
+ size += (uint32)sizeof(korp_mutex);
+ }
+
+ return size;
+}
+
+uint32
+bh_hash_map_get_elem_struct_size()
+{
+ return (uint32)sizeof(HashMapElem);
+}
+
+bool
+bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
+ void *user_data)
+{
+ uint32 index;
+ HashMapElem *elem, *next;
+
+ if (!map || !callback) {
+ LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
+ return false;
+ }
+
+ if (map->lock) {
+ os_mutex_lock(map->lock);
+ }
+
+ for (index = 0; index < map->size; index++) {
+ elem = map->elements[index];
+ while (elem) {
+ next = elem->next;
+ callback(elem->key, elem->value, user_data);
+ elem = next;
+ }
+ }
+
+ if (map->lock) {
+ os_mutex_unlock(map->lock);
+ }
+
+ return true;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.h
new file mode 100644
index 000000000..38aa2c668
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_hashmap.h
@@ -0,0 +1,168 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef WASM_HASHMAP_H
+#define WASM_HASHMAP_H
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Minimum initial size of hash map */
+#define HASH_MAP_MIN_SIZE 4
+
+/* Maximum initial size of hash map */
+#define HASH_MAP_MAX_SIZE 65536
+
+struct HashMap;
+typedef struct HashMap HashMap;
+
+/* Hash function: to get the hash value of key. */
+typedef uint32 (*HashFunc)(const void *key);
+
+/* Key equal function: to check whether two keys are equal. */
+typedef bool (*KeyEqualFunc)(void *key1, void *key2);
+
+/* Key destroy function: to destroy the key, auto called
+ for each key when the hash map is destroyed. */
+typedef void (*KeyDestroyFunc)(void *key);
+
+/* Value destroy function: to destroy the value, auto called
+ for each value when the hash map is destroyed. */
+typedef void (*ValueDestroyFunc)(void *value);
+
+/* traverse callback function:
+ auto called when traverse every hash element */
+typedef void (*TraverseCallbackFunc)(void *key, void *value, void *user_data);
+
+/**
+ * Create a hash map.
+ *
+ * @param size: the initial size of the hash map
+ * @param use_lock whether to lock the hash map when operating on it
+ * @param hash_func hash function of the key, must be specified
+ * @param key_equal_func key equal function, check whether two keys
+ * are equal, must be specified
+ * @param key_destroy_func key destroy function, called for each key if not NULL
+ * when the hash map is destroyed
+ * @param value_destroy_func value destroy function, called for each value if
+ * not NULL when the hash map is destroyed
+ *
+ * @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,
+ ValueDestroyFunc value_destroy_func);
+
+/**
+ * Insert an element to the hash map
+ *
+ * @param map the hash map to insert element
+ * @key the key of the element
+ * @value the value of the element
+ *
+ * @return true if success, false otherwise
+ * Note: fail if key is NULL or duplicated key exists in the hash map,
+ */
+bool
+bh_hash_map_insert(HashMap *map, void *key, void *value);
+
+/**
+ * Find an element in the hash map
+ *
+ * @param map the hash map to find element
+ * @key the key of the element
+ *
+ * @return the value of the found element if success, NULL otherwise
+ */
+void *
+bh_hash_map_find(HashMap *map, void *key);
+
+/**
+ * Update an element in the hash map with new value
+ *
+ * @param map the hash map to update element
+ * @key the key of the element
+ * @value the new value of the element
+ * @p_old_value if not NULL, copies the old value to it
+ *
+ * @return true if success, false otherwise
+ * Note: the old value won't be destroyed by value destroy function,
+ * 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);
+
+/**
+ * Remove an element from the hash map
+ *
+ * @param map the hash map to remove element
+ * @key the key of the element
+ * @p_old_key if not NULL, copies the old key to it
+ * @p_old_value if not NULL, copies the old value to it
+ *
+ * @return true if success, false otherwise
+ * Note: the old key and old value won't be destroyed by key destroy
+ * function and value destroy function, they will be copied to
+ * 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);
+
+/**
+ * Destroy the hashmap
+ *
+ * @param map the hash map to destroy
+ *
+ * @return true if success, false otherwise
+ * Note: the key destroy function and value destroy function will be
+ * called to destroy each element's key and value if they are
+ * not NULL.
+ */
+bool
+bh_hash_map_destroy(HashMap *map);
+
+/**
+ * Get the structure size of HashMap
+ *
+ * @param map the hash map to calculate
+ *
+ * @return the memory space occupied by HashMap structure
+ */
+uint32
+bh_hash_map_get_struct_size(HashMap *hashmap);
+
+/**
+ * Get the structure size of HashMap Element
+ *
+ * @return the memory space occupied by HashMapElem structure
+ */
+uint32
+bh_hash_map_get_elem_struct_size(void);
+
+/**
+ * Traverse the hash map and call the callback function
+ *
+ * @param map the hash map to traverse
+ * @param callback the function to be called for every element
+ * @param user_data the argument to be passed to the callback function
+ *
+ * @return true if success, false otherwise
+ * Note: if the hash map has lock, the map will be locked during traverse,
+ * keep the callback function as simple as possible.
+ */
+bool
+bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
+ void *user_data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* endof WASM_HASHMAP_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.c
new file mode 100644
index 000000000..7102d42a1
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_list.h"
+
+#if BH_DEBUG != 0
+/**
+ * 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.
+ */
+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;
+#if BH_DEBUG != 0
+ 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);
+}
+
+#if BH_DEBUG != 0
+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
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.h
new file mode 100644
index 000000000..f10215324
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_list.h
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_LIST_H
+#define _BH_LIST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#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;
+
+/* 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.
+ */
+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.
+ *
+ * @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 */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.c
new file mode 100644
index 000000000..78c058065
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.c
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_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 uint32 log_verbose_level = BH_LOG_LEVEL_WARNING;
+
+void
+bh_log_set_verbose_level(uint32 level)
+{
+ log_verbose_level = level;
+}
+
+void
+bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
+{
+ va_list ap;
+ korp_tid self;
+ char buf[32] = { 0 };
+ uint64 usec;
+ uint32 t, h, m, s, mills;
+
+ if ((uint32)log_level > log_verbose_level)
+ return;
+
+ self = os_self_thread();
+
+ usec = os_time_get_boot_microsecond();
+ t = (uint32)(usec / 1000000) % (24 * 60 * 60);
+ h = t / (60 * 60);
+ t = t % (60 * 60);
+ m = t / 60;
+ s = t % 60;
+ mills = (uint32)(usec % 1000);
+
+ snprintf(buf, sizeof(buf),
+ "%02" PRIu32 ":%02" PRIu32 ":%02" PRIu32 ":%03" PRIu32, h, m, s,
+ mills);
+
+ os_printf("[%s - %" PRIXPTR "]: ", buf, (uintptr_t)self);
+
+ if (file)
+ os_printf("%s, line %d, ", file, line);
+
+ va_start(ap, fmt);
+ os_vprintf(fmt, ap);
+ va_end(ap);
+
+ os_printf("\n");
+}
+
+static uint32 last_time_ms = 0;
+static uint32 total_time_ms = 0;
+
+void
+bh_print_time(const char *prompt)
+{
+ uint32 curr_time_ms;
+
+ if (log_verbose_level < 3)
+ return;
+
+ curr_time_ms = (uint32)bh_get_tick_ms();
+
+ if (last_time_ms == 0)
+ last_time_ms = curr_time_ms;
+
+ total_time_ms += curr_time_ms - last_time_ms;
+
+ os_printf("%-48s time of last stage: %" PRIu32 " ms, total time: %" PRIu32
+ " ms\n",
+ prompt, curr_time_ms - last_time_ms, total_time_ms);
+
+ last_time_ms = curr_time_ms;
+}
+
+void
+bh_print_proc_mem(const char *prompt)
+{
+ char buf[1024] = { 0 };
+
+ if (log_verbose_level < BH_LOG_LEVEL_DEBUG)
+ return;
+
+ if (os_dumps_proc_mem_info(buf, sizeof(buf)) != 0)
+ return;
+
+ os_printf("%s\n", prompt);
+ os_printf("===== memory usage =====\n");
+ os_printf("%s", buf);
+ os_printf("==========\n");
+ return;
+}
+
+void
+bh_log_proc_mem(const char *function, uint32 line)
+{
+ char prompt[128] = { 0 };
+ snprintf(prompt, sizeof(prompt), "[MEM] %s(...) L%" PRIu32, function, line);
+ bh_print_proc_mem(prompt);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.h
new file mode 100644
index 000000000..e0bc61da2
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_log.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+/**
+ * @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 "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef enum {
+ BH_LOG_LEVEL_FATAL = 0,
+ BH_LOG_LEVEL_ERROR = 1,
+ BH_LOG_LEVEL_WARNING = 2,
+ BH_LOG_LEVEL_DEBUG = 3,
+ BH_LOG_LEVEL_VERBOSE = 4
+} LogLevel;
+
+void
+bh_log_set_verbose_level(uint32 level);
+
+void
+bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
+
+#ifdef BH_PLATFORM_NUTTX
+
+#undef LOG_FATAL
+#undef LOG_ERROR
+#undef LOG_WARNING
+#undef LOG_VERBOSE
+#undef LOG_DEBUG
+
+#endif
+
+#if BH_DEBUG != 0
+#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__)
+#endif
+
+#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__)
+#else
+#define LOG_DEBUG(...) (void)0
+#endif
+
+void
+bh_print_time(const char *prompt);
+
+void
+bh_print_proc_mem(const char *prompt);
+
+void
+bh_log_proc_mem(const char *function, uint32 line);
+
+#define LOG_PROC_MEM(...) bh_log_proc_mem(__FUNCTION__, __LINE__)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _BH_LOG_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_platform.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_platform.h
new file mode 100644
index 000000000..86aef839d
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_platform.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_PLATFORM_H
+#define _BH_PLATFORM_H
+
+#include "../platform/include/platform_common.h"
+#include "../platform/include/platform_api_vmcore.h"
+#include "../platform/include/platform_api_extension.h"
+#include "bh_assert.h"
+#include "bh_common.h"
+#include "bh_hashmap.h"
+#include "bh_list.h"
+#include "bh_log.h"
+#include "bh_queue.h"
+#include "bh_vector.h"
+#include "runtime_timer.h"
+
+/**
+ * WA_MALLOC/WA_FREE need to be redefined for both
+ * runtime native and WASM app respectively.
+ *
+ * Some source files are shared for building native and WASM,
+ * and this the mem allocator API for these files.
+ *
+ * Here we define it for the native world
+ */
+#ifndef WA_MALLOC
+#define WA_MALLOC wasm_runtime_malloc
+#endif
+
+#ifndef WA_FREE
+#define WA_FREE wasm_runtime_free
+#endif
+
+#endif /* #ifndef _BH_PLATFORM_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.c
new file mode 100644
index 000000000..7c860d11a
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.c
@@ -0,0 +1,256 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_queue.h"
+
+typedef struct bh_queue_node {
+ struct bh_queue_node *next;
+ struct bh_queue_node *prev;
+ unsigned short tag;
+ unsigned int 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;
+}
+
+uint32
+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, uint64 timeout_us)
+{
+ 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_us == 0) {
+ bh_queue_mutex_unlock(&queue->queue_lock);
+ return NULL;
+ }
+
+ bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
+ timeout_us);
+ }
+
+ 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,
+ void *arg)
+{
+ if (!queue)
+ return;
+
+ while (!queue->exit_loop_run) {
+ bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);
+
+ if (message) {
+ handle_cb(message, arg);
+ 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);
+ }
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.h
new file mode 100644
index 000000000..c15f43526
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_queue.h
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_QUEUE_H
+#define _BH_QUEUE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#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, void *arg);
+
+#define bh_queue_malloc BH_MALLOC
+#define bh_queue_free BH_FREE
+
+#define bh_queue_mutex korp_mutex
+#define bh_queue_cond korp_cond
+
+#define bh_queue_mutex_init os_mutex_init
+#define bh_queue_mutex_destroy os_mutex_destroy
+#define bh_queue_mutex_lock os_mutex_lock
+#define bh_queue_mutex_unlock os_mutex_unlock
+
+#define bh_queue_cond_init os_cond_init
+#define bh_queue_cond_destroy os_cond_destroy
+#define bh_queue_cond_wait os_cond_wait
+#define bh_queue_cond_timedwait os_cond_reltimedwait
+#define bh_queue_cond_signal os_cond_signal
+#define bh_queue_cond_broadcast os_cond_broadcast
+
+typedef void (*bh_msg_cleaner)(void *msg);
+
+bh_queue *
+bh_queue_create(void);
+
+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);
+
+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);
+
+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 *arg);
+void
+bh_queue_exit_loop_run(bh_queue *queue);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* #ifndef _BH_QUEUE_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.c
new file mode 100644
index 000000000..352ce7192
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.c
@@ -0,0 +1,279 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "bh_vector.h"
+
+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
+ || total_size > UINT32_MAX) {
+ return NULL;
+ }
+
+ if ((data = BH_MALLOC((uint32)total_size))) {
+ memset(data, 0, (uint32)total_size);
+ }
+
+ return data;
+}
+
+/**
+ * every caller of `extend_vector` must provide
+ * a thread-safe environment.
+ */
+static bool
+extend_vector(Vector *vector, size_t length)
+{
+ uint8 *data;
+
+ if (length <= vector->max_elems)
+ return true;
+
+ if (length < vector->size_elem * 3 / 2)
+ length = vector->size_elem * 3 / 2;
+
+ if (!(data = alloc_vector_data(length, vector->size_elem))) {
+ return false;
+ }
+
+ bh_memcpy_s(data, (uint32)(vector->size_elem * length), vector->data,
+ (uint32)(vector->size_elem * vector->max_elems));
+ BH_FREE(vector->data);
+
+ vector->data = data;
+ vector->max_elems = length;
+ return true;
+}
+
+bool
+bh_vector_init(Vector *vector, size_t init_length, size_t size_elem,
+ bool use_lock)
+{
+ if (!vector) {
+ LOG_ERROR("Init vector failed: vector is NULL.\n");
+ return false;
+ }
+
+ if (init_length == 0) {
+ init_length = 4;
+ }
+
+ if (!(vector->data = alloc_vector_data(init_length, size_elem))) {
+ LOG_ERROR("Init vector failed: alloc memory failed.\n");
+ return false;
+ }
+
+ vector->size_elem = size_elem;
+ vector->max_elems = init_length;
+ vector->num_elems = 0;
+ vector->lock = NULL;
+
+ if (use_lock) {
+ if (!(vector->lock = BH_MALLOC(sizeof(korp_mutex)))) {
+ LOG_ERROR("Init vector failed: alloc locker failed.\n");
+ bh_vector_destroy(vector);
+ return false;
+ }
+
+ if (BHT_OK != os_mutex_init(vector->lock)) {
+ LOG_ERROR("Init vector failed: init locker failed.\n");
+
+ BH_FREE(vector->lock);
+ vector->lock = NULL;
+
+ bh_vector_destroy(vector);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+bool
+bh_vector_set(Vector *vector, uint32 index, const void *elem_buf)
+{
+ if (!vector || !elem_buf) {
+ LOG_ERROR("Set vector elem failed: vector or elem buf is NULL.\n");
+ return false;
+ }
+
+ if (index >= vector->num_elems) {
+ LOG_ERROR("Set vector elem failed: invalid elem index.\n");
+ return false;
+ }
+
+ if (vector->lock)
+ os_mutex_lock(vector->lock);
+ bh_memcpy_s(vector->data + vector->size_elem * index,
+ (uint32)vector->size_elem, elem_buf, (uint32)vector->size_elem);
+ if (vector->lock)
+ os_mutex_unlock(vector->lock);
+ return true;
+}
+
+bool
+bh_vector_get(Vector *vector, uint32 index, void *elem_buf)
+{
+ if (!vector || !elem_buf) {
+ LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n");
+ return false;
+ }
+
+ if (index >= vector->num_elems) {
+ LOG_ERROR("Get vector elem failed: invalid elem index.\n");
+ return false;
+ }
+
+ if (vector->lock)
+ os_mutex_lock(vector->lock);
+ bh_memcpy_s(elem_buf, (uint32)vector->size_elem,
+ vector->data + vector->size_elem * index,
+ (uint32)vector->size_elem);
+ if (vector->lock)
+ os_mutex_unlock(vector->lock);
+ return true;
+}
+
+bool
+bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
+{
+ size_t i;
+ uint8 *p;
+ bool ret = false;
+
+ if (!vector || !elem_buf) {
+ LOG_ERROR("Insert vector elem failed: vector or elem buf is NULL.\n");
+ goto just_return;
+ }
+
+ if (index >= vector->num_elems) {
+ LOG_ERROR("Insert vector elem failed: invalid elem index.\n");
+ goto just_return;
+ }
+
+ if (vector->lock)
+ os_mutex_lock(vector->lock);
+
+ if (!extend_vector(vector, vector->num_elems + 1)) {
+ LOG_ERROR("Insert vector elem failed: extend vector failed.\n");
+ goto unlock_return;
+ }
+
+ p = vector->data + vector->size_elem * vector->num_elems;
+ for (i = vector->num_elems - 1; i > index; i--) {
+ bh_memcpy_s(p, (uint32)vector->size_elem, p - vector->size_elem,
+ (uint32)vector->size_elem);
+ p -= vector->size_elem;
+ }
+
+ bh_memcpy_s(p, (uint32)vector->size_elem, elem_buf,
+ (uint32)vector->size_elem);
+ vector->num_elems++;
+ ret = true;
+
+unlock_return:
+ if (vector->lock)
+ os_mutex_unlock(vector->lock);
+just_return:
+ return ret;
+}
+
+bool
+bh_vector_append(Vector *vector, const void *elem_buf)
+{
+ bool ret = false;
+
+ if (!vector || !elem_buf) {
+ LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n");
+ goto just_return;
+ }
+
+ /* make sure one more slot is used by the thread who allocas it */
+ if (vector->lock)
+ os_mutex_lock(vector->lock);
+
+ if (!extend_vector(vector, vector->num_elems + 1)) {
+ LOG_ERROR("Append ector elem failed: extend vector failed.\n");
+ goto unlock_return;
+ }
+
+ bh_memcpy_s(vector->data + vector->size_elem * vector->num_elems,
+ (uint32)vector->size_elem, elem_buf, (uint32)vector->size_elem);
+ vector->num_elems++;
+ ret = true;
+
+unlock_return:
+ if (vector->lock)
+ os_mutex_unlock(vector->lock);
+just_return:
+ return ret;
+}
+
+bool
+bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf)
+{
+ uint32 i;
+ uint8 *p;
+
+ if (!vector) {
+ LOG_ERROR("Remove vector elem failed: vector is NULL.\n");
+ return false;
+ }
+
+ if (index >= vector->num_elems) {
+ LOG_ERROR("Remove vector elem failed: invalid elem index.\n");
+ return false;
+ }
+
+ if (vector->lock)
+ os_mutex_lock(vector->lock);
+ p = vector->data + vector->size_elem * index;
+
+ if (old_elem_buf) {
+ bh_memcpy_s(old_elem_buf, (uint32)vector->size_elem, p,
+ (uint32)vector->size_elem);
+ }
+
+ for (i = index; i < vector->num_elems - 1; i++) {
+ bh_memcpy_s(p, (uint32)vector->size_elem, p + vector->size_elem,
+ (uint32)vector->size_elem);
+ p += vector->size_elem;
+ }
+
+ vector->num_elems--;
+ if (vector->lock)
+ os_mutex_unlock(vector->lock);
+ return true;
+}
+
+size_t
+bh_vector_size(const Vector *vector)
+{
+ return vector ? vector->num_elems : 0;
+}
+
+bool
+bh_vector_destroy(Vector *vector)
+{
+ if (!vector) {
+ LOG_ERROR("Destroy vector elem failed: vector is NULL.\n");
+ return false;
+ }
+
+ if (vector->data)
+ BH_FREE(vector->data);
+
+ if (vector->lock) {
+ os_mutex_destroy(vector->lock);
+ BH_FREE(vector->lock);
+ }
+
+ memset(vector, 0, sizeof(Vector));
+ return true;
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.h
new file mode 100644
index 000000000..d0aaaf19b
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/bh_vector.h
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _WASM_VECTOR_H
+#define _WASM_VECTOR_H
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define DEFAULT_VECTOR_INIT_SIZE 8
+
+typedef struct Vector {
+ /* max element number */
+ size_t max_elems;
+ /* vector data allocated */
+ uint8 *data;
+ /* current element num */
+ size_t num_elems;
+ /* size of each element */
+ size_t size_elem;
+ void *lock;
+} Vector;
+
+/**
+ * Initialize vector
+ *
+ * @param vector the vector to init
+ * @param init_length the initial length of the vector
+ * @param size_elem size of each element
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_init(Vector *vector, size_t init_length, size_t size_elem,
+ bool use_lock);
+
+/**
+ * Set element of vector
+ *
+ * @param vector the vector to set
+ * @param index the index of the element to set
+ * @param elem_buf the element buffer which stores the element data
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_set(Vector *vector, uint32 index, const void *elem_buf);
+
+/**
+ * Get element of vector
+ *
+ * @param vector the vector to get
+ * @param index the index of the element to get
+ * @param elem_buf the element buffer to store the element data,
+ * whose length must be no less than element size
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_get(Vector *vector, uint32 index, void *elem_buf);
+
+/**
+ * Insert element of vector
+ *
+ * @param vector the vector to insert
+ * @param index the index of the element to insert
+ * @param elem_buf the element buffer which stores the element data
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf);
+
+/**
+ * Append element to the end of vector
+ *
+ * @param vector the vector to append
+ * @param elem_buf the element buffer which stores the element data
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_append(Vector *vector, const void *elem_buf);
+
+/**
+ * Remove element from vector
+ *
+ * @param vector the vector to remove element
+ * @param index the index of the element to remove
+ * @param old_elem_buf if not NULL, copies the element data to the buffer
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf);
+
+/**
+ * Return the size of the vector
+ *
+ * @param vector the vector to get size
+ *
+ * @return return the size of the vector
+ */
+size_t
+bh_vector_size(const Vector *vector);
+
+/**
+ * Destroy the vector
+ *
+ * @param vector the vector to destroy
+ *
+ * @return true if success, false otherwise
+ */
+bool
+bh_vector_destroy(Vector *vector);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* endof _WASM_VECTOR_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.c
new file mode 100644
index 000000000..8fccf4c2f
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.c
@@ -0,0 +1,469 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#include "runtime_timer.h"
+
+#if 1
+#define PRINT(...) (void)0
+#else
+#define PRINT printf
+#endif
+
+typedef struct _app_timer {
+ struct _app_timer *next;
+ uint32 id;
+ uint32 interval;
+ uint64 expiry;
+ bool is_periodic;
+} app_timer_t;
+
+struct _timer_ctx {
+ app_timer_t *app_timers;
+ app_timer_t *idle_timers;
+ app_timer_t *free_timers;
+ uint32 max_timer_id;
+ int pre_allocated;
+ uint32 owner;
+
+ /* mutex and condition */
+ korp_cond cond;
+ korp_mutex mutex;
+
+ timer_callback_f timer_callback;
+ check_timer_expiry_f refresh_checker;
+};
+
+uint64
+bh_get_tick_ms()
+{
+ return os_time_get_boot_microsecond() / 1000;
+}
+
+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) {
+ PRINT("system clock overrun!\n");
+ elpased_ms = now + (UINT32_MAX - *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)
+{
+ app_timer_t **head, *prev, *t;
+
+ os_mutex_lock(&ctx->mutex);
+
+ if (active_list)
+ head = &ctx->app_timers;
+ else
+ head = &ctx->idle_timers;
+
+ t = *head;
+ 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);
+ }
+ os_mutex_unlock(&ctx->mutex);
+
+ if (active_list && prev == NULL && ctx->refresh_checker)
+ ctx->refresh_checker(ctx);
+ return t;
+ }
+ else {
+ prev = t;
+ t = t->next;
+ }
+ }
+
+ os_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)
+{
+ app_timer_t *t;
+ app_timer_t *prev = NULL;
+
+ os_mutex_lock(&ctx->mutex);
+
+ t = ctx->app_timers;
+ timer->next = NULL;
+ timer->expiry = bh_get_tick_ms() + timer->interval;
+
+ while (t) {
+ if (timer->expiry < t->expiry) {
+ if (prev == NULL) {
+ timer->next = ctx->app_timers;
+ ctx->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);
+ }
+
+ goto out;
+ }
+ 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->app_timers == NULL);
+ ctx->app_timers = timer;
+ PRINT("rescheduled timer [%d] as first\n", timer->id);
+ }
+
+out:
+ os_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) {
+ os_mutex_lock(&ctx->mutex);
+ t->next = ctx->free_timers;
+ ctx->free_timers = t;
+ PRINT("recycle timer :%d\n", t->id);
+ os_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--;
+ }
+
+ if (os_cond_init(&ctx->cond) != 0)
+ goto cleanup;
+
+ if (os_mutex_init(&ctx->mutex) != 0) {
+ os_cond_destroy(&ctx->cond);
+ goto cleanup;
+ }
+
+ 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);
+ }
+ PRINT("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);
+
+ os_cond_destroy(&ctx->cond);
+ os_mutex_destroy(&ctx->mutex);
+ BH_FREE(ctx);
+}
+
+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)
+{
+ os_mutex_lock(&ctx->mutex);
+ timer->next = ctx->idle_timers;
+ ctx->idle_timers = timer;
+ os_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 (uint32)-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 (uint32)-1;
+ }
+
+ memset(timer, 0, sizeof(*timer));
+
+ ctx->max_timer_id++;
+ if (ctx->max_timer_id == (uint32)-1)
+ ctx->max_timer_id++;
+ timer->id = ctx->max_timer_id;
+ timer->interval = (uint32)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_destroy(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_destroy 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;
+
+ t->interval = (uint32)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);
+
+ /* get next expired timer first, since the following
+ operation may change expired->next */
+ 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);
+ }
+ }
+}
+
+uint32
+get_expiry_ms(timer_ctx_t ctx)
+{
+ uint32 ms_to_next_expiry;
+ uint64 now = bh_get_tick_ms();
+
+ os_mutex_lock(&ctx->mutex);
+ if (ctx->app_timers == NULL)
+ ms_to_next_expiry = (uint32)-1;
+ else if (ctx->app_timers->expiry >= now)
+ ms_to_next_expiry = (uint32)(ctx->app_timers->expiry - now);
+ else
+ ms_to_next_expiry = 0;
+ os_mutex_unlock(&ctx->mutex);
+
+ return ms_to_next_expiry;
+}
+
+uint32
+check_app_timers(timer_ctx_t ctx)
+{
+ app_timer_t *t, *expired = NULL, *expired_end = NULL;
+ uint64 now = bh_get_tick_ms();
+
+ os_mutex_lock(&ctx->mutex);
+
+ t = ctx->app_timers;
+ while (t) {
+ if (now >= t->expiry) {
+ ctx->app_timers = t->next;
+
+ /* append t to the end of expired list */
+ t->next = NULL;
+ if (!expired_end) {
+ expired = expired_end = t;
+ }
+ else {
+ expired_end->next = t;
+ expired_end = t;
+ }
+
+ t = ctx->app_timers;
+ }
+ else {
+ break;
+ }
+ }
+ os_mutex_unlock(&ctx->mutex);
+
+ handle_expired_timers(ctx, expired);
+ return get_expiry_ms(ctx);
+}
+
+void
+cleanup_app_timers(timer_ctx_t ctx)
+{
+ os_mutex_lock(&ctx->mutex);
+
+ release_timer_list(&ctx->app_timers);
+ release_timer_list(&ctx->idle_timers);
+
+ os_mutex_unlock(&ctx->mutex);
+}
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.h
new file mode 100644
index 000000000..b8d90c5ff
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/runtime_timer.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef LIB_BASE_RUNTIME_TIMER_H_
+#define LIB_BASE_RUNTIME_TIMER_H_
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+uint64
+bh_get_tick_ms(void);
+uint32
+bh_get_elpased_ms(uint32 *last_system_clock);
+
+struct _timer_ctx;
+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);
+void destroy_timer_ctx(timer_ctx_t);
+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);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* LIB_BASE_RUNTIME_TIMER_H_ */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/shared_utils.cmake b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/shared_utils.cmake
new file mode 100644
index 000000000..5b7d02dde
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/shared_utils.cmake
@@ -0,0 +1,12 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+set (UTILS_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+include_directories(${UTILS_SHARED_DIR})
+
+file (GLOB source_all ${UTILS_SHARED_DIR}/*.c)
+
+set (UTILS_SHARED_SOURCE ${source_all})
+
+LIST (APPEND RUNTIME_LIB_HEADER_LIST "${UTILS_SHARED_DIR}/runtime_timer.h")
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/SConscript b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/SConscript
new file mode 100644
index 000000000..f608645fe
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/SConscript
@@ -0,0 +1,32 @@
+#
+# Copyright (c) 2021, RT-Thread Development Team
+#
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+
+from building import *
+import os
+
+cwd = GetCurrentDir()
+
+# src = Split('''
+# ''')
+
+
+def addSrcFiles(arr, path):
+ for f in os.listdir(path):
+ fpath = os.path.join(path, f);
+ if os.path.isfile(fpath):
+ ext = os.path.splitext(fpath)[-1]
+ if ext == '.c' or ext == '.cpp':
+ arr += [fpath]
+ #elif os.path.isdir(fpath):
+ # addSrcFiles(arr, fpath)
+
+src = Glob('*.c')
+src += Glob('*.cpp')
+CPPPATH = [cwd]
+
+group = DefineGroup('iwasm_shared_utils_uncommon', src, depend = [''], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.c
new file mode 100644
index 000000000..19e23a7b5
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2020 Ant Financial Services Group. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef __GNUC__
+
+#include "bh_getopt.h"
+#include <stdio.h>
+#include <string.h>
+
+char *optarg = NULL;
+int optind = 1;
+
+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)) {
+ return -1;
+ }
+ else if (!strcmp(argv[optind], "--")) {
+ optind++;
+ return -1;
+ }
+ }
+
+ opt = argv[optind][sp];
+ p = strchr(optstring, opt);
+ if (opt == ':' || p == NULL) {
+ printf("illegal option : '-%c'\n", opt);
+ if (argv[optind][++sp] == '\0') {
+ optind++;
+ sp = 1;
+ }
+ return ('?');
+ }
+ 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 {
+ optarg = argv[optind++];
+ }
+ sp = 1;
+ }
+ else {
+ if (argv[optind][++sp] == '\0') {
+ sp = 1;
+ optind++;
+ }
+ optarg = NULL;
+ }
+ return (opt);
+}
+#endif
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.h
new file mode 100644
index 000000000..efd3ab403
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_getopt.h
@@ -0,0 +1,28 @@
+/*
+ * Copyright (C) 2020 Ant Financial Services Group. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifdef __GNUC__
+#include <getopt.h>
+#endif
+#ifndef __GNUC__
+#ifndef GETOPT_H__
+#define GETOPT_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern char *optarg;
+extern int optind;
+
+int
+getopt(int argc, char *const argv[], const char *optstring);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of GETOPT_H__ */
+#endif /* end of __GNUC__ */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.c b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.c
new file mode 100644
index 000000000..5ddf1b601
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.c
@@ -0,0 +1,117 @@
+#include "bh_read_file.h"
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#if defined(_WIN32) || defined(_WIN32_)
+#include <io.h>
+#else
+#include <unistd.h>
+#endif
+
+#if defined(_WIN32) || defined(_WIN32_)
+
+#if defined(__MINGW32__) && !defined(_SH_DENYNO)
+#define _SH_DENYNO 0x40
+#endif
+
+char *
+bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
+{
+ char *buffer;
+ int file;
+ uint32 file_size, buf_size, read_size;
+ struct stat stat_buf;
+
+ if (!filename || !ret_size) {
+ printf("Read file to buffer failed: invalid filename or ret size.\n");
+ 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);
+ return NULL;
+ }
+
+ if (fstat(file, &stat_buf) != 0) {
+ printf("Read file to buffer failed: fstat file %s failed.\n", filename);
+ _close(file);
+ return NULL;
+ }
+ file_size = (uint32)stat_buf.st_size;
+
+ /* At lease alloc 1 byte to avoid malloc failed */
+ buf_size = file_size > 0 ? file_size : 1;
+
+ if (!(buffer = (char *)BH_MALLOC(buf_size))) {
+ printf("Read file to buffer failed: alloc memory failed.\n");
+ _close(file);
+ return NULL;
+ }
+#if WASM_ENABLE_MEMORY_TRACING != 0
+ printf("Read file, total size: %u\n", file_size);
+#endif
+
+ read_size = _read(file, buffer, file_size);
+ _close(file);
+
+ if (read_size < file_size) {
+ printf("Read file to buffer failed: read file content failed.\n");
+ BH_FREE(buffer);
+ return NULL;
+ }
+
+ *ret_size = file_size;
+ return buffer;
+}
+#else /* else of defined(_WIN32) || defined(_WIN32_) */
+char *
+bh_read_file_to_buffer(const char *filename, uint32 *ret_size)
+{
+ char *buffer;
+ int file;
+ uint32 file_size, buf_size, read_size;
+ struct stat stat_buf;
+
+ if (!filename || !ret_size) {
+ printf("Read file to buffer failed: invalid filename or ret size.\n");
+ return NULL;
+ }
+
+ if ((file = open(filename, O_RDONLY, 0)) == -1) {
+ 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);
+ close(file);
+ return NULL;
+ }
+
+ file_size = (uint32)stat_buf.st_size;
+
+ /* At lease alloc 1 byte to avoid malloc failed */
+ buf_size = file_size > 0 ? file_size : 1;
+
+ if (!(buffer = BH_MALLOC(buf_size))) {
+ printf("Read file to buffer failed: alloc memory failed.\n");
+ close(file);
+ return NULL;
+ }
+#if WASM_ENABLE_MEMORY_TRACING != 0
+ printf("Read file, total size: %u\n", file_size);
+#endif
+
+ read_size = (uint32)read(file, buffer, file_size);
+ close(file);
+
+ if (read_size < file_size) {
+ printf("Read file to buffer failed: read file content failed.\n");
+ BH_FREE(buffer);
+ return NULL;
+ }
+
+ *ret_size = file_size;
+ return buffer;
+}
+#endif /* end of defined(_WIN32) || defined(_WIN32_) */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.h b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.h
new file mode 100644
index 000000000..bbebf847f
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/bh_read_file.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ */
+
+#ifndef _BH_FILE_H
+#define _BH_FILE_H
+
+#include "bh_platform.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *
+bh_read_file_to_buffer(const char *filename, uint32 *ret_size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* end of _BH_FILE_H */
diff --git a/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/shared_uncommon.cmake b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/shared_uncommon.cmake
new file mode 100644
index 000000000..0a15b87b8
--- /dev/null
+++ b/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/utils/uncommon/shared_uncommon.cmake
@@ -0,0 +1,11 @@
+# Copyright (C) 2019 Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+set (UNCOMMON_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})
+
+include_directories(${UNCOMMON_SHARED_DIR})
+
+file (GLOB_RECURSE source_all ${UNCOMMON_SHARED_DIR}/*.c)
+
+set (UNCOMMON_SHARED_SOURCE ${source_all})
+