Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Core / System / IO / MemoryMappedFiles / MemoryMappedViewAccessor.cs
blobc79b81dfe1a633999f78cef7987862035e022b19
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class: MemoryMappedViewAccessor
9 **
10 ** Purpose: View accessor for managed MemoryMappedFiles
12 ** Date: February 7, 2007
14 ===========================================================*/
16 using System;
17 using System.Diagnostics;
18 using System.Security;
19 using System.Security.Permissions;
20 using Microsoft.Win32.SafeHandles;
22 namespace System.IO.MemoryMappedFiles {
24 public sealed class MemoryMappedViewAccessor : UnmanagedMemoryAccessor {
26 private MemoryMappedView m_view;
28 [System.Security.SecurityCritical]
29 internal MemoryMappedViewAccessor(MemoryMappedView view) {
30 Debug.Assert(view != null, "view is null");
32 m_view = view;
33 Initialize(m_view.ViewHandle, m_view.PointerOffset, m_view.Size, MemoryMappedFile.GetFileAccess(m_view.Access));
36 public SafeMemoryMappedViewHandle SafeMemoryMappedViewHandle {
38 [System.Security.SecurityCritical]
39 [SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
40 get {
41 return m_view != null ? m_view.ViewHandle : null;
45 public long PointerOffset
47 get
49 if (m_view == null)
51 throw new InvalidOperationException(SR.GetString(SR.InvalidOperation_ViewIsNull));
54 return m_view.PointerOffset;
58 [SecuritySafeCritical]
59 protected override void Dispose(bool disposing) {
60 try {
61 // Explicitly flush the changes. The OS will do this for us anyway, but not until after the
62 // MemoryMappedFile object itself is closed.
63 if (disposing && m_view != null && !m_view.IsClosed) {
64 Flush();
67 finally {
68 try {
69 if (m_view != null) {
70 m_view.Dispose();
73 finally {
74 base.Dispose(disposing);
79 // Flushes the changes such that they are in sync with the FileStream bits (ones obtained
80 // with the win32 ReadFile and WriteFile functions). Need to call FileStream's Flush to
81 // flush to the disk.
82 // NOTE: This will flush all bytes before and after the view up until an offset that is a
83 // multiple of SystemPageSize.
84 [System.Security.SecurityCritical]
85 public void Flush() {
86 if (!IsOpen) {
87 throw new ObjectDisposedException("MemoryMappedViewAccessor", SR.GetString(SR.ObjectDisposed_ViewAccessorClosed));
90 unsafe {
91 if (m_view != null) {
92 m_view.Flush((IntPtr)Capacity);