-
Notifications
You must be signed in to change notification settings - Fork 119
/
MemoryMappedFileExtensions.cs
47 lines (42 loc) · 2.57 KB
/
MemoryMappedFileExtensions.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
using System.IO.MemoryMappedFiles;
namespace DotNext.IO.MemoryMappedFiles;
/// <summary>
/// Represents various extensions for <see cref="MemoryMappedFile"/> class.
/// </summary>
public static class MemoryMappedFileExtensions
{
/// <summary>
/// Creates direct accessor to the virtual memory associated with the memory-mapped file.
/// </summary>
/// <param name="file">The memory-mapped file.</param>
/// <param name="offset">The byte at which to start the view.</param>
/// <param name="size">The size of the view. Specify 0 (zero) to create a view that starts at offset and ends approximately at the end of the memory-mapped file.</param>
/// <param name="access">the type of access allowed to the memory-mapped file.</param>
/// <returns>The direct accessor.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="size"/> is less than zero.</exception>
public static MemoryMappedDirectAccessor CreateDirectAccessor(this MemoryMappedFile file, long offset = 0, long size = 0, MemoryMappedFileAccess access = MemoryMappedFileAccess.ReadWrite)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(size);
return new(file.CreateViewAccessor(offset, size, access));
}
/// <summary>
/// Creates memory accessor to the virtual memory associated with the memory-mapped file.
/// </summary>
/// <remarks>
/// This method is suitable if you need to represent memory-mapped file segment as <see cref="System.Memory{T}"/>.
/// </remarks>
/// <param name="file">The memory-mapped file.</param>
/// <param name="offset">The byte at which to start the view.</param>
/// <param name="size">The size of the view. Specify 0 (zero) to create a view that starts at offset and ends approximately at the end of the memory-mapped file.</param>
/// <param name="access">the type of access allowed to the memory-mapped file.</param>
/// <returns>The direct accessor.</returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="offset"/> or <paramref name="size"/> is less than zero.</exception>
[CLSCompliant(false)]
public static IMappedMemory CreateMemoryAccessor(this MemoryMappedFile file, long offset = 0, int size = 0, MemoryMappedFileAccess access = MemoryMappedFileAccess.ReadWrite)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfNegative(size);
return new MappedMemory(file.CreateViewAccessor(offset, size, access));
}
}