diff --git a/crates/rust/src/bindgen.rs b/crates/rust/src/bindgen.rs index f26adc7c4..8bc47bfde 100644 --- a/crates/rust/src/bindgen.rs +++ b/crates/rust/src/bindgen.rs @@ -825,6 +825,11 @@ impl Bindgen for FunctionBindgen<'_, '_> { self.push_str(&format!( "let ptr = {alloc}::alloc({layout}).cast::();\n", )); + if self.gen.gen.opts.symmetric && self.gen.in_import { + self.push_str(&format!( + "if !ptr.is_null() {{ _deallocate.push((ptr, {layout})); }}\n" + )); + } self.push_str(&format!( "if ptr.is_null()\n{{\n{alloc}::handle_alloc_error({layout});\n}}\nptr\n}}", )); @@ -909,6 +914,9 @@ impl Bindgen for FunctionBindgen<'_, '_> { self.push_str("("); self.push_str(&operands.join(", ")); self.push_str(");\n"); + if self.gen.gen.opts.symmetric && self.gen.in_import { + self.push_str(&format!("for (ptr,layout) in _deallocate.drain(..) {{ _rt::alloc::dealloc(ptr, layout); }}\n")); + } } Instruction::AsyncCallWasm { name, size, align } => { diff --git a/crates/rust/src/interface.rs b/crates/rust/src/interface.rs index 36102c88e..21ddf709b 100644 --- a/crates/rust/src/interface.rs +++ b/crates/rust/src/interface.rs @@ -867,6 +867,12 @@ impl {async_support}::StreamPayload for {name} {{ self.src.push_str("#[allow(unused_unsafe, clippy::all)]\n"); let params = self.print_signature(func, false, &sig, true); self.src.push_str("{\n"); + if self.gen.opts.symmetric { + uwriteln!( + self.src, + "let mut _deallocate: Vec<(*mut u8, _rt::alloc::Layout)> = Vec::new();" + ); + } self.src.push_str("unsafe {\n"); self.generate_guest_import_body(&self.wasm_import_module, func, params, async_);