b340e4ac713f3c78ac4df92665e28f9b279ae3fe
[platform/upstream/coreclr.git] / src / mscorlib / src / 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 ////////////////////////////////////////////////////////////////////////////////
6 ////////////////////////////////////////////////////////////////////////////////
7 //
8 // RuntimeHelpers
9 //    This class defines a set of static methods that provide support for compilers.
10 //
11 //
12
13 namespace System.Runtime.CompilerServices
14 {
15     using System;
16     using System.Security;
17     using System.Runtime;
18     using System.Runtime.CompilerServices;
19     using System.Runtime.InteropServices;
20     using System.Runtime.ConstrainedExecution;
21     using System.Runtime.Serialization;
22     using System.Threading;
23     using System.Runtime.Versioning;
24
25     public static class RuntimeHelpers
26     {
27         // Exposed here as a more appropriate place than on FormatterServices itself,
28         // which is a high level reflection heavy type.
29         public static Object GetUninitializedObject(Type type)
30         {
31             return FormatterServices.GetUninitializedObject(type);
32         }
33
34         [MethodImplAttribute(MethodImplOptions.InternalCall)]
35         public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle);
36
37         // GetObjectValue is intended to allow value classes to be manipulated as 'Object'
38         // but have aliasing behavior of a value class.  The intent is that you would use
39         // this function just before an assignment to a variable of type 'Object'.  If the
40         // value being assigned is a mutable value class, then a shallow copy is returned 
41         // (because value classes have copy semantics), but otherwise the object itself
42         // is returned.  
43         //
44         // Note: VB calls this method when they're about to assign to an Object
45         // or pass it as a parameter.  The goal is to make sure that boxed 
46         // value types work identical to unboxed value types - ie, they get 
47         // cloned when you pass them around, and are always passed by value.  
48         // Of course, reference types are not cloned.
49         //
50         [MethodImplAttribute(MethodImplOptions.InternalCall)]
51         public static extern Object GetObjectValue(Object obj);
52
53         // RunClassConstructor causes the class constructor for the given type to be triggered
54         // in the current domain.  After this call returns, the class constructor is guaranteed to
55         // have at least been started by some thread.  In the absence of class constructor
56         // deadlock conditions, the call is further guaranteed to have completed.
57         //
58         // This call will generate an exception if the specified class constructor threw an 
59         // exception when it ran. 
60
61         [MethodImplAttribute(MethodImplOptions.InternalCall)]
62         private static extern void _RunClassConstructor(RuntimeType type);
63
64         public static void RunClassConstructor(RuntimeTypeHandle type)
65         {
66             _RunClassConstructor(type.GetRuntimeType());
67         }
68
69         // RunModuleConstructor causes the module constructor for the given type to be triggered
70         // in the current domain.  After this call returns, the module constructor is guaranteed to
71         // have at least been started by some thread.  In the absence of module constructor
72         // deadlock conditions, the call is further guaranteed to have completed.
73         //
74         // This call will generate an exception if the specified module constructor threw an 
75         // exception when it ran. 
76
77         [MethodImplAttribute(MethodImplOptions.InternalCall)]
78         private static extern void _RunModuleConstructor(System.Reflection.RuntimeModule module);
79
80         public static void RunModuleConstructor(ModuleHandle module)
81         {
82             _RunModuleConstructor(module.GetRuntimeModule());
83         }
84
85
86         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
87         internal static extern void _CompileMethod(IRuntimeMethodInfo method);
88
89         public static void PrepareMethod(RuntimeMethodHandle method) { }
90         public static void PrepareMethod(RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation) { }
91         public static void PrepareContractedDelegate(Delegate d) { }
92
93         public static void PrepareDelegate(Delegate d)
94         {
95             if (d == null)
96             {
97                 throw new ArgumentNullException("d");
98             }
99         }
100
101         [MethodImplAttribute(MethodImplOptions.InternalCall)]
102         public static extern int GetHashCode(Object o);
103
104         [MethodImplAttribute(MethodImplOptions.InternalCall)]
105         public new static extern bool Equals(Object o1, Object o2);
106
107         public static int OffsetToStringData
108         {
109             // This offset is baked in by string indexer intrinsic, so there is no harm
110             // in getting it baked in here as well.
111             [System.Runtime.Versioning.NonVersionable]
112             get
113             {
114                 // Number of bytes from the address pointed to by a reference to
115                 // a String to the first 16-bit character in the String.  Skip 
116                 // over the MethodTable pointer, & String 
117                 // length.  Of course, the String reference points to the memory 
118                 // after the sync block, so don't count that.  
119                 // This property allows C#'s fixed statement to work on Strings.
120                 // On 64 bit platforms, this should be 12 (8+4) and on 32 bit 8 (4+4).
121 #if BIT64
122                 return 12;
123 #else // 32
124                 return 8;
125 #endif // BIT64
126             }
127         }
128
129         // This method ensures that there is sufficient stack to execute the average Framework function.
130         // If there is not enough stack, then it throws System.InsufficientExecutionStackException.
131         // Note: this method is not part of the CER support, and is not to be confused with ProbeForSufficientStack
132         // below.
133         [MethodImplAttribute(MethodImplOptions.InternalCall)]
134         public static extern void EnsureSufficientExecutionStack();
135
136         // This method ensures that there is sufficient stack to execute the average Framework function.
137         // If there is not enough stack, then it return false.
138         // Note: this method is not part of the CER support, and is not to be confused with ProbeForSufficientStack
139         // below.
140         [MethodImplAttribute(MethodImplOptions.InternalCall)]
141         public static extern bool TryEnsureSufficientExecutionStack();
142
143         public static void ProbeForSufficientStack()
144         {
145         }
146
147         // This method is a marker placed immediately before a try clause to mark the corresponding catch and finally blocks as
148         // constrained. There's no code here other than the probe because most of the work is done at JIT time when we spot a call to this routine.
149         public static void PrepareConstrainedRegions()
150         {
151             ProbeForSufficientStack();
152         }
153
154         // When we detect a CER with no calls, we can point the JIT to this non-probing version instead
155         // as we don't need to probe.
156         public static void PrepareConstrainedRegionsNoOP()
157         {
158         }
159
160         public delegate void TryCode(Object userData);
161
162         public delegate void CleanupCode(Object userData, bool exceptionThrown);
163
164         [MethodImplAttribute(MethodImplOptions.InternalCall)]
165         public static extern void ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData);
166
167         internal static void ExecuteBackoutCodeHelper(Object backoutCode, Object userData, bool exceptionThrown)
168         {
169             ((CleanupCode)backoutCode)(userData, exceptionThrown);
170         }
171
172         /// <returns>true if given type is reference type or value type that contains references</returns>
173         static public bool IsReferenceOrContainsReferences<T>()
174         {
175             // The body of this function will be replaced by the EE with unsafe code!!!
176             // See getILIntrinsicImplementation for how this happens.
177             throw new InvalidOperationException();
178         }
179     }
180 }
181