Refactor: improve memory management in module deinstantiation and handle global imports correctly

This commit is contained in:
liang.he@intel.com 2024-11-21 02:09:20 +00:00
parent 77607aa8ea
commit 0401cf3e94
3 changed files with 39 additions and 0 deletions

View File

@ -2537,6 +2537,10 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
wasm_runtime_free(extra->functions);
}
if (extra->globals) {
wasm_runtime_free(extra->globals);
}
if (module_inst->func_ptrs)
wasm_runtime_free(module_inst->func_ptrs);
@ -2566,6 +2570,21 @@ aot_deinstantiate(AOTModuleInstance *module_inst, bool is_sub_inst)
bh_bitmap_delete(common->elem_dropped);
#endif
#if WASM_ENABLE_MULTI_MODULE == 0
AOTModule *module = (AOTModule *)module_inst->module;
for (uint32 i = 0; i < module->import_table_count; i++) {
AOTTableInstance *table = module_inst->tables[i];
if (!table)
continue;
void *table_imported =
(void *)((uint8 *)(table)-offsetof(AOTTableInstance, elems));
wasm_runtime_free(table_imported);
}
#endif
wasm_runtime_free(module_inst);
}

View File

@ -8085,6 +8085,12 @@ wasm_runtime_instantiate_with_builtin_linker(WASMModuleCommon *module,
* WASMExternInstance->u.global
*/
if (imports) {
for (uint32 i = 0; i < import_count; i++) {
if (imports[i].kind == WASM_IMPORT_EXPORT_KIND_GLOBAL) {
wasm_runtime_destroy_global(module, imports[i].u.global);
}
}
wasm_runtime_free(imports);
}

View File

@ -3675,6 +3675,20 @@ wasm_deinstantiate(WASMModuleInstance *module_inst, bool is_sub_inst)
bh_bitmap_delete(module_inst->e->common.elem_dropped);
#endif
#if WASM_ENABLE_MULTI_MODULE == 0
for (uint32 i = 0; i < module_inst->module->import_table_count; i++) {
WASMTableInstance *table = module_inst->tables[i];
if (!table)
continue;
void *table_imported =
(void *)((uint8 *)table - offsetof(WASMTableInstance, elems));
wasm_runtime_free(table_imported);
}
#endif
wasm_runtime_free(module_inst);
}