[sgen] Clear the card table in the finishing pause
[mono.git] / mcs / class / corlib / System / UIntPtr.cs
1 //
2 // System.UIntPtr.cs
3 //
4 // Author:
5 //   Michael Lambert (michaellambert@email.com)
6 //
7 // (C) 2001 Michael Lambert, All Rights Reserved
8 //
9 // Remarks:
10 // Requires '/unsafe' compiler option.  This class uses void*,
11 // ulong, and uint in overloaded constructors, conversion, and
12 // cast members in the public interface.  Using pointers is not
13 // valid CLS and the methods in question have been marked with
14 // the CLSCompliant attribute that avoid compiler warnings.
15
16 //
17 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
18 //
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
26 // 
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
29 // 
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 //
38
39 using System.Globalization;
40 using System.Runtime.Serialization;
41 using System.Runtime.InteropServices;
42 using System.Runtime.ConstrainedExecution;
43
44 namespace System
45 {
46         [Serializable]
47         [CLSCompliant (false)]
48         [System.Runtime.InteropServices.ComVisible (true)]
49         public unsafe struct UIntPtr : ISerializable
50         {
51                 public static readonly UIntPtr Zero = new UIntPtr (0u);
52                 private void* _pointer;
53         
54                 public UIntPtr (ulong value)
55                 {
56                         if ((value > UInt32.MaxValue) && (UIntPtr.Size < 8)) {
57                                 throw new OverflowException (
58                                         Locale.GetText ("This isn't a 64bits machine."));
59                         }
60
61                         _pointer = (void*) value;
62                 }
63                 
64                 public UIntPtr (uint value)
65                 {
66                         _pointer = (void*)value;
67                 }
68         
69                 [CLSCompliant (false)]
70                 public unsafe UIntPtr (void* value)
71                 {
72                         _pointer = value;
73                 }
74         
75                 public override bool Equals (object obj)
76                 {
77                         if( obj is UIntPtr ) {
78                                 UIntPtr obj2 = (UIntPtr)obj;
79                                 return this._pointer == obj2._pointer;
80                         }
81                         return false;
82                 }
83
84                 public override int GetHashCode ()
85                 {
86                         return (int)_pointer;
87                 }
88
89                 public uint ToUInt32 ()
90                 {
91                         return (uint) _pointer;
92                 }
93
94                 public ulong ToUInt64 ()
95                 {
96                         return (ulong) _pointer;
97                 }
98
99                 [CLSCompliant (false)]
100                 public unsafe void* ToPointer ()
101                 {
102                         return _pointer;
103                 }
104
105                 public override string ToString ()
106                 {
107                         return ((uint) _pointer).ToString();
108                 }
109
110                 // Interface ISerializable
111                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
112                 {
113                         if (info == null)
114                                 throw new ArgumentNullException ("info");
115
116                         info.AddValue ("pointer", (ulong)_pointer);
117                 }
118
119                 public static bool operator == (UIntPtr value1, UIntPtr value2)
120                 {
121                         return value1._pointer == value2._pointer;
122                 }
123
124                 public static bool operator != (UIntPtr value1, UIntPtr value2)
125                 {
126                         return value1._pointer != value2._pointer;
127                 }
128
129                 public static explicit operator ulong (UIntPtr value)
130                 {
131                         return (ulong)value._pointer;
132                 }
133
134                 public static explicit operator uint (UIntPtr value)
135                 {
136                         return (uint)value._pointer;
137                 }
138
139                 public static explicit operator UIntPtr (ulong value)
140                 {
141                         return new UIntPtr (value);
142                 }
143
144                 [CLSCompliant (false)]
145                 public unsafe static explicit operator UIntPtr (void* value)
146                 {
147                         return new UIntPtr (value);
148                 }
149
150                 [CLSCompliant (false)]
151                 public unsafe static explicit operator void* (UIntPtr value)
152                 {
153                         return value.ToPointer ();
154                 }
155
156                 public static explicit operator UIntPtr (uint value)
157                 {
158                         return new UIntPtr (value);
159                 }
160
161                 public static int Size {
162                         get { return sizeof (void*); }
163                 }
164
165                 public static UIntPtr Add (UIntPtr pointer, int offset)
166                 {
167                         return (UIntPtr) (unchecked (((byte *) pointer) + offset));
168                 }
169
170                 public static UIntPtr Subtract (UIntPtr pointer, int offset)
171                 {
172                         return (UIntPtr) (unchecked (((byte *) pointer) - offset));
173                 }
174
175                 public static UIntPtr operator + (UIntPtr pointer, int offset)
176                 {
177                         return (UIntPtr) (unchecked (((byte *) pointer) + offset));
178                 }
179
180                 public static UIntPtr operator - (UIntPtr pointer, int offset)
181                 {
182                         return (UIntPtr) (unchecked (((byte *) pointer) - offset));
183                 }
184         }
185 }