SpanIOHelper.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Audio.Renderer.Utils
  6. {
  7. /// <summary>
  8. /// Helper for IO operations on <see cref="Span{T}"/> and <see cref="Memory{T}"/>.
  9. /// </summary>
  10. public static class SpanIOHelper
  11. {
  12. /// <summary>
  13. /// Write the given data to the given backing <see cref="Memory{T}"/> and move cursor after the written data.
  14. /// </summary>
  15. /// <typeparam name="T">The data type.</typeparam>
  16. /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to store the data.</param>
  17. /// <param name="data">The data to write to the backing <see cref="Memory{T}"/>.</param>
  18. public static void Write<T>(ref Memory<byte> backingMemory, ref T data) where T : unmanaged
  19. {
  20. int size = Unsafe.SizeOf<T>();
  21. if (size > backingMemory.Length)
  22. {
  23. throw new ArgumentOutOfRangeException();
  24. }
  25. MemoryMarshal.Write<T>(backingMemory.Span.Slice(0, size), ref data);
  26. backingMemory = backingMemory.Slice(size);
  27. }
  28. /// <summary>
  29. /// Write the given data to the given backing <see cref="Span{T}"/> and move cursor after the written data.
  30. /// </summary>
  31. /// <typeparam name="T">The data type.</typeparam>
  32. /// <param name="backingMemory">The backing <see cref="Span{T}"/> to store the data.</param>
  33. /// <param name="data">The data to write to the backing <see cref="Span{T}"/>.</param>
  34. public static void Write<T>(ref Span<byte> backingMemory, ref T data) where T : unmanaged
  35. {
  36. int size = Unsafe.SizeOf<T>();
  37. if (size > backingMemory.Length)
  38. {
  39. throw new ArgumentOutOfRangeException();
  40. }
  41. MemoryMarshal.Write<T>(backingMemory.Slice(0, size), ref data);
  42. backingMemory = backingMemory.Slice(size);
  43. }
  44. /// <summary>
  45. /// Get a <see cref="Span{T}"/> out of a <paramref name="backingMemory"/> and move cursor after T size.
  46. /// </summary>
  47. /// <typeparam name="T">The data type.</typeparam>
  48. /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to get a <see cref="Span{T}"/> from.</param>
  49. /// <returns>A <see cref="Span{T}"/> from backing <see cref="Memory{T}"/>.</returns>
  50. public static Span<T> GetWriteRef<T>(ref Memory<byte> backingMemory) where T : unmanaged
  51. {
  52. int size = Unsafe.SizeOf<T>();
  53. if (size > backingMemory.Length)
  54. {
  55. throw new ArgumentOutOfRangeException();
  56. }
  57. Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Span.Slice(0, size));
  58. backingMemory = backingMemory.Slice(size);
  59. return result;
  60. }
  61. /// <summary>
  62. /// Get a <see cref="Span{T}"/> out of a backingMemory and move cursor after T size.
  63. /// </summary>
  64. /// <typeparam name="T">The data type.</typeparam>
  65. /// <param name="backingMemory">The backing <see cref="Span{T}"/> to get a <see cref="Span{T}"/> from.</param>
  66. /// <returns>A <see cref="Span{T}"/> from backing <see cref="Span{T}"/>.</returns>
  67. public static Span<T> GetWriteRef<T>(ref Span<byte> backingMemory) where T : unmanaged
  68. {
  69. int size = Unsafe.SizeOf<T>();
  70. if (size > backingMemory.Length)
  71. {
  72. throw new ArgumentOutOfRangeException();
  73. }
  74. Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Slice(0, size));
  75. backingMemory = backingMemory.Slice(size);
  76. return result;
  77. }
  78. /// <summary>
  79. /// Read data from the given backing <see cref="ReadOnlyMemory{T}"/> and move cursor after the read data.
  80. /// </summary>
  81. /// <typeparam name="T">The data type.</typeparam>
  82. /// <param name="backingMemory">The backing <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
  83. /// <returns>Return the read data.</returns>
  84. public static T Read<T>(ref ReadOnlyMemory<byte> backingMemory) where T : unmanaged
  85. {
  86. int size = Unsafe.SizeOf<T>();
  87. if (size > backingMemory.Length)
  88. {
  89. throw new ArgumentOutOfRangeException();
  90. }
  91. T result = MemoryMarshal.Read<T>(backingMemory.Span.Slice(0, size));
  92. backingMemory = backingMemory.Slice(size);
  93. return result;
  94. }
  95. /// <summary>
  96. /// Read data from the given backing <see cref="ReadOnlySpan{T}"/> and move cursor after the read data.
  97. /// </summary>
  98. /// <typeparam name="T">The data type.</typeparam>
  99. /// <param name="backingMemory">The backing <see cref="ReadOnlySpan{T}"/> to read data from.</param>
  100. /// <returns>Return the read data.</returns>
  101. public static T Read<T>(ref ReadOnlySpan<byte> backingMemory) where T : unmanaged
  102. {
  103. int size = Unsafe.SizeOf<T>();
  104. if (size > backingMemory.Length)
  105. {
  106. throw new ArgumentOutOfRangeException();
  107. }
  108. T result = MemoryMarshal.Read<T>(backingMemory.Slice(0, size));
  109. backingMemory = backingMemory.Slice(size);
  110. return result;
  111. }
  112. /// <summary>
  113. /// Extract a <see cref="Memory{T}"/> at the given index.
  114. /// </summary>
  115. /// <typeparam name="T">The data type.</typeparam>
  116. /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
  117. /// <param name="id">The id in the provided memory.</param>
  118. /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
  119. /// <returns>a <see cref="Memory{T}"/> at the given id.</returns>
  120. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  121. public static Memory<T> GetMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
  122. {
  123. Debug.Assert(id >= 0 && id < count);
  124. return memory.Slice(id, 1);
  125. }
  126. /// <summary>
  127. /// Extract a ref T at the given index.
  128. /// </summary>
  129. /// <typeparam name="T">The data type.</typeparam>
  130. /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
  131. /// <param name="id">The id in the provided memory.</param>
  132. /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
  133. /// <returns>a ref T at the given id.</returns>
  134. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  135. public static ref T GetFromMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
  136. {
  137. return ref GetMemory(memory, id, count).Span[0];
  138. }
  139. }
  140. }