-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemoryUtils.cs
180 lines (154 loc) · 5.48 KB
/
MemoryUtils.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Microsoft.CSharp.RuntimeBinder;
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace IS4.SFI.Tools
{
static class MemoryUtils
{
public static Memory<TTo> Cast<TFrom, TTo>(Memory<TFrom> from) where TFrom : unmanaged where TTo : unmanaged
{
if(MemoryMarshal.TryGetArray<TFrom>(from, out var array) && array.Array is TTo[] compatible)
{
return compatible.AsMemory(array.Offset, array.Count);
}
return new CastMemoryManager<TFrom, TTo>(from).Memory;
}
sealed class CastMemoryManager<TFrom, TTo> : MemoryManager<TTo> where TFrom : unmanaged where TTo : unmanaged
{
Memory<TFrom> memory;
public CastMemoryManager(Memory<TFrom> memory)
{
this.memory = memory;
}
public override Span<TTo> GetSpan()
{
return MemoryMarshal.Cast<TFrom, TTo>(memory.Span);
}
public override MemoryHandle Pin(int elementIndex = 0)
{
if(elementIndex != 0)
{
throw new NotImplementedException();
}
return memory.Pin();
}
public override void Unpin()
{
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
memory = Memory<TFrom>.Empty;
}
}
}
public static Memory<byte> GetObjectMemory(object obj)
{
if(obj is Array array)
{
return GetArrayMemory(array);
}
if(!obj.GetType().IsValueType)
{
throw new ArgumentException("Argument must be an array or a boxed value type.", nameof(obj));
}
return new ObjectMemoryManager(obj).Memory;
}
[DynamicDependency(nameof(GetDynamicArrayMemory), typeof(MemoryUtils))]
static Memory<byte> GetArrayMemory(Array array)
{
if(array is byte[] byteArray)
{
return byteArray.AsMemory();
}
try{
return GetDynamicArrayMemory((dynamic)array);
}catch(RuntimeBinderException)
{
}
return new ArrayMemoryManager(array).Memory;
}
static Memory<byte> GetDynamicArrayMemory<T>(T[] array) where T : unmanaged
{
return Cast<T, byte>(array.AsMemory());
}
public static Memory<byte> GetValueMemory<T>(T value) where T : unmanaged
{
return GetDynamicArrayMemory(new[] { value });
}
sealed class ArrayMemoryManager : MemoryManager<byte>
{
readonly Array array;
GCHandle handle;
public ArrayMemoryManager(Array array)
{
this.array = array;
handle = GCHandle.Alloc(array, GCHandleType.Pinned);
}
public unsafe override Span<byte> GetSpan()
{
return new Span<byte>((void*)handle.AddrOfPinnedObject(), Buffer.ByteLength(array));
}
public unsafe override MemoryHandle Pin(int elementIndex = 0)
{
if(elementIndex < 0 || elementIndex >= Buffer.ByteLength(array))
{
throw new ArgumentOutOfRangeException(nameof(elementIndex));
}
return new MemoryHandle((byte*)handle.AddrOfPinnedObject() + elementIndex, handle, this);
}
public override void Unpin()
{
handle.Free();
}
protected override void Dispose(bool disposing)
{
if(handle.IsAllocated)
{
handle.Free();
}
}
}
sealed class ObjectMemoryManager : MemoryManager<byte>
{
readonly object obj;
readonly int length;
GCHandle handle;
static readonly MethodInfo sizeOf = typeof(Unsafe).GetMethod(nameof(Unsafe.SizeOf), BindingFlags.Public | BindingFlags.Static, null, Type.EmptyTypes, null);
public ObjectMemoryManager(object obj)
{
this.obj = obj;
length = (int)sizeOf.MakeGenericMethod(obj.GetType()).Invoke(null, null);
handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
}
public unsafe override Span<byte> GetSpan()
{
return new Span<byte>((void*)handle.AddrOfPinnedObject(), length);
}
public unsafe override MemoryHandle Pin(int elementIndex = 0)
{
if(elementIndex < 0 || elementIndex >= length)
{
throw new ArgumentOutOfRangeException(nameof(elementIndex));
}
return new MemoryHandle((byte*)handle.AddrOfPinnedObject() + elementIndex, handle, this);
}
public override void Unpin()
{
handle.Free();
}
protected override void Dispose(bool disposing)
{
if(handle.IsAllocated)
{
handle.Free();
}
}
}
}
}