-
Notifications
You must be signed in to change notification settings - Fork 5
/
Pointer.cs
643 lines (516 loc) · 18.3 KB
/
Pointer.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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using RazorSharp.CoreClr;
using RazorSharp.CoreClr.Meta;
using RazorSharp.Interop;
using SimpleSharp.Diagnostics;
using SimpleSharp.Strings;
using SimpleSharp.Strings.Formatting;
using SimpleSharp.Utilities;
namespace RazorSharp.Memory.Pointers
{
/// <summary>
/// <para>Represents a native pointer. Equals the size of <see cref="P:System.IntPtr.Size" />.</para>
/// <para>Can be represented as a native pointer in memory. </para>
/// <para>Has identical or better performance than native pointers.</para>
/// <para>Type safety is not enforced in methods suffixed with "Any" for accessibility</para>
/// <para>
/// Supports pointer arithmetic, reading/writing different types other than
/// type <typeparamref name="T" /> (in select methods), and bitwise operations.
/// </para>
/// <list type="bullet">
/// <item>
/// <description>No bounds checking</description>
/// </item>
/// <item>
/// <description>No safety systems</description>
/// </item>
/// <item>
/// <description>Minimum type safety</description>
/// </item>
/// </list>
/// <remarks>
/// Note: <c>AltPointer<byte></c> is used as an opaque pointer where applicable.
/// </remarks>
/// </summary>
/// <typeparam name="T">Element type to point to</typeparam>
public unsafe struct Pointer<T> : IFormattable
{
/// <summary>
/// <para>The address we're pointing to.</para>
/// <para>We want this to be the only field so it can be represented as a pointer in memory.</para>
/// </summary>
private void* m_value;
#region Properties
/// <summary>
/// Indexes <see cref="Address" /> as a reference.
/// </summary>
public ref T this[int index] => ref AsRef(index);
/// <summary>
/// Returns the value as a reference.
/// </summary>
public ref T Reference => ref AsRef();
/// <summary>
/// Dereferences the pointer as the specified type.
/// </summary>
public T Value {
get => Read();
set => Write(value);
}
/// <summary>
/// Address being pointed to.
/// </summary>
public IntPtr Address {
get => (IntPtr) m_value;
set => m_value = (void*) value;
}
/// <summary>
/// Size of type <typeparamref name="T" />.
/// </summary>
public int ElementSize => Unsafe.SizeOf<T>();
/// <summary>
/// Whether <see cref="Address" /> is <c>null</c> (<see cref="IntPtr.Zero" />).
/// </summary>
public bool IsNull => this == Mem.Nullptr;
/// <summary>
/// Whether the value being pointed to is <c>default</c> or <c>null</c> bytes,
/// or this pointer is <c>null</c>.
/// </summary>
public bool IsNil => Runtime.Info.IsNil(Reference);
#endregion
#region Constructors
/// <summary>
/// Creates a new <see cref="T:RazorSharp.Memory.Pointers.AltPointer`1" /> pointing to the address <paramref name="value" />
/// </summary>
/// <param name="value">Address to point to</param>
public Pointer(IntPtr value) : this(value.ToPointer()) { }
/// <summary>
/// Creates a new <see cref="Pointer{T}" /> pointing to the address <paramref name="value" />
/// </summary>
/// <remarks>
/// Root constructor.
/// </remarks>
/// <param name="value">Address to point to</param>
public Pointer(void* value)
{
m_value = value;
}
#endregion
#region Collection-esque operations
[Pure]
public IEnumerator<T> GetEnumerator(int elemCount)
{
for (int i = 0; i < elemCount; i++) {
yield return this[i];
}
}
#endregion
#region Offset
// (void*) (((long) m_value) + byteOffset)
// (void*) (((long) m_value) + (elemOffset * ElementSize))
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void* OffsetFast(int elemCnt) => (void*) ((long) m_value + (FullSize(elemCnt)));
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int FullSize(int elemCnt) => elemCnt * ElementSize;
/// <summary>
/// Returns the element index of a pointer relative to <see cref="Address" />
/// </summary>
/// <param name="current">Current pointer (high address)</param>
/// <returns>The index</returns>
[Pure]
public int OffsetIndex(Pointer<byte> current)
{
long delta = current.ToInt64() - ToInt64();
return (int) delta / ElementSize;
}
#endregion
#region Read / write
#region Pointer
[Pure]
public Pointer<byte> ReadPointer(int elemOffset = 0) => ReadPointer<byte>(elemOffset);
[Pure]
public Pointer<TType> ReadPointer<TType>(int elemOffset = 0)
{
return Cast<Pointer<TType>>().Read(elemOffset);
}
public void WritePointer<TType>(Pointer<TType> ptr, int elemOffset = 0)
{
Cast<Pointer<TType>>().Write(ptr, elemOffset);
}
#endregion
#region WriteAll
/// <summary>
/// Writes all elements of <paramref name="enumerable" /> to the current pointer.
/// </summary>
/// <param name="enumerable">Values to write</param>
public void WriteAll(IEnumerable<T> enumerable)
{
int i = 0;
IEnumerator<T> enumerator = enumerable.GetEnumerator();
while (enumerator.MoveNext())
this[i++] = enumerator.Current;
enumerator.Dispose();
}
/// <summary>
/// Writes all elements of <paramref name="values" /> to the current pointer.
/// </summary>
/// <param name="values">Values to write</param>
public void WriteAll(params T[] values)
{
Conditions.NotNull(values, nameof(values));
Conditions.Require(values.Length > 0);
for (int i = 0; i < values.Length; i++)
this[i] = values[i];
}
#endregion
#region Any
private MethodInfo GetMethod(Type t, string name, out object ptr)
{
ptr = CastAny(t);
var fn = ptr.GetType().GetMethod(name);
return fn;
}
public void WriteAny(Type type, object value, int elemOffset = 0)
{
var fn = GetMethod(type, nameof(Write), out var ptr);
fn.Invoke(ptr, new object[] {value, elemOffset});
}
public object ReadAny(Type type, int elemOffset = 0)
{
var fn = GetMethod(type, nameof(Read), out var ptr);
return fn.Invoke(ptr, new object[] {elemOffset});
}
#endregion
/// <summary>
/// Writes a value of type <typeparamref name="T" /> to <see cref="Address" />
/// </summary>
/// <param name="value">Value to write</param>
/// <param name="elemOffset">Element offset (of type <typeparamref name="T" />)</param>
public void Write(T value, int elemOffset = 0) => Unsafe.Write(OffsetFast(elemOffset), value);
/// <summary>
/// Reads a value of type <typeparamref name="T" /> from <see cref="Address" />
/// </summary>
/// <param name="elemOffset">Element offset (of type <typeparamref name="T" />)</param>
/// <returns>The value read from the offset <see cref="Address" /></returns>
[Pure]
public T Read(int elemOffset = 0) => Unsafe.Read<T>(OffsetFast(elemOffset));
/// <summary>
/// Reinterprets <see cref="Address" /> as a reference to a value of type <typeparamref name="T" />
/// </summary>
/// <param name="elemOffset">Element offset (of type <typeparamref name="T" />)</param>
/// <returns>A reference to a value of type <typeparamref name="T" /></returns>
[Pure]
public ref T AsRef(int elemOffset = 0) => ref Unsafe.AsRef<T>(OffsetFast(elemOffset));
#region Copy
/// <summary>
/// Copies <paramref name="elemCnt" /> elements into an array of type <typeparamref name="T" />,
/// starting from index <paramref name="startIndex" />
/// </summary>
/// <param name="startIndex">Index to begin copying from</param>
/// <param name="elemCnt">Number of elements to copy</param>
/// <returns>
/// An array of length <paramref name="elemCnt" /> of type <typeparamref name="T" /> copied from
/// the current pointer
/// </returns>
[Pure]
public T[] Copy(int startIndex, int elemCnt)
{
var rg = new T[elemCnt];
for (int i = startIndex; i < elemCnt + startIndex; i++)
rg[i - startIndex] = this[i];
return rg;
}
/// <summary>
/// Copies <paramref name="elemCnt" /> elements into an array of type <typeparamref name="T" />,
/// starting from index 0.
/// </summary>
/// <param name="elemCnt">Number of elements to copy</param>
/// <returns>
/// An array of length <paramref name="elemCnt" /> of type <typeparamref name="T" /> copied from
/// the current pointer
/// </returns>
[Pure]
public T[] Copy(int elemCnt)
{
return Copy(0, elemCnt);
}
[Pure]
public byte[] CopyBytes(int elemCnt) => Cast().Copy(elemCnt);
[Pure]
public byte[] CopyBytes(int startIndex, int elemCnt) => Cast().Copy(startIndex, elemCnt);
#endregion
#endregion
#region Other methods
[Pure]
internal static bool IsCharPointer() => typeof(T) == typeof(char);
[Pure]
public Pointer<T> AddressOfIndex(int index) => OffsetFast(index);
/// <summary>
/// Zeros <paramref name="elemCnt"/> elements.
/// </summary>
/// <param name="elemCnt">Number of elements to zero</param>
public void Clear(int elemCnt = 1)
{
for (int i = 0; i < elemCnt; i++)
this[i] = default;
}
/// <summary>
/// Zeros <paramref name="byteCnt"/> bytes.
/// </summary>
/// <param name="byteCnt">Number of bytes to zero</param>
public void ClearBytes(int byteCnt)
{
var bytePtr = Cast();
for (int i = 0; i < byteCnt; i++)
bytePtr[i] = default;
}
#region Cast
/// <summary>
/// Creates a new <see cref="Pointer{T}" /> of type <typeparamref name="TNew" />, pointing to
/// <see cref="Address" />
/// </summary>
/// <typeparam name="TNew">Type to point to</typeparam>
/// <returns>A new <see cref="Pointer{T}" /> of type <typeparamref name="TNew" /></returns>
public Pointer<TNew> Cast<TNew>() => new Pointer<TNew>(Address);
public Pointer<byte> Cast() => Cast<byte>();
public object CastAny(Type type)
{
var cast = GetType().GetMethods().First(f => f.Name == nameof(Cast) && f.IsGenericMethod);
var ptr = Functions.Reflection.CallGeneric(cast, type, this);
return ptr;
}
#endregion
/// <summary>
/// Returns <see cref="Address" /> as a native pointer.
/// </summary>
/// <returns></returns>
[Pure]
public void* ToPointer() => m_value;
[Pure]
public TUnmanaged* ToPointer<TUnmanaged>() where TUnmanaged : unmanaged => (TUnmanaged*) m_value;
#region Integer conversions
/// <summary>
/// Converts <see cref="Address" /> to a 32-bit signed integer.
/// </summary>
/// <returns></returns>
[Pure]
public int ToInt32() => (int) m_value;
/// <summary>
/// Converts <see cref="Address" /> to a 64-bit signed integer.
/// </summary>
/// <returns></returns>
[Pure]
public long ToInt64() => (long) m_value;
/// <summary>
/// Converts <see cref="Address" /> to a 64-bit unsigned integer.
/// </summary>
/// <returns></returns>
[Pure]
public ulong ToUInt64() => (ulong) m_value;
/// <summary>
/// Converts <see cref="Address" /> to a 32-bit unsigned integer.
/// </summary>
/// <returns></returns>
[Pure]
public uint ToUInt32() => (uint) m_value;
#endregion
#endregion
#region Operators
#region Implicit and explicit conversions
public static explicit /*implicit*/ operator bool(Pointer<T> ptr) => !ptr.IsNil;
public static implicit operator Pointer<T>(Pointer<byte> v) => v.Address;
public static implicit operator Pointer<T>(void* v) => new Pointer<T>(v);
public static implicit operator Pointer<T>(IntPtr p) => new Pointer<T>(p.ToPointer());
public static explicit operator Pointer<T>(long l) => new Pointer<T>((void*) l);
public static explicit operator Pointer<T>(ulong ul) => new Pointer<T>((void*) ul);
public static explicit operator void*(Pointer<T> ptr) => ptr.ToPointer();
public static explicit operator long(Pointer<T> ptr) => ptr.ToInt64();
public static explicit operator ulong(Pointer<T> ptr) => ptr.ToUInt64();
public static explicit operator Pointer<byte>(Pointer<T> ptr) => ptr.ToPointer();
#endregion
#region Arithmetic
public static Pointer<T> operator +(Pointer<T> left, long right)
{
return (void*) (left.ToInt64() + right);
}
public static Pointer<T> operator -(Pointer<T> left, long right)
{
return (void*) (left.ToInt64() - right);
}
public static Pointer<T> operator +(Pointer<T> left, Pointer<T> right)
{
return (void*) (left.ToInt64() + right.ToInt64());
}
public static Pointer<T> operator -(Pointer<T> left, Pointer<T> right)
{
return (void*) (left.ToInt64() - right.ToInt64());
}
/// <summary>
/// Increment <see cref="Address" /> by the specified number of bytes
/// </summary>
/// <param name="byteCnt">Number of bytes to add</param>
/// <returns>
/// A new <see cref="Pointer{T}"/> with <paramref name="byteCnt"/> bytes added
/// </returns>
[Pure]
public Pointer<T> Add(long byteCnt = 1)
{
// m_value = PointerUtil.Add(m_value, bytes).ToPointer();
// return this;
long val = ToInt64() + byteCnt;
return (void*) val;
}
/// <summary>
/// Decrement <see cref="Address" /> by the specified number of bytes
/// </summary>
/// <param name="byteCnt">Number of bytes to subtract</param>
/// <returns>
/// A new <see cref="Pointer{T}"/> with <paramref name="byteCnt"/> bytes subtracted
/// </returns>
[Pure]
public Pointer<T> Subtract(long byteCnt = 1) => Add(-byteCnt);
/// <summary>
/// Increment <see cref="Address" /> by the specified number of elements
/// </summary>
/// <param name="elemCnt">Number of elements</param>
/// <returns>
/// A new <see cref="Pointer{T}"/> with <paramref name="elemCnt"/> elements incremented
/// </returns>
[Pure]
public Pointer<T> Increment(int elemCnt = 1)
{
return OffsetFast(elemCnt);
}
/// <summary>
/// Decrement <see cref="Address" /> by the specified number of elements
/// </summary>
/// <param name="elemCnt">Number of elements</param>
/// <returns>
/// A new <see cref="Pointer{T}"/> with <paramref name="elemCnt"/> elements decremented
/// </returns>
[Pure]
public Pointer<T> Decrement(int elemCnt = 1) => Increment(-elemCnt);
/// <summary>
/// Increments the <see cref="Address" /> by the specified number of elements.
/// <remarks>
/// Equal to <see cref="Pointer{T}.Increment" />
/// </remarks>
/// </summary>
/// <param name="p">
/// <see cref="Pointer{T}" />
/// </param>
/// <param name="i">Number of elements (<see cref="ElementSize" />)</param>
public static Pointer<T> operator +(Pointer<T> p, int i)
{
return p.Increment(i);
}
/// <summary>
/// Decrements the <see cref="Address" /> by the specified number of elements.
/// <remarks>
/// Equal to <see cref="Pointer{T}.Decrement" />
/// </remarks>
/// </summary>
/// <param name="p">
/// <see cref="Pointer{T}" />
/// </param>
/// <param name="i">Number of elements (<see cref="ElementSize" />)</param>
public static Pointer<T> operator -(Pointer<T> p, int i)
{
return p.Decrement(i);
}
/// <summary>
/// Increments the <see cref="Pointer{T}" /> by one element.
/// </summary>
/// <param name="p">
/// <see cref="Pointer{T}" />
/// </param>
/// <returns>The offset <see cref="Address" /></returns>
public static Pointer<T> operator ++(Pointer<T> p)
{
return p.Increment();
}
/// <summary>
/// Decrements the <see cref="Pointer{T}" /> by one element.
/// </summary>
/// <param name="p">
/// <see cref="Pointer{T}" />
/// </param>
/// <returns>The offset <see cref="Address" /></returns>
public static Pointer<T> operator --(Pointer<T> p)
{
return p.Decrement();
}
public static bool operator >=(Pointer<T> left, Pointer<T> right) => left.ToInt64() >= right.ToInt64();
public static bool operator <=(Pointer<T> left, Pointer<T> right) => left.ToInt64() <= right.ToInt64();
/// <summary>
/// Checks if <paramref name="left" /> <see cref="Address" /> is higher than <paramref name="right" />'s.
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>
/// <c>true</c> if <paramref name="left" /> points to a higher address than <paramref name="right" />;
/// <c>false</c> otherwise
/// </returns>
public static bool operator >(Pointer<T> left, Pointer<T> right) => left.ToInt64() > right.ToInt64();
/// <summary>
/// Checks if <paramref name="left" /> <see cref="Address" /> is lower than <paramref name="right" />'s.
/// </summary>
/// <param name="left">Left operand</param>
/// <param name="right">Right operand</param>
/// <returns>
/// <c>true</c> if <paramref name="left" /> points to a lower address than <paramref name="right" />; <c>false</c>
/// otherwise
/// </returns>
public static bool operator <(Pointer<T> left, Pointer<T> right) => left.ToInt64() < right.ToInt64();
#endregion
#region Equality operators
/// <summary>
/// Checks to see if <see cref="other" /> is equal to the current instance.
/// </summary>
/// <param name="other">Other <see cref="Pointer{T}" /></param>
/// <returns></returns>
public bool Equals(Pointer<T> other) => Address == other.Address;
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
return obj is Pointer<T> pointer && Equals(pointer);
}
public override int GetHashCode()
{
// ReSharper disable once NonReadonlyMemberInGetHashCode
return unchecked((int) (long) m_value);
}
public static bool operator ==(Pointer<T> left, Pointer<T> right) => left.Equals(right);
public static bool operator !=(Pointer<T> left, Pointer<T> right) => !left.Equals(right);
#endregion
#endregion
#region Overrides
/// <inheritdoc />
/// <summary>
/// </summary>
/// <param name="format">
/// See <see cref="Handle" /> for format options
/// </param>
/// <param name="formatProvider"></param>
/// <returns></returns>
[Pure]
public string ToString(string format, IFormatProvider formatProvider)
{
return Handle.FormatPointer(this, format, formatProvider);
}
[Pure]
public string ToString(string format) => ToString(format, CultureInfo.CurrentCulture);
[Pure]
public override string ToString() => ToString(Handle.DefaultFormat);
#endregion
}
}