1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: IntPtr
**
**
** Purpose: Platform independent integer
**
**
===========================================================*/
namespace System {
using System;
using System.Globalization;
using System.Runtime;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Security;
using System.Diagnostics.Contracts;
[Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct IntPtr : ISerializable
{
[SecurityCritical]
unsafe private void* m_value; // The compiler treats void* closest to uint hence explicit casts are required to preserve int behavior
public static readonly IntPtr Zero;
// fast way to compare IntPtr to (IntPtr)0 while IntPtr.Zero doesn't work due to slow statics access
[System.Security.SecuritySafeCritical] // auto-generated
[Pure]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
internal unsafe bool IsNull()
{
return (this.m_value == null);
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public unsafe IntPtr(int value)
{
#if WIN32
m_value = (void *)value;
#else
m_value = (void *)(long)value;
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public unsafe IntPtr(long value)
{
#if WIN32
m_value = (void *)checked((int)value);
#else
m_value = (void *)value;
#endif
}
[System.Security.SecurityCritical]
[CLSCompliant(false)]
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public unsafe IntPtr(void* value)
{
m_value = value;
}
[System.Security.SecurityCritical] // auto-generated
private unsafe IntPtr(SerializationInfo info, StreamingContext context) {
long l = info.GetInt64("value");
if (Size==4 && (l>Int32.MaxValue || l<Int32.MinValue)) {
throw new ArgumentException(Environment.GetResourceString("Serialization_InvalidPtrValue"));
}
m_value = (void *)l;
}
#if FEATURE_SERIALIZATION
[System.Security.SecurityCritical]
unsafe void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) {
if (info==null) {
throw new ArgumentNullException("info");
}
Contract.EndContractBlock();
#if WIN32
info.AddValue("value", (long)((int)m_value));
#else
info.AddValue("value", (long)(m_value));
#endif
}
#endif
[System.Security.SecuritySafeCritical] // auto-generated
public unsafe override bool Equals(Object obj) {
if (obj is IntPtr) {
return (m_value == ((IntPtr)obj).m_value);
}
return false;
}
[System.Security.SecuritySafeCritical] // auto-generated
public unsafe override int GetHashCode() {
return unchecked((int)((long)m_value));
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
public unsafe int ToInt32() {
#if WIN32
return (int)m_value;
#else
long l = (long)m_value;
return checked((int)l);
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
public unsafe long ToInt64() {
#if WIN32
return (long)(int)m_value;
#else
return (long)m_value;
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
public unsafe override String ToString() {
#if WIN32
return ((int)m_value).ToString(CultureInfo.InvariantCulture);
#else
return ((long)m_value).ToString(CultureInfo.InvariantCulture);
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
public unsafe String ToString(String format)
{
Contract.Ensures(Contract.Result<String>() != null);
#if WIN32
return ((int)m_value).ToString(format, CultureInfo.InvariantCulture);
#else
return ((long)m_value).ToString(format, CultureInfo.InvariantCulture);
#endif
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static explicit operator IntPtr (int value)
{
return new IntPtr(value);
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static explicit operator IntPtr (long value)
{
return new IntPtr(value);
}
[System.Security.SecurityCritical]
[CLSCompliant(false), ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static unsafe explicit operator IntPtr (void* value)
{
return new IntPtr(value);
}
[System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[System.Runtime.Versioning.NonVersionable]
public static unsafe explicit operator void* (IntPtr value)
{
return value.m_value;
}
[System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.Versioning.NonVersionable]
public unsafe static explicit operator int (IntPtr value)
{
#if WIN32
return (int)value.m_value;
#else
long l = (long)value.m_value;
return checked((int)l);
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.Versioning.NonVersionable]
public unsafe static explicit operator long (IntPtr value)
{
#if WIN32
return (long)(int)value.m_value;
#else
return (long)value.m_value;
#endif
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static bool operator == (IntPtr value1, IntPtr value2)
{
return value1.m_value == value2.m_value;
}
[System.Security.SecuritySafeCritical] // auto-generated
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
public unsafe static bool operator != (IntPtr value1, IntPtr value2)
{
return value1.m_value != value2.m_value;
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static IntPtr Add(IntPtr pointer, int offset)
{
return pointer + offset;
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static IntPtr operator +(IntPtr pointer, int offset)
{
#if WIN32
return new IntPtr(pointer.ToInt32() + offset);
#else
return new IntPtr(pointer.ToInt64() + offset);
#endif
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static IntPtr Subtract(IntPtr pointer, int offset) {
return pointer - offset;
}
[ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
[System.Runtime.Versioning.NonVersionable]
public static IntPtr operator -(IntPtr pointer, int offset) {
#if WIN32
return new IntPtr(pointer.ToInt32() - offset);
#else
return new IntPtr(pointer.ToInt64() - offset);
#endif
}
public static int Size
{
[Pure]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
get
{
#if WIN32
return 4;
#else
return 8;
#endif
}
}
[System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[System.Runtime.Versioning.NonVersionable]
public unsafe void* ToPointer()
{
return m_value;
}
}
}
|