[corlib] Fixed StringBuilder construction bugs in marshalling caused by changes to...
[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.Runtime.CompilerServices;
34 using System.Runtime.InteropServices;
35
36 namespace System.Runtime.InteropServices
37 {
38
39         [ComVisible(true)]
40         [MonoTODO("Struct should be [StructLayout(LayoutKind.Sequential)] but will need to be reordered for that.")]
41         public struct GCHandle 
42         {
43                 // fields
44                 private int handle;
45
46                 private GCHandle(IntPtr h)
47                 {
48                         handle = (int)h;
49                 }
50                 
51                 // Constructors
52                 private GCHandle(object obj)
53                         : this(obj, GCHandleType.Normal)
54                 {}
55
56                 private GCHandle(object value, GCHandleType type)
57                 {
58                         // MS does not crash/throw on (most) invalid GCHandleType values (except -1)
59                         if ((type < GCHandleType.Weak) || (type > GCHandleType.Pinned))
60                                 type = GCHandleType.Normal;
61                         handle = GetTargetHandle (value, 0, type);
62                 }
63
64                 // Properties
65
66                 public bool IsAllocated 
67                 { 
68                         get
69                         {
70                                 return (handle != 0);
71                         }
72                 }
73
74                 public object Target
75                 { 
76                         get
77                         {
78                                 if (!IsAllocated)
79                                         throw new InvalidOperationException (Locale.GetText ("Handle is not allocated"));
80                                 return GetTarget (handle);
81                         } 
82                         set
83                         {
84                                 handle = GetTargetHandle (value, handle, (GCHandleType)(-1));
85                         } 
86                 }
87
88                 // Methods
89                 public IntPtr AddrOfPinnedObject()
90                 {
91                         IntPtr res = GetAddrOfPinnedObject(handle);
92                         if (res == (IntPtr)(-1))
93                                 throw new ArgumentException ("Object contains non-primitive or non-blittable data.");
94                         if (res == (IntPtr)(-2))
95                                 throw new InvalidOperationException("Handle is not pinned.");
96                         return res;
97                 }
98
99                 public static System.Runtime.InteropServices.GCHandle Alloc(object value)
100                 {
101                         return new GCHandle (value);
102                 }
103
104                 public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
105                 {
106                         return new GCHandle (value,type);
107                 }
108
109                 public void Free()
110                 {
111                         FreeHandle(handle);
112                         handle = 0;
113                 }
114                 
115                 public static explicit operator IntPtr (GCHandle value)
116                 {
117                         return (IntPtr) value.handle;
118                 }
119                 
120                 public static explicit operator GCHandle(IntPtr value)
121                 {
122                         if (value == IntPtr.Zero)
123                                 throw new ArgumentException ("GCHandle value cannot be zero");
124                         if (!CheckCurrentDomain ((int)value))
125                                 throw new ArgumentException ("GCHandle value belongs to a different domain");
126                         return new GCHandle (value);
127                 }
128
129                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
130                 private extern static bool CheckCurrentDomain (int handle);
131                 
132                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
133                 private extern static object GetTarget(int handle);
134
135                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
136                 private extern static int GetTargetHandle(object obj, int handle, GCHandleType type);
137                 
138                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139                 private extern static void FreeHandle(int handle);
140                 
141                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
142                 private extern static IntPtr GetAddrOfPinnedObject(int handle);
143
144                 public static bool operator ==(GCHandle a, GCHandle b)
145                 {
146                         return a.handle == b.handle;
147                 }
148
149                 public static bool operator !=(GCHandle a, GCHandle b)
150                 {
151                         return !(a == b);
152                 }
153                 
154                 public override bool Equals(object o)
155                 {
156                         return o is GCHandle ? this == (GCHandle)o : false;
157                 }
158
159                 public override int GetHashCode()
160                 {
161                         return handle.GetHashCode ();
162                 }
163
164                 public static GCHandle FromIntPtr (IntPtr value)
165                 {
166                         return (GCHandle)value;
167                 }
168
169                 public static IntPtr ToIntPtr (GCHandle value)
170                 {
171                         return (IntPtr)value;
172                 }
173         } 
174 }
175