SpanIOHelper.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // Copyright (c) 2019-2021 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Diagnostics;
  19. using System.Runtime.CompilerServices;
  20. using System.Runtime.InteropServices;
  21. namespace Ryujinx.Audio.Renderer.Utils
  22. {
  23. /// <summary>
  24. /// Helper for IO operations on <see cref="Span{T}"/> and <see cref="Memory{T}"/>.
  25. /// </summary>
  26. public static class SpanIOHelper
  27. {
  28. /// <summary>
  29. /// Write the given data to the given backing <see cref="Memory{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="Memory{T}"/> to store the data.</param>
  33. /// <param name="data">The data to write to the backing <see cref="Memory{T}"/>.</param>
  34. public static void Write<T>(ref Memory<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.Span.Slice(0, size), ref data);
  42. backingMemory = backingMemory.Slice(size);
  43. }
  44. /// <summary>
  45. /// Write the given data to the given backing <see cref="Span{T}"/> and move cursor after the written data.
  46. /// </summary>
  47. /// <typeparam name="T">The data type.</typeparam>
  48. /// <param name="backingMemory">The backing <see cref="Span{T}"/> to store the data.</param>
  49. /// <param name="data">The data to write to the backing <see cref="Span{T}"/>.</param>
  50. public static void Write<T>(ref Span<byte> backingMemory, ref T data) where T : unmanaged
  51. {
  52. int size = Unsafe.SizeOf<T>();
  53. if (size > backingMemory.Length)
  54. {
  55. throw new ArgumentOutOfRangeException();
  56. }
  57. MemoryMarshal.Write<T>(backingMemory.Slice(0, size), ref data);
  58. backingMemory = backingMemory.Slice(size);
  59. }
  60. /// <summary>
  61. /// Get a <see cref="Span{T}"/> out of a <paramref name="backingMemory"/> and move cursor after T size.
  62. /// </summary>
  63. /// <typeparam name="T">The data type.</typeparam>
  64. /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to get a <see cref="Span{T}"/> from.</param>
  65. /// <returns>A <see cref="Span{T}"/> from backing <see cref="Memory{T}"/>.</returns>
  66. public static Span<T> GetWriteRef<T>(ref Memory<byte> backingMemory) where T : unmanaged
  67. {
  68. int size = Unsafe.SizeOf<T>();
  69. if (size > backingMemory.Length)
  70. {
  71. throw new ArgumentOutOfRangeException();
  72. }
  73. Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Span.Slice(0, size));
  74. backingMemory = backingMemory.Slice(size);
  75. return result;
  76. }
  77. /// <summary>
  78. /// Get a <see cref="Span{T}"/> out of a backingMemory and move cursor after T size.
  79. /// </summary>
  80. /// <typeparam name="T">The data type.</typeparam>
  81. /// <param name="backingMemory">The backing <see cref="Span{T}"/> to get a <see cref="Span{T}"/> from.</param>
  82. /// <returns>A <see cref="Span{T}"/> from backing <see cref="Span{T}"/>.</returns>
  83. public static Span<T> GetWriteRef<T>(ref Span<byte> backingMemory) where T : unmanaged
  84. {
  85. int size = Unsafe.SizeOf<T>();
  86. if (size > backingMemory.Length)
  87. {
  88. throw new ArgumentOutOfRangeException();
  89. }
  90. Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Slice(0, size));
  91. backingMemory = backingMemory.Slice(size);
  92. return result;
  93. }
  94. /// <summary>
  95. /// Read data from the given backing <see cref="ReadOnlyMemory{T}"/> and move cursor after the read data.
  96. /// </summary>
  97. /// <typeparam name="T">The data type.</typeparam>
  98. /// <param name="backingMemory">The backing <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
  99. /// <returns>Return the read data.</returns>
  100. public static T Read<T>(ref ReadOnlyMemory<byte> backingMemory) where T : unmanaged
  101. {
  102. int size = Unsafe.SizeOf<T>();
  103. if (size > backingMemory.Length)
  104. {
  105. throw new ArgumentOutOfRangeException();
  106. }
  107. T result = MemoryMarshal.Read<T>(backingMemory.Span.Slice(0, size));
  108. backingMemory = backingMemory.Slice(size);
  109. return result;
  110. }
  111. /// <summary>
  112. /// Read data from the given backing <see cref="ReadOnlySpan{T}"/> and move cursor after the read data.
  113. /// </summary>
  114. /// <typeparam name="T">The data type.</typeparam>
  115. /// <param name="backingMemory">The backing <see cref="ReadOnlySpan{T}"/> to read data from.</param>
  116. /// <returns>Return the read data.</returns>
  117. public static T Read<T>(ref ReadOnlySpan<byte> backingMemory) where T : unmanaged
  118. {
  119. int size = Unsafe.SizeOf<T>();
  120. if (size > backingMemory.Length)
  121. {
  122. throw new ArgumentOutOfRangeException();
  123. }
  124. T result = MemoryMarshal.Read<T>(backingMemory.Slice(0, size));
  125. backingMemory = backingMemory.Slice(size);
  126. return result;
  127. }
  128. /// <summary>
  129. /// Extract a <see cref="Memory{T}"/> at the given index.
  130. /// </summary>
  131. /// <typeparam name="T">The data type.</typeparam>
  132. /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
  133. /// <param name="id">The id in the provided memory.</param>
  134. /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
  135. /// <returns>a <see cref="Memory{T}"/> at the given id.</returns>
  136. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  137. public static Memory<T> GetMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
  138. {
  139. Debug.Assert(id >= 0 && id < count);
  140. return memory.Slice(id, 1);
  141. }
  142. /// <summary>
  143. /// Extract a ref T at the given index.
  144. /// </summary>
  145. /// <typeparam name="T">The data type.</typeparam>
  146. /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
  147. /// <param name="id">The id in the provided memory.</param>
  148. /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
  149. /// <returns>a ref T at the given id.</returns>
  150. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  151. public static ref T GetFromMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
  152. {
  153. return ref GetMemory(memory, id, count).Span[0];
  154. }
  155. }
  156. }