krillplayer-plugin 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -1
- package/README.md +1 -0
- package/dist/cli.js +351 -73
- package/dist/crypto.js +37 -11
- package/dist/engine/engine.c +6 -3
- package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
- package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
- package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
- package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
- package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
- package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
- package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
- package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
- package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
- package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
- package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
- package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
- package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
- package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
- package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
- package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
- package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
- package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
- package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
- package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
- package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
- package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
- package/kernel/Tests/KernelTests/CasePins.js +50 -0
- package/package.json +2 -2
- package/skill/krillplayer-plugin/SKILL.md +2 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +8 -0
- package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +26 -9
- package/skill/krillplayer-plugin/references/cases.md +19 -1
- package/skill/krillplayer-plugin/references/contract.d.ts +104 -11
- package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
- package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#ifndef _BH_PLATFORM_H
|
|
7
|
+
#define _BH_PLATFORM_H
|
|
8
|
+
|
|
9
|
+
#include "../platform/include/platform_common.h"
|
|
10
|
+
#include "../platform/include/platform_api_vmcore.h"
|
|
11
|
+
#include "../platform/include/platform_api_extension.h"
|
|
12
|
+
#include "bh_assert.h"
|
|
13
|
+
#include "bh_common.h"
|
|
14
|
+
#include "bh_hashmap.h"
|
|
15
|
+
#include "bh_list.h"
|
|
16
|
+
#include "bh_log.h"
|
|
17
|
+
#include "bh_queue.h"
|
|
18
|
+
#include "bh_vector.h"
|
|
19
|
+
#include "runtime_timer.h"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* WA_MALLOC/WA_FREE need to be redefined for both
|
|
23
|
+
* runtime native and WASM app respectively.
|
|
24
|
+
*
|
|
25
|
+
* Some source files are shared for building native and WASM,
|
|
26
|
+
* and this the mem allocator API for these files.
|
|
27
|
+
*
|
|
28
|
+
* Here we define it for the native world
|
|
29
|
+
*/
|
|
30
|
+
#ifndef WA_MALLOC
|
|
31
|
+
#define WA_MALLOC wasm_runtime_malloc
|
|
32
|
+
#endif
|
|
33
|
+
|
|
34
|
+
#ifndef WA_FREE
|
|
35
|
+
#define WA_FREE wasm_runtime_free
|
|
36
|
+
#endif
|
|
37
|
+
|
|
38
|
+
#endif /* #ifndef _BH_PLATFORM_H */
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#include "bh_queue.h"
|
|
7
|
+
|
|
8
|
+
typedef struct bh_queue_node {
|
|
9
|
+
struct bh_queue_node *next;
|
|
10
|
+
struct bh_queue_node *prev;
|
|
11
|
+
unsigned short tag;
|
|
12
|
+
unsigned int len;
|
|
13
|
+
void *body;
|
|
14
|
+
bh_msg_cleaner msg_cleaner;
|
|
15
|
+
} bh_queue_node;
|
|
16
|
+
|
|
17
|
+
struct bh_queue {
|
|
18
|
+
bh_queue_mutex queue_lock;
|
|
19
|
+
bh_queue_cond queue_wait_cond;
|
|
20
|
+
unsigned int cnt;
|
|
21
|
+
unsigned int max;
|
|
22
|
+
unsigned int drops;
|
|
23
|
+
bh_queue_node *head;
|
|
24
|
+
bh_queue_node *tail;
|
|
25
|
+
|
|
26
|
+
bool exit_loop_run;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
char *
|
|
30
|
+
bh_message_payload(bh_message_t message)
|
|
31
|
+
{
|
|
32
|
+
return message->body;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
uint32
|
|
36
|
+
bh_message_payload_len(bh_message_t message)
|
|
37
|
+
{
|
|
38
|
+
return message->len;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
int
|
|
42
|
+
bh_message_type(bh_message_t message)
|
|
43
|
+
{
|
|
44
|
+
return message->tag;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
bh_queue *
|
|
48
|
+
bh_queue_create()
|
|
49
|
+
{
|
|
50
|
+
int ret;
|
|
51
|
+
bh_queue *queue = bh_queue_malloc(sizeof(bh_queue));
|
|
52
|
+
|
|
53
|
+
if (queue) {
|
|
54
|
+
memset(queue, 0, sizeof(bh_queue));
|
|
55
|
+
queue->max = DEFAULT_QUEUE_LENGTH;
|
|
56
|
+
|
|
57
|
+
ret = bh_queue_mutex_init(&queue->queue_lock);
|
|
58
|
+
if (ret != 0) {
|
|
59
|
+
bh_queue_free(queue);
|
|
60
|
+
return NULL;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
ret = bh_queue_cond_init(&queue->queue_wait_cond);
|
|
64
|
+
if (ret != 0) {
|
|
65
|
+
bh_queue_mutex_destroy(&queue->queue_lock);
|
|
66
|
+
bh_queue_free(queue);
|
|
67
|
+
return NULL;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return queue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
void
|
|
75
|
+
bh_queue_destroy(bh_queue *queue)
|
|
76
|
+
{
|
|
77
|
+
bh_queue_node *node;
|
|
78
|
+
|
|
79
|
+
if (!queue)
|
|
80
|
+
return;
|
|
81
|
+
|
|
82
|
+
bh_queue_mutex_lock(&queue->queue_lock);
|
|
83
|
+
while (queue->head) {
|
|
84
|
+
node = queue->head;
|
|
85
|
+
queue->head = node->next;
|
|
86
|
+
|
|
87
|
+
bh_free_msg(node);
|
|
88
|
+
}
|
|
89
|
+
bh_queue_mutex_unlock(&queue->queue_lock);
|
|
90
|
+
|
|
91
|
+
bh_queue_cond_destroy(&queue->queue_wait_cond);
|
|
92
|
+
bh_queue_mutex_destroy(&queue->queue_lock);
|
|
93
|
+
bh_queue_free(queue);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
bool
|
|
97
|
+
bh_post_msg2(bh_queue *queue, bh_queue_node *msg)
|
|
98
|
+
{
|
|
99
|
+
if (queue->cnt >= queue->max) {
|
|
100
|
+
queue->drops++;
|
|
101
|
+
bh_free_msg(msg);
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
bh_queue_mutex_lock(&queue->queue_lock);
|
|
106
|
+
|
|
107
|
+
if (queue->cnt == 0) {
|
|
108
|
+
bh_assert(queue->head == NULL);
|
|
109
|
+
bh_assert(queue->tail == NULL);
|
|
110
|
+
queue->head = queue->tail = msg;
|
|
111
|
+
msg->next = msg->prev = NULL;
|
|
112
|
+
queue->cnt = 1;
|
|
113
|
+
|
|
114
|
+
bh_queue_cond_signal(&queue->queue_wait_cond);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
msg->next = NULL;
|
|
118
|
+
msg->prev = queue->tail;
|
|
119
|
+
queue->tail->next = msg;
|
|
120
|
+
queue->tail = msg;
|
|
121
|
+
queue->cnt++;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
bh_queue_mutex_unlock(&queue->queue_lock);
|
|
125
|
+
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
bool
|
|
130
|
+
bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len)
|
|
131
|
+
{
|
|
132
|
+
bh_queue_node *msg = bh_new_msg(tag, body, len, NULL);
|
|
133
|
+
if (msg == NULL) {
|
|
134
|
+
queue->drops++;
|
|
135
|
+
if (len != 0 && body)
|
|
136
|
+
BH_FREE(body);
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (!bh_post_msg2(queue, msg)) {
|
|
141
|
+
// bh_post_msg2 already freed the msg for failure
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
bh_queue_node *
|
|
149
|
+
bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler)
|
|
150
|
+
{
|
|
151
|
+
bh_queue_node *msg =
|
|
152
|
+
(bh_queue_node *)bh_queue_malloc(sizeof(bh_queue_node));
|
|
153
|
+
if (msg == NULL)
|
|
154
|
+
return NULL;
|
|
155
|
+
memset(msg, 0, sizeof(bh_queue_node));
|
|
156
|
+
msg->len = len;
|
|
157
|
+
msg->body = body;
|
|
158
|
+
msg->tag = tag;
|
|
159
|
+
msg->msg_cleaner = (bh_msg_cleaner)handler;
|
|
160
|
+
|
|
161
|
+
return msg;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
void
|
|
165
|
+
bh_free_msg(bh_queue_node *msg)
|
|
166
|
+
{
|
|
167
|
+
if (msg->msg_cleaner) {
|
|
168
|
+
msg->msg_cleaner(msg->body);
|
|
169
|
+
bh_queue_free(msg);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// note: sometimes we just use the payload pointer for an integer value
|
|
174
|
+
// len!=0 is the only indicator about the body is an allocated buffer.
|
|
175
|
+
if (msg->body && msg->len)
|
|
176
|
+
bh_queue_free(msg->body);
|
|
177
|
+
|
|
178
|
+
bh_queue_free(msg);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
bh_message_t
|
|
182
|
+
bh_get_msg(bh_queue *queue, uint64 timeout_us)
|
|
183
|
+
{
|
|
184
|
+
bh_queue_node *msg = NULL;
|
|
185
|
+
bh_queue_mutex_lock(&queue->queue_lock);
|
|
186
|
+
|
|
187
|
+
if (queue->cnt == 0) {
|
|
188
|
+
bh_assert(queue->head == NULL);
|
|
189
|
+
bh_assert(queue->tail == NULL);
|
|
190
|
+
|
|
191
|
+
if (timeout_us == 0) {
|
|
192
|
+
bh_queue_mutex_unlock(&queue->queue_lock);
|
|
193
|
+
return NULL;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
bh_queue_cond_timedwait(&queue->queue_wait_cond, &queue->queue_lock,
|
|
197
|
+
timeout_us);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (queue->cnt == 0) {
|
|
201
|
+
bh_assert(queue->head == NULL);
|
|
202
|
+
bh_assert(queue->tail == NULL);
|
|
203
|
+
}
|
|
204
|
+
else if (queue->cnt == 1) {
|
|
205
|
+
bh_assert(queue->head == queue->tail);
|
|
206
|
+
|
|
207
|
+
msg = queue->head;
|
|
208
|
+
queue->head = queue->tail = NULL;
|
|
209
|
+
queue->cnt = 0;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
msg = queue->head;
|
|
213
|
+
queue->head = queue->head->next;
|
|
214
|
+
queue->head->prev = NULL;
|
|
215
|
+
queue->cnt--;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
bh_queue_mutex_unlock(&queue->queue_lock);
|
|
219
|
+
|
|
220
|
+
return msg;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
unsigned
|
|
224
|
+
bh_queue_get_message_count(bh_queue *queue)
|
|
225
|
+
{
|
|
226
|
+
if (!queue)
|
|
227
|
+
return 0;
|
|
228
|
+
|
|
229
|
+
return queue->cnt;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
void
|
|
233
|
+
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
|
|
234
|
+
void *arg)
|
|
235
|
+
{
|
|
236
|
+
if (!queue)
|
|
237
|
+
return;
|
|
238
|
+
|
|
239
|
+
while (!queue->exit_loop_run) {
|
|
240
|
+
bh_queue_node *message = bh_get_msg(queue, BHT_WAIT_FOREVER);
|
|
241
|
+
|
|
242
|
+
if (message) {
|
|
243
|
+
handle_cb(message, arg);
|
|
244
|
+
bh_free_msg(message);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
void
|
|
250
|
+
bh_queue_exit_loop_run(bh_queue *queue)
|
|
251
|
+
{
|
|
252
|
+
if (queue) {
|
|
253
|
+
queue->exit_loop_run = true;
|
|
254
|
+
bh_queue_cond_signal(&queue->queue_wait_cond);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#ifndef _BH_QUEUE_H
|
|
7
|
+
#define _BH_QUEUE_H
|
|
8
|
+
|
|
9
|
+
#ifdef __cplusplus
|
|
10
|
+
extern "C" {
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
#include "bh_platform.h"
|
|
14
|
+
|
|
15
|
+
struct bh_queue_node;
|
|
16
|
+
typedef struct bh_queue_node *bh_message_t;
|
|
17
|
+
struct bh_queue;
|
|
18
|
+
typedef struct bh_queue bh_queue;
|
|
19
|
+
|
|
20
|
+
typedef void (*bh_queue_handle_msg_callback)(void *message, void *arg);
|
|
21
|
+
|
|
22
|
+
#define bh_queue_malloc BH_MALLOC
|
|
23
|
+
#define bh_queue_free BH_FREE
|
|
24
|
+
|
|
25
|
+
#define bh_queue_mutex korp_mutex
|
|
26
|
+
#define bh_queue_cond korp_cond
|
|
27
|
+
|
|
28
|
+
#define bh_queue_mutex_init os_mutex_init
|
|
29
|
+
#define bh_queue_mutex_destroy os_mutex_destroy
|
|
30
|
+
#define bh_queue_mutex_lock os_mutex_lock
|
|
31
|
+
#define bh_queue_mutex_unlock os_mutex_unlock
|
|
32
|
+
|
|
33
|
+
#define bh_queue_cond_init os_cond_init
|
|
34
|
+
#define bh_queue_cond_destroy os_cond_destroy
|
|
35
|
+
#define bh_queue_cond_wait os_cond_wait
|
|
36
|
+
#define bh_queue_cond_timedwait os_cond_reltimedwait
|
|
37
|
+
#define bh_queue_cond_signal os_cond_signal
|
|
38
|
+
#define bh_queue_cond_broadcast os_cond_broadcast
|
|
39
|
+
|
|
40
|
+
typedef void (*bh_msg_cleaner)(void *msg);
|
|
41
|
+
|
|
42
|
+
bh_queue *
|
|
43
|
+
bh_queue_create(void);
|
|
44
|
+
|
|
45
|
+
void
|
|
46
|
+
bh_queue_destroy(bh_queue *queue);
|
|
47
|
+
|
|
48
|
+
char *
|
|
49
|
+
bh_message_payload(bh_message_t message);
|
|
50
|
+
uint32
|
|
51
|
+
bh_message_payload_len(bh_message_t message);
|
|
52
|
+
int
|
|
53
|
+
bh_message_type(bh_message_t message);
|
|
54
|
+
|
|
55
|
+
bh_message_t
|
|
56
|
+
bh_new_msg(unsigned short tag, void *body, unsigned int len, void *handler);
|
|
57
|
+
void
|
|
58
|
+
bh_free_msg(bh_message_t msg);
|
|
59
|
+
bool
|
|
60
|
+
bh_post_msg(bh_queue *queue, unsigned short tag, void *body, unsigned int len);
|
|
61
|
+
bool
|
|
62
|
+
bh_post_msg2(bh_queue *queue, bh_message_t msg);
|
|
63
|
+
|
|
64
|
+
bh_message_t
|
|
65
|
+
bh_get_msg(bh_queue *queue, uint64 timeout_us);
|
|
66
|
+
|
|
67
|
+
unsigned
|
|
68
|
+
bh_queue_get_message_count(bh_queue *queue);
|
|
69
|
+
|
|
70
|
+
void
|
|
71
|
+
bh_queue_enter_loop_run(bh_queue *queue, bh_queue_handle_msg_callback handle_cb,
|
|
72
|
+
void *arg);
|
|
73
|
+
void
|
|
74
|
+
bh_queue_exit_loop_run(bh_queue *queue);
|
|
75
|
+
|
|
76
|
+
#ifdef __cplusplus
|
|
77
|
+
}
|
|
78
|
+
#endif
|
|
79
|
+
|
|
80
|
+
#endif /* #ifndef _BH_QUEUE_H */
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (C) 2019 Intel Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#include "bh_vector.h"
|
|
7
|
+
|
|
8
|
+
static uint8 *
|
|
9
|
+
alloc_vector_data(size_t length, size_t size_elem)
|
|
10
|
+
{
|
|
11
|
+
uint64 total_size = ((uint64)size_elem) * length;
|
|
12
|
+
uint8 *data;
|
|
13
|
+
|
|
14
|
+
if (length > UINT32_MAX || size_elem > UINT32_MAX
|
|
15
|
+
|| total_size > UINT32_MAX) {
|
|
16
|
+
return NULL;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if ((data = BH_MALLOC((uint32)total_size))) {
|
|
20
|
+
memset(data, 0, (uint32)total_size);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* every caller of `extend_vector` must provide
|
|
28
|
+
* a thread-safe environment.
|
|
29
|
+
*/
|
|
30
|
+
static bool
|
|
31
|
+
extend_vector(Vector *vector, size_t length)
|
|
32
|
+
{
|
|
33
|
+
uint8 *data;
|
|
34
|
+
|
|
35
|
+
if (length <= vector->max_elems)
|
|
36
|
+
return true;
|
|
37
|
+
|
|
38
|
+
if (length < vector->max_elems * 3 / 2)
|
|
39
|
+
length = vector->max_elems * 3 / 2;
|
|
40
|
+
|
|
41
|
+
if (!(data = alloc_vector_data(length, vector->size_elem))) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
bh_memcpy_s(data, (uint32)(vector->size_elem * length), vector->data,
|
|
46
|
+
(uint32)(vector->size_elem * vector->max_elems));
|
|
47
|
+
BH_FREE(vector->data);
|
|
48
|
+
|
|
49
|
+
vector->data = data;
|
|
50
|
+
vector->max_elems = length;
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
bool
|
|
55
|
+
bh_vector_init(Vector *vector, size_t init_length, size_t size_elem,
|
|
56
|
+
bool use_lock)
|
|
57
|
+
{
|
|
58
|
+
if (!vector) {
|
|
59
|
+
LOG_ERROR("Init vector failed: vector is NULL.\n");
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (init_length == 0) {
|
|
64
|
+
init_length = 4;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (!(vector->data = alloc_vector_data(init_length, size_elem))) {
|
|
68
|
+
LOG_ERROR("Init vector failed: alloc memory failed.\n");
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
vector->size_elem = size_elem;
|
|
73
|
+
vector->max_elems = init_length;
|
|
74
|
+
vector->num_elems = 0;
|
|
75
|
+
vector->lock = NULL;
|
|
76
|
+
|
|
77
|
+
if (use_lock) {
|
|
78
|
+
if (!(vector->lock = BH_MALLOC(sizeof(korp_mutex)))) {
|
|
79
|
+
LOG_ERROR("Init vector failed: alloc locker failed.\n");
|
|
80
|
+
bh_vector_destroy(vector);
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (BHT_OK != os_mutex_init(vector->lock)) {
|
|
85
|
+
LOG_ERROR("Init vector failed: init locker failed.\n");
|
|
86
|
+
|
|
87
|
+
BH_FREE(vector->lock);
|
|
88
|
+
vector->lock = NULL;
|
|
89
|
+
|
|
90
|
+
bh_vector_destroy(vector);
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
bool
|
|
99
|
+
bh_vector_set(Vector *vector, uint32 index, const void *elem_buf)
|
|
100
|
+
{
|
|
101
|
+
if (!vector || !elem_buf) {
|
|
102
|
+
LOG_ERROR("Set vector elem failed: vector or elem buf is NULL.\n");
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (index >= vector->num_elems) {
|
|
107
|
+
LOG_ERROR("Set vector elem failed: invalid elem index.\n");
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (vector->lock)
|
|
112
|
+
os_mutex_lock(vector->lock);
|
|
113
|
+
bh_memcpy_s(vector->data + vector->size_elem * index,
|
|
114
|
+
(uint32)vector->size_elem, elem_buf, (uint32)vector->size_elem);
|
|
115
|
+
if (vector->lock)
|
|
116
|
+
os_mutex_unlock(vector->lock);
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
bool
|
|
121
|
+
bh_vector_get(Vector *vector, uint32 index, void *elem_buf)
|
|
122
|
+
{
|
|
123
|
+
if (!vector || !elem_buf) {
|
|
124
|
+
LOG_ERROR("Get vector elem failed: vector or elem buf is NULL.\n");
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (index >= vector->num_elems) {
|
|
129
|
+
LOG_ERROR("Get vector elem failed: invalid elem index.\n");
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (vector->lock)
|
|
134
|
+
os_mutex_lock(vector->lock);
|
|
135
|
+
bh_memcpy_s(elem_buf, (uint32)vector->size_elem,
|
|
136
|
+
vector->data + vector->size_elem * index,
|
|
137
|
+
(uint32)vector->size_elem);
|
|
138
|
+
if (vector->lock)
|
|
139
|
+
os_mutex_unlock(vector->lock);
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
bool
|
|
144
|
+
bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf)
|
|
145
|
+
{
|
|
146
|
+
size_t i;
|
|
147
|
+
uint8 *p;
|
|
148
|
+
bool ret = false;
|
|
149
|
+
|
|
150
|
+
if (!vector || !elem_buf) {
|
|
151
|
+
LOG_ERROR("Insert vector elem failed: vector or elem buf is NULL.\n");
|
|
152
|
+
goto just_return;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
if (index >= vector->num_elems) {
|
|
156
|
+
LOG_ERROR("Insert vector elem failed: invalid elem index.\n");
|
|
157
|
+
goto just_return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (vector->lock)
|
|
161
|
+
os_mutex_lock(vector->lock);
|
|
162
|
+
|
|
163
|
+
if (!extend_vector(vector, vector->num_elems + 1)) {
|
|
164
|
+
LOG_ERROR("Insert vector elem failed: extend vector failed.\n");
|
|
165
|
+
goto unlock_return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
p = vector->data + vector->size_elem * vector->num_elems;
|
|
169
|
+
for (i = vector->num_elems - 1; i > index; i--) {
|
|
170
|
+
bh_memcpy_s(p, (uint32)vector->size_elem, p - vector->size_elem,
|
|
171
|
+
(uint32)vector->size_elem);
|
|
172
|
+
p -= vector->size_elem;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
bh_memcpy_s(p, (uint32)vector->size_elem, elem_buf,
|
|
176
|
+
(uint32)vector->size_elem);
|
|
177
|
+
vector->num_elems++;
|
|
178
|
+
ret = true;
|
|
179
|
+
|
|
180
|
+
unlock_return:
|
|
181
|
+
if (vector->lock)
|
|
182
|
+
os_mutex_unlock(vector->lock);
|
|
183
|
+
just_return:
|
|
184
|
+
return ret;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
bool
|
|
188
|
+
bh_vector_append(Vector *vector, const void *elem_buf)
|
|
189
|
+
{
|
|
190
|
+
bool ret = false;
|
|
191
|
+
|
|
192
|
+
if (!vector || !elem_buf) {
|
|
193
|
+
LOG_ERROR("Append vector elem failed: vector or elem buf is NULL.\n");
|
|
194
|
+
goto just_return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* make sure one more slot is used by the thread who allocates it */
|
|
198
|
+
if (vector->lock)
|
|
199
|
+
os_mutex_lock(vector->lock);
|
|
200
|
+
|
|
201
|
+
if (!extend_vector(vector, vector->num_elems + 1)) {
|
|
202
|
+
LOG_ERROR("Append vector elem failed: extend vector failed.\n");
|
|
203
|
+
goto unlock_return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
bh_memcpy_s(vector->data + vector->size_elem * vector->num_elems,
|
|
207
|
+
(uint32)vector->size_elem, elem_buf, (uint32)vector->size_elem);
|
|
208
|
+
vector->num_elems++;
|
|
209
|
+
ret = true;
|
|
210
|
+
|
|
211
|
+
unlock_return:
|
|
212
|
+
if (vector->lock)
|
|
213
|
+
os_mutex_unlock(vector->lock);
|
|
214
|
+
just_return:
|
|
215
|
+
return ret;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
bool
|
|
219
|
+
bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf)
|
|
220
|
+
{
|
|
221
|
+
uint32 i;
|
|
222
|
+
uint8 *p;
|
|
223
|
+
|
|
224
|
+
if (!vector) {
|
|
225
|
+
LOG_ERROR("Remove vector elem failed: vector is NULL.\n");
|
|
226
|
+
return false;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (index >= vector->num_elems) {
|
|
230
|
+
LOG_ERROR("Remove vector elem failed: invalid elem index.\n");
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
if (vector->lock)
|
|
235
|
+
os_mutex_lock(vector->lock);
|
|
236
|
+
p = vector->data + vector->size_elem * index;
|
|
237
|
+
|
|
238
|
+
if (old_elem_buf) {
|
|
239
|
+
bh_memcpy_s(old_elem_buf, (uint32)vector->size_elem, p,
|
|
240
|
+
(uint32)vector->size_elem);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
for (i = index; i < vector->num_elems - 1; i++) {
|
|
244
|
+
bh_memcpy_s(p, (uint32)vector->size_elem, p + vector->size_elem,
|
|
245
|
+
(uint32)vector->size_elem);
|
|
246
|
+
p += vector->size_elem;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
vector->num_elems--;
|
|
250
|
+
if (vector->lock)
|
|
251
|
+
os_mutex_unlock(vector->lock);
|
|
252
|
+
return true;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
size_t
|
|
256
|
+
bh_vector_size(const Vector *vector)
|
|
257
|
+
{
|
|
258
|
+
return vector ? vector->num_elems : 0;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
bool
|
|
262
|
+
bh_vector_destroy(Vector *vector)
|
|
263
|
+
{
|
|
264
|
+
if (!vector) {
|
|
265
|
+
LOG_ERROR("Destroy vector elem failed: vector is NULL.\n");
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (vector->data)
|
|
270
|
+
BH_FREE(vector->data);
|
|
271
|
+
|
|
272
|
+
if (vector->lock) {
|
|
273
|
+
os_mutex_destroy(vector->lock);
|
|
274
|
+
BH_FREE(vector->lock);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
memset(vector, 0, sizeof(Vector));
|
|
278
|
+
return true;
|
|
279
|
+
}
|