-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadOnlyPinnedMemory.cs
More file actions
298 lines (269 loc) · 11.1 KB
/
Copy pathReadOnlyPinnedMemory.cs
File metadata and controls
298 lines (269 loc) · 11.1 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using RapidField.SolidInstruments.Core.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
namespace RapidField.SolidInstruments.Collections
{
/// <summary>
/// Represents a read-only, fixed-length bit field that is pinned in memory.
/// </summary>
/// <remarks>
/// <see cref="ReadOnlyPinnedMemory" /> is the default implementation of <see cref="IReadOnlyPinnedMemory" />.
/// </remarks>
public class ReadOnlyPinnedMemory : ReadOnlyPinnedMemory<Byte>
{
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyPinnedMemory" /> class.
/// </summary>
/// <param name="length">
/// The length of the array.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="length" /> is less than zero.
/// </exception>
public ReadOnlyPinnedMemory(Int32 length)
: base(length)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyPinnedMemory" /> class.
/// </summary>
/// <param name="field">
/// The structure collection comprising the memory field.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="field" /> is <see langword="null" />.
/// </exception>
public ReadOnlyPinnedMemory(Byte[] field)
: base(field)
{
return;
}
/// <summary>
/// Releases all resources consumed by the current <see cref="ReadOnlyPinnedMemory" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing) => base.Dispose(disposing);
}
/// <summary>
/// Represents a read-only, fixed-length bit field that is pinned in memory.
/// </summary>
/// <remarks>
/// <see cref="ReadOnlyPinnedMemory{T}" /> is the default implementation of <see cref="IReadOnlyPinnedMemory{T}" />.
/// </remarks>
/// <typeparam name="T">
/// The element type of the memory field.
/// </typeparam>
public class ReadOnlyPinnedMemory<T> : Instrument, IReadOnlyPinnedMemory<T>
where T : struct, IComparable, IComparable<T>, IConvertible, IEquatable<T>, IFormattable
{
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyPinnedMemory{T}" /> class.
/// </summary>
/// <param name="length">
/// The length of the array.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="length" /> is less than zero.
/// </exception>
public ReadOnlyPinnedMemory(Int32 length)
: this(new T[length.RejectIf().IsLessThan(0, nameof(length))])
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyPinnedMemory{T}" /> class.
/// </summary>
/// <param name="field">
/// The structure collection comprising the memory field.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="field" /> is <see langword="null" />.
/// </exception>
public ReadOnlyPinnedMemory(T[] field)
: base()
{
Handle = GCHandle.Alloc(field.RejectIf().IsNull(nameof(field)).TargetArgument, GCHandleType.Pinned);
Field = (T[])Handle.Target;
LazyFieldMemory = new Lazy<Memory<T>>(InitializeFieldMemory, LazyThreadSafetyMode.ExecutionAndPublication);
Length = field.Length;
Pointer = Handle.AddrOfPinnedObject();
}
/// <summary>
/// Gets or sets the element at the specified index.
/// </summary>
/// <param name="index">
/// The index of the element to access.
/// </param>
/// <returns>
/// The element at the specified index if one exists.
/// </returns>
/// <exception cref="IndexOutOfRangeException">
/// The specified index is out of range.
/// </exception>
public T this[Int32 index] => Field[index];
/// <summary>
/// Facilitates implicit <typeparamref name="T" /> array to <see cref="ReadOnlyPinnedMemory{T}" /> casting.
/// </summary>
/// <param name="target">
/// The object to cast from.
/// </param>
public static implicit operator ReadOnlyPinnedMemory<T>(T[] target) => target is null ? null : new ReadOnlyPinnedMemory<T>(target);
/// <summary>
/// Facilitates implicit <see cref="ReadOnlyPinnedMemory{T}" /> to <see cref="ReadOnlySpan{T}" /> casting.
/// </summary>
/// <param name="target">
/// The object to cast from.
/// </param>
public static implicit operator ReadOnlySpan<T>(ReadOnlyPinnedMemory<T> target) => target is null ? ReadOnlySpan<T>.Empty : target.ReadOnlySpan;
/// <summary>
/// Facilitates implicit <see cref="ReadOnlyPinnedMemory{T}" /> to <typeparamref name="T" /> array casting.
/// </summary>
/// <param name="target">
/// The object to cast from.
/// </param>
public static implicit operator T[](ReadOnlyPinnedMemory<T> target) => target?.Field;
/// <summary>
/// Returns an enumerator that iterates through the elements of the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </summary>
/// <returns>
/// An enumerator that iterates through the elements of the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </returns>
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i < Length; i++)
{
yield return Field[i];
}
}
/// <summary>
/// Returns an enumerator that iterates through the elements of the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </summary>
/// <returns>
/// An enumerator that iterates through the elements of the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>
/// A 32-bit signed integer hash code.
/// </returns>
public override Int32 GetHashCode()
{
var hashCode = 0x3acf3acf;
foreach (var element in this)
{
hashCode ^= element.GetHashCode() ^ 0x66666666;
using (var memory = new ReadOnlyPinnedMemory(hashCode.ToByteArray()))
{
hashCode = memory.ComputeThirtyTwoBitHash() ^ 0x33333333;
}
}
return hashCode ^ 0x55555555;
}
/// <summary>
/// Converts the value of the current <see cref="ReadOnlyPinnedMemory{T}" /> to its equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </returns>
public override String ToString() => $"{{ \"{nameof(Length)}\": {Length}, \"{nameof(LengthInBytes)}\": {LengthInBytes} }}";
/// <summary>
/// Releases all resources consumed by the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </summary>
/// <param name="disposing">
/// A value indicating whether or not managed resources should be released.
/// </param>
protected override void Dispose(Boolean disposing)
{
try
{
if (disposing && IsDisposed == false)
{
Handle.Free();
Pointer = IntPtr.Zero;
}
}
finally
{
base.Dispose(disposing);
}
}
/// <summary>
/// Initializes the lazily-initialized structure collection as a <see cref="Memory{T}" />.
/// </summary>
[DebuggerHidden]
private Memory<T> InitializeFieldMemory() => new Memory<T>(Field);
/// <summary>
/// Gets a value indicating whether or not the memory field is empty.
/// </summary>
public Boolean IsEmpty => Length == 0;
/// <summary>
/// Gets the number of elements comprising the memory field.
/// </summary>
public Int32 Length
{
get;
}
/// <summary>
/// Gets the length of the memory field, in bytes.
/// </summary>
public Int32 LengthInBytes => (Length * StructureSize);
/// <summary>
/// Gets a <see cref="ReadOnlySpan{T}" /> for the current <see cref="ReadOnlyPinnedMemory{T}" />.
/// </summary>
/// <exception cref="ObjectDisposedException">
/// The object is disposed.
/// </exception>
public ReadOnlySpan<T> ReadOnlySpan
{
get
{
RejectIfDisposed();
return FieldMemory.Span;
}
}
/// <summary>
/// Gets the structure collection as a <see cref="Memory{T}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal Memory<T> FieldMemory => LazyFieldMemory.Value;
/// <summary>
/// Represents the structure collection as an array of <typeparamref name="T" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal readonly T[] Field;
/// <summary>
/// Represents a pointer for <see cref="Field" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal IntPtr Pointer;
/// <summary>
/// Represents the byte length of the element type.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private static readonly Int32 StructureSize = Marshal.SizeOf<T>();
/// <summary>
/// Represents a handle for the array.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly GCHandle Handle;
/// <summary>
/// Represents the lazily-initialized structure collection as a <see cref="Memory{T}" />.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Lazy<Memory<T>> LazyFieldMemory;
}
}