[Mono-list] GCHandleType Patch

Dwivedi , Ajay Kumar AjayKumar.Dwivedi@dresdner-bank.com
Tue, 26 Feb 2002 11:21:47 -0000


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000_01C1BEB7.C734BF60
Content-Type: text/plain;
	charset="ISO-8859-1"

hi all,
	The patch for System.Runtime.InteropServices.GCHandleType is
attached. As always, it is generated against my local CVS and so the
versions might not match.
I had submitted System.WeakReference.cs earlier, which hasn't made its way
to CVS yet.  
I am also submitting a quick hack of System.Runtime.InteropServices.GCHandle
which makes the WeakReference class compile in mcs.

Regards,

Ajay


------_=_NextPart_000_01C1BEB7.C734BF60
Content-Type: application/octet-stream;
	name="GCHandleType.patch"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="GCHandleType.patch"

Index: GCHandleType.cs=0A=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=0A=
RCS file: =
/home/cvsroot/Mono/mcs/class/corlib/System.Runtime.InteropServices/GCHan=
dleType.cs,v=0A=
retrieving revision 1.1.1.1=0A=
diff -c -r1.1.1.1 GCHandleType.cs=0A=
*** GCHandleType.cs	2002/02/18 05:42:19	1.1.1.1=0A=
--- GCHandleType.cs	2002/02/25 05:44:28=0A=
***************=0A=
*** 17,22 ****=0A=
--- 17,30 ----=0A=
  	/// </summary>=0A=
  	public enum GCHandleType {=0A=
  =0A=
+                 /// <summary>=0A=
+                 /// </summary>=0A=
+                 Weak =3D 0,=0A=
+  =0A=
+                 /// <summary>=0A=
+                 /// </summary>=0A=
+                 WeakTrackResurrection =3D 1,=0A=
+ =0A=
  		/// <summary>=0A=
  		/// </summary>=0A=
  		Normal =3D 2,=0A=

------_=_NextPart_000_01C1BEB7.C734BF60
Content-Type: application/octet-stream;
	name="GCHandle.cs"
Content-Disposition: attachment;
	filename="GCHandle.cs"

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace System.Runtime.InteropServices
{
	/// <summary>
	/// Summary description for GCHandle.
	/// </summary>
	public struct GCHandle 
	{
		// fields
		private IntPtr handle;
		private GCHandleType handleType;

		// Constructors
		private GCHandle(object obj)
			: this(obj, GCHandleType.Normal)
		{}

		private GCHandle(object value, GCHandleType type)
		{
			handle = IntPtr.Zero;
			handleType = type;
		}

		// Properties

		public bool IsAllocated 
		{ 
			get
			{
				return (handle != IntPtr.Zero);
			}
		}

		public object Target
		{ 
			get
			{
				return GetTarget(handle);
			} 
			set
			{
				SetTarget(handle,value);
			} 
		}

		// Methods
		public IntPtr AddrOfPinnedObject()
		{
			if(this.handleType == System.Runtime.InteropServices.GCHandleType.Pinned)
			{
				throw new InvalidOperationException("The handle is not of Pinned type");
			}
			return GetAddrOfPinnedObject();
		}

		public static System.Runtime.InteropServices.GCHandle Alloc(object value)
		{
			return new GCHandle(value);
		}

		public static System.Runtime.InteropServices.GCHandle Alloc(object value, GCHandleType type)
		{
			return new GCHandle(value,type);
		}

		public void Free()
		{
			FreeHandle(handle);
			handle = IntPtr.Zero;
		}
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		public extern static explicit operator IntPtr(GCHandle value);
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		public extern static explicit operator GCHandle(IntPtr value);

		//TODO: Private Native Functions
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern object GetTarget(IntPtr pointer);

		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern void SetTarget(IntPtr pointer,object obj);
		
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern void FreeHandle(IntPtr pointer);
		
		[MethodImplAttribute(MethodImplOptions.InternalCall)]
		private extern IntPtr GetAddrOfPinnedObject();
	} 
}
------_=_NextPart_000_01C1BEB7.C734BF60--