Skip to content

Commit ad2fd9f

Browse files
Use LDC 1.42, etc.
1 parent 37b77e0 commit ad2fd9f

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ jobs:
124124
- name: Setup D
125125
uses: dlang-community/setup-dlang@v2
126126
with:
127-
compiler: ldc-1.41
127+
compiler: ldc-1.42
128128
if: matrix.lang == 'd'
129129

130130
# Hacky work-around for https://github.com/dotnet/runtime/issues/80619

crates/d/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,7 +2634,8 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26342634

26352635
self.push_str(&format!(
26362636
"auto {list_src} = {};
2637-
auto {list} = {}.malloc({list_src}.length * ({size_str}));\n",
2637+
auto {list} = {list_src}.length ? {}.malloc({list_src}.length * ({size_str})) : null;
2638+
assert({list_src}.length || {list});\n",
26382639
operands[0], self.r#gen.r#gen.common_module
26392640
));
26402641

@@ -2655,7 +2656,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26552656

26562657
if !matches!(self.r#gen.direction, Some(Direction::Import)) {
26572658
self.push_str(&format!(
2658-
"{}.free({list_src}.ptr);\n",
2659+
"if ({list_src}.length) {}.free({list_src}.ptr);\n",
26592660
self.r#gen.r#gen.common_module
26602661
));
26612662
}
@@ -2714,7 +2715,8 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27142715
self.push_str(&format!("auto {list_src} = {};\n", operands[0]));
27152716
self.push_str(&format!("auto {list_len} = {};\n", operands[1]));
27162717
self.push_str(&format!(
2717-
"auto {list} = {}.mallocSlice!({elem_type_name})({list_len});\n",
2718+
"auto {list} = {list_len} ? {}.mallocSlice!({elem_type_name})({list_len}) : [];
2719+
assert({list_len} || {list}.ptr);\n",
27182720
self.r#gen.r#gen.common_module
27192721
));
27202722

@@ -2735,7 +2737,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27352737

27362738
if !matches!(self.r#gen.direction, Some(Direction::Export)) {
27372739
self.push_str(&format!(
2738-
"{}.free({list_src});\n",
2740+
"if ({list_len}) {}.free({list_src});\n",
27392741
self.r#gen.r#gen.common_module
27402742
));
27412743
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ extern(C) @nogc nothrow:
2323

2424
/// MODIFIED FOR wit-bindgen TESTS
2525
enum MAX_ALLOCATIONS = 32;
26-
void*[MAX_ALLOCATIONS] activePointers;
26+
extern(D) void*[MAX_ALLOCATIONS] activePointers;
27+
extern(D) size_t[MAX_ALLOCATIONS] activeAllocSizes;
28+
29+
// extern(C) to make it "public" for the `lists` test
30+
extern(C) size_t walloc_allocated_bytes = 0;
2731
/// END
2832

2933
void* malloc(size_t size) @nogc nothrow @system {
@@ -36,9 +40,11 @@ void* malloc(size_t size) @nogc nothrow @system {
3640
/// MODIFIED FOR wit-bindgen TESTS
3741
auto result = (kind == chunk_kind.LARGE_OBJECT) ? allocate_large(size) : allocate_small(kind);
3842
assert(result !is null);
39-
foreach (ref ptr; activePointers) {
43+
foreach (i, ref ptr; activePointers) {
4044
if (ptr !is null) continue;
4145
ptr = result;
46+
activeAllocSizes[i] = size;
47+
walloc_allocated_bytes += size;
4248
return result;
4349
}
4450
assert(0);
@@ -51,9 +57,10 @@ void free(void *ptr) @nogc nothrow @system {
5157
assert(ptr !is null);
5258

5359
bool found = false;
54-
foreach (ref existingPtr; activePointers) {
60+
foreach (i, ref existingPtr; activePointers) {
5561
if (ptr !is existingPtr) continue;
5662
existingPtr = null;
63+
walloc_allocated_bytes -= activeAllocSizes[i];
5764
found = true;
5865
break;
5966
}

0 commit comments

Comments
 (0)