Convert uses of the Dangerous APIs to use MemoryMarshal.GetReference (#15532)
authorAhson Khan <ahkha@microsoft.com>
Sat, 16 Dec 2017 06:36:38 +0000 (22:36 -0800)
committerGitHub <noreply@github.com>
Sat, 16 Dec 2017 06:36:38 +0000 (22:36 -0800)
* Convert uses of the Dangerous APIs to use MemoryMarshal.GetReference

* Adding Unsafe.AsRef(in...) and using that for ReadOnlySpan GetReference

* Fix typo - add missing bracket.

* Change AsRef(ref...) to AsRef(in...)

* Remove unnecessary whitespace

* Remove Unsafe.AsRef(in...) and its uses.

* Revert "Remove unnecessary whitespace"

This reverts commit 4dbe38cae472e868f74afbabde358f3a7609f6ae.

* Revert "Revert "Remove unnecessary whitespace""

This reverts commit 44d79483423ac9ffcf7c566b79d4a42c6aa15dac.

* Remove extra space to fix formatting.

25 files changed:
src/mscorlib/shared/System/BitConverter.cs
src/mscorlib/shared/System/Convert.cs
src/mscorlib/shared/System/Globalization/CompareInfo.cs
src/mscorlib/shared/System/Guid.cs
src/mscorlib/shared/System/IO/FileStream.Unix.cs
src/mscorlib/shared/System/IO/FileStream.Windows.cs
src/mscorlib/shared/System/IO/FileStreamCompletionSource.Win32.cs
src/mscorlib/shared/System/IO/MemoryStream.cs
src/mscorlib/shared/System/IO/UnmanagedMemoryStream.cs
src/mscorlib/shared/System/MemoryDebugView.cs
src/mscorlib/shared/System/Number.Formatting.cs
src/mscorlib/shared/System/Number.Parsing.cs
src/mscorlib/shared/System/Text/Decoder.cs
src/mscorlib/shared/System/Text/Encoder.cs
src/mscorlib/shared/System/Text/Encoding.cs
src/mscorlib/shared/System/Text/StringBuilder.cs
src/mscorlib/src/Interop/Windows/Kernel32/Interop.GetSystemDirectoryW.cs
src/mscorlib/src/Microsoft/Win32/Win32Native.cs
src/mscorlib/src/System/Globalization/CompareInfo.Unix.cs
src/mscorlib/src/System/Globalization/CompareInfo.Windows.cs
src/mscorlib/src/System/IO/BinaryReader.cs
src/mscorlib/src/System/IO/Stream.cs
src/mscorlib/src/System/String.cs
src/vm/metasig.h
tests/src/JIT/Performance/CodeQuality/Span/Indexer.cs

index c7f4d04..e3cf20e 100644 (file)
@@ -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)]
index 6c592a4..488ea77 100644 (file)
@@ -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;
index 84fadd3..e088a82 100644 (file)
@@ -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;
index db5f932..423d5bc 100644 (file)
@@ -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;
 
index 99a3377..34164ab 100644 (file)
@@ -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;
index eec11b4..477b943 100644 (file)
@@ -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) :
index e3871bc..4e19f46 100644 (file)
@@ -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);
             }
 
index 727d492..c5e5ea9 100644 (file)
@@ -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);
                 }
index 9e1cfef..1711135 100644 (file)
@@ -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);
                 }
index 2155691..2706d09 100644 (file)
@@ -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);
index ddf9ef1..70b35a0 100644 (file)
@@ -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 (;;)
index 9d40e49..4695109 100644 (file)
@@ -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)
index 8dccaac..b827648 100644 (file)
@@ -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);
             }
index 1670608..fb1bdb8 100644 (file)
@@ -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);
             }
index 8e4d29d..e469180 100644 (file)
@@ -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);
             }
index ced656c..d9da937 100644 (file)
@@ -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);
             }
         }
index 25c59d7..cf1b31d 100644 (file)
@@ -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);
             }
index 1c16c13..be86024 100644 (file)
@@ -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);
             }
index cad9696..7fbd49f 100644 (file)
@@ -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);
             }
index 4e56581..bbb5455 100644 (file)
@@ -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,
index 05de184..fc7dba4 100644 (file)
@@ -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);
                         }
index 477fe8b..d9ed08f 100644 (file)
@@ -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);
             }
index 6b06533..bbc96d2 100644 (file)
@@ -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);
             }
index 9976ed1..5fb4721 100644 (file)
@@ -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)))
index 63f59e0..82583ad 100644 (file)
@@ -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;