diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs index afb120d07f71f..acb5c684efd43 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs @@ -187,21 +187,18 @@ public T Read(ulong byteOffset) where T : struct byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, sizeofT); - // return *(T*) (_ptr + byteOffset); - T value = default; bool mustCallRelease = false; try { DangerousAddRef(ref mustCallRelease); - SpanHelpers.Memmove(ref Unsafe.As(ref value), ref *ptr, sizeofT); + return Unsafe.ReadUnaligned(ptr); } finally { if (mustCallRelease) DangerousRelease(); } - return value; } /// @@ -248,7 +245,9 @@ public void ReadSpan(ulong byteOffset, Span buffer) ref T structure = ref MemoryMarshal.GetReference(buffer); for (int i = 0; i < buffer.Length; i++) - Buffer.Memmove(ref Unsafe.Add(ref structure, i), ref Unsafe.AsRef(ptr + alignedSizeofT * i), 1); + { + Unsafe.Add(ref structure, (nint)(uint)i) = Unsafe.ReadUnaligned(ptr + alignedSizeofT * (uint)i); + } } finally { @@ -275,13 +274,12 @@ public void Write(ulong byteOffset, T value) where T : struct byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, sizeofT); - // *((T*) (_ptr + byteOffset)) = value; bool mustCallRelease = false; try { DangerousAddRef(ref mustCallRelease); - SpanHelpers.Memmove(ref *ptr, ref Unsafe.As(ref value), sizeofT); + Unsafe.WriteUnaligned(ptr, value); } finally { @@ -336,7 +334,9 @@ public void WriteSpan(ulong byteOffset, ReadOnlySpan data) ref T structure = ref MemoryMarshal.GetReference(data); for (int i = 0; i < data.Length; i++) - Buffer.Memmove(ref Unsafe.AsRef(ptr + alignedSizeofT * i), ref Unsafe.Add(ref structure, i), 1); + { + Unsafe.WriteUnaligned(ptr + alignedSizeofT * (uint)i, Unsafe.Add(ref structure, (nint)(uint)i)); + } } finally {