We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Runtime.InteropServices
- {
- using System;
- using System.Security.Permissions;
- using System.Runtime.CompilerServices;
- using System.Threading;
-
-
-
- [Serializable()]
- [System.Runtime.InteropServices.ComVisible(true)]
- public enum GCHandleType
- {
- Weak = 0,
- WeakTrackResurrection = 1,
- Normal = 2,
- Pinned = 3
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- [System.Runtime.InteropServices.ComVisible(true)]
- public struct GCHandle
- {
- static internal readonly IntPtr InvalidCookie = new IntPtr(unchecked((int)4294967295u));
-
-
-
- internal GCHandle(object value, GCHandleType type)
- {
- m_handle = InternalAlloc(value, type);
-
-
- if (type == GCHandleType.Pinned)
- SetIsPinned();
- }
-
-
- internal GCHandle(IntPtr handle)
- {
- InternalCheckDomain(handle);
- m_handle = handle;
- }
-
-
-
-
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public static GCHandle Alloc(object value)
- {
- return new GCHandle(value, GCHandleType.Normal);
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public static GCHandle Alloc(object value, GCHandleType type)
- {
- return new GCHandle(value, type);
- }
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public void Free()
- {
-
-
- IntPtr handle = m_handle;
-
-
- if (handle != IntPtr.Zero && Interlocked.CompareExchange(ref m_handle, IntPtr.Zero, handle) == handle) {
- #if WIN32
- InternalFree((IntPtr)(((int)handle) & ~1));
- #else
- InternalFree((IntPtr)(((long)handle) & ~1l));
- #endif
-
- }
- else {
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotInitialized"));
- }
- }
-
-
- public object Target {
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- get {
-
- if (m_handle == IntPtr.Zero)
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotInitialized"));
-
- return InternalGet(GetHandleValue());
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- set {
-
- if (m_handle == IntPtr.Zero)
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotInitialized"));
-
- InternalSet(GetHandleValue(), value, IsPinned());
- }
- }
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public IntPtr AddrOfPinnedObject()
- {
-
- if (!IsPinned()) {
-
- if (m_handle == IntPtr.Zero)
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotInitialized"));
-
-
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotPinned"));
- }
-
-
- return InternalAddrOfPinnedObject(GetHandleValue());
- }
-
-
- public bool IsAllocated {
- get { return m_handle != IntPtr.Zero; }
- }
-
-
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public static explicit operator GCHandle(IntPtr value)
- {
- return FromIntPtr(value);
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public static GCHandle FromIntPtr(IntPtr value)
- {
- if (value == IntPtr.Zero)
- throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_HandleIsNotInitialized"));
-
- IntPtr handle = value;
-
-
- return new GCHandle(handle);
- }
-
-
- public static explicit operator IntPtr(GCHandle value)
- {
- return ToIntPtr(value);
- }
-
- public static IntPtr ToIntPtr(GCHandle value)
- {
- return value.m_handle;
- }
-
- public override int GetHashCode()
- {
- return (int)m_handle;
- }
-
- public override bool Equals(object o)
- {
- GCHandle hnd;
-
-
- if (o == null || !(o is GCHandle))
- return false;
- else
- hnd = (GCHandle)o;
-
- return m_handle == hnd.m_handle;
- }
-
- public static bool operator ==(GCHandle a, GCHandle b)
- {
- return a.m_handle == b.m_handle;
- }
-
- public static bool operator !=(GCHandle a, GCHandle b)
- {
- return a.m_handle != b.m_handle;
- }
-
- internal IntPtr GetHandleValue()
- {
- #if WIN32
- return new IntPtr(((int)m_handle) & ~1);
- #else
- return new IntPtr(((long)m_handle) & ~1l);
- #endif
- }
-
- internal bool IsPinned()
- {
- #if WIN32
- return (((int)m_handle) & 1) != 0;
- #else
- return (((long)m_handle) & 1) != 0;
- #endif
- }
-
- internal void SetIsPinned()
- {
- #if WIN32
- m_handle = new IntPtr(((int)m_handle) | 1);
- #else
- m_handle = new IntPtr(((long)m_handle) | 1l);
- #endif
- }
-
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern IntPtr InternalAlloc(object value, GCHandleType type);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void InternalFree(IntPtr handle);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern object InternalGet(IntPtr handle);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void InternalSet(IntPtr handle, object value, bool isPinned);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern object InternalCompareExchange(IntPtr handle, object value, object oldValue, bool isPinned);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern IntPtr InternalAddrOfPinnedObject(IntPtr handle);
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void InternalCheckDomain(IntPtr handle);
-
-
- private IntPtr m_handle;
-
- }
- }