Skip to content

Commit b122451

Browse files
committed
Unify WASINNGlobalContext and WASINNArguments into a single structure named WASINNRegistry.
Replace malloc/free to wasm_runtime_malloc/_free, replace strdup to bh_strdup.
1 parent 6501e4b commit b122451

8 files changed

Lines changed: 158 additions & 245 deletions

File tree

core/iwasm/common/wasm_native.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static void *g_wasi_context_key;
2626
#endif /* WASM_ENABLE_LIBC_WASI */
2727

2828
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
29-
static void *g_wasi_nn_context_key;
29+
static void *g_wasi_nn_registry_key;
3030
#endif
3131

3232
uint32
@@ -478,17 +478,17 @@ wasi_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
478478
#endif /* end of WASM_ENABLE_LIBC_WASI */
479479

480480
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
481-
WASINNGlobalContext *
482-
wasm_runtime_get_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst_comm)
481+
WASINNRegistry *
482+
wasm_runtime_get_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm)
483483
{
484-
return wasm_native_get_context(module_inst_comm, g_wasi_nn_context_key);
484+
return wasm_native_get_context(module_inst_comm, g_wasi_nn_registry_key);
485485
}
486486

487487
void
488-
wasm_runtime_set_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst_comm,
489-
WASINNGlobalContext *wasi_nn_ctx)
488+
wasm_runtime_set_wasi_nn_registry(WASMModuleInstanceCommon *module_inst_comm,
489+
WASINNRegistry *wasi_nn_ctx)
490490
{
491-
wasm_native_set_context(module_inst_comm, g_wasi_nn_context_key,
491+
wasm_native_set_context(module_inst_comm, g_wasi_nn_registry_key,
492492
wasi_nn_ctx);
493493
}
494494

@@ -499,7 +499,7 @@ wasi_nn_context_dtor(WASMModuleInstanceCommon *inst, void *ctx)
499499
return;
500500
}
501501

502-
wasm_runtime_destroy_wasi_nn_global_ctx(inst);
502+
wasm_runtime_wasi_nn_registry_destroy(ctx);
503503
}
504504
#endif
505505

@@ -612,9 +612,9 @@ wasm_native_init()
612612
#endif /* WASM_ENABLE_LIB_RATS */
613613

