diff --git a/lib/runtime-c-api/src/lib.rs b/lib/runtime-c-api/src/lib.rs index 2543e8fd8..421b44ab2 100644 --- a/lib/runtime-c-api/src/lib.rs +++ b/lib/runtime-c-api/src/lib.rs @@ -576,9 +576,7 @@ pub unsafe extern "C" fn wasmer_export_descriptor_kind( /// Serialize the given Module. /// -/// It's up to the caller to free the memory of the -/// `serialized_module` byte array (both the `bytes` field and the -/// structure). +/// The caller owns the object and should call `wasmer_memory_serialization_destroy` to free it. /// /// Returns `wasmer_result_t::WASMER_OK` upon success. /// @@ -620,6 +618,15 @@ pub unsafe extern "C" fn wasmer_module_serialize( } } +/// Frees memory for the given serialized Module. +#[allow(clippy::cast_ptr_alignment)] +#[no_mangle] +pub extern "C" fn wasmer_module_serialization_destroy(serialized_module: *mut wasmer_byte_array) { + if !serialized_module.is_null() { + unsafe { Box::from_raw(serialized_module as *mut wasmer_byte_array) }; + } +} + /// Deserialize the given bytes into a Module. /// /// Returns `wasmer_result_t::WASMER_OK` upon success. diff --git a/lib/runtime-c-api/tests/test-module-serialize.c b/lib/runtime-c-api/tests/test-module-serialize.c index 7c1ac092e..badcebd46 100644 --- a/lib/runtime-c-api/tests/test-module-serialize.c +++ b/lib/runtime-c-api/tests/test-module-serialize.c @@ -62,8 +62,7 @@ int main() assert(call_result == WASMER_OK); printf("Destroy the serialized module\n"); - free((uint8_t *) serialized_module->bytes); - free(serialized_module); + wasmer_module_serialization_destroy(serialized_module); printf("Destroy instance\n"); wasmer_instance_destroy(instance); diff --git a/lib/runtime-c-api/wasmer.h b/lib/runtime-c-api/wasmer.h index add281466..758590a37 100644 --- a/lib/runtime-c-api/wasmer.h +++ b/lib/runtime-c-api/wasmer.h @@ -514,11 +514,14 @@ wasmer_result_t wasmer_module_instantiate(const wasmer_module_t *module, wasmer_import_t *imports, int imports_len); +/** + * Frees memory for the given serialized Module. + */ +void wasmer_module_serialization_destroy(wasmer_byte_array *serialized_module); + /** * Serialize the given Module. - * It's up to the caller to free the memory of the - * `serialized_module` byte array (both the `bytes` field and the - * structure). + * The caller owns the object and should call `wasmer_memory_serialization_destroy` to free it. * Returns `wasmer_result_t::WASMER_OK` upon success. * Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` * and `wasmer_last_error_message` to get an error message. diff --git a/lib/runtime-c-api/wasmer.hh b/lib/runtime-c-api/wasmer.hh index d3619dd9a..e05f78a53 100644 --- a/lib/runtime-c-api/wasmer.hh +++ b/lib/runtime-c-api/wasmer.hh @@ -405,10 +405,11 @@ wasmer_result_t wasmer_module_instantiate(const wasmer_module_t *module, wasmer_import_t *imports, int imports_len); +/// Frees memory for the given serialized Module. +void wasmer_module_serialization_destroy(wasmer_byte_array *serialized_module); + /// Serialize the given Module. -/// It's up to the caller to free the memory of the -/// `serialized_module` byte array (both the `bytes` field and the -/// structure). +/// The caller owns the object and should call `wasmer_memory_serialization_destroy` to free it. /// Returns `wasmer_result_t::WASMER_OK` upon success. /// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length` /// and `wasmer_last_error_message` to get an error message.