diff options
25 files changed, 94 insertions, 80 deletions
diff --git a/src/mscorlib/shared/System/BitConverter.cs b/src/mscorlib/shared/System/BitConverter.cs index c7f4d0459a..e3cf20eb6a 100644 --- a/src/mscorlib/shared/System/BitConverter.cs +++ b/src/mscorlib/shared/System/BitConverter.cs @@ -4,6 +4,8 @@ using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + using Internal.Runtime.CompilerServices; namespace System @@ -37,7 +39,7 @@ namespace System if (destination.Length < sizeof(byte)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value ? (byte)1 : (byte)0); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value ? (byte)1 : (byte)0); return true; } @@ -55,7 +57,7 @@ namespace System if (destination.Length < sizeof(char)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -74,7 +76,7 @@ namespace System if (destination.Length < sizeof(short)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -93,7 +95,7 @@ namespace System if (destination.Length < sizeof(int)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -112,7 +114,7 @@ namespace System if (destination.Length < sizeof(long)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -133,7 +135,7 @@ namespace System if (destination.Length < sizeof(ushort)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -154,7 +156,7 @@ namespace System if (destination.Length < sizeof(uint)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -175,7 +177,7 @@ namespace System if (destination.Length < sizeof(ulong)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -194,7 +196,7 @@ namespace System if (destination.Length < sizeof(float)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -213,7 +215,7 @@ namespace System if (destination.Length < sizeof(double)) return false; - Unsafe.WriteUnaligned(ref destination.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(destination), value); return true; } @@ -225,7 +227,7 @@ namespace System { if (value.Length < sizeof(char)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<char>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<char>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into a short. @@ -246,7 +248,7 @@ namespace System { if (value.Length < sizeof(short)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<short>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<short>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into an int. @@ -267,7 +269,7 @@ namespace System { if (value.Length < sizeof(int)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<int>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<int>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into a long. @@ -288,7 +290,7 @@ namespace System { if (value.Length < sizeof(long)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<long>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<long>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into an ushort. @@ -302,7 +304,7 @@ namespace System { if (value.Length < sizeof(ushort)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<ushort>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<ushort>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into an uint. @@ -316,7 +318,7 @@ namespace System { if (value.Length < sizeof(uint)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<uint>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<uint>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into an unsigned long. @@ -330,7 +332,7 @@ namespace System { if (value.Length < sizeof(ulong)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<ulong>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into a float. @@ -341,7 +343,7 @@ namespace System { if (value.Length < sizeof(float)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<float>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<float>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into a double. @@ -352,7 +354,7 @@ namespace System { if (value.Length < sizeof(double)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<double>(ref value.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned<double>(ref MemoryMarshal.GetReference(value)); } // Converts an array of bytes into a String. @@ -442,7 +444,7 @@ namespace System { if (value.Length < sizeof(byte)) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); - return Unsafe.ReadUnaligned<byte>(ref value.DangerousGetPinnableReference()) != 0; + return Unsafe.ReadUnaligned<byte>(ref MemoryMarshal.GetReference(value)) != 0; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/mscorlib/shared/System/Convert.cs b/src/mscorlib/shared/System/Convert.cs index 6c592a4923..488ea77338 100644 --- a/src/mscorlib/shared/System/Convert.cs +++ b/src/mscorlib/shared/System/Convert.cs @@ -2434,7 +2434,7 @@ namespace System unsafe { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) fixed (char* charsPtr = result) { int charsWritten = ConvertToBase64Array(charsPtr, bytesPtr, 0, bytes.Length, insertLineBreaks); @@ -2527,8 +2527,8 @@ namespace System return false; } - fixed (char* outChars = &chars.DangerousGetPinnableReference()) - fixed (byte* inData = &bytes.DangerousGetPinnableReference()) + fixed (char* outChars = &MemoryMarshal.GetReference(chars)) + fixed (byte* inData = &MemoryMarshal.GetReference(bytes)) { charsWritten = ConvertToBase64Array(outChars, inData, 0, bytes.Length, insertLineBreaks); return true; @@ -2676,7 +2676,7 @@ namespace System chars = chars.Slice(0, chars.Length - 1); } - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { int resultLength = FromBase64_ComputeResultLength(charsPtr, chars.Length); Debug.Assert(resultLength >= 0); @@ -2686,7 +2686,7 @@ namespace System return false; } - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { bytesWritten = FromBase64_Decode(charsPtr, chars.Length, bytesPtr, bytes.Length); return true; diff --git a/src/mscorlib/shared/System/Globalization/CompareInfo.cs b/src/mscorlib/shared/System/Globalization/CompareInfo.cs index 84fadd376e..e088a82ded 100644 --- a/src/mscorlib/shared/System/Globalization/CompareInfo.cs +++ b/src/mscorlib/shared/System/Globalization/CompareInfo.cs @@ -14,6 +14,7 @@ using System.Reflection; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Runtime.Serialization; namespace System.Globalization @@ -555,8 +556,8 @@ namespace System.Globalization int length = Math.Min(strA.Length, strB.Length); int range = length; - fixed (char* ap = &strA.DangerousGetPinnableReference()) - fixed (char* bp = &strB.DangerousGetPinnableReference()) + fixed (char* ap = &MemoryMarshal.GetReference(strA)) + fixed (char* bp = &MemoryMarshal.GetReference(strB)) { char* a = ap; char* b = bp; diff --git a/src/mscorlib/shared/System/Guid.cs b/src/mscorlib/shared/System/Guid.cs index db5f932129..423d5bc78c 100644 --- a/src/mscorlib/shared/System/Guid.cs +++ b/src/mscorlib/shared/System/Guid.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using Internal.Runtime.CompilerServices; namespace System @@ -1361,7 +1362,7 @@ namespace System unsafe { - fixed (char* guidChars = &destination.DangerousGetPinnableReference()) + fixed (char* guidChars = &MemoryMarshal.GetReference(destination)) { char * p = guidChars; diff --git a/src/mscorlib/shared/System/IO/FileStream.Unix.cs b/src/mscorlib/shared/System/IO/FileStream.Unix.cs index 99a3377288..34164abc33 100644 --- a/src/mscorlib/shared/System/IO/FileStream.Unix.cs +++ b/src/mscorlib/shared/System/IO/FileStream.Unix.cs @@ -4,6 +4,7 @@ using Microsoft.Win32.SafeHandles; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -459,7 +460,7 @@ namespace System.IO VerifyOSHandlePosition(); int bytesRead; - fixed (byte* bufPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufPtr = &MemoryMarshal.GetReference(buffer)) { bytesRead = CheckFileCall(Interop.Sys.Read(_fileHandle, bufPtr, buffer.Length)); Debug.Assert(bytesRead <= buffer.Length); @@ -612,7 +613,7 @@ namespace System.IO { VerifyOSHandlePosition(); - fixed (byte* bufPtr = &source.DangerousGetPinnableReference()) + fixed (byte* bufPtr = &MemoryMarshal.GetReference(source)) { int offset = 0; int count = source.Length; diff --git a/src/mscorlib/shared/System/IO/FileStream.Windows.cs b/src/mscorlib/shared/System/IO/FileStream.Windows.cs index eec11b4b13..477b9430fc 100644 --- a/src/mscorlib/shared/System/IO/FileStream.Windows.cs +++ b/src/mscorlib/shared/System/IO/FileStream.Windows.cs @@ -1069,7 +1069,7 @@ namespace System.IO Debug.Assert(_useAsyncIO, "WriteInternalCoreAsync doesn't work on synchronous file streams!"); // Create and store async stream class library specific data in the async result - FileStreamCompletionSource completionSource = source.DangerousTryGetArray(out ArraySegment<byte> array) ? + FileStreamCompletionSource completionSource = MemoryMarshal.TryGetArray(source, out ArraySegment<byte> array) ? new FileStreamCompletionSource(this, 0, array.Array) : new MemoryFileStreamCompletionSource(this, 0, source); NativeOverlapped* intOverlapped = completionSource.Overlapped; @@ -1188,7 +1188,7 @@ namespace System.IO int r; int numBytesRead = 0; - fixed (byte* p = &bytes.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(bytes)) { r = _useAsyncIO ? Interop.Kernel32.ReadFile(handle, p, bytes.Length, IntPtr.Zero, overlapped) : @@ -1215,7 +1215,7 @@ namespace System.IO int numBytesWritten = 0; int r; - fixed (byte* p = &buffer.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(buffer)) { r = _useAsyncIO ? Interop.Kernel32.WriteFile(handle, p, buffer.Length, IntPtr.Zero, overlapped) : diff --git a/src/mscorlib/shared/System/IO/FileStreamCompletionSource.Win32.cs b/src/mscorlib/shared/System/IO/FileStreamCompletionSource.Win32.cs index e3871bcd6c..4e19f465bd 100644 --- a/src/mscorlib/shared/System/IO/FileStreamCompletionSource.Win32.cs +++ b/src/mscorlib/shared/System/IO/FileStreamCompletionSource.Win32.cs @@ -231,7 +231,7 @@ namespace System.IO internal MemoryFileStreamCompletionSource(FileStream stream, int numBufferedBytes, ReadOnlyMemory<byte> memory) : base(stream, numBufferedBytes, bytes: null) // this type handles the pinning, so null is passed for bytes { - Debug.Assert(!memory.DangerousTryGetArray(out ArraySegment<byte> array), "The base should be used directly if we can get the array."); + Debug.Assert(!MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> array), "The base should be used directly if we can get the array."); _handle = memory.Retain(pin: true); } diff --git a/src/mscorlib/shared/System/IO/MemoryStream.cs b/src/mscorlib/shared/System/IO/MemoryStream.cs index 727d492eae..c5e5ea918b 100644 --- a/src/mscorlib/shared/System/IO/MemoryStream.cs +++ b/src/mscorlib/shared/System/IO/MemoryStream.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -762,7 +763,7 @@ namespace System.IO { // See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>). // Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency. - if (source.DangerousTryGetArray(out ArraySegment<byte> sourceArray)) + if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray)) { Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count); } diff --git a/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs b/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs index 9e1cfef49d..171113542f 100644 --- a/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs +++ b/src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs @@ -418,7 +418,7 @@ namespace System.IO unsafe { - fixed (byte* pBuffer = &destination.DangerousGetPinnableReference()) + fixed (byte* pBuffer = &MemoryMarshal.GetReference(destination)) { if (_buffer != null) { @@ -709,7 +709,7 @@ namespace System.IO } } - fixed (byte* pBuffer = &source.DangerousGetPinnableReference()) + fixed (byte* pBuffer = &MemoryMarshal.GetReference(source)) { if (_buffer != null) { @@ -794,7 +794,7 @@ namespace System.IO { // See corresponding comment in ReadAsync for why we don't just always use Write(ReadOnlySpan<byte>). // Unlike ReadAsync, we could delegate to WriteAsync(byte[], ...) here, but we don't for consistency. - if (source.DangerousTryGetArray(out ArraySegment<byte> sourceArray)) + if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> sourceArray)) { Write(sourceArray.Array, sourceArray.Offset, sourceArray.Count); } diff --git a/src/mscorlib/shared/System/MemoryDebugView.cs b/src/mscorlib/shared/System/MemoryDebugView.cs index 215569191e..2706d09279 100644 --- a/src/mscorlib/shared/System/MemoryDebugView.cs +++ b/src/mscorlib/shared/System/MemoryDebugView.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; namespace System { @@ -27,7 +28,7 @@ namespace System // https://devdiv.visualstudio.com/DevDiv/_workitems?id=286592 get { - if (_memory.DangerousTryGetArray(out ArraySegment<T> segment)) + if (MemoryMarshal.TryGetArray(_memory, out ArraySegment<T> segment)) { T[] array = new T[_memory.Length]; Array.Copy(segment.Array, segment.Offset, array, 0, array.Length); diff --git a/src/mscorlib/shared/System/Number.Formatting.cs b/src/mscorlib/shared/System/Number.Formatting.cs index ddf9ef1b2d..70b35a08aa 100644 --- a/src/mscorlib/shared/System/Number.Formatting.cs +++ b/src/mscorlib/shared/System/Number.Formatting.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Globalization; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text; namespace System @@ -1558,7 +1559,7 @@ namespace System scaleAdjust = 0; src = section; - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';') { @@ -1728,7 +1729,7 @@ namespace System bool decimalWritten = false; - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { char* cur = dig; @@ -1948,7 +1949,7 @@ namespace System int digitCount = 0; int digLength = string.wcslen(dig); int digStart = (digPos < digLength) ? digPos : digLength; - fixed (char* spanPtr = &sb.AppendSpan(bufferSize).DangerousGetPinnableReference()) + fixed (char* spanPtr = &MemoryMarshal.GetReference(sb.AppendSpan(bufferSize))) { char* p = spanPtr + bufferSize - 1; for (int i = digPos - 1; i >= 0; i--) @@ -2188,7 +2189,7 @@ namespace System if (section == 0) return 0; - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { src = 0; for (;;) diff --git a/src/mscorlib/shared/System/Number.Parsing.cs b/src/mscorlib/shared/System/Number.Parsing.cs index 9d40e49af5..46951094eb 100644 --- a/src/mscorlib/shared/System/Number.Parsing.cs +++ b/src/mscorlib/shared/System/Number.Parsing.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Globalization; +using System.Runtime.InteropServices; namespace System { @@ -855,7 +856,7 @@ namespace System private static unsafe void StringToNumber(ReadOnlySpan<char> str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo info, bool parseDecimal) { Debug.Assert(info != null); - fixed (char* stringPointer = &str.DangerousGetPinnableReference()) + fixed (char* stringPointer = &MemoryMarshal.GetReference(str)) { char* p = stringPointer; if (!ParseNumber(ref p, options, ref number, info, parseDecimal) @@ -869,7 +870,7 @@ namespace System internal static unsafe bool TryStringToNumber(ReadOnlySpan<char> str, NumberStyles options, ref NumberBuffer number, NumberFormatInfo numfmt, bool parseDecimal) { Debug.Assert(numfmt != null); - fixed (char* stringPointer = &str.DangerousGetPinnableReference()) + fixed (char* stringPointer = &MemoryMarshal.GetReference(str)) { char* p = stringPointer; if (!ParseNumber(ref p, options, ref number, numfmt, parseDecimal) diff --git a/src/mscorlib/shared/System/Text/Decoder.cs b/src/mscorlib/shared/System/Text/Decoder.cs index 8dccaacd98..b827648fc1 100644 --- a/src/mscorlib/shared/System/Text/Decoder.cs +++ b/src/mscorlib/shared/System/Text/Decoder.cs @@ -5,6 +5,7 @@ using System.Text; using System; using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.Text { @@ -133,7 +134,7 @@ namespace System.Text public virtual unsafe int GetCharCount(ReadOnlySpan<byte> bytes, bool flush) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { return GetCharCount(bytesPtr, bytes.Length, flush); } @@ -226,8 +227,8 @@ namespace System.Text public virtual unsafe int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars, bool flush) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { return GetChars(bytesPtr, bytes.Length, charsPtr, chars.Length, flush); } @@ -340,8 +341,8 @@ namespace System.Text public virtual unsafe void Convert(ReadOnlySpan<byte> bytes, Span<char> chars, bool flush, out int bytesUsed, out int charsUsed, out bool completed) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { Convert(bytesPtr, bytes.Length, charsPtr, chars.Length, flush, out bytesUsed, out charsUsed, out completed); } diff --git a/src/mscorlib/shared/System/Text/Encoder.cs b/src/mscorlib/shared/System/Text/Encoder.cs index 1670608599..fb1bdb8038 100644 --- a/src/mscorlib/shared/System/Text/Encoder.cs +++ b/src/mscorlib/shared/System/Text/Encoder.cs @@ -5,6 +5,7 @@ using System.Text; using System; using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.Text { @@ -131,7 +132,7 @@ namespace System.Text public virtual unsafe int GetByteCount(ReadOnlySpan<char> chars, bool flush) { - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { return GetByteCount(charsPtr, chars.Length, flush); } @@ -220,8 +221,8 @@ namespace System.Text public virtual unsafe int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush) { - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { return GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length, flush); } @@ -334,8 +335,8 @@ namespace System.Text public virtual unsafe void Convert(ReadOnlySpan<char> chars, Span<byte> bytes, bool flush, out int charsUsed, out int bytesUsed, out bool completed) { - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { Convert(charsPtr, chars.Length, bytesPtr, bytes.Length, flush, out charsUsed, out bytesUsed, out completed); } diff --git a/src/mscorlib/shared/System/Text/Encoding.cs b/src/mscorlib/shared/System/Text/Encoding.cs index 8e4d29d24c..e469180ce6 100644 --- a/src/mscorlib/shared/System/Text/Encoding.cs +++ b/src/mscorlib/shared/System/Text/Encoding.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Globalization; using System.Threading; +using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Diagnostics.CodeAnalysis; @@ -712,7 +713,7 @@ namespace System.Text public virtual unsafe int GetByteCount(ReadOnlySpan<char> chars) { - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { return GetByteCount(charsPtr, chars.Length); } @@ -894,8 +895,8 @@ namespace System.Text public virtual unsafe int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes) { - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { return GetBytes(charsPtr, chars.Length, bytesPtr, bytes.Length); } @@ -944,7 +945,7 @@ namespace System.Text public virtual unsafe int GetCharCount(ReadOnlySpan<byte> bytes) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { return GetCharCount(bytesPtr, bytes.Length); } @@ -1056,8 +1057,8 @@ namespace System.Text public virtual unsafe int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) - fixed (char* charsPtr = &chars.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) + fixed (char* charsPtr = &MemoryMarshal.GetReference(chars)) { return GetChars(bytesPtr, bytes.Length, charsPtr, chars.Length); } @@ -1086,7 +1087,7 @@ namespace System.Text public unsafe string GetString(ReadOnlySpan<byte> bytes) { - fixed (byte* bytesPtr = &bytes.DangerousGetPinnableReference()) + fixed (byte* bytesPtr = &MemoryMarshal.GetReference(bytes)) { return GetString(bytesPtr, bytes.Length); } diff --git a/src/mscorlib/shared/System/Text/StringBuilder.cs b/src/mscorlib/shared/System/Text/StringBuilder.cs index ced656c5b8..d9da9377d0 100644 --- a/src/mscorlib/shared/System/Text/StringBuilder.cs +++ b/src/mscorlib/shared/System/Text/StringBuilder.cs @@ -7,6 +7,7 @@ using System.Runtime; using System.Runtime.Serialization; using System; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Security; using System.Threading; @@ -1030,7 +1031,7 @@ namespace System.Text { unsafe { - fixed (char* valueChars = &value.DangerousGetPinnableReference()) + fixed (char* valueChars = &MemoryMarshal.GetReference(value)) { Append(valueChars, value.Length); } @@ -1272,7 +1273,7 @@ namespace System.Text { unsafe { - fixed (char* sourcePtr = &value.DangerousGetPinnableReference()) + fixed (char* sourcePtr = &MemoryMarshal.GetReference(value)) Insert(index, sourcePtr, value.Length); } } @@ -2046,7 +2047,7 @@ namespace System.Text } fixed (char* sourcePtr = &source[sourceIndex]) - fixed (char* destinationPtr = &destination.DangerousGetPinnableReference()) + fixed (char* destinationPtr = &MemoryMarshal.GetReference(destination)) string.wstrcpy(destinationPtr + destinationIndex, sourcePtr, count); } } diff --git a/src/mscorlib/src/Interop/Windows/Kernel32/Interop.GetSystemDirectoryW.cs b/src/mscorlib/src/Interop/Windows/Kernel32/Interop.GetSystemDirectoryW.cs index 25c59d743b..cf1b31d62c 100644 --- a/src/mscorlib/src/Interop/Windows/Kernel32/Interop.GetSystemDirectoryW.cs +++ b/src/mscorlib/src/Interop/Windows/Kernel32/Interop.GetSystemDirectoryW.cs @@ -14,7 +14,7 @@ internal static partial class Interop internal static unsafe int GetSystemDirectoryW(Span<char> buffer) { - fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (char* bufferPtr = &MemoryMarshal.GetReference(buffer)) { return GetSystemDirectoryW(bufferPtr, buffer.Length); } diff --git a/src/mscorlib/src/Microsoft/Win32/Win32Native.cs b/src/mscorlib/src/Microsoft/Win32/Win32Native.cs index 1c16c13bbf..be86024416 100644 --- a/src/mscorlib/src/Microsoft/Win32/Win32Native.cs +++ b/src/mscorlib/src/Microsoft/Win32/Win32Native.cs @@ -688,7 +688,7 @@ namespace Microsoft.Win32 internal static unsafe int GetEnvironmentVariable(string lpName, Span<char> lpValue) { - fixed (char* lpValuePtr = &lpValue.DangerousGetPinnableReference()) + fixed (char* lpValuePtr = &MemoryMarshal.GetReference(lpValue)) { return GetEnvironmentVariable(lpName, lpValuePtr, lpValue.Length); } diff --git a/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs b/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs index cad9696488..7fbd49f656 100644 --- a/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs +++ b/src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs @@ -152,7 +152,7 @@ namespace System.Globalization Debug.Assert(string2 != null); Debug.Assert((options & (CompareOptions.Ordinal | CompareOptions.OrdinalIgnoreCase)) == 0); - fixed (char* pString1 = &string1.DangerousGetPinnableReference()) + fixed (char* pString1 = &MemoryMarshal.GetReference(string1)) fixed (char* pString2 = &string2.GetRawStringData()) { return Interop.GlobalizationInterop.CompareString(_sortHandle, pString1, string1.Length, pString2, string2.Length, options); @@ -164,8 +164,8 @@ namespace System.Globalization Debug.Assert(!_invariantMode); Debug.Assert((options & (CompareOptions.Ordinal | CompareOptions.OrdinalIgnoreCase)) == 0); - fixed (char* pString1 = &string1.DangerousGetPinnableReference()) - fixed (char* pString2 = &string2.DangerousGetPinnableReference()) + fixed (char* pString1 = &MemoryMarshal.GetReference(string1)) + fixed (char* pString2 = &MemoryMarshal.GetReference(string2)) { return Interop.GlobalizationInterop.CompareString(_sortHandle, pString1, string1.Length, pString2, string2.Length, options); } diff --git a/src/mscorlib/src/System/Globalization/CompareInfo.Windows.cs b/src/mscorlib/src/System/Globalization/CompareInfo.Windows.cs index 4e56581bad..bbb5455938 100644 --- a/src/mscorlib/src/System/Globalization/CompareInfo.Windows.cs +++ b/src/mscorlib/src/System/Globalization/CompareInfo.Windows.cs @@ -128,7 +128,7 @@ namespace System.Globalization string localeName = _sortHandle != IntPtr.Zero ? null : _sortName; fixed (char* pLocaleName = localeName) - fixed (char* pString1 = &string1.DangerousGetPinnableReference()) + fixed (char* pString1 = &MemoryMarshal.GetReference(string1)) fixed (char* pString2 = &string2.GetRawStringData()) { int result = Interop.Kernel32.CompareStringEx( @@ -160,8 +160,8 @@ namespace System.Globalization string localeName = _sortHandle != IntPtr.Zero ? null : _sortName; fixed (char* pLocaleName = localeName) - fixed (char* pString1 = &string1.DangerousGetPinnableReference()) - fixed (char* pString2 = &string2.DangerousGetPinnableReference()) + fixed (char* pString1 = &MemoryMarshal.GetReference(string1)) + fixed (char* pString2 = &MemoryMarshal.GetReference(string2)) { int result = Interop.Kernel32.CompareStringEx( pLocaleName, diff --git a/src/mscorlib/src/System/IO/BinaryReader.cs b/src/mscorlib/src/System/IO/BinaryReader.cs index 05de184335..fc7dba4a3f 100644 --- a/src/mscorlib/src/System/IO/BinaryReader.cs +++ b/src/mscorlib/src/System/IO/BinaryReader.cs @@ -16,6 +16,7 @@ using System; using System.Runtime; +using System.Runtime.InteropServices; using System.Text; using System.Globalization; using System.Diagnostics; @@ -424,7 +425,7 @@ namespace System.IO unsafe { fixed (byte* pBytes = byteBuffer) - fixed (char* pChars = &buffer.DangerousGetPinnableReference()) + fixed (char* pChars = &MemoryMarshal.GetReference(buffer)) { charsRead = _decoder.GetChars(pBytes + position, numBytes, pChars + index, charsRemaining, flush: false); } diff --git a/src/mscorlib/src/System/IO/Stream.cs b/src/mscorlib/src/System/IO/Stream.cs index 477fe8bb92..d9ed08f737 100644 --- a/src/mscorlib/src/System/IO/Stream.cs +++ b/src/mscorlib/src/System/IO/Stream.cs @@ -696,7 +696,7 @@ namespace System.IO public virtual Task WriteAsync(ReadOnlyMemory<byte> source, CancellationToken cancellationToken = default(CancellationToken)) { - if (source.DangerousTryGetArray(out ArraySegment<byte> array)) + if (MemoryMarshal.TryGetArray(source, out ArraySegment<byte> array)) { return WriteAsync(array.Array, array.Offset, array.Count, cancellationToken); } diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs index 6b0653322f..bbc96d266c 100644 --- a/src/mscorlib/src/System/String.cs +++ b/src/mscorlib/src/System/String.cs @@ -668,7 +668,7 @@ namespace System } string result = FastAllocateString(value.Length); - fixed (char* dest = &result._firstChar, src = &value.DangerousGetPinnableReference()) + fixed (char* dest = &result._firstChar, src = &MemoryMarshal.GetReference(value)) { wstrcpy(dest, src, value.Length); } diff --git a/src/vm/metasig.h b/src/vm/metasig.h index 9976ed12f2..5fb4721221 100644 --- a/src/vm/metasig.h +++ b/src/vm/metasig.h @@ -282,7 +282,7 @@ DEFINE_METASIG(SM(RefFlt_RetFlt, r(f), f)) DEFINE_METASIG(SM(RefFlt_Flt, r(f) f, v)) DEFINE_METASIG(SM(RefDbl_RetDbl, r(d), d)) DEFINE_METASIG(SM(RefDbl_Dbl, r(d) d, v)) -DEFINE_METASIG(GM(RefT_RetT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(M(0)) , M(0))) +DEFINE_METASIG(GM(RefT_RetT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(M(0)), M(0))) DEFINE_METASIG(GM(RefT_T, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(M(0)) M(0), v)) DEFINE_METASIG(GM(RefByte_RetT, IMAGE_CEE_CS_CALLCONV_DEFAULT, 1, r(b), M(0))) diff --git a/tests/src/JIT/Performance/CodeQuality/Span/Indexer.cs b/tests/src/JIT/Performance/CodeQuality/Span/Indexer.cs index 63f59e04be..82583ad1e2 100644 --- a/tests/src/JIT/Performance/CodeQuality/Span/Indexer.cs +++ b/tests/src/JIT/Performance/CodeQuality/Span/Indexer.cs @@ -67,7 +67,7 @@ namespace Span [MethodImpl(MethodImplOptions.NoInlining)] static byte TestRef(Span<byte> data) { - ref byte p = ref data.DangerousGetPinnableReference(); + ref byte p = ref MemoryMarshal.GetReference(data); int length = data.Length; byte x = 0; @@ -102,7 +102,7 @@ namespace Span [MethodImpl(MethodImplOptions.NoInlining)] static unsafe byte TestFixed1(Span<byte> data) { - fixed (byte* pData = &data.DangerousGetPinnableReference()) + fixed (byte* pData = &MemoryMarshal.GetReference(data)) { int length = data.Length; byte x = 0; @@ -140,7 +140,7 @@ namespace Span [MethodImpl(MethodImplOptions.NoInlining)] static unsafe byte TestFixed2(Span<byte> data) { - fixed (byte* pData = &data.DangerousGetPinnableReference()) + fixed (byte* pData = &MemoryMarshal.GetReference(data)) { int length = data.Length; byte x = 0; |