[System] Collections from CoreFX
[mono.git] / mcs / class / corlib / System.Runtime.CompilerServices / RuntimeHelpers.cs
1 // System.Runtime.CompilerServices.RuntimeHelpers
2 //
3 // Sean MacIsaac (macisaac@ximian.com)
4 // Paolo Molaro (lupus@ximian.com)
5 //
6 // (C) Ximian, Inc. 2001
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Runtime.Versioning;
32 using System.Runtime.ConstrainedExecution;
33 using System.Reflection;
34
35 namespace System.Runtime.CompilerServices
36 {
37         public static class RuntimeHelpers
38         {
39                 public delegate void TryCode (Object userData);
40
41                 public delegate void CleanupCode (Object userData, bool exceptionThrown);
42
43                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44                 static extern void InitializeArray (Array array, IntPtr fldHandle);
45
46                 public static void InitializeArray (Array array, RuntimeFieldHandle fldHandle)
47                 {
48                         if ((array == null) || (fldHandle.Value == IntPtr.Zero))
49                                 throw new ArgumentNullException ();
50
51                         InitializeArray (array, fldHandle.Value);
52                 }
53
54                 public static extern int OffsetToStringData {
55                         [MethodImpl (MethodImplOptions.InternalCall)]
56                         get;
57                 }
58
59                 public static int GetHashCode (object o) {
60                         return Object.InternalGetHashCode (o);
61                 }
62
63                 public static new bool Equals (object o1, object o2) {
64                         // LAMESPEC: According to MSDN, this is equivalent to 
65                         // Object::Equals (). But the MS version of Object::Equals()
66                         // includes the functionality of ValueType::Equals(), while
67                         // our version does not.
68                         if (o1 == o2)
69                                 return true;
70                         if ((o1 == null) || (o2 == null))
71                                 return false;
72                         if (o1 is ValueType)
73                                 return ValueType.DefaultEquals (o1, o2);
74                         else
75                                 return Object.Equals (o1, o2);
76                 }
77
78                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
79                 public static extern object GetObjectValue (object obj);
80
81                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
82                 static extern void RunClassConstructor (IntPtr type);
83
84                 public static void RunClassConstructor (RuntimeTypeHandle type)
85                 {
86                         if (type.Value == IntPtr.Zero)
87                                 throw new ArgumentException ("Handle is not initialized.", "type");
88
89                         RunClassConstructor (type.Value);
90                 }
91
92                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
93                 static extern bool SufficientExecutionStack ();
94
95                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
96                 public static void EnsureSufficientExecutionStack ()
97                 {
98                         if (SufficientExecutionStack ())
99                                 return;
100                         throw new InsufficientExecutionStackException ();
101                 }
102
103                 public static bool TryEnsureSufficientExecutionStack ()
104                 {
105                         return SufficientExecutionStack ();
106                 }
107
108                 [MonoTODO("Currently a no-op")]
109                 public static void ExecuteCodeWithGuaranteedCleanup (TryCode code, CleanupCode backoutCode, Object userData)
110                 {
111                 }
112
113                 [MonoTODO("Currently a no-op")]
114                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
115                 public static void PrepareConstrainedRegions ()
116                 {
117                 }
118
119                 [MonoTODO("Currently a no-op")]
120                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
121                 public static void PrepareConstrainedRegionsNoOP ()
122                 {
123                 }
124
125                 [MonoTODO("Currently a no-op")]
126                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.MayFail)]
127                 public static void ProbeForSufficientStack()
128                 {
129                 }
130
131                 // This method triggers a given delegate to be prepared.  This involves preparing the
132                 // delegate's Invoke method and preparing the target of that Invoke.  In the case of
133                 // a multi-cast delegate, we rely on the fact that each individual component was prepared
134                 // prior to the Combine.  In other words, this service does not navigate through the
135                 // entire multicasting list.
136                 // If our own reliable event sinks perform the Combine (for example AppDomain.DomainUnload),
137                 // then the result is fully prepared.  But if a client calls Combine himself and then
138                 // then adds that combination to e.g. AppDomain.DomainUnload, then the client is responsible
139                 // for his own preparation.
140                 [System.Security.SecurityCritical]  // auto-generated_required
141                 [MonoTODO("Currently a no-op")]
142                 public static void PrepareDelegate (Delegate d)
143                 {
144                         if (d == null)
145                                 throw new ArgumentNullException ("d");
146                 }
147
148                 // extracted from ../../../../external/referencesource/mscorlib/system/runtime/compilerservices/runtimehelpers.cs
149                 //
150                 // See comment above for PrepareDelegate
151                 //
152                 // PrepareContractedDelegate weakens this a bit by only assuring that we prepare 
153                 // delegates which also have a ReliabilityContract. This is useful for services that
154                 // want to provide opt-in reliability, generally some random event sink providing
155                 // always reliable semantics to random event handlers that are likely to have not
156                 // been written with relability in mind is a lost cause anyway.
157                 //
158                 // NOTE: that for the NGen case you can sidestep the required ReliabilityContract
159                 // by using the [PrePrepareMethod] attribute.
160                 [System.Security.SecurityCritical]  // auto-generated_required
161                 [ResourceExposure(ResourceScope.None)]
162                 [MonoTODO("Currently a no-op")]
163                 public static void PrepareContractedDelegate(Delegate d)
164                 {
165                 }
166
167                 [MonoTODO("Currently a no-op")]
168                 public static void PrepareMethod (RuntimeMethodHandle method)
169                 {
170                 }
171
172                 [MonoTODO("Currently a no-op")]
173                 public static void PrepareMethod (RuntimeMethodHandle method, RuntimeTypeHandle[] instantiation)
174                 {
175                 }
176
177                 public static void RunModuleConstructor (ModuleHandle module)
178                 {
179                         if (module == ModuleHandle.EmptyHandle)
180                                 throw new ArgumentException ("Handle is not initialized.", "module");
181
182                         RunModuleConstructor (module.Value);
183                 }
184
185                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
186                 static extern void RunModuleConstructor (IntPtr module);
187
188                 [MonoTODO]
189                 public static bool IsReferenceOrContainsReferences<T>()
190                 {
191                         return true;
192                 }
193         }
194 }