1 //------------------------------------------------------------------------------
2 // <copyright file="GCUtil.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
7 namespace System.Web.Util {
9 using System.Runtime.InteropServices;
10 using System.Security.Permissions;
12 // Provides helper methods for wrapping managed objects so that they can be passed to and from unmanaged code.
13 // Unmanaged code can't actually inspect these objects at all since they're just GCHandles. And these handles
14 // cannot be reused (they're single-shot).
16 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
17 internal static class GCUtil {
19 public static IntPtr RootObject(object obj) {
21 ? (IntPtr)GCHandle.Alloc(obj)
25 public static object UnrootObject(IntPtr pointer) {
26 if (pointer != IntPtr.Zero) {
27 GCHandle gcHandle = (GCHandle)pointer;
28 if (gcHandle.IsAllocated) {
29 object target = gcHandle.Target;
40 // This wrapper around a managed object is opaque to SizedReference GC handle
41 // and therefore helps with calculating size of only relevant graph of objects
42 internal class DisposableGCHandleRef<T> : IDisposable
43 where T : class, IDisposable {
45 [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
46 public DisposableGCHandleRef(T t) {
47 Debug.Assert(t != null);
48 _handle = GCHandle.Alloc(t);
52 [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
54 Debug.Assert(_handle.IsAllocated);
55 return (T)_handle.Target;
59 [PermissionSet(SecurityAction.Assert, Unrestricted = true)]
60 public void Dispose() {
62 Debug.Assert(_handle.IsAllocated);
63 if (_handle.IsAllocated) {