Merge pull request #2802 from BrzVlad/feature-evacuation-opt2
[mono.git] / mcs / class / corlib / System.Runtime.InteropServices / GCHandle.cs
1 //
2 // System.Runtime.InteropServices/GCHandle.cs
3 //
4 // Authors:
5 //   Ajay kumar Dwivedi (adwiv@yahoo.com) ??
6 //   Paolo Molaro (lupus@ximian.com)
7 //
8
9 //
10 // Copyright (C) 2004, 2009 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Threading;
34 using System.Runtime.CompilerServices;
35 using System.Runtime.InteropServices;
36
37 namespace System.Runtime.InteropServices
38 {
39
40         [ComVisible(true)]
41         [MonoTODO("Struct should be [StructLayout(LayoutKind.Sequential)] but will need to be reordered for that.")]
42         public struct GCHandle 
43         {
44                 // fields
45                 private int handle;
46
47                 private GCHandle(IntPtr h)
48                 {
49                         handle = (int)h;
50                 }
51                 
52                 // Constructors
53                 private GCHandle(object obj)
54                         : this(obj, GCHandleType.Normal)
55                 {}
56
57                 internal GCHandle(object value, GCHandleType type)
58                 {
59                         // MS does not crash/throw on (most) invalid GCHandleType values (except -1)
60                         if ((type < GCHandleType.Weak) || (type > GCHandleType.Pinned))
61                                 type = GCHandleType.Normal;
62                         handle = GetTargetHandle (value, 0, type);
63                 }
64
65                 // Properties
66
67                 public bool IsAllocated 
68                 { 
69                         get
70                         {
71                                 return (handle != 0);
72                         }
73                 }
74
75                 public object Target
76                 { 
77                         get
78                         {
79                                 if (!IsAllocated)
80                                         throw new InvalidOperationException (Locale.GetText ("Handle is not allocated"));
81                                 return GetTarget (handle);
82                         } 
83                         set
84                         {
85                                 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
86                         } 
87                 }
88
89                 // Methods
90                 public IntPtr AddrOfPinnedObject()
91                 {
92                         IntPtr res = GetAddrOfPinnedObject(handle);
93                         if (res == (IntPtr)(-1))
94                                 throw new ArgumentException ("Object contains non-primitive or non-blittable data.");
95                         if (res == (IntPtr)(-2))
96                                 throw new InvalidOperationException("Handle is not pinned.");
97                         return res;
98                 }
99
100                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
101                 {
102                         return new GCHandle (value);
103                 }
104
105                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
106                 {
107                         return new GCHandle (value,type);
108                 }
109
110                 public void Free()
111                 {
112                         // Copy the handle instance member to a local variable. This is required to prevent
113                         // race conditions releasing the handle.
114                         int local_handle = handle;
115
116                         // Free the handle if it hasn't already been freed.
117                         if (local_handle != 0 && Interlocked.CompareExchange (ref handle, 0, local_handle) == local_handle) {
118                                 FreeHandle (local_handle);
119                         }
120                         else {
121                                 throw new InvalidOperationException ("Handle is not initialized.");
122                         }
123                 }
124                 
125                 public static explicit operator IntPtr (GCHandle value)
126                 {
127                         return (IntPtr) value.handle;
128                 }
129                 
130                 public static explicit operator GCHandle(IntPtr value)
131                 {
132                         if (value == IntPtr.Zero)
133                                 throw new InvalidOperationException ("GCHandle value cannot be zero");
134                         if (!CheckCurrentDomain ((int)value))
135                                 throw new ArgumentException ("GCHandle value belongs to a different domain");
136                         return new GCHandle (value);
137                 }
138
139                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
140                 private extern static bool CheckCurrentDomain (int handle);
141                 
142                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
143                 private extern static object GetTarget(int handle);
144
145                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
146                 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
147                 
148                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
149                 private extern static void FreeHandle(int handle);
150                 
151                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
152                 private extern static IntPtr GetAddrOfPinnedObject(int handle);
153
154                 public static bool operator ==(GCHandle a, GCHandle b)
155                 {
156                         return a.handle == b.handle;
157                 }
158
159                 public static bool operator !=(GCHandle a, GCHandle b)
160                 {
161                         return !(a == b);
162                 }
163                 
164                 public override bool Equals(object o)
165                 {
166                         return o is GCHandle ? this == (GCHandle)o : false;
167                 }
168
169                 public override int GetHashCode()
170                 {
171                         return handle.GetHashCode ();
172                 }
173
174                 public static GCHandle FromIntPtr (IntPtr value)
175                 {
176                         return (GCHandle)value;
177                 }
178
179                 public static IntPtr ToIntPtr (GCHandle value)
180                 {
181                         return (IntPtr)value;
182                 }
183         } 
184 }
185