From bd4697644e7024d1e5be94b674f73066bee112a4 Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Sat, 30 May 2020 18:04:26 +0200 Subject: [PATCH] Use nuint for bytes counter inside SafeBuffer (#37173) * Use nuint for bytes counter inside SafeBuffer * One more place to update --- .../System/Runtime/InteropServices/SafeBuffer.cs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) 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 19e021c..60e59d1 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 @@ -73,17 +73,15 @@ namespace System.Runtime.InteropServices { public abstract unsafe class SafeBuffer : SafeHandleZeroOrMinusOneIsInvalid { - // Steal UIntPtr.MaxValue as our uninitialized value. - private static readonly UIntPtr Uninitialized = (UIntPtr.Size == 4) ? - ((UIntPtr)uint.MaxValue) : ((UIntPtr)ulong.MaxValue); - - private UIntPtr _numBytes; + private nuint _numBytes; protected SafeBuffer(bool ownsHandle) : base(ownsHandle) { _numBytes = Uninitialized; } + private static nuint Uninitialized => nuint.MaxValue; + /// /// Specifies the size of the region of memory, in bytes. Must be /// called before using the SafeBuffer. @@ -98,7 +96,7 @@ namespace System.Runtime.InteropServices if (numBytes >= (ulong)Uninitialized) throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_UIntPtrMax); - _numBytes = (UIntPtr)numBytes; + _numBytes = (nuint)numBytes; } /// @@ -226,7 +224,7 @@ namespace System.Runtime.InteropServices uint sizeofT = SizeOf(); uint alignedSizeofT = AlignedSizeOf(); byte* ptr = (byte*)handle + byteOffset; - SpaceCheck(ptr, checked((ulong)(alignedSizeofT * count))); + SpaceCheck(ptr, checked((nuint)(alignedSizeofT * count))); bool mustCallRelease = false; try @@ -302,7 +300,7 @@ namespace System.Runtime.InteropServices uint sizeofT = SizeOf(); uint alignedSizeofT = AlignedSizeOf(); byte* ptr = (byte*)handle + byteOffset; - SpaceCheck(ptr, checked((ulong)(alignedSizeofT * count))); + SpaceCheck(ptr, checked((nuint)(alignedSizeofT * count))); bool mustCallRelease = false; try @@ -338,18 +336,18 @@ namespace System.Runtime.InteropServices if (_numBytes == Uninitialized) throw NotInitialized(); - return (ulong)_numBytes; + return _numBytes; } } /* No indexer. The perf would be misleadingly bad. People should use * AcquirePointer and ReleasePointer instead. */ - private void SpaceCheck(byte* ptr, ulong sizeInBytes) + private void SpaceCheck(byte* ptr, nuint sizeInBytes) { - if ((ulong)_numBytes < sizeInBytes) + if (_numBytes < sizeInBytes) NotEnoughRoom(); - if ((ulong)(ptr - (byte*)handle) > ((ulong)_numBytes) - sizeInBytes) + if ((ulong)(ptr - (byte*)handle) > (_numBytes - sizeInBytes)) NotEnoughRoom(); } -- 2.7.4