Update Range.GetOffsetAndLength (#23855)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / shared / System / Runtime / CompilerServices / RuntimeHelpers.cs
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.Serialization;
6 using Internal.Runtime.CompilerServices;
7
8 namespace System.Runtime.CompilerServices
9 {
10     public static partial class RuntimeHelpers
11     {
12         public delegate void TryCode(object userData);
13
14         public delegate void CleanupCode(object userData, bool exceptionThrown);
15
16         /// <summary>
17         /// Slices the specified array using the specified range.
18         /// </summary>
19         public static T[] GetSubArray<T>(T[] array, Range range)
20         {
21             if (array == null)
22             {
23                 ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
24             }
25
26             (int offset, int length) = range.GetOffsetAndLength(array.Length);
27
28             if (default(T) != null || typeof(T[]) == array.GetType())
29             {
30                 // We know the type of the array to be exactly T[].
31
32                 if (length == 0)
33                 {
34                     return Array.Empty<T>();
35                 }
36
37                 var dest = new T[length];
38                 Buffer.Memmove(
39                     ref Unsafe.As<byte, T>(ref dest.GetRawSzArrayData()),
40                     ref Unsafe.Add(ref Unsafe.As<byte, T>(ref array.GetRawSzArrayData()), offset),
41                     (uint)length);
42                 return dest;
43             }
44             else
45             {
46                 // The array is actually a U[] where U:T.
47                 T[] dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length);
48                 Array.Copy(array, offset, dest, 0, length);
49                 return dest;
50             }
51         }
52
53         public static object GetUninitializedObject(Type type)
54         {
55             if (type is null)
56             {
57                 throw new ArgumentNullException(nameof(type), SR.ArgumentNull_Type);
58             }
59
60             if (!type.IsRuntimeImplemented())
61             {
62                 throw new SerializationException(SR.Format(SR.Serialization_InvalidType, type.ToString()));
63             }
64
65             return GetUninitializedObjectInternal(type);
66         }
67
68         public static void PrepareContractedDelegate(Delegate d)
69         {
70         }
71
72         public static void ProbeForSufficientStack()
73         {
74         }
75
76         public static void PrepareConstrainedRegions()
77         {
78         }
79
80         public static void PrepareConstrainedRegionsNoOP()
81         {
82         }
83     }
84 }