Skip to content

Commit 9dd1262

Browse files
author
Timmy Silesmo
committed
Fixes alignment
1 parent 3e830f5 commit 9dd1262

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

crates/csharp/src/function.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,15 +882,22 @@ impl Bindgen for FunctionBindgen<'_, '_> {
882882
);
883883
let ret_area = self.locals.tmp("retArea");
884884

885+
let array_size = if align > 1 {
886+
// Add one additional element in case the starting address is not aligned
887+
format!("{array_size} * {list}.Count + 1")
888+
} else {
889+
format!("{array_size} * {list}.Count")
890+
};
891+
885892
match realloc {
886893
None => {
887894
self.needs_cleanup = true;
888895
uwrite!(self.src,
889896
"
890897
void* {address};
891898
if (({size} * {list}.Count) < 1024) {{
892-
var {ret_area} = stackalloc {element_type}[{array_size}*{list}.Count];
893-
{address} = (void*)((int){ret_area});
899+
var {ret_area} = stackalloc {element_type}[{array_size}];
900+
{address} = (void*)(((int){ret_area}) + ({align} - 1) & -{align});
894901
}}
895902
else
896903
{{
@@ -1288,10 +1295,17 @@ impl Bindgen for FunctionBindgen<'_, '_> {
12881295
// to align the allocation via the stackalloc command, unlike with a fixed array where the pointer will be aligned.
12891296
// We get the final ptr to pass to the wasm runtime by shifting to the
12901297
// correctly aligned pointer (sometimes it can be already aligned).
1298+
let array_size = if self.import_return_pointer_area_align > 1 {
1299+
// Add one additional element in case the starting address is not aligned
1300+
array_size + 1
1301+
} else {
1302+
array_size
1303+
};
1304+
12911305
uwrite!(
12921306
self.src,
12931307
"
1294-
var {ret_area} = stackalloc {element_type}[{array_size}+1];
1308+
var {ret_area} = stackalloc {element_type}[{array_size}];
12951309
var {ptr} = ((int){ret_area}) + ({align} - 1) & -{align};
12961310
",
12971311
align = align.align_wasm32()

crates/csharp/src/world_generator.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,10 @@ enum Stubs<'a> {
797797
// We cant use "StructLayout.Pack" as dotnet will use the minimum of the type and the "Pack" field,
798798
// so for byte it would always use 1 regardless of the "Pack".
799799
pub fn dotnet_aligned_array(array_size: usize, required_alignment: usize) -> (usize, String) {
800-
let num_elements = if array_size % required_alignment > 0 {
801-
array_size / required_alignment + 1
802-
} else {
803-
array_size / required_alignment
804-
};
800+
let num_elements = array_size.div_ceil(required_alignment);
805801
match required_alignment {
806802
1 => (num_elements, "byte".to_owned()),
803+
// Add one additional element in case the starting address is not aligned
807804
2 => (num_elements, "ushort".to_owned()),
808805
4 => (num_elements, "uint".to_owned()),
809806
8 => (num_elements, "ulong".to_owned()),

0 commit comments

Comments
 (0)