3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
8 ** Class: MemoryMappedViewAccessor
10 ** Purpose: View accessor for managed MemoryMappedFiles
12 ** Date: February 7, 2007
14 ===========================================================*/
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");
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
)]
41 return m_view
!= null ? m_view
.ViewHandle
: null;
45 public long PointerOffset
51 throw new InvalidOperationException(SR
.GetString(SR
.InvalidOperation_ViewIsNull
));
54 return m_view
.PointerOffset
;
58 [SecuritySafeCritical
]
59 protected override void Dispose(bool disposing
) {
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
) {
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
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
]
87 throw new ObjectDisposedException("MemoryMappedViewAccessor", SR
.GetString(SR
.ObjectDisposed_ViewAccessorClosed
));
92 m_view
.Flush((IntPtr
)Capacity
);