[corlib] Fixed change in 4b4ddb66b3 that prevented use of remoting fast path.
[mono.git] / mcs / class / corlib / System / IntPtr.cs
1 //
2 // System.IntPtr.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // Maintainer:
8 //       Michael Lambert, michaellambert@email.com
9 //
10 // (C) Ximian, Inc.  http://www.ximian.com
11 //
12 // Remarks:
13 //   Requires '/unsafe' compiler option.  This class uses void*,
14 //   in overloaded constructors, conversion, and cast members in 
15 //   the public interface.  Using pointers is not valid CLS and 
16 //   the methods in question have been marked with  the 
17 //   CLSCompliant attribute that avoid compiler warnings.
18 //
19 // FIXME: How do you specify a native int in C#?  I am going to have to do some figuring out
20 //
21
22 //
23 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
24 //
25 // Permission is hereby granted, free of charge, to any person obtaining
26 // a copy of this software and associated documentation files (the
27 // "Software"), to deal in the Software without restriction, including
28 // without limitation the rights to use, copy, modify, merge, publish,
29 // distribute, sublicense, and/or sell copies of the Software, and to
30 // permit persons to whom the Software is furnished to do so, subject to
31 // the following conditions:
32 // 
33 // The above copyright notice and this permission notice shall be
34 // included in all copies or substantial portions of the Software.
35 // 
36 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
40 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
41 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
42 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 //
44
45 using System.Globalization;
46 using System.Runtime.Serialization;
47 using System.Runtime.ConstrainedExecution;
48
49 namespace System
50 {
51         [Serializable]
52         [System.Runtime.InteropServices.ComVisible (true)]
53         public unsafe struct IntPtr : ISerializable
54         {
55                 private void *m_value;
56
57                 public static readonly IntPtr Zero;
58
59                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
60                 public IntPtr (int value)
61                 {
62                         m_value = (void *) value;
63                 }
64
65                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
66                 public IntPtr (long value)
67                 {
68                         /* FIXME: Needs to figure the exact check which works on all architectures */
69                         /*
70                         if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
71                                 throw new OverflowException (
72                                         Locale.GetText ("This isn't a 64bits machine."));
73                         }
74                         */
75
76                         m_value = (void *) value;
77                 }
78
79                 [CLSCompliant (false)]
80                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
81                 unsafe public IntPtr (void *value)
82                 {
83                         m_value = value;
84                 }
85
86                 private IntPtr (SerializationInfo info, StreamingContext context)
87                 {
88                         long savedValue = info.GetInt64 ("value");
89                         m_value = (void *) savedValue;
90                 }
91
92                 public static int Size {
93                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
94                         get {
95                                 return sizeof (void *);
96                         }
97                 }
98
99                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
100                 {
101                         if (info == null)
102                                 throw new ArgumentNullException ("info");
103
104                         info.AddValue ("value", ToInt64 ());
105                 }
106
107                 public override bool Equals (object obj)
108                 {
109                         if (!(obj is System.IntPtr))
110                                 return false;
111
112                         return ((IntPtr) obj).m_value == m_value;
113                 }
114
115                 public override int GetHashCode ()
116                 {
117                         return (int) m_value;
118                 }
119
120                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
121                 public int ToInt32 ()
122                 {
123                         return (int) m_value;
124                 }
125
126                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
127                 public long ToInt64 ()
128                 {
129                         // pointer to long conversion is done using conv.u8 by the compiler
130                         if (Size == 4)
131                                 return (long)(int)m_value;
132                         else
133                                 return (long)m_value;
134                 }
135
136                 [CLSCompliant (false)]
137                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
138                 unsafe public void *ToPointer ()
139                 {
140                         return m_value;
141                 }
142
143                 override public string ToString ()
144                 {
145                         return ToString (null);
146                 }
147
148                 public string ToString (string format)
149                 {
150                         if (Size == 4)
151                                 return ((int) m_value).ToString (format, null);
152                         else
153                                 return ((long) m_value).ToString (format, null);
154                 }
155
156                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
157                 public static bool operator == (IntPtr value1, IntPtr value2)
158                 {
159                         return (value1.m_value == value2.m_value);
160                 }
161
162                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
163                 public static bool operator != (IntPtr value1, IntPtr value2)
164                 {
165                         return (value1.m_value != value2.m_value);
166                 }
167
168                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
169                 public static explicit operator IntPtr (int value)
170                 {
171                         return new IntPtr (value);
172                 }
173
174                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
175                 public static explicit operator IntPtr (long value)
176                 {
177                         return new IntPtr (value);
178                 }
179
180                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
181                 [CLSCompliant (false)]
182                 unsafe public static explicit operator IntPtr (void *value)
183                 {
184                         return new IntPtr (value);
185                 }
186
187                 public static explicit operator int (IntPtr value)
188                 {
189                         return (int) value.m_value;
190                 }
191
192                 public static explicit operator long (IntPtr value)
193                 {
194                         return value.ToInt64 ();
195                 }
196
197                 [CLSCompliant (false)]
198                 unsafe public static explicit operator void * (IntPtr value)
199                 {
200                         return value.m_value;
201                 }
202
203                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
204                 public static IntPtr Add (IntPtr pointer, int offset)
205                 {
206                         return (IntPtr) (unchecked (((byte *) pointer) + offset));
207                 }
208
209                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
210                 public static IntPtr Subtract (IntPtr pointer, int offset)
211                 {
212                         return (IntPtr) (unchecked (((byte *) pointer) - offset));
213                 }
214
215                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
216                 public static IntPtr operator + (IntPtr pointer, int offset)
217                 {
218                         return (IntPtr) (unchecked (((byte *) pointer) + offset));
219                 }
220
221                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
222                 public static IntPtr operator - (IntPtr pointer, int offset)
223                 {
224                         return (IntPtr) (unchecked (((byte *) pointer) - offset));
225                 }
226         }
227 }