@@ -8,15 +8,13 @@ namespace Stride.Core;
88[ Obsolete ( "Obtain Memory<T> using GC.Allocate*Array or a Stride-specific allocator mechanism." ) ]
99public class UnmanagedArray < T > : IDisposable where T : struct
1010{
11- private readonly int sizeOfT ;
1211 private readonly bool isShared ;
1312
1413 [ Obsolete ( "Obtain Memory<T> using GC.Allocate*Array or a Stride-specific allocator mechanism." ) ]
1514 public UnmanagedArray ( int length )
1615 {
1716 Length = length ;
18- sizeOfT = Unsafe . SizeOf < T > ( ) ;
19- var finalSize = length * sizeOfT ;
17+ var finalSize = length * Unsafe . SizeOf < T > ( ) ;
2018 Pointer = Utilities . AllocateMemory ( finalSize ) ;
2119 isShared = false ;
2220 }
@@ -43,8 +41,10 @@ public T this[int index]
4341 unsafe
4442 {
4543 var bptr = ( byte * ) Pointer ;
46- bptr += index * sizeOfT ;
47- res = Unsafe . ReadUnaligned < T > ( bptr ) ;
44+ bptr += index * Unsafe . SizeOf < T > ( ) ;
45+ // Pointer is aligned, we expect the struct to be aligned as well;
46+ // If the user decides to Pack=1, this scope is the least of their worries
47+ res = Unsafe . Read < T > ( bptr ) ;
4848 }
4949
5050 return res ;
@@ -60,8 +60,10 @@ public T this[int index]
6060 unsafe
6161 {
6262 var bptr = ( byte * ) Pointer ;
63- bptr += index * sizeOfT ;
64- Unsafe . WriteUnaligned ( bptr , value ) ;
63+ bptr += index * Unsafe . SizeOf < T > ( ) ;
64+ // Pointer is aligned, we expect the struct to be aligned as well;
65+ // If the user decides to Pack=1, this scope is the least of their worries
66+ Unsafe . Write ( bptr , value ) ;
6567 }
6668 }
6769 }
@@ -72,7 +74,6 @@ public unsafe void Read(T[] destination, int offset = 0)
7274 {
7375 throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
7476 }
75- // Interop.Read((void*)Pointer, destination, offset, destination.Length);
7677 new Span < T > ( ( void * ) Pointer , destination . Length - offset ) . CopyTo ( destination . AsSpan ( offset ) ) ;
7778 }
7879
@@ -87,8 +88,7 @@ public void Read(T[] destination, int pointerByteOffset, int arrayOffset, int ar
8788 {
8889 var ptr = ( byte * ) Pointer ;
8990 ptr += pointerByteOffset ;
90- // Interop.Read(ptr, destination, arrayOffset, arrayLen);
91- new Span < T > ( ptr , sizeOfT * arrayLen )
91+ new Span < T > ( ptr , Unsafe . SizeOf < T > ( ) * arrayLen )
9292 . CopyTo ( destination . AsSpan ( arrayOffset , arrayLen ) ) ;
9393 }
9494 }
@@ -99,7 +99,6 @@ public unsafe void Write(T[] source, int offset = 0)
9999 {
100100 throw new ArgumentOutOfRangeException ( ) ;
101101 }
102- // Interop.Write((void*)Pointer, source, offset, source.Length);
103102 source . AsSpan ( offset ) . CopyTo ( new Span < T > ( ( void * ) Pointer , source . Length - offset ) ) ;
104103 }
105104
@@ -112,7 +111,6 @@ public unsafe void Write(T[] source, int pointerByteOffset, int arrayOffset, int
112111
113112 var ptr = ( byte * ) Pointer ;
114113 ptr += pointerByteOffset ;
115- // Interop.Write(ptr, source, arrayOffset, arrayLen);
116114 source . AsSpan ( arrayOffset , arrayLen ) . CopyTo ( new Span < T > ( ptr , arrayLen ) ) ;
117115 }
118116
0 commit comments