Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / corlib / System / IntPtr.cs
index c75905ebf7e295b0831aadd5c1cae916576ba114..c449de83997a3b308b1df8fbe881eb053f166dc3 100644 (file)
 
 using System.Globalization;
 using System.Runtime.Serialization;
-
-#if NET_2_0
 using System.Runtime.ConstrainedExecution;
-#endif
+using System.Diagnostics.Contracts;
 
 namespace System
 {
        [Serializable]
-#if NET_2_0
        [System.Runtime.InteropServices.ComVisible (true)]
-#endif
        public unsafe struct IntPtr : ISerializable
        {
                private void *m_value;
 
                public static readonly IntPtr Zero;
 
-#if NET_2_0
                [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif
                public IntPtr (int value)
                {
                        m_value = (void *) value;
                }
 
-#if NET_2_0
                [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif
                public IntPtr (long value)
                {
                        /* FIXME: Needs to figure the exact check which works on all architectures */
@@ -86,9 +78,7 @@ namespace System
                }
 
                [CLSCompliant (false)]
-#if NET_2_0
                [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif
                unsafe public IntPtr (void *value)
                {
                        m_value = value;
@@ -101,9 +91,7 @@ namespace System
                }
 
                public static int Size {
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                        get {
                                return sizeof (void *);
                        }
@@ -130,17 +118,13 @@ namespace System
                        return (int) m_value;
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public int ToInt32 ()
                {
                        return (int) m_value;
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public long ToInt64 ()
                {
                        // pointer to long conversion is done using conv.u8 by the compiler
@@ -151,9 +135,7 @@ namespace System
                }
 
                [CLSCompliant (false)]
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                unsafe public void *ToPointer ()
                {
                        return m_value;
@@ -164,52 +146,39 @@ namespace System
                        return ToString (null);
                }
 
-#if NET_2_0
-               public
-#endif
-               string ToString (string format)
+               public string ToString (string format)
                {
                        if (Size == 4)
-                               return ((int) m_value).ToString (format);
+                               return ((int) m_value).ToString (format, null);
                        else
-                               return ((long) m_value).ToString (format);
+                               return ((long) m_value).ToString (format, null);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static bool operator == (IntPtr value1, IntPtr value2)
                {
                        return (value1.m_value == value2.m_value);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
-#endif
                public static bool operator != (IntPtr value1, IntPtr value2)
                {
                        return (value1.m_value != value2.m_value);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif
                public static explicit operator IntPtr (int value)
                {
                        return new IntPtr (value);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif
                public static explicit operator IntPtr (long value)
                {
                        return new IntPtr (value);
                }
 
-#if NET_2_0
                [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
-#endif         
                [CLSCompliant (false)]
                unsafe public static explicit operator IntPtr (void *value)
                {
@@ -231,5 +200,37 @@ namespace System
                {
                        return value.m_value;
                }
+
+               [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
+               public static IntPtr Add (IntPtr pointer, int offset)
+               {
+                       return (IntPtr) (unchecked (((byte *) pointer) + offset));
+               }
+
+               [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
+               public static IntPtr Subtract (IntPtr pointer, int offset)
+               {
+                       return (IntPtr) (unchecked (((byte *) pointer) - offset));
+               }
+
+               [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
+               public static IntPtr operator + (IntPtr pointer, int offset)
+               {
+                       return (IntPtr) (unchecked (((byte *) pointer) + offset));
+               }
+
+               [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
+               public static IntPtr operator - (IntPtr pointer, int offset)
+               {
+                       return (IntPtr) (unchecked (((byte *) pointer) - offset));
+               }
+
+               // fast way to compare IntPtr to (IntPtr)0 while IntPtr.Zero doesn't work due to slow statics access
+               [Pure]
+               [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
+               internal unsafe bool IsNull()
+               {
+                       return m_value == null;
+               }
        }
 }