5 // Miguel de Icaza (miguel@ximian.com)
8 // Michael Lambert, michaellambert@email.com
10 // (C) Ximian, Inc. http://www.ximian.com
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.
19 // FIXME: How do you specify a native int in C#? I am going to have to do some figuring out
23 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
33 // The above copyright notice and this permission notice shall be
34 // included in all copies or substantial portions of the Software.
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.
45 using System.Globalization;
46 using System.Runtime.Serialization;
47 using System.Runtime.ConstrainedExecution;
48 using System.Diagnostics.Contracts;
53 [System.Runtime.InteropServices.ComVisible (true)]
54 public unsafe struct IntPtr : ISerializable
56 private void *m_value;
58 public static readonly IntPtr Zero;
60 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
61 public IntPtr (int value)
63 m_value = (void *) value;
66 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
67 public IntPtr (long value)
69 /* FIXME: Needs to figure the exact check which works on all architectures */
71 if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
72 throw new OverflowException (
73 Locale.GetText ("This isn't a 64bits machine."));
77 m_value = (void *) value;
80 [CLSCompliant (false)]
81 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
82 unsafe public IntPtr (void *value)
87 private IntPtr (SerializationInfo info, StreamingContext context)
89 long savedValue = info.GetInt64 ("value");
90 m_value = (void *) savedValue;
93 public static int Size {
94 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
96 return sizeof (void *);
100 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
103 throw new ArgumentNullException ("info");
105 info.AddValue ("value", ToInt64 ());
108 public override bool Equals (object obj)
110 if (!(obj is System.IntPtr))
113 return ((IntPtr) obj).m_value == m_value;
116 public override int GetHashCode ()
118 return (int) m_value;
121 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
122 public int ToInt32 ()
124 return (int) m_value;
127 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
128 public long ToInt64 ()
130 // pointer to long conversion is done using conv.u8 by the compiler
132 return (long)(int)m_value;
134 return (long)m_value;
137 [CLSCompliant (false)]
138 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
139 unsafe public void *ToPointer ()
144 override public string ToString ()
146 return ToString (null);
149 public string ToString (string format)
152 return ((int) m_value).ToString (format, null);
154 return ((long) m_value).ToString (format, null);
157 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
158 public static bool operator == (IntPtr value1, IntPtr value2)
160 return (value1.m_value == value2.m_value);
163 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
164 public static bool operator != (IntPtr value1, IntPtr value2)
166 return (value1.m_value != value2.m_value);
169 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
170 public static explicit operator IntPtr (int value)
172 return new IntPtr (value);
175 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
176 public static explicit operator IntPtr (long value)
178 return new IntPtr (value);
181 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
182 [CLSCompliant (false)]
183 unsafe public static explicit operator IntPtr (void *value)
185 return new IntPtr (value);
188 public static explicit operator int (IntPtr value)
190 return (int) value.m_value;
193 public static explicit operator long (IntPtr value)
195 return value.ToInt64 ();
198 [CLSCompliant (false)]
199 unsafe public static explicit operator void * (IntPtr value)
201 return value.m_value;
204 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
205 public static IntPtr Add (IntPtr pointer, int offset)
207 return (IntPtr) (unchecked (((byte *) pointer) + offset));
210 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
211 public static IntPtr Subtract (IntPtr pointer, int offset)
213 return (IntPtr) (unchecked (((byte *) pointer) - offset));
216 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
217 public static IntPtr operator + (IntPtr pointer, int offset)
219 return (IntPtr) (unchecked (((byte *) pointer) + offset));
222 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
223 public static IntPtr operator - (IntPtr pointer, int offset)
225 return (IntPtr) (unchecked (((byte *) pointer) - offset));
228 // fast way to compare IntPtr to (IntPtr)0 while IntPtr.Zero doesn't work due to slow statics access
230 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
231 internal unsafe bool IsNull()
233 return m_value == null;