diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs index 3aae57e98ef6c1..58e75f34040b91 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs @@ -93,7 +93,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -111,7 +111,7 @@ public override unsafe int GetByteCount(string chars) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); } - fixed (char* pChars = chars) + fixed (char* pChars = &chars.GetPinnableReference()) { return GetByteCountCommon(pChars, chars!.Length); } @@ -232,8 +232,8 @@ public override unsafe int GetBytes(string chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &chars.GetPinnableReference()) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -280,8 +280,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -453,7 +453,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetCharCountCommon(pBytes + index, count); } @@ -571,8 +571,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -747,7 +747,7 @@ public override unsafe string GetString(byte[] bytes, int byteIndex, int byteCou if (byteCount == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return string.CreateStringFromEncoding(pBytes + byteIndex, byteCount, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs index e60a5eca24b1c7..d722a8d45cb9de 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Encoding.cs @@ -556,7 +556,7 @@ public int GetByteCount(string s, int index, int count) unsafe { - fixed (char* pChar = s) + fixed (char* pChar = &s.GetPinnableReference()) { return GetByteCount(pChar + index, count); } @@ -644,14 +644,14 @@ public byte[] GetBytes(string s, int index, int count) unsafe { - fixed (char* pChar = s) + fixed (char* pChar = &s.GetPinnableReference()) { int byteCount = GetByteCount(pChar + index, count); if (byteCount == 0) return Array.Empty(); byte[] bytes = new byte[byteCount]; - fixed (byte* pBytes = &bytes[0]) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { int bytesReceived = GetBytes(pChar + index, count, pBytes, byteCount); Debug.Assert(byteCount == bytesReceived); diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs index e129b5284bcb2a..476fd14c3afebc 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs @@ -70,7 +70,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -93,7 +93,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) { return GetByteCountCommon(pChars, s.Length); } @@ -216,8 +216,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -278,8 +278,8 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, byte ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = s) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &s.GetPinnableReference()) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -442,8 +442,8 @@ public override unsafe char[] GetChars(byte[] bytes) char[] chars = new char[bytes.Length]; - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { GetCharsCommon(pBytes, bytes.Length, pChars, chars.Length); } @@ -477,8 +477,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -509,8 +509,8 @@ public override unsafe char[] GetChars(byte[] bytes, int index, int count) char[] chars = new char[count]; - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { GetCharsCommon(pBytes + index, count, pChars, chars.Length); } @@ -554,8 +554,8 @@ public override unsafe string GetString(byte[] bytes) } string result = string.FastAllocateString(bytes.Length); - fixed (byte* pBytes = bytes) - fixed (char* pChars = result) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes, bytes.Length, pChars, result.Length); } @@ -584,8 +584,8 @@ public override unsafe string GetString(byte[] bytes, int index, int count) } string result = string.FastAllocateString(count); - fixed (byte* pBytes = bytes) - fixed (char* pChars = result) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &result.GetPinnableReference()) { GetCharsCommon(pBytes + index, count, pChars, count); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs index 41031f77344809..a8340371134410 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF32Encoding.cs @@ -105,7 +105,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -121,7 +121,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -162,7 +162,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); @@ -205,7 +205,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -252,7 +252,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -297,7 +297,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -342,7 +342,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs index 1ae72a0fbd3931..16fa6895649813 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF7Encoding.cs @@ -141,7 +141,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -157,7 +157,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -198,7 +198,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); @@ -241,7 +241,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -288,7 +288,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -333,7 +333,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -378,7 +378,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs index a0358cce2a980b..d97761bf770c8a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs @@ -62,7 +62,7 @@ private unsafe byte[] GetBytesForSmallInput(string s) int sourceLength = s.Length; // hoist this to avoid having the JIT auto-insert null checks int bytesWritten; - fixed (char* pSource = s) + fixed (char* pSource = &s.GetPinnableReference()) { bytesWritten = GetBytesCommon(pSource, sourceLength, pDestination, MaxSmallInputElementCount * MaxUtf8BytesPerChar); Debug.Assert(0 <= bytesWritten && bytesWritten <= s.Length * MaxUtf8BytesPerChar); @@ -140,7 +140,7 @@ private unsafe string GetStringForSmallInput(byte[] bytes) int sourceLength = bytes.Length; // hoist this to avoid having the JIT auto-insert null checks int charsWritten; - fixed (byte* pSource = bytes) + fixed (byte* pSource = &MemoryMarshal.GetArrayDataReference(bytes)) { charsWritten = GetCharsCommon(pSource, sourceLength, pDestination, MaxSmallInputElementCount); Debug.Assert(0 <= charsWritten && charsWritten <= sourceLength); // should never have more output chars than input bytes diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs index f4c4d4385ada75..e4fb8fa93c328d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.cs @@ -143,7 +143,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetByteCountCommon(pChars + index, count); } @@ -161,7 +161,7 @@ public override unsafe int GetByteCount(string chars) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.chars); } - fixed (char* pChars = chars) + fixed (char* pChars = &chars.GetPinnableReference()) { return GetByteCountCommon(pChars, chars.Length); } @@ -279,8 +279,8 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = s) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &s.GetPinnableReference()) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -327,8 +327,8 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.byteIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (char* pChars = chars) - fixed (byte* pBytes = bytes) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytesCommon(pChars + charIndex, charCount, pBytes + byteIndex, bytes.Length - byteIndex); } @@ -454,7 +454,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer); } - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetCharCountCommon(pBytes + index, count); } @@ -522,8 +522,8 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.charIndex, ExceptionResource.ArgumentOutOfRange_IndexMustBeLessOrEqual); } - fixed (byte* pBytes = bytes) - fixed (char* pChars = chars) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { return GetCharsCommon(pBytes + byteIndex, byteCount, pChars + charIndex, chars.Length - charIndex); } @@ -697,7 +697,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return string.CreateStringFromEncoding(pBytes + index, count, this); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs index 7eaf078feae5ad..2d6ef64c6f4c92 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Text/UnicodeEncoding.cs @@ -97,7 +97,7 @@ public override unsafe int GetByteCount(char[] chars, int index, int count) return 0; // Just call the pointer version - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) return GetByteCount(pChars + index, count, null); } @@ -113,7 +113,7 @@ public override unsafe int GetByteCount(string s) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.s); } - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) return GetByteCount(pChars, s.Length, null); } @@ -154,7 +154,7 @@ public override unsafe int GetBytes(string s, int charIndex, int charCount, int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = s) + fixed (char* pChars = &s.GetPinnableReference()) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { return GetBytes(pChars + charIndex, charCount, pBytes + byteIndex, byteCount, null); @@ -197,7 +197,7 @@ public override unsafe int GetBytes(char[] chars, int charIndex, int charCount, // Just call pointer version int byteCount = bytes.Length - byteIndex; - fixed (char* pChars = chars) + fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) { // Remember that byteCount is # to decode, not size of array. @@ -244,7 +244,7 @@ public override unsafe int GetCharCount(byte[] bytes, int index, int count) return 0; // Just call pointer version - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return GetCharCount(pBytes + index, count, null); } @@ -289,7 +289,7 @@ public override unsafe int GetChars(byte[] bytes, int byteIndex, int byteCount, // Just call pointer version int charCount = chars.Length - charIndex; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) fixed (char* pChars = &MemoryMarshal.GetArrayDataReference(chars)) { // Remember that charCount is # to decode, not size of array @@ -334,7 +334,7 @@ public override unsafe string GetString(byte[] bytes, int index, int count) // Avoid problems with empty input buffer if (count == 0) return string.Empty; - fixed (byte* pBytes = bytes) + fixed (byte* pBytes = &MemoryMarshal.GetArrayDataReference(bytes)) return string.CreateStringFromEncoding( pBytes + index, count, this); }