UIntPtr.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Globalization;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Versioning;
  8. #if BIT64
  9. using nuint = System.UInt64;
  10. #else
  11. using nuint = System.UInt32;
  12. #endif
  13. namespace System
  14. {
  15. [Serializable]
  16. [CLSCompliant(false)]
  17. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  18. public readonly struct UIntPtr : IEquatable<UIntPtr>, ISerializable
  19. {
  20. private readonly unsafe void* _value; // Do not rename (binary serialization)
  21. [Intrinsic]
  22. public static readonly UIntPtr Zero;
  23. [Intrinsic]
  24. [NonVersionable]
  25. public unsafe UIntPtr(uint value)
  26. {
  27. _value = (void*)value;
  28. }
  29. [Intrinsic]
  30. [NonVersionable]
  31. public unsafe UIntPtr(ulong value)
  32. {
  33. #if BIT64
  34. _value = (void*)value;
  35. #else
  36. _value = (void*)checked((uint)value);
  37. #endif
  38. }
  39. [Intrinsic]
  40. [NonVersionable]
  41. public unsafe UIntPtr(void* value)
  42. {
  43. _value = value;
  44. }
  45. private unsafe UIntPtr(SerializationInfo info, StreamingContext context)
  46. {
  47. ulong l = info.GetUInt64("value");
  48. if (Size == 4 && l > uint.MaxValue)
  49. throw new ArgumentException(SR.Serialization_InvalidPtrValue);
  50. _value = (void*)l;
  51. }
  52. void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
  53. {
  54. if (info == null)
  55. throw new ArgumentNullException(nameof(info));
  56. info.AddValue("value", ToUInt64());
  57. }
  58. public unsafe override bool Equals(object obj)
  59. {
  60. if (obj is UIntPtr)
  61. {
  62. return (_value == ((UIntPtr)obj)._value);
  63. }
  64. return false;
  65. }
  66. unsafe bool IEquatable<UIntPtr>.Equals(UIntPtr other)
  67. {
  68. return _value == other._value;
  69. }
  70. public unsafe override int GetHashCode()
  71. {
  72. #if BIT64
  73. ulong l = (ulong)_value;
  74. return (unchecked((int)l) ^ (int)(l >> 32));
  75. #else
  76. return unchecked((int)_value);
  77. #endif
  78. }
  79. [Intrinsic]
  80. [NonVersionable]
  81. public unsafe uint ToUInt32()
  82. {
  83. #if BIT64
  84. return checked((uint)_value);
  85. #else
  86. return (uint)_value;
  87. #endif
  88. }
  89. [Intrinsic]
  90. [NonVersionable]
  91. public unsafe ulong ToUInt64()
  92. {
  93. return (ulong)_value;
  94. }
  95. [Intrinsic]
  96. [NonVersionable]
  97. public static explicit operator UIntPtr(uint value)
  98. {
  99. return new UIntPtr(value);
  100. }
  101. [Intrinsic]
  102. [NonVersionable]
  103. public static explicit operator UIntPtr(ulong value)
  104. {
  105. return new UIntPtr(value);
  106. }
  107. [Intrinsic]
  108. [NonVersionable]
  109. public static unsafe explicit operator UIntPtr(void* value)
  110. {
  111. return new UIntPtr(value);
  112. }
  113. [Intrinsic]
  114. [NonVersionable]
  115. public static unsafe explicit operator void* (UIntPtr value)
  116. {
  117. return value._value;
  118. }
  119. [Intrinsic]
  120. [NonVersionable]
  121. public static unsafe explicit operator uint(UIntPtr value)
  122. {
  123. #if BIT64
  124. return checked((uint)value._value);
  125. #else
  126. return (uint)value._value;
  127. #endif
  128. }
  129. [Intrinsic]
  130. [NonVersionable]
  131. public static unsafe explicit operator ulong(UIntPtr value)
  132. {
  133. return (ulong)value._value;
  134. }
  135. [Intrinsic]
  136. [NonVersionable]
  137. public static unsafe bool operator ==(UIntPtr value1, UIntPtr value2)
  138. {
  139. return value1._value == value2._value;
  140. }
  141. [Intrinsic]
  142. [NonVersionable]
  143. public static unsafe bool operator !=(UIntPtr value1, UIntPtr value2)
  144. {
  145. return value1._value != value2._value;
  146. }
  147. [NonVersionable]
  148. public static UIntPtr Add(UIntPtr pointer, int offset)
  149. {
  150. return pointer + offset;
  151. }
  152. [Intrinsic]
  153. [NonVersionable]
  154. public static unsafe UIntPtr operator +(UIntPtr pointer, int offset)
  155. {
  156. return new UIntPtr((nuint)pointer._value + (nuint)offset);
  157. }
  158. [NonVersionable]
  159. public static UIntPtr Subtract(UIntPtr pointer, int offset)
  160. {
  161. return pointer - offset;
  162. }
  163. [Intrinsic]
  164. [NonVersionable]
  165. public static unsafe UIntPtr operator -(UIntPtr pointer, int offset)
  166. {
  167. return new UIntPtr((nuint)pointer._value - (nuint)offset);
  168. }
  169. public static int Size
  170. {
  171. [Intrinsic]
  172. [NonVersionable]
  173. get
  174. {
  175. return sizeof(nuint);
  176. }
  177. }
  178. [Intrinsic]
  179. [NonVersionable]
  180. #if PROJECTN
  181. [System.Runtime.CompilerServices.DependencyReductionRootAttribute]
  182. #endif
  183. public unsafe void* ToPointer()
  184. {
  185. return _value;
  186. }
  187. public unsafe override string ToString()
  188. {
  189. return ((nuint)_value).ToString(CultureInfo.InvariantCulture);
  190. }
  191. }
  192. }