-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMemoryView.cs
More file actions
60 lines (51 loc) · 1.53 KB
/
MemoryView.cs
File metadata and controls
60 lines (51 loc) · 1.53 KB
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
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using Cloudtoid.Interprocess.Memory.Unix;
using Cloudtoid.Interprocess.Memory.Windows;
namespace Cloudtoid.Interprocess;
// This class manages the underlying Memory Mapped File
internal sealed class MemoryView : IDisposable
{
private readonly IMemoryFile file;
private readonly MemoryMappedViewAccessor view;
internal unsafe MemoryView(QueueOptions options, ILoggerFactory loggerFactory)
{
file = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? new MemoryFileWindows(options)
: new MemoryFileUnix(options, loggerFactory);
try
{
view = file.MappedFile.CreateViewAccessor(0, 0, MemoryMappedFileAccess.ReadWrite);
try
{
Pointer = AcquirePointer();
}
catch
{
view.Dispose();
throw;
}
}
catch
{
file.Dispose();
throw;
}
}
public unsafe byte* Pointer { get; }
public void Dispose()
{
view.SafeMemoryMappedViewHandle.ReleasePointer();
view.Flush();
view.Dispose();
file.Dispose();
}
private unsafe byte* AcquirePointer()
{
byte* ptr = null;
view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr);
if (ptr is null)
throw new InvalidOperationException("Failed to acquire a pointer to the memory mapped file view.");
return ptr;
}
}