@@ -1805,20 +1805,28 @@ wasm_runtime_wasi_nn_graph_registry_args_set_defaults(WASINNArguments *args)
18051805}
18061806
18071807bool
1808- wasi_nn_graph_registry_set_args (WASINNArguments * registry , const char * encoding ,
1809- const char * target , uint32_t n_graphs ,
1808+ wasi_nn_graph_registry_set_args (WASINNArguments * registry , const char * * encoding ,
1809+ const char * * target , uint32_t n_graphs ,
18101810 const char * * graph_paths )
18111811{
18121812 if (!registry || !encoding || !target || !graph_paths ) {
18131813 return false;
18141814 }
1815- registry -> encoding = strdup (encoding );
1816- registry -> target = strdup (target );
1815+
18171816 registry -> n_graphs = n_graphs ;
1817+ registry -> target = (uint32_t * * )malloc (sizeof (uint32_t * ) * n_graphs );
1818+ registry -> encoding = (uint32_t * * )malloc (sizeof (uint32_t * ) * n_graphs );
18181819 registry -> graph_paths = (uint32_t * * )malloc (sizeof (uint32_t * ) * n_graphs );
1820+ memset (registry -> target , 0 , sizeof (uint32_t * ) * n_graphs );
1821+ memset (registry -> encoding , 0 , sizeof (uint32_t * ) * n_graphs );
18191822 memset (registry -> graph_paths , 0 , sizeof (uint32_t * ) * n_graphs );
1823+
18201824 for (uint32_t i = 0 ; i < registry -> n_graphs ; i ++ )
1825+ {
18211826 registry -> graph_paths [i ] = strdup (graph_paths [i ]);
1827+ registry -> encoding [i ] = strdup (encoding [i ]);
1828+ registry -> target [i ] = strdup (target [i ]);
1829+ }
18221830
18231831 return true;
18241832}
@@ -1841,14 +1849,12 @@ wasi_nn_graph_registry_destroy(WASINNArguments *registry)
18411849 if (registry ) {
18421850 for (uint32_t i = 0 ; i < registry -> n_graphs ; i ++ )
18431851 if (registry -> graph_paths [i ]) {
1844- // wasi_nn_graph_registry_unregister_graph(registry,
1845- // registry->name[i]);
18461852 free (registry -> graph_paths [i ]);
1853+ if (registry -> encoding [i ])
1854+ free (registry -> encoding [i ]);
1855+ if (registry -> target [i ])
1856+ free (registry -> target [i ]);
18471857 }
1848- if (registry -> encoding )
1849- free (registry -> encoding );
1850- if (registry -> target )
1851- free (registry -> target );
18521858 free (registry );
18531859 }
18541860}
@@ -8150,7 +8156,7 @@ wasm_runtime_check_and_update_last_used_shared_heap(
81508156#if WASM_ENABLE_WASI_NN != 0 || WASM_ENABLE_WASI_EPHEMERAL_NN != 0
81518157bool
81528158wasm_runtime_init_wasi_nn_global_ctx (WASMModuleInstanceCommon * module_inst ,
8153- const char * encoding , const char * target ,
8159+ const char * * encoding , const char * * target ,
81548160 const uint32_t n_graphs ,
81558161 char * graph_paths [], char * error_buf ,
81568162 uint32_t error_buf_size )
@@ -8162,16 +8168,21 @@ wasm_runtime_init_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst,
81628168 if (!ctx )
81638169 return false;
81648170
8165- ctx -> encoding = strdup (encoding );
8166- ctx -> target = strdup (target );
81678171 ctx -> n_graphs = n_graphs ;
8172+
8173+ ctx -> encoding = (uint32_t * )malloc (sizeof (uint32_t ) * n_graphs );
8174+ memset (ctx -> encoding , 0 , sizeof (uint32_t ) * n_graphs );
8175+ ctx -> target = (uint32_t * )malloc (sizeof (uint32_t ) * n_graphs );
8176+ memset (ctx -> target , 0 , sizeof (uint32_t ) * n_graphs );
81688177 ctx -> loaded = (uint32_t * )malloc (sizeof (uint32_t ) * n_graphs );
81698178 memset (ctx -> loaded , 0 , sizeof (uint32_t ) * n_graphs );
8170-
81718179 ctx -> graph_paths = (uint32_t * * )malloc (sizeof (uint32_t * ) * n_graphs );
81728180 memset (ctx -> graph_paths , 0 , sizeof (uint32_t * ) * n_graphs );
8181+
81738182 for (uint32_t i = 0 ; i < n_graphs ; i ++ ) {
81748183 ctx -> graph_paths [i ] = strdup (graph_paths [i ]);
8184+ ctx -> target [i ] = strdup (target [i ]);
8185+ ctx -> encoding [i ] = strdup (encoding [i ]);
81758186 }
81768187
81778188 wasm_runtime_set_wasi_nn_global_ctx (module_inst , ctx );
@@ -8191,6 +8202,10 @@ wasm_runtime_destroy_wasi_nn_global_ctx(WASMModuleInstanceCommon *module_inst)
81918202 // All graphs will be unregistered in deinit()
81928203 if (wasi_nn_global_ctx -> graph_paths [i ])
81938204 free (wasi_nn_global_ctx -> graph_paths [i ]);
8205+ if (wasi_nn_global_ctx -> encoding [i ])
8206+ free (wasi_nn_global_ctx -> encoding [i ]);
8207+ if (wasi_nn_global_ctx -> encoding [i ])
8208+ free (wasi_nn_global_ctx -> target [i ]);
81948209 }
81958210 free (wasi_nn_global_ctx -> encoding );
81968211 free (wasi_nn_global_ctx -> target );
@@ -8243,21 +8258,21 @@ wasm_runtime_set_wasi_nn_global_ctx_loaded_i(
82438258}
82448259
82458260char *
8246- wasm_runtime_get_wasi_nn_global_ctx_encoding (
8247- WASINNGlobalContext * wasi_nn_global_ctx )
8261+ wasm_runtime_get_wasi_nn_global_ctx_encoding_i (
8262+ WASINNGlobalContext * wasi_nn_global_ctx , uint32_t idx )
82488263{
8249- if (wasi_nn_global_ctx )
8250- return wasi_nn_global_ctx -> encoding ;
8264+ if (wasi_nn_global_ctx && ( idx < wasi_nn_global_ctx -> n_graphs ) )
8265+ return wasi_nn_global_ctx -> encoding [ idx ] ;
82518266
82528267 return NULL ;
82538268}
82548269
82558270char *
8256- wasm_runtime_get_wasi_nn_global_ctx_target (
8257- WASINNGlobalContext * wasi_nn_global_ctx )
8271+ wasm_runtime_get_wasi_nn_global_ctx_target_i (
8272+ WASINNGlobalContext * wasi_nn_global_ctx , uint32_t idx )
82588273{
8259- if (wasi_nn_global_ctx )
8260- return wasi_nn_global_ctx -> target ;
8274+ if (wasi_nn_global_ctx && ( idx < wasi_nn_global_ctx -> n_graphs ) )
8275+ return wasi_nn_global_ctx -> target [ idx ] ;
82618276
82628277 return NULL ;
82638278}
0 commit comments