Initial commit
[mono.git] / mcs / class / referencesource / System.Core / Microsoft / Win32 / SafeHandles / SafeMemoryMappedViewHandle.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  SafeMemoryMappedViewHandle
9 **
10 ** Purpose: Safe handle wrapping a MMF view pointer
11 **
12 ** Date:  February 7, 2007
13 **
14 ===========================================================*/
15
16 using System;
17 using System.Runtime.InteropServices;
18 using System.Runtime.Versioning;
19 using System.Security.Permissions;
20 using Microsoft.Win32;
21 using Microsoft.Win32.SafeHandles;
22
23
24 namespace Microsoft.Win32.SafeHandles {
25
26     // Reliability notes:
27     // ReleaseHandle has reliability guarantee of Cer.Success, as defined by SafeHandle.
28     // It gets prepared as a CER at instance construction time. This safe handle doesn't
29     // need to override IsInvalid because the one it inherits from 
30     // SafeHandleZeroOrMinusOneIsInvalid is correct.
31
32 #pragma warning disable 618    // Have not migrated to v4 transparency yet
33     [System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
34 #pragma warning restore 618
35     public sealed class SafeMemoryMappedViewHandle : SafeBuffer {
36
37         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
38         internal SafeMemoryMappedViewHandle() : base(true) { }
39
40         [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
41         internal SafeMemoryMappedViewHandle(IntPtr handle, bool ownsHandle) : base(ownsHandle) {
42             base.SetHandle(handle);
43         }
44
45         [ResourceExposure(ResourceScope.Machine)]
46         [ResourceConsumption(ResourceScope.Machine)]
47         override protected bool ReleaseHandle() {
48             if (UnsafeNativeMethods.UnmapViewOfFile(handle)) {
49                 handle = IntPtr.Zero;
50                 return true;
51             }
52             return false;
53         }
54     }
55
56 }