| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | // See the LICENSE file in the project root for more information. |
| | | 4 | | |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Buffers; |
| | | 7 | | |
| | | 8 | | namespace System |
| | | 9 | | { |
| | | 10 | | // |
| | | 11 | | // This pattern of easily inlinable "void Throw" routines that stack on top of NoInlining factory methods |
| | | 12 | | // is a compromise between older JITs and newer JITs (RyuJIT in Core CLR 1.1.0+ and desktop CLR in 4.6.3+). |
| | | 13 | | // This package is explicitly targeted at older JITs as newer runtimes expect to implement Span intrinsically for |
| | | 14 | | // best performance. |
| | | 15 | | // |
| | | 16 | | // The aim of this pattern is three-fold |
| | | 17 | | // 1. Extracting the throw makes the method preforming the throw in a conditional branch smaller and more inlinable |
| | | 18 | | // 2. Extracting the throw from generic method to non-generic method reduces the repeated codegen size for value typ |
| | | 19 | | // 3a. Newer JITs will not inline the methods that only throw and also recognise them, move the call to cold section |
| | | 20 | | // and not add stack prep and unwind before calling https://github.com/dotnet/coreclr/pull/6103 |
| | | 21 | | // 3b. Older JITs will inline the throw itself and move to cold section; but not inline the non-inlinable exception |
| | | 22 | | // factory methods - still maintaining advantages 1 & 2 |
| | | 23 | | // |
| | | 24 | | |
| | | 25 | | internal static class ThrowHelper |
| | | 26 | | { |
| | 0 | 27 | | internal static void ThrowArgumentNullException(ExceptionArgument argument) { throw CreateArgumentNullException( |
| | | 28 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 29 | | private static Exception CreateArgumentNullException(ExceptionArgument argument) { return new ArgumentNullExcept |
| | | 30 | | |
| | | 31 | | internal static void ThrowArrayTypeMismatchException() { throw CreateArrayTypeMismatchException(); } |
| | | 32 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 33 | | private static Exception CreateArrayTypeMismatchException() { return new ArrayTypeMismatchException(); } |
| | | 34 | | |
| | 2 | 35 | | internal static void ThrowArgumentException_InvalidTypeWithPointersNotSupported(Type type) { throw CreateArgumen |
| | | 36 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 37 | | private static Exception CreateArgumentException_InvalidTypeWithPointersNotSupported(Type type) { return new Arg |
| | | 38 | | |
| | | 39 | | internal static void ThrowArgumentException_DestinationTooShort() { throw CreateArgumentException_DestinationToo |
| | | 40 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 41 | | private static Exception CreateArgumentException_DestinationTooShort() { return new ArgumentException(SR.Argumen |
| | | 42 | | |
| | | 43 | | internal static void ThrowIndexOutOfRangeException() { throw CreateIndexOutOfRangeException(); } |
| | | 44 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 45 | | private static Exception CreateIndexOutOfRangeException() { return new IndexOutOfRangeException(); } |
| | | 46 | | |
| | | 47 | | internal static void ThrowArgumentOutOfRangeException() { throw CreateArgumentOutOfRangeException(); } |
| | | 48 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 49 | | private static Exception CreateArgumentOutOfRangeException() { return new ArgumentOutOfRangeException(); } |
| | | 50 | | |
| | 2 | 51 | | internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument) { throw CreateArgumentOutOfRan |
| | | 52 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 53 | | private static Exception CreateArgumentOutOfRangeException(ExceptionArgument argument) { return new ArgumentOutO |
| | | 54 | | |
| | 2 | 55 | | internal static void ThrowArgumentOutOfRangeException_PrecisionTooLarge() { throw CreateArgumentOutOfRangeExcept |
| | | 56 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 57 | | private static Exception CreateArgumentOutOfRangeException_PrecisionTooLarge() { return new ArgumentOutOfRangeEx |
| | | 58 | | |
| | 2 | 59 | | internal static void ThrowArgumentOutOfRangeException_SymbolDoesNotFit() { throw CreateArgumentOutOfRangeExcepti |
| | | 60 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 61 | | private static Exception CreateArgumentOutOfRangeException_SymbolDoesNotFit() { return new ArgumentOutOfRangeExc |
| | | 62 | | |
| | 2 | 63 | | internal static void ThrowInvalidOperationException() { throw CreateInvalidOperationException(); } |
| | | 64 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 65 | | private static Exception CreateInvalidOperationException() { return new InvalidOperationException(); } |
| | | 66 | | |
| | | 67 | | internal static void ThrowInvalidOperationException_OutstandingReferences() { throw CreateInvalidOperationExcept |
| | | 68 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 69 | | private static Exception CreateInvalidOperationException_OutstandingReferences() { return new InvalidOperationEx |
| | | 70 | | |
| | | 71 | | internal static void ThrowInvalidOperationException_UnexpectedSegmentType() { throw CreateInvalidOperationExcept |
| | | 72 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 73 | | private static Exception CreateInvalidOperationException_UnexpectedSegmentType() { return new InvalidOperationEx |
| | | 74 | | |
| | 0 | 75 | | internal static void ThrowInvalidOperationException_EndPositionNotReached() { throw CreateInvalidOperationExcept |
| | | 76 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 0 | 77 | | private static Exception CreateInvalidOperationException_EndPositionNotReached() { return new InvalidOperationEx |
| | | 78 | | |
| | 2 | 79 | | internal static void ThrowArgumentOutOfRangeException_PositionOutOfRange() { throw CreateArgumentOutOfRangeExcep |
| | | 80 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 81 | | private static Exception CreateArgumentOutOfRangeException_PositionOutOfRange() { return new ArgumentOutOfRangeE |
| | | 82 | | |
| | 2 | 83 | | internal static void ThrowArgumentOutOfRangeException_CountOutOfRange() { throw CreateArgumentOutOfRangeExceptio |
| | | 84 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 85 | | private static Exception CreateArgumentOutOfRangeException_CountOutOfRange() { return new ArgumentOutOfRangeExce |
| | | 86 | | |
| | 2 | 87 | | internal static void ThrowObjectDisposedException_ArrayMemoryPoolBuffer() { throw CreateObjectDisposedException_ |
| | | 88 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 89 | | private static Exception CreateObjectDisposedException_ArrayMemoryPoolBuffer() { return new ObjectDisposedExcept |
| | | 90 | | |
| | | 91 | | internal static void ThrowObjectDisposedException_MemoryDisposed() { throw CreateObjectDisposedException_MemoryD |
| | | 92 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 93 | | private static Exception CreateObjectDisposedException_MemoryDisposed() { return new ObjectDisposedException("Ow |
| | | 94 | | |
| | 2 | 95 | | internal static void ThrowFormatException_BadFormatSpecifier() { throw CreateFormatException_BadFormatSpecifier( |
| | | 96 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 3 | 97 | | private static Exception CreateFormatException_BadFormatSpecifier() { return new FormatException(SR.Argument_Bad |
| | | 98 | | |
| | | 99 | | internal static void ThrowArgumentException_OverlapAlignmentMismatch() { throw CreateArgumentException_OverlapAl |
| | | 100 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 101 | | private static Exception CreateArgumentException_OverlapAlignmentMismatch() { return new ArgumentException(SR.Ar |
| | | 102 | | |
| | | 103 | | internal static void ThrowNotSupportedException() { throw CreateThrowNotSupportedException(); } |
| | | 104 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | | 105 | | private static Exception CreateThrowNotSupportedException() { return new NotSupportedException(); } |
| | | 106 | | |
| | | 107 | | // |
| | | 108 | | // Enable use of ThrowHelper from TryFormat() routines without introducing dozens of non-code-coveraged "bytesWr |
| | | 109 | | // |
| | | 110 | | public static bool TryFormatThrowFormatException(out int bytesWritten) |
| | 1 | 111 | | { |
| | 1 | 112 | | bytesWritten = 0; |
| | 1 | 113 | | ThrowHelper.ThrowFormatException_BadFormatSpecifier(); |
| | 0 | 114 | | return false; |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | // |
| | | 118 | | // Enable use of ThrowHelper from TryParse() routines without introducing dozens of non-code-coveraged "value= d |
| | | 119 | | // |
| | | 120 | | public static bool TryParseThrowFormatException<T>(out T value, out int bytesConsumed) |
| | 1 | 121 | | { |
| | 1 | 122 | | value = default; |
| | 1 | 123 | | bytesConsumed = 0; |
| | 1 | 124 | | ThrowHelper.ThrowFormatException_BadFormatSpecifier(); |
| | 0 | 125 | | return false; |
| | 0 | 126 | | } |
| | | 127 | | |
| | | 128 | | // |
| | | 129 | | // ReadOnlySequence .ctor validation Throws coalesced to enable inlining of the .ctor |
| | | 130 | | // |
| | | 131 | | public static void ThrowArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int startIndex, |
| | 1 | 132 | | => throw CreateArgumentValidationException(startSegment, startIndex, endSegment); |
| | | 133 | | |
| | | 134 | | private static Exception CreateArgumentValidationException<T>(ReadOnlySequenceSegment<T> startSegment, int start |
| | 1 | 135 | | { |
| | 1 | 136 | | if (startSegment == null) |
| | 1 | 137 | | return CreateArgumentNullException(ExceptionArgument.startSegment); |
| | 1 | 138 | | else if (endSegment == null) |
| | 1 | 139 | | return CreateArgumentNullException(ExceptionArgument.endSegment); |
| | 1 | 140 | | else if ((uint)startSegment.Memory.Length < (uint)startIndex) |
| | 1 | 141 | | return CreateArgumentOutOfRangeException(ExceptionArgument.startIndex); |
| | | 142 | | else |
| | 1 | 143 | | return CreateArgumentOutOfRangeException(ExceptionArgument.endIndex); |
| | 1 | 144 | | } |
| | | 145 | | |
| | | 146 | | public static void ThrowArgumentValidationException(Array array, int start) |
| | 1 | 147 | | => throw CreateArgumentValidationException(array, start); |
| | | 148 | | |
| | | 149 | | private static Exception CreateArgumentValidationException(Array array, int start) |
| | 1 | 150 | | { |
| | 1 | 151 | | if (array == null) |
| | 1 | 152 | | return CreateArgumentNullException(ExceptionArgument.array); |
| | 1 | 153 | | else if ((uint)start > (uint)array.Length) |
| | 1 | 154 | | return CreateArgumentOutOfRangeException(ExceptionArgument.start); |
| | | 155 | | else |
| | 1 | 156 | | return CreateArgumentOutOfRangeException(ExceptionArgument.length); |
| | 1 | 157 | | } |
| | | 158 | | |
| | | 159 | | public static void ThrowArgumentValidationException<T>(OwnedMemory<T> ownedMemory, int start) |
| | 1 | 160 | | => throw CreateArgumentValidationException(ownedMemory, start); |
| | | 161 | | |
| | | 162 | | private static Exception CreateArgumentValidationException<T>(OwnedMemory<T> ownedMemory, int start) |
| | 1 | 163 | | { |
| | 1 | 164 | | if (ownedMemory == null) |
| | 1 | 165 | | return CreateArgumentNullException(ExceptionArgument.ownedMemory); |
| | 1 | 166 | | else if ((uint)start > (uint)ownedMemory.Length) |
| | 1 | 167 | | return CreateArgumentOutOfRangeException(ExceptionArgument.start); |
| | | 168 | | else |
| | 1 | 169 | | return CreateArgumentOutOfRangeException(ExceptionArgument.length); |
| | 1 | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | // |
| | | 174 | | // The convention for this enum is using the argument name as the enum name |
| | | 175 | | // |
| | | 176 | | internal enum ExceptionArgument |
| | | 177 | | { |
| | | 178 | | length, |
| | | 179 | | start, |
| | | 180 | | minimumBufferSize, |
| | | 181 | | byteOffset, |
| | | 182 | | comparable, |
| | | 183 | | comparer, |
| | | 184 | | destination, |
| | | 185 | | offset, |
| | | 186 | | startSegment, |
| | | 187 | | endSegment, |
| | | 188 | | startIndex, |
| | | 189 | | endIndex, |
| | | 190 | | array, |
| | | 191 | | ownedMemory, |
| | | 192 | | culture |
| | | 193 | | } |
| | | 194 | | } |