@@ -8,15 +8,13 @@ namespace Stride.Core;
8
8
[ Obsolete ( "Obtain Memory<T> using GC.Allocate*Array or a Stride-specific allocator mechanism." ) ]
9
9
public class UnmanagedArray < T > : IDisposable where T : struct
10
10
{
11
- private readonly int sizeOfT ;
12
11
private readonly bool isShared ;
13
12
14
13
[ Obsolete ( "Obtain Memory<T> using GC.Allocate*Array or a Stride-specific allocator mechanism." ) ]
15
14
public UnmanagedArray ( int length )
16
15
{
17
16
Length = length ;
18
- sizeOfT = Unsafe . SizeOf < T > ( ) ;
19
- var finalSize = length * sizeOfT ;
17
+ var finalSize = length * Unsafe . SizeOf < T > ( ) ;
20
18
Pointer = Utilities . AllocateMemory ( finalSize ) ;
21
19
isShared = false ;
22
20
}
@@ -43,8 +41,10 @@ public T this[int index]
43
41
unsafe
44
42
{
45
43
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 ) ;
48
48
}
49
49
50
50
return res ;
@@ -60,8 +60,10 @@ public T this[int index]
60
60
unsafe
61
61
{
62
62
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 ) ;
65
67
}
66
68
}
67
69
}
@@ -72,7 +74,6 @@ public unsafe void Read(T[] destination, int offset = 0)
72
74
{
73
75
throw new ArgumentOutOfRangeException ( nameof ( offset ) ) ;
74
76
}
75
- // Interop.Read((void*)Pointer, destination, offset, destination.Length);
76
77
new Span < T > ( ( void * ) Pointer , destination . Length - offset ) . CopyTo ( destination . AsSpan ( offset ) ) ;
77
78
}
78
79
@@ -87,8 +88,7 @@ public void Read(T[] destination, int pointerByteOffset, int arrayOffset, int ar
87
88
{
88
89
var ptr = ( byte * ) Pointer ;
89
90
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 )
92
92
. CopyTo ( destination . AsSpan ( arrayOffset , arrayLen ) ) ;
93
93
}
94
94
}
@@ -99,7 +99,6 @@ public unsafe void Write(T[] source, int offset = 0)
99
99
{
100
100
throw new ArgumentOutOfRangeException ( ) ;
101
101
}
102
- // Interop.Write((void*)Pointer, source, offset, source.Length);
103
102
source . AsSpan ( offset ) . CopyTo ( new Span < T > ( ( void * ) Pointer , source . Length - offset ) ) ;
104
103
}
105
104
@@ -112,7 +111,6 @@ public unsafe void Write(T[] source, int pointerByteOffset, int arrayOffset, int
112
111
113
112
var ptr = ( byte * ) Pointer ;
114
113
ptr += pointerByteOffset ;
115
- // Interop.Write(ptr, source, arrayOffset, arrayLen);
116
114
source . AsSpan ( arrayOffset , arrayLen ) . CopyTo ( new Span < T > ( ptr , arrayLen ) ) ;
117
115
}
118
116
0 commit comments