diff options
Diffstat (limited to 'src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs')
-rw-r--r-- | src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs | 67 |
1 files changed, 35 insertions, 32 deletions
diff --git a/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs b/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs index ee5c3d8e87..aba25e94a3 100644 --- a/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs +++ b/src/mscorlib/src/System/Runtime/InteropServices/SafeBuffer.cs @@ -66,8 +66,6 @@ // assignments in a static class constructor are under a lock implicitly. -namespace System.Runtime.InteropServices -{ using System; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; @@ -77,12 +75,13 @@ using Microsoft.Win32.SafeHandles; using System.Diagnostics; using System.Diagnostics.Contracts; - +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) UInt32.MaxValue) : ((UIntPtr) UInt64.MaxValue); + private static readonly UIntPtr Uninitialized = (UIntPtr.Size == 4) ? + ((UIntPtr)UInt32.MaxValue) : ((UIntPtr)UInt64.MaxValue); private UIntPtr _numBytes; @@ -100,15 +99,15 @@ using System.Diagnostics.Contracts; public void Initialize(ulong numBytes) { if (numBytes < 0) - throw new ArgumentOutOfRangeException(nameof(numBytes), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_NeedNonNegNum); if (IntPtr.Size == 4 && numBytes > UInt32.MaxValue) - throw new ArgumentOutOfRangeException(nameof(numBytes), Environment.GetResourceString("ArgumentOutOfRange_AddressSpace")); + throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_AddressSpace); Contract.EndContractBlock(); if (numBytes >= (ulong)Uninitialized) - throw new ArgumentOutOfRangeException(nameof(numBytes), Environment.GetResourceString("ArgumentOutOfRange_UIntPtrMax-1")); + throw new ArgumentOutOfRangeException(nameof(numBytes), SR.ArgumentOutOfRange_UIntPtrMax); - _numBytes = (UIntPtr) numBytes; + _numBytes = (UIntPtr)numBytes; } /// <summary> @@ -119,18 +118,18 @@ using System.Diagnostics.Contracts; public void Initialize(uint numElements, uint sizeOfEachElement) { if (numElements < 0) - throw new ArgumentOutOfRangeException(nameof(numElements), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(numElements), SR.ArgumentOutOfRange_NeedNonNegNum); if (sizeOfEachElement < 0) - throw new ArgumentOutOfRangeException(nameof(sizeOfEachElement), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(sizeOfEachElement), SR.ArgumentOutOfRange_NeedNonNegNum); if (IntPtr.Size == 4 && numElements * sizeOfEachElement > UInt32.MaxValue) - throw new ArgumentOutOfRangeException("numBytes", Environment.GetResourceString("ArgumentOutOfRange_AddressSpace")); + throw new ArgumentOutOfRangeException("numBytes", SR.ArgumentOutOfRange_AddressSpace); Contract.EndContractBlock(); if (numElements * sizeOfEachElement >= (ulong)Uninitialized) - throw new ArgumentOutOfRangeException(nameof(numElements), Environment.GetResourceString("ArgumentOutOfRange_UIntPtrMax-1")); + throw new ArgumentOutOfRangeException(nameof(numElements), SR.ArgumentOutOfRange_UIntPtrMax); - _numBytes = checked((UIntPtr) (numElements * sizeOfEachElement)); + _numBytes = checked((UIntPtr)(numElements * sizeOfEachElement)); } /// <summary> @@ -209,7 +208,8 @@ using System.Diagnostics.Contracts; /// may have to consider alignment.</param> /// <returns>An instance of T read from memory.</returns> [CLSCompliant(false)] - public T Read<T>(ulong byteOffset) where T : struct { + public T Read<T>(ulong byteOffset) where T : struct + { if (_numBytes == Uninitialized) throw NotInitialized(); @@ -240,13 +240,13 @@ using System.Diagnostics.Contracts; where T : struct { if (array == null) - throw new ArgumentNullException(nameof(array), Environment.GetResourceString("ArgumentNull_Buffer")); + throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer); if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (array.Length - index < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); if (_numBytes == Uninitialized) @@ -282,7 +282,8 @@ using System.Diagnostics.Contracts; /// may have to consider alignment.</param> /// <param name="value">The value type to write to memory.</param> [CLSCompliant(false)] - public void Write<T>(ulong byteOffset, T value) where T : struct { + public void Write<T>(ulong byteOffset, T value) where T : struct + { if (_numBytes == Uninitialized) throw NotInitialized(); @@ -310,13 +311,13 @@ using System.Diagnostics.Contracts; where T : struct { if (array == null) - throw new ArgumentNullException(nameof(array), Environment.GetResourceString("ArgumentNull_Buffer")); + throw new ArgumentNullException(nameof(array), SR.ArgumentNull_Buffer); if (index < 0) - throw new ArgumentOutOfRangeException(nameof(index), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) - throw new ArgumentOutOfRangeException(nameof(count), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum); if (array.Length - index < count) - throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen")); + throw new ArgumentException(SR.Argument_InvalidOffLen); Contract.EndContractBlock(); if (_numBytes == Uninitialized) @@ -326,7 +327,7 @@ using System.Diagnostics.Contracts; uint alignedSizeofT = Marshal.AlignedSizeOf<T>(); byte* ptr = (byte*)handle + byteOffset; SpaceCheck(ptr, checked((ulong)(alignedSizeofT * count))); - + bool mustCallRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try @@ -347,35 +348,37 @@ using System.Diagnostics.Contracts; /// Returns the number of bytes in the memory region. /// </summary> [CLSCompliant(false)] - public ulong ByteLength { - get { + public ulong ByteLength + { + get + { if (_numBytes == Uninitialized) throw NotInitialized(); - return (ulong) _numBytes; + return (ulong)_numBytes; } } /* No indexer. The perf would be misleadingly bad. People should use * AcquirePointer and ReleasePointer instead. */ - + private void SpaceCheck(byte* ptr, ulong sizeInBytes) { if ((ulong)_numBytes < sizeInBytes) NotEnoughRoom(); - if ((ulong)(ptr - (byte*) handle) > ((ulong)_numBytes) - sizeInBytes) + if ((ulong)(ptr - (byte*)handle) > ((ulong)_numBytes) - sizeInBytes) NotEnoughRoom(); } private static void NotEnoughRoom() { - throw new ArgumentException(Environment.GetResourceString("Arg_BufferTooSmall")); + throw new ArgumentException(SR.Arg_BufferTooSmall); } private static InvalidOperationException NotInitialized() { Debug.Assert(false, "Uninitialized SafeBuffer! Someone needs to call Initialize before using this instance!"); - return new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MustCallInitialize")); + return new InvalidOperationException(SR.InvalidOperation_MustCallInitialize); } // FCALL limitations mean we can't have generic FCALL methods. However, we can pass |