ArrayPtr.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Common.Memory
  6. {
  7. /// <summary>
  8. /// Represents an array of unmanaged resources.
  9. /// </summary>
  10. /// <typeparam name="T">Array element type</typeparam>
  11. public unsafe struct ArrayPtr<T> : IEquatable<ArrayPtr<T>>, IArray<T> where T : unmanaged
  12. {
  13. private IntPtr _ptr;
  14. /// <summary>
  15. /// Null pointer.
  16. /// </summary>
  17. public static ArrayPtr<T> Null => new ArrayPtr<T>() { _ptr = IntPtr.Zero };
  18. /// <summary>
  19. /// True if the pointer is null, false otherwise.
  20. /// </summary>
  21. public bool IsNull => _ptr == IntPtr.Zero;
  22. /// <summary>
  23. /// Number of elements on the array.
  24. /// </summary>
  25. public int Length { get; }
  26. /// <summary>
  27. /// Gets a reference to the item at the given index.
  28. /// </summary>
  29. /// <remarks>
  30. /// No bounds checks are performed, this allows negative indexing,
  31. /// but care must be taken if the index may be out of bounds.
  32. /// </remarks>
  33. /// <param name="index">Index of the element</param>
  34. /// <returns>Reference to the element at the given index</returns>
  35. public ref T this[int index] => ref Unsafe.AsRef<T>((T*)_ptr + index);
  36. /// <summary>
  37. /// Creates a new array from a given reference.
  38. /// </summary>
  39. /// <remarks>
  40. /// For data on the heap, proper pinning is necessary during
  41. /// use. Failure to do so will result in memory corruption and crashes.
  42. /// </remarks>
  43. /// <param name="value">Reference of the first array element</param>
  44. /// <param name="length">Number of elements on the array</param>
  45. public ArrayPtr(ref T value, int length)
  46. {
  47. _ptr = (IntPtr)Unsafe.AsPointer(ref value);
  48. Length = length;
  49. }
  50. /// <summary>
  51. /// Creates a new array from a given pointer.
  52. /// </summary>
  53. /// <param name="ptr">Array base pointer</param>
  54. /// <param name="length">Number of elements on the array</param>
  55. public ArrayPtr(T* ptr, int length)
  56. {
  57. _ptr = (IntPtr)ptr;
  58. Length = length;
  59. }
  60. /// <summary>
  61. /// Creates a new array from a given pointer.
  62. /// </summary>
  63. /// <param name="ptr">Array base pointer</param>
  64. /// <param name="length">Number of elements on the array</param>
  65. public ArrayPtr(IntPtr ptr, int length)
  66. {
  67. _ptr = ptr;
  68. Length = length;
  69. }
  70. /// <summary>
  71. /// Splits the array starting at the specified position.
  72. /// </summary>
  73. /// <param name="start">Index where the new array should start</param>
  74. /// <returns>New array starting at the specified position</returns>
  75. public ArrayPtr<T> Slice(int start) => new ArrayPtr<T>(ref this[start], Length - start);
  76. /// <summary>
  77. /// Gets a span from the array.
  78. /// </summary>
  79. /// <returns>Span of the array</returns>
  80. public Span<T> ToSpan() => Length == 0 ? Span<T>.Empty : MemoryMarshal.CreateSpan(ref this[0], Length);
  81. /// <summary>
  82. /// Gets the array base pointer.
  83. /// </summary>
  84. /// <returns>Base pointer</returns>
  85. public T* ToPointer() => (T*)_ptr;
  86. public override bool Equals(object obj)
  87. {
  88. return obj is ArrayPtr<T> other && Equals(other);
  89. }
  90. public bool Equals([AllowNull] ArrayPtr<T> other)
  91. {
  92. return _ptr == other._ptr && Length == other.Length;
  93. }
  94. public override int GetHashCode()
  95. {
  96. return HashCode.Combine(_ptr, Length);
  97. }
  98. public static bool operator ==(ArrayPtr<T> left, ArrayPtr<T> right)
  99. {
  100. return left.Equals(right);
  101. }
  102. public static bool operator !=(ArrayPtr<T> left, ArrayPtr<T> right)
  103. {
  104. return !(left == right);
  105. }
  106. }
  107. }