Skip to content

Commit 039e80e

Browse files
Fix more memory leaks
1 parent 7d8f305 commit 039e80e

3 files changed

Lines changed: 31 additions & 11 deletions

File tree

crates/d/src/lib.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26382638

26392639
if matches!(self.r#gen.direction, Some(Direction::Import)) {
26402640
self.needs_deallocate = true;
2641-
self.push_str(&format!("deallocate ~= {list};\n"));
2641+
self.push_str(&format!("if ({list_src}.length) deallocate ~= {list};\n"));
26422642
}
26432643

26442644
self.push_str(&format!(
@@ -2671,12 +2671,17 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26712671
let len = tempname("_len", tmp);
26722672

26732673
self.push_str(&format!(
2674-
"auto {ptr} = cast({elem_name}*)({});
2675-
auto {len} = {};
2674+
"auto {len} = {};
2675+
auto {ptr} = {len} ? cast({elem_name}*)({}) : null;
26762676
",
2677-
operands[0], operands[1]
2677+
operands[1], operands[0]
26782678
));
26792679

2680+
if matches!(self.r#gen.direction, Some(Direction::Export)) {
2681+
self.needs_deallocate = true;
2682+
self.push_str(&format!("if ({len}) deallocate ~= cast(){ptr};\n"));
2683+
}
2684+
26802685
results.push(format!("{list_name}({ptr}[0..{len}])"));
26812686
}
26822687
abi::Instruction::StringLift => {
@@ -2686,12 +2691,17 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26862691
let len = tempname("_len", tmp);
26872692

26882693
self.push_str(&format!(
2689-
"auto {ptr} = cast(char*)({});
2690-
auto {len} = {};
2694+
"auto {len} = {};
2695+
auto {ptr} = {len} ? cast(char*)({}) : null;
26912696
",
2692-
operands[0], operands[1]
2697+
operands[1], operands[0]
26932698
));
26942699

2700+
if matches!(self.r#gen.direction, Some(Direction::Export)) {
2701+
self.needs_deallocate = true;
2702+
self.push_str(&format!("if ({len}) deallocate ~= cast(){ptr};\n"));
2703+
}
2704+
26952705
results.push(format!("WitString({ptr}[0..{len}])"));
26962706
}
26972707
abi::Instruction::ListLift { ty, element, .. } => {
@@ -2719,7 +2729,9 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27192729

27202730
if matches!(self.r#gen.direction, Some(Direction::Export)) {
27212731
self.needs_deallocate = true;
2722-
self.push_str(&format!("deallocate ~= cast(void*){list}.ptr;\n"));
2732+
self.push_str(&format!(
2733+
"if ({list_len}) deallocate ~= cast(void*){list}.ptr;\n"
2734+
));
27232735
}
27242736

27252737
self.push_str(&format!(
@@ -2732,11 +2744,14 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27322744
self.push_str(&format!("{block_element} = {};", block_results[0]));
27332745
self.push_str("\n}\n");
27342746

2735-
if !matches!(self.r#gen.direction, Some(Direction::Export)) {
2747+
if matches!(self.r#gen.direction, Some(Direction::Import)) {
27362748
self.push_str(&format!(
27372749
"if ({list_len}) {}.free({list_src});\n",
27382750
self.r#gen.r#gen.common_module
27392751
));
2752+
} else {
2753+
self.needs_deallocate = true;
2754+
self.push_str(&format!("if ({list_len}) deallocate ~= {list_src};\n"));
27402755
}
27412756

27422757
let list_name = self.r#gen.type_name(&Type::Id(*ty), self.r#gen.fqn);

crates/d/src/wit_common.d

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,12 @@ struct DeallocateBuffer {
499499
}
500500
}
501501

502-
version (CRuntime_WASI) {}
502+
version (CRuntime_WASI) {
503+
version (WASIp1) {}
504+
else version = LibcDefinesCABIRealloc;
505+
}
506+
507+
version (LibcDefinesCABIRealloc) {}
503508
else
504509
@wasmExport!("cabi_realloc")
505510
void* cabi_realloc(void *ptr, size_t oldSize, size_t alignment, size_t newSize) {

crates/test/d-test-support/walloc.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import ldc.intrinsics :
2222
extern(C) @nogc nothrow:
2323

2424
/// MODIFIED FOR wit-bindgen TESTS
25-
enum MAX_ALLOCATIONS = 32;
25+
enum MAX_ALLOCATIONS = 2048;
2626
extern(D) void*[MAX_ALLOCATIONS] activePointers;
2727
extern(D) size_t[MAX_ALLOCATIONS] activeAllocSizes;
2828

0 commit comments

Comments
 (0)