/
SafeMemoryMappedFileHandle.Unix.cs
91 lines (74 loc) · 3.43 KB
/
SafeMemoryMappedFileHandle.Unix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
namespace Microsoft.Win32.SafeHandles
{
public sealed partial class SafeMemoryMappedFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
/// <summary>File handle.</summary>
internal SafeFileHandle? _fileStreamHandle;
/// <summary>Whether this instance owns the _fileStreamHandle and should Dispose it when disposed.</summary>
internal readonly bool _ownsFileHandle;
/// <summary>The inheritability of the memory-mapped file.</summary>
internal readonly HandleInheritability _inheritability;
/// <summary>The access to the memory-mapped file.</summary>
internal readonly MemoryMappedFileAccess _access;
/// <summary>The options for the memory-mapped file.</summary>
internal readonly MemoryMappedFileOptions _options;
/// <summary>The capacity of the memory-mapped file.</summary>
internal readonly long _capacity;
/// <summary>Initializes the memory-mapped file handle.</summary>
/// <param name="fileHandle">The underlying file handle; may be null.</param>
/// <param name="ownsFileHandle">Whether this SafeHandle is responsible for Disposing the fileStream.</param>
/// <param name="inheritability">The inheritability of the memory-mapped file.</param>
/// <param name="access">The access for the memory-mapped file.</param>
/// <param name="options">The options for the memory-mapped file.</param>
/// <param name="capacity">The capacity of the memory-mapped file.</param>
internal SafeMemoryMappedFileHandle(
SafeFileHandle? fileHandle, bool ownsFileHandle, HandleInheritability inheritability,
MemoryMappedFileAccess access, MemoryMappedFileOptions options,
long capacity)
: base(ownsHandle: true)
{
Debug.Assert(!ownsFileHandle || fileHandle != null, "We can only own a FileStream we're actually given.");
// Store the arguments. We'll actually open the map when the view is created.
_ownsFileHandle = ownsFileHandle;
_inheritability = inheritability;
_access = access;
_options = options;
_capacity = capacity;
IntPtr handlePtr;
if (fileHandle != null)
{
bool ignored = false;
fileHandle.DangerousAddRef(ref ignored);
_fileStreamHandle = fileHandle;
handlePtr = fileHandle.DangerousGetHandle();
}
else
{
handlePtr = IntPtr.MaxValue;
}
SetHandle(handlePtr);
}
protected override bool ReleaseHandle()
{
if (_fileStreamHandle != null)
{
SetHandle((IntPtr)(-1));
if (_ownsFileHandle)
{
// Clean up the file descriptor (either for a file on disk or a shared memory object) if we created it
_fileStreamHandle.Dispose();
}
_fileStreamHandle.DangerousRelease();
_fileStreamHandle = null;
}
return true;
}
public override bool IsInvalid => (long)handle <= 0;
}
}