614614
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
615-
g_wasi_nn_context_key =
615+
g_wasi_nn_registry_key =
616616
wasm_native_create_context_key(wasi_nn_context_dtor);
617-
if (g_wasi_nn_context_key == NULL) {
617+
if (g_wasi_nn_registry_key == NULL) {
618618
goto fail;
619619
}
620620

@@ -684,9 +684,9 @@ wasm_native_destroy()
684684
#endif
685685

686686
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
687-
if (g_wasi_nn_context_key != NULL) {
688-
wasm_native_destroy_context_key(g_wasi_nn_context_key);
689-
g_wasi_nn_context_key = NULL;
687+
if (g_wasi_nn_registry_key != NULL) {
688+
wasm_native_destroy_context_key(g_wasi_nn_registry_key);
689+
g_wasi_nn_registry_key = NULL;
690690
}
691691
wasi_nn_destroy();
692692
#endif

core/iwasm/common/wasm_runtime_common.c

Lines changed: 74 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,48 +1796,48 @@ wasm_runtime_instantiation_args_set_wasi_ns_lookup_pool(
17961796
#endif /* WASM_ENABLE_LIBC_WASI != 0 */
17971797

17981798
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
1799-
typedef struct WASINNArguments WASINNArguments;
1800-
18011799
void
1802-
wasm_runtime_wasi_nn_graph_registry_args_set_defaults(WASINNArguments *args)
1800+
wasm_runtime_wasi_nn_graph_registry_args_set_defaults(WASINNRegistry *args)
18031801
{
18041802
memset(args, 0, sizeof(*args));
18051803
}
18061804

18071805
bool
1808-
wasi_nn_graph_registry_set_args(WASINNArguments *registry,
1809-
const char **model_names, const char **encoding,
1810-
const char **target, uint32_t n_graphs,
1811-
const char **graph_paths)
1806+
wasm_runtime_wasi_nn_registry_set_args(WASINNRegistry *registry,
1807+
const char **model_names, const char **encoding,
1808+
const char **target, uint32_t n_graphs,
1809+
const char **graph_paths)
18121810
{
18131811
if (!registry || !model_names || !encoding || !target || !graph_paths) {
18141812
return false;
18151813
}
18161814

18171815
registry->n_graphs = n_graphs;
1818-
registry->target = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
1819-
registry->encoding = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
1820-
registry->model_names = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
1821-
registry->graph_paths = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
1816+
registry->target = (uint32_t **)wasm_runtime_malloc(sizeof(uint32_t *) * n_graphs);
1817+
registry->encoding = (uint32_t **)wasm_runtime_malloc(sizeof(uint32_t *) * n_graphs);
1818+
registry->loaded = (uint32_t **)wasm_runtime_malloc(sizeof(uint32_t *) * n_graphs);
1819+
registry->model_names = (uint32_t **)wasm_runtime_malloc(sizeof(uint32_t *) * n_graphs);
1820+
registry->graph_paths = (uint32_t **)wasm_runtime_malloc(sizeof(uint32_t *) * n_graphs);
18221821
memset(registry->target, 0, sizeof(uint32_t *) * n_graphs);
18231822
memset(registry->encoding, 0, sizeof(uint32_t *) * n_graphs);
1823+
memset(registry->loaded, 0, sizeof(uint32_t *) * n_graphs);
18241824
memset(registry->model_names, 0, sizeof(uint32_t *) * n_graphs);
18251825
memset(registry->graph_paths, 0, sizeof(uint32_t *) * n_graphs);
18261826

18271827
for (uint32_t i = 0; i < registry->n_graphs; i++) {
1828-
registry->graph_paths[i] = strdup(graph_paths[i]);
1829-
registry->model_names[i] = strdup(model_names[i]);
1830-
registry->encoding[i] = strdup(encoding[i]);
1831-
registry->target[i] = strdup(target[i]);
1828+
registry->graph_paths[i] = bh_strdup(graph_paths[i]);
1829+
registry->model_names[i] = bh_strdup(model_names[i]);
1830+
registry->encoding[i] = bh_strdup(encoding[i]);
1831+
registry->target[i] = bh_strdup(target[i]);
18321832
}
18331833

18341834
return true;
18351835
}
18361836

18371837
int
1838-
wasi_nn_graph_registry_create(WASINNArguments **registryp)
1838+
wasm_runtime_wasi_nn_registry_create(WASINNRegistry **registryp)
18391839
{
1840-
WASINNArguments *args = wasm_runtime_malloc(sizeof(*args));
1840+
WASINNRegistry *args = wasm_runtime_malloc(sizeof(*args));
18411841
if (args == NULL) {
18421842
return -1;
18431843
}
@@ -1847,28 +1847,45 @@ wasi_nn_graph_registry_create(WASINNArguments **registryp)
18471847
}
18481848

18491849
void
1850-
wasi_nn_graph_registry_destroy(WASINNArguments *registry)
1850+
wasm_runtime_wasi_nn_registry_destroy(WASINNRegistry *registry)
18511851
{
18521852
if (registry) {
18531853
for (uint32_t i = 0; i < registry->n_graphs; i++)
18541854
if (registry->graph_paths[i]) {
1855-
free(registry->graph_paths[i]);
1856-
if (registry->model_names[i])
1857-
free(registry->model_names[i]);
1858-
if (registry->encoding[i])
1859-
free(registry->encoding[i]);
1860-
if (registry->target[i])
1861-
free(registry->target[i]);
1855+
wasm_runtime_free(registry->graph_paths[i]);
1856+
if (registry->model_names[i])
1857+
wasm_runtime_free(registry->model_names[i]);
1858+
if (registry->encoding[i])
1859+
wasm_runtime_free(registry->encoding[i]);
1860+
if (registry->target[i])
1861+
wasm_runtime_free(registry->target[i]);
18621862
}
1863-
free(registry);
1863+
if (registry->loaded)
1864+
wasm_runtime_free(registry->loaded);
1865+
wasm_runtime_free(registry);
18641866
}
18651867
}
18661868

18671869
void
1868-
wasm_runtime_instantiation_args_set_wasi_nn_graph_registry(
1869-
struct InstantiationArgs2 *p, WASINNArguments *registry)
1870+
wasm_runtime_instantiation_args_set_wasi_nn_registry(
1871+
struct InstantiationArgs2 *p, WASINNRegistry *registry)
18701872
{
1871-
p->nn_registry = *registry;
1873+
if (!registry)
1874+
return;
1875+
WASINNRegistry *wasi_nn_registry = &p->nn_registry;
1876+
1877+
wasi_nn_registry->n_graphs = registry->n_graphs;
1878+
1879+
if (registry->model_names)
1880+
wasi_nn_registry->model_names = bh_strdup(registry->model_names);
1881+
if (registry->encoding)
1882+
wasi_nn_registry->encoding = bh_strdup(registry->encoding);
1883+
if (registry->target)
1884+
wasi_nn_registry->target = bh_strdup(registry->target);
1885+
if (registry->loaded)
1886+
wasi_nn_registry->loaded = bh_strdup(registry->loaded);
1887+
if (registry->graph_paths)
1888+
wasi_nn_registry->graph_paths = bh_strdup(registry->graph_paths);
18721889
}
18731890
#endif
18741891

@@ -8159,142 +8176,73 @@ wasm_runtime_check_and_update_last_used_shared_heap(
81598176
#endif
81608177

81618178
#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
8162-
bool
8163-
wasm_runtime_init_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst,
8164-
const char **model_names,
8165-
const char **encoding, const char **target,
8166-
const uint32_t n_graphs,
8167-
char *graph_paths[], char *error_buf,
8168-
uint32_t error_buf_size)
8169-
{
8170-
WASINNGlobalContext *ctx;
8171-
bool ret = false;
8172-
8173-
ctx = runtime_malloc(sizeof(*ctx), module_inst, error_buf, error_buf_size);
8174-
if (!ctx)
8175-
return false;
8176-
8177-
ctx->n_graphs = n_graphs;
8178-
8179-
ctx->encoding = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
8180-
memset(ctx->encoding, 0, sizeof(uint32_t) * n_graphs);
8181-
ctx->target = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
8182-
memset(ctx->target, 0, sizeof(uint32_t) * n_graphs);
8183-
ctx->loaded = (uint32_t *)malloc(sizeof(uint32_t) * n_graphs);
8184-
memset(ctx->loaded, 0, sizeof(uint32_t) * n_graphs);
8185-
ctx->model_names = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
8186-
memset(ctx->model_names, 0, sizeof(uint32_t *) * n_graphs);
8187-
ctx->graph_paths = (uint32_t **)malloc(sizeof(uint32_t *) * n_graphs);
8188-
memset(ctx->graph_paths, 0, sizeof(uint32_t *) * n_graphs);
8189-
8190-
for (uint32_t i = 0; i < n_graphs; i++) {
8191-
ctx->graph_paths[i] = strdup(graph_paths[i]);
8192-
ctx->model_names[i] = strdup(model_names[i]);
8193-
ctx->target[i] = strdup(target[i]);
8194-
ctx->encoding[i] = strdup(encoding[i]);
8195-
}
8196-
8197-
wasm_runtime_set_wasi_nn_global_ctx(module_inst, ctx);
8198-
8199-
ret = true;
8200-
8201-
return ret;
8202-
}
8203-
8204-
void
8205-
wasm_runtime_destroy_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst)
8206-
{
8207-
WASINNGlobalContext *wasi_nn_global_ctx =
8208-
wasm_runtime_get_wasi_nn_global_ctx(module_inst);
8209-
8210-
for (uint32 i = 0; i < wasi_nn_global_ctx->n_graphs; i++) {
8211-
// All graphs will be unregistered in deinit()
8212-
if (wasi_nn_global_ctx->graph_paths[i])
8213-
free(wasi_nn_global_ctx->graph_paths[i]);
8214-
if (wasi_nn_global_ctx->model_names[i])
8215-
free(wasi_nn_global_ctx->model_names[i]);
8216-
if (wasi_nn_global_ctx->encoding[i])
8217-
free(wasi_nn_global_ctx->encoding[i]);
8218-
if (wasi_nn_global_ctx->target[i])
8219-
free(wasi_nn_global_ctx->target[i]);
8220-
}
8221-
free(wasi_nn_global_ctx->encoding);
8222-
free(wasi_nn_global_ctx->target);
8223-
free(wasi_nn_global_ctx->loaded);
8224-
free(wasi_nn_global_ctx->model_names);
8225-
free(wasi_nn_global_ctx->graph_paths);
8226-
8227-
if (wasi_nn_global_ctx) {
8228-
wasm_runtime_free(wasi_nn_global_ctx);
8229-
}
8230-
}
82318179

82328180
uint32_t
8233-
wasm_runtime_get_wasi_nn_global_ctx_ngraphs(
8234-
WASINNGlobalContext *wasi_nn_global_ctx)
8181+
wasm_runtime_get_wasi_nn_registry_ngraphs(
8182+
WASINNRegistry *wasi_nn_registry)
82358183
{
8236-
if (wasi_nn_global_ctx)
8237-
return wasi_nn_global_ctx->n_graphs;
8184+
if (wasi_nn_registry)
8185+
return wasi_nn_registry->n_graphs;
82388186

82398187
return -1;
82408188
}
82418189

82428190
char *
8243-
wasm_runtime_get_wasi_nn_global_ctx_model_names_i(
8244-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
8191+
wasm_runtime_get_wasi_nn_registry_model_names_i(
8192+
WASINNRegistry *wasi_nn_registry, uint32_t idx)
82458193
{
8246-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8247-
return wasi_nn_global_ctx->model_names[idx];
8194+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8195+
return wasi_nn_registry->model_names[idx];
82488196

82498197
return NULL;
82508198
}
82518199

82528200
char *
8253-
wasm_runtime_get_wasi_nn_global_ctx_graph_paths_i(
8254-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
8201+
wasm_runtime_get_wasi_nn_registry_graph_paths_i(
8202+
WASINNRegistry *wasi_nn_registry, uint32_t idx)
82558203
{
8256-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8257-
return wasi_nn_global_ctx->graph_paths[idx];
8204+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8205+
return wasi_nn_registry->graph_paths[idx];
82588206

82598207
return NULL;
82608208
}
82618209

82628210
uint32_t
8263-
wasm_runtime_get_wasi_nn_global_ctx_loaded_i(
8264-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
8211+
wasm_runtime_get_wasi_nn_registry_loaded_i(
8212+
WASINNRegistry *wasi_nn_registry, uint32_t idx)
82658213
{
8266-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8267-
return wasi_nn_global_ctx->loaded[idx];
8214+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8215+
return wasi_nn_registry->loaded[idx];
82688216

82698217
return -1;
82708218
}
82718219

82728220
uint32_t
8273-
wasm_runtime_set_wasi_nn_global_ctx_loaded_i(
8274-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx, uint32_t value)
8221+
wasm_runtime_set_wasi_nn_registry_loaded_i(
8222+
WASINNRegistry *wasi_nn_registry, uint32_t idx, uint32_t value)
82758223
{
8276-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8277-
wasi_nn_global_ctx->loaded[idx] = value;
8224+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8225+
wasi_nn_registry->loaded[idx] = value;
82788226

82798227
return 0;
82808228
}
82818229

82828230
char *
8283-
wasm_runtime_get_wasi_nn_global_ctx_encoding_i(
8284-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
8231+
wasm_runtime_get_wasi_nn_registry_encoding_i(
8232+
WASINNRegistry *wasi_nn_registry, uint32_t idx)
82858233
{
8286-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8287-
return wasi_nn_global_ctx->encoding[idx];
8234+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8235+
return wasi_nn_registry->encoding[idx];
82888236

82898237
return NULL;
82908238
}
82918239

82928240
char *
8293-
wasm_runtime_get_wasi_nn_global_ctx_target_i(
8294-
WASINNGlobalContext *wasi_nn_global_ctx, uint32_t idx)
8241+
wasm_runtime_get_wasi_nn_registry_target_i(
8242+
WASINNRegistry *wasi_nn_registry, uint32_t idx)
82958243
{
8296-
if (wasi_nn_global_ctx && (idx < wasi_nn_global_ctx->n_graphs))
8297-
return wasi_nn_global_ctx->target[idx];
8244+
if (wasi_nn_registry && (idx < wasi_nn_registry->n_graphs))
8245+
return wasi_nn_registry->target[idx];
82988246

82998247
return NULL;
83008248
}

0 commit comments

Comments
 (0)