// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace GLibSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate IntPtr ReallocFuncNative(IntPtr data, UIntPtr size);

	internal class ReallocFuncInvoker {

		ReallocFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~ReallocFuncInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal ReallocFuncInvoker (ReallocFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal ReallocFuncInvoker (ReallocFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal ReallocFuncInvoker (ReallocFuncNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal GLib.ReallocFunc Handler {
			get {
				return new GLib.ReallocFunc(InvokeNative);
			}
		}

		IntPtr InvokeNative (IntPtr data, ulong size)
		{
			IntPtr __result = native_cb (__data, new UIntPtr (size));
			return __result;
		}
	}

	internal class ReallocFuncWrapper {

		public IntPtr NativeCallback (IntPtr data, UIntPtr size)
		{
			try {
				IntPtr __ret = managed (data, (ulong) size);
				if (release_on_call)
					gch.Free ();
				return __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: Above call does not return.
				throw;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal ReallocFuncNative NativeDelegate;
		GLib.ReallocFunc managed;

		public ReallocFuncWrapper (GLib.ReallocFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new ReallocFuncNative (NativeCallback);
		}

		public static GLib.ReallocFunc GetManagedDelegate (ReallocFuncNative native)
		{
			if (native == null)
				return null;
			ReallocFuncWrapper wrapper = (ReallocFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
