-
Notifications
You must be signed in to change notification settings - Fork 3
/
UnmanagedRef.cs
481 lines (398 loc) · 16.3 KB
/
UnmanagedRef.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using static Arenas.TypeHandle;
using static Arenas.TypeInfo;
namespace Arenas {
[DebuggerTypeProxy(typeof(UnmanagedRefDebugView<>))]
[DebuggerDisplay("{HasValue ? ToString() : null}")]
[StructLayout(LayoutKind.Sequential)]
unsafe readonly public struct UnmanagedRef<T> : IUnmanagedRef, IEquatable<UnmanagedRef<T>> where T : unmanaged {
public readonly UnmanagedRef Reference;
public UnmanagedRef(UnmanagedRef reference) {
Reference = reference;
}
public UnmanagedRef(T* pointer, RefVersion version, int elementCount) {
Reference = new UnmanagedRef((IntPtr)pointer, version, elementCount);
}
public bool TryGetValue(out T* ptr) {
ptr = Value;
return ptr != null;
}
#region Copying
public void CopyTo(T[] dest) {
var elementCount = ElementCount;
Reference.CopyTo(dest, 0, 0, elementCount, elementCount, typeof(T));
}
public void CopyTo(T[] dest, int destIndex) {
var elementCount = ElementCount;
Reference.CopyTo(dest, destIndex, 0, elementCount, elementCount, typeof(T));
}
public void CopyTo(T[] dest, int destIndex, int sourceIndex, int count) {
var elementCount = ElementCount;
Reference.CopyTo(dest, destIndex, sourceIndex, count, elementCount, typeof(T));
}
public void CopyTo(UnmanagedRef<T> dest) {
var elementCount = ElementCount;
Reference.CopyTo(dest.Value, dest.ElementCount, 0, 0, elementCount, elementCount);
}
public void CopyTo(UnmanagedRef<T> dest, int destIndex) {
var elementCount = ElementCount;
Reference.CopyTo(dest.Value, dest.ElementCount, destIndex, 0, elementCount, elementCount);
}
public void CopyTo(UnmanagedRef<T> dest, int destIndex, int sourceIndex, int count) {
var elementCount = ElementCount;
Reference.CopyTo(dest.Value, dest.ElementCount, destIndex, sourceIndex, count, -1);
}
public void CopyTo(T* dest, int destLength) {
var elementCount = ElementCount;
Reference.CopyTo(dest, destLength, 0, 0, elementCount, elementCount);
}
public void CopyTo(T* dest, int destLength, int destIndex) {
var elementCount = ElementCount;
Reference.CopyTo(dest, destLength, destIndex, 0, elementCount, elementCount);
}
public void CopyTo(T* dest, int destLength, int destIndex, int sourceIndex, int count) {
Reference.CopyTo(dest, destLength, destIndex, sourceIndex, count, -1);
}
public T[] ToArray() {
if (!HasValue) {
throw new NullReferenceException($"Error in UnmanagedRef<{typeof(T)}>.ToArray: HasValue was false");
}
var elementCount = ElementCount;
var items = new T[elementCount];
Reference.CopyTo(items, 0, 0, elementCount, elementCount, typeof(T));
return items;
}
#endregion
public override string ToString() {
var elementCount = ElementCount;
if (elementCount > 1) {
return $"UnmanagedRef<{GetType().GenericTypeArguments[0].Name}>(ElementCount={elementCount})";
}
var ptr = Value;
return ptr == null ? string.Empty : (*ptr).ToString();
}
#region Equality
public override bool Equals(object obj) {
return obj is UnmanagedRef<T> @ref &&
Reference.Equals(@ref.Reference);
}
public bool Equals(UnmanagedRef<T> other) {
return Reference.Equals(other.Reference);
}
public override int GetHashCode() {
return Reference.GetHashCode();
}
public bool Equals(UnmanagedRef other) {
return Reference.Equals(other);
}
public static bool operator ==(UnmanagedRef<T> left, UnmanagedRef<T> right) {
return left.Equals(right);
}
public static bool operator !=(UnmanagedRef<T> left, UnmanagedRef<T> right) {
return !(left == right);
}
#endregion
public static explicit operator IntPtr(UnmanagedRef<T> uref) {
return (IntPtr)uref.Reference;
}
public static explicit operator UnmanagedRef(UnmanagedRef<T> uref) {
return uref.Reference;
}
public static explicit operator UnmanagedRef<T>(UnmanagedRef uref) {
return new UnmanagedRef<T>(uref);
}
public T* this[int index] {
get {
if (index < 0 || index >= ElementCount) {
throw new ArgumentOutOfRangeException(nameof(index));
}
return Value + index;
}
}
public int ElementCount {
get {
if (!HasValue) {
return 0;
}
var packedValue = Reference.PointerPackedValue;
if (packedValue == 0) {
if (Reference.Version.Item.HasElementCount) {
return Reference.Version.Item.ElementCount;
}
// this is the slow path
return Size / sizeof(T);
}
return packedValue;
}
}
public Arena Arena { get { return Reference.Arena; } }
public T* Value { get { return (T*)Reference.Value; } }
public bool HasValue { get { return Reference.HasValue; } }
public RefVersion Version { get { return Reference.Version; } }
public int Size { get { return Reference.Size; } }
UnmanagedRef IUnmanagedRef.Reference { get { return Reference; } }
}
[DebuggerTypeProxy(typeof(UnmanagedRefDebugView))]
[DebuggerDisplay("{HasValue ? ToString() : null}")]
[StructLayout(LayoutKind.Sequential)]
unsafe readonly public struct UnmanagedRef : IUnmanagedRef, IEquatable<UnmanagedRef> {
private readonly BitpackedPtr pointer;
private readonly RefVersion version;
public UnmanagedRef(IntPtr pointer, RefVersion version, int elementCount) {
if (elementCount > 0 && elementCount <= BitpackedPtr.LowerMask) {
this.pointer = new BitpackedPtr(pointer, elementCount);
}
else {
this.pointer = new BitpackedPtr(pointer, 0);
}
this.version = version;
}
public bool TryGetValue(out IntPtr ptr) {
ptr = Value;
return ptr != IntPtr.Zero;
}
#region Copying
public void CopyTo<T>(T[] dest) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest, 0, 0, elementCount, elementCount);
}
public void CopyTo<T>(T[] dest, int destIndex) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest, destIndex, 0, elementCount, elementCount);
}
public void CopyTo<T>(T[] dest, int destIndex, int sourceIndex, int count) where T : unmanaged {
CopyTo(dest, destIndex, sourceIndex, count, -1);
}
public void CopyTo<T>(UnmanagedRef<T> dest) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest.Value, dest.ElementCount, 0, 0, elementCount, elementCount);
}
public void CopyTo<T>(UnmanagedRef<T> dest, int destIndex) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest.Value, dest.ElementCount, destIndex, 0, elementCount, elementCount);
}
public void CopyTo<T>(UnmanagedRef<T> dest, int destIndex, int sourceIndex, int count) where T : unmanaged {
CopyTo(dest.Value, dest.ElementCount, destIndex, sourceIndex, count, -1);
}
public void CopyTo<T>(T* dest, int destLength) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest, destLength, 0, 0, elementCount, elementCount);
}
public void CopyTo<T>(T* dest, int destLength, int destIndex) where T : unmanaged {
var elementCount = ElementCount;
CopyTo(dest, destLength, destIndex, 0, elementCount, elementCount);
}
public void CopyTo<T>(T* dest, int destLength, int destIndex, int sourceIndex, int count) where T : unmanaged {
CopyTo(dest, destLength, destIndex, sourceIndex, count, -1);
}
internal void CopyTo<T>(T[] dest, int destIndex, int sourceIndex, int count, int elementCount, Type type = null) where T : unmanaged {
if (dest is null) {
throw new ArgumentNullException(nameof(dest));
}
fixed (T* destPtr = dest) {
CopyTo(destPtr, dest.Length, destIndex, sourceIndex, count, elementCount, type);
}
}
internal void CopyTo<T>(T* dest, int destLength, int destIndex, int sourceIndex, int count, int elementCount, Type type = null) where T : unmanaged {
if (!HasValue) {
throw new NullReferenceException($"Error in UnmanagedRef.CopyTo: HasValue was false");
}
if (destLength < 0) {
throw new ArgumentOutOfRangeException(nameof(destLength));
}
if (count < 0) {
throw new ArgumentOutOfRangeException(nameof(count));
}
if (destIndex < 0 || destIndex + count > destLength) {
throw new ArgumentOutOfRangeException(nameof(destIndex));
}
if (elementCount < 0) {
elementCount = ElementCount;
}
if (sourceIndex < 0 || sourceIndex + count > elementCount) {
throw new ArgumentOutOfRangeException(nameof(sourceIndex));
}
if (count == 0) {
return;
}
if (dest == null) {
throw new ArgumentNullException(nameof(dest));
}
type = type ?? Type;
var info = GetTypeInfo(type);
var elementSize = info.Size;
var cur = RawUnsafePointer + elementSize * sourceIndex;
for (int i = 0; i < count; i++) {
dest[destIndex + i] = *(T*)cur;
cur += elementSize;
}
}
public T[] ToArray<T>() where T : unmanaged {
if (!HasValue) {
throw new NullReferenceException($"Error in UnmanagedRef.ToArray: HasValue was false");
}
var elementCount = ElementCount;
var items = new T[elementCount];
CopyTo(items, 0, 0, elementCount, elementCount);
return items;
}
public object[] ToArray() {
if (!HasValue) {
throw new NullReferenceException($"Error in UnmanagedRef.ToArray: HasValue was false");
}
var elementCount = ElementCount;
var items = new object[elementCount];
var info = GetTypeInfo(TypeHandle);
var elementSize = info.Size;
var cur = RawUnsafePointer;
for (int i = 0; i < elementCount; i++) {
items[i] = info.PtrToStruct(cur);
cur += elementSize;
}
return items;
}
#endregion
public override string ToString() {
var elementCount = ElementCount;
if (elementCount > 1) {
return $"UnmanagedRef(Type={Type}, ElementCount={elementCount})";
}
var ptr = Value;
if (ptr == IntPtr.Zero) {
return string.Empty;
}
TypeInfo info;
if (!TryGetTypeInfo(TypeHandle, out info)) {
return $"UnmanagedRef(Type=<invalid>, ElementCount={elementCount})";
}
return info.ToString(ptr);
}
public static explicit operator IntPtr(UnmanagedRef uref) {
return uref.Value;
}
public T* As<T>() where T : unmanaged {
if (Type != typeof(T)) {
return null;
}
return (T*)Value;
}
#region Equality
public override bool Equals(object obj) {
return obj is UnmanagedRef @ref &&
EqualityComparer<BitpackedPtr>.Default.Equals(pointer, @ref.pointer) &&
version.Equals(@ref.version);
}
public bool Equals(UnmanagedRef other) {
return
EqualityComparer<BitpackedPtr>.Default.Equals(pointer, other.pointer) &&
version.Equals(other.version);
}
public override int GetHashCode() {
int hashCode = 598475582;
hashCode = hashCode * -1521134295 + pointer.GetHashCode();
hashCode = hashCode * -1521134295 + version.GetHashCode();
return hashCode;
}
public static bool operator ==(UnmanagedRef left, UnmanagedRef right) {
return left.Equals(right);
}
public static bool operator !=(UnmanagedRef left, UnmanagedRef right) {
return !(left == right);
}
#endregion
//public IntPtr this[int index] {
// get {
// if (index < 0 || index >= ElementCount) {
// throw new ArgumentOutOfRangeException(nameof(index));
// }
// return Value + index;
// }
//}
public Type Type {
get {
if (!HasValue) {
return null;
}
Type type;
if (!TryGetTypeFromHandle(Arena.ItemHeader.GetTypeHandle(pointer.Value), out type)) {
return null;
}
return type;
}
}
public TypeHandle TypeHandle {
get {
if (!HasValue) {
return TypeHandle.None;
}
return Arena.ItemHeader.GetTypeHandle(pointer.Value);
}
}
public Arena Arena {
get {
return Arena.Get(version.Arena);
}
}
public IntPtr Value {
get {
var arena = Arena;
if (arena is null) {
return IntPtr.Zero;
}
var ptr = pointer.Value;
return ptr != IntPtr.Zero && !arena.VersionsMatch(version, ptr) ? IntPtr.Zero : ptr;
}
}
public bool HasValue {
get {
var arena = Arena;
if (arena is null) {
return false;
}
var ptr = pointer.Value;
return ptr != IntPtr.Zero && arena.VersionsMatch(version, ptr);
}
}
public int ElementCount {
get {
if (!HasValue) {
return 0;
}
var packedValue = pointer.PackedValue;
if (packedValue == 0) {
if (version.Item.HasElementCount) {
return version.Item.ElementCount;
}
// this is the slow path
var handle = TypeHandle;
TypeInfo info;
if (!TryGetTypeInfo(handle, out info)) {
return 0;
}
return Size / info.Size;
}
return packedValue;
}
}
public int Size {
get {
var ptr = pointer.Value;
var arena = Arena;
if (arena is null || !arena.VersionsMatch(version, ptr)) {
return 0;
}
return Arena.ItemHeader.GetSize(ptr);
}
}
public RefVersion Version { get { return version; } }
public IntPtr RawUnsafePointer { get { return pointer.Value; } }
internal int PointerPackedValue { get { return pointer.PackedValue; } }
UnmanagedRef IUnmanagedRef.Reference { get { return this; } }
}
}