@@ -1956,6 +1956,7 @@ struct FunctionBindgen<'a, 'b> {
1956
1956
blocks : Vec < Block > ,
1957
1957
payloads : Vec < String > ,
1958
1958
needs_cleanup_list : bool ,
1959
+ needs_native_alloc_list : bool ,
1959
1960
cleanup : Vec < Cleanup > ,
1960
1961
import_return_pointer_area_size : usize ,
1961
1962
import_return_pointer_area_align : usize ,
@@ -1989,6 +1990,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
1989
1990
blocks : Vec :: new ( ) ,
1990
1991
payloads : Vec :: new ( ) ,
1991
1992
needs_cleanup_list : false ,
1993
+ needs_native_alloc_list : false ,
1992
1994
cleanup : Vec :: new ( ) ,
1993
1995
import_return_pointer_area_size : 0 ,
1994
1996
import_return_pointer_area_align : 0 ,
@@ -2185,7 +2187,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2185
2187
Instruction :: F64Load { offset } => results. push ( format ! ( "BitConverter.ToDouble(new Span<byte>((void*)({} + {offset}), 8))" , operands[ 0 ] ) ) ,
2186
2188
Instruction :: I32Store { offset }
2187
2189
| Instruction :: PointerStore { offset }
2188
- | Instruction :: LengthStore { offset } => uwriteln ! ( self . src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 4), unchecked((int){}) );" , operands[ 1 ] , operands[ 0 ] ) ,
2190
+ | Instruction :: LengthStore { offset } => uwriteln ! ( self . src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 4), {} );" , operands[ 1 ] , operands[ 0 ] ) ,
2189
2191
Instruction :: I32Store8 { offset } => uwriteln ! ( self . src, "*(byte*)({} + {offset}) = (byte){};" , operands[ 1 ] , operands[ 0 ] ) ,
2190
2192
Instruction :: I32Store16 { offset } => uwriteln ! ( self . src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 2), (short){});" , operands[ 1 ] , operands[ 0 ] ) ,
2191
2193
Instruction :: I64Store { offset } => uwriteln ! ( self . src, "BitConverter.TryWriteBytes(new Span<byte>((void*)({} + {offset}), 8), unchecked((long){}));" , operands[ 1 ] , operands[ 0 ] ) ,
@@ -2601,16 +2603,18 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2601
2603
let ty = self . gen . type_name_with_qualifier ( element, true ) ;
2602
2604
let index = self . locals . tmp ( "index" ) ;
2603
2605
2604
- let buffer: String = self . locals . tmp ( "buffer" ) ;
2605
- let gc_handle = self . locals . tmp ( "gcHandle" ) ;
2606
2606
let address = self . locals . tmp ( "address" ) ;
2607
+ let buffer_size = self . locals . tmp ( "bufferSize" ) ;
2608
+ //TODO: wasm64
2609
+ let align = self . gen . gen . sizes . align ( element) . align_wasm32 ( ) ;
2610
+ self . needs_native_alloc_list = true ;
2607
2611
2608
2612
uwrite ! (
2609
2613
self . src,
2610
2614
"
2611
- byte[] {buffer } = new byte[ {size} * {list}.Count] ;
2612
- var {gc_handle } = GCHandle.Alloc({buffer }, GCHandleType.Pinned );
2613
- var {address} = {gc_handle}.AddrOfPinnedObject( );
2615
+ var {buffer_size } = {size} * (nuint) {list}.Count;
2616
+ var {address } = NativeMemory.AlignedAlloc({buffer_size }, {align} );
2617
+ nativeAllocs.Add((IntPtr) {address});
2614
2618
2615
2619
for (int {index} = 0; {index} < {list}.Count; ++{index}) {{
2616
2620
{ty} {block_element} = {list}[{index}];
@@ -2620,12 +2624,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2620
2624
"
2621
2625
) ;
2622
2626
2623
- if realloc. is_none ( ) {
2624
- self . cleanup . push ( Cleanup {
2625
- address : gc_handle. clone ( ) ,
2626
- } ) ;
2627
- }
2628
-
2629
2627
results. push ( format ! ( "(int){address}" ) ) ;
2630
2628
results. push ( format ! ( "{list}.Count" ) ) ;
2631
2629
}
@@ -2829,6 +2827,17 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2829
2827
uwriteln ! ( self . src, "{address}.Free();" ) ;
2830
2828
}
2831
2829
2830
+ if self . needs_native_alloc_list {
2831
+ self . src . insert_str ( 0 , "var nativeAllocs = new List<IntPtr>();
2832
+ " ) ;
2833
+
2834
+ uwriteln ! ( self . src, "\
2835
+ foreach (var nativeAlloc in nativeAllocs)
2836
+ {{
2837
+ NativeMemory.AlignedFree((void*)nativeAlloc);
2838
+ }}" ) ;
2839
+ }
2840
+
2832
2841
if !matches ! ( ( self . gen . direction, self . kind) , ( Direction :: Import , FunctionKind :: Constructor ( _) ) ) {
2833
2842
match func. results . len ( ) {
2834
2843
0 => ( ) ,
@@ -2920,7 +2929,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
2920
2929
Direction :: Export => {
2921
2930
self . gen . gen . needs_rep_table = true ;
2922
2931
let local_rep = self . locals . tmp ( "localRep" ) ;
2923
- let export_name = self . gen . gen . all_resources [ & id] . export_impl_name ( ) ;
2932
+ let export_name = self . gen . gen . all_resources [ & id] . export_impl_name ( ) ;
2924
2933
if is_own {
2925
2934
// Note that we set `{op}.Handle` to zero below to ensure that application code doesn't
2926
2935
// try to use the instance while the host has ownership. We'll set it back to non-zero
0 commit comments