-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMM.cs
61 lines (53 loc) · 1.84 KB
/
MM.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
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace MemoryMappedFileTest
{
public interface IMemoryMappedHandle
{
}
/// <summary>
/// Container to store a pointer into a memory mapped file view.
/// </summary>
public unsafe readonly struct MemoryMappedHandle : IMemoryMappedHandle
{
/// <summary>
/// Gets the base pointer.
/// </summary>
private readonly nint _pointer;
/// <summary>
/// The length of the memory in the stored data format.
/// </summary>
private readonly int _length;
/// <summary>
/// Creates a new instance of <see cref="MemoryMappedHandle"/>.
/// </summary>
/// <param name="pointer"></param>
/// <param name="length"></param>
[SkipLocalsInit]
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
public MemoryMappedHandle(nint pointer, int length)
{
_pointer = pointer;
_length = length;
}
/// <summary>
/// Creates a <see cref="ReadOnlySpan{T}"/> out of the <see cref="MemoryMappedHandle"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public ReadOnlySpan<T> AsReadOnlySpan<T>() where T : unmanaged => new((void*)_pointer, _length * sizeof(T));
/// <summary>
/// Creates a <see cref="Span{T}"/> out of the <see cref="MemoryMappedHandle"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Span<T> AsSpan<T>() where T : unmanaged => new((void*)_pointer, _length * sizeof(T));
}
}