aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Audio/Renderer/Utils/SpanIOHelper.cs
blob: 103fb6a04fa0c9ca66fcd84fc57ffdee825cd877 (plain) (blame)
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
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace Ryujinx.Audio.Renderer.Utils
{
    /// <summary>
    /// Helper for IO operations on <see cref="Span{T}"/> and <see cref="Memory{T}"/>.
    /// </summary>
    public static class SpanIOHelper
    {
        /// <summary>
        /// Write the given data to the given backing <see cref="Memory{T}"/> and move cursor after the written data.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to store the data.</param>
        /// <param name="data">The data to write to the backing <see cref="Memory{T}"/>.</param>
        public static void Write<T>(ref Memory<byte> backingMemory, ref T data) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            MemoryMarshal.Write<T>(backingMemory.Span.Slice(0, size), ref data);

            backingMemory = backingMemory.Slice(size);
        }

        /// <summary>
        /// Write the given data to the given backing <see cref="Span{T}"/> and move cursor after the written data.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="Span{T}"/> to store the data.</param>
        /// <param name="data">The data to write to the backing <see cref="Span{T}"/>.</param>
        public static void Write<T>(ref Span<byte> backingMemory, ref T data) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            MemoryMarshal.Write<T>(backingMemory.Slice(0, size), ref data);

            backingMemory = backingMemory.Slice(size);
        }

        /// <summary>
        /// Get a <see cref="Span{T}"/> out of a <paramref name="backingMemory"/> and move cursor after T size.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="Memory{T}"/> to get a <see cref="Span{T}"/> from.</param>
        /// <returns>A <see cref="Span{T}"/> from backing <see cref="Memory{T}"/>.</returns>
        public static Span<T> GetWriteRef<T>(ref Memory<byte> backingMemory) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Span.Slice(0, size));

            backingMemory = backingMemory.Slice(size);

            return result;
        }

        /// <summary>
        /// Get a <see cref="Span{T}"/> out of a backingMemory and move cursor after T size.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="Span{T}"/> to get a <see cref="Span{T}"/> from.</param>
        /// <returns>A <see cref="Span{T}"/> from backing <see cref="Span{T}"/>.</returns>
        public static Span<T> GetWriteRef<T>(ref Span<byte> backingMemory) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            Span<T> result = MemoryMarshal.Cast<byte, T>(backingMemory.Slice(0, size));

            backingMemory = backingMemory.Slice(size);

            return result;
        }

        /// <summary>
        /// Read data from the given backing <see cref="ReadOnlyMemory{T}"/> and move cursor after the read data.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="ReadOnlyMemory{T}"/> to read data from.</param>
        /// <returns>Return the read data.</returns>
        public static T Read<T>(ref ReadOnlyMemory<byte> backingMemory) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            T result = MemoryMarshal.Read<T>(backingMemory.Span.Slice(0, size));

            backingMemory = backingMemory.Slice(size);

            return result;
        }

        /// <summary>
        /// Read data from the given backing <see cref="ReadOnlySpan{T}"/> and move cursor after the read data.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="backingMemory">The backing <see cref="ReadOnlySpan{T}"/> to read data from.</param>
        /// <returns>Return the read data.</returns>
        public static T Read<T>(ref ReadOnlySpan<byte> backingMemory) where T : unmanaged
        {
            int size = Unsafe.SizeOf<T>();

            if (size > backingMemory.Length)
            {
                throw new ArgumentOutOfRangeException();
            }

            T result = MemoryMarshal.Read<T>(backingMemory.Slice(0, size));

            backingMemory = backingMemory.Slice(size);

            return result;
        }

        /// <summary>
        /// Extract a <see cref="Memory{T}"/> at the given index.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
        /// <param name="id">The id in the provided memory.</param>
        /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
        /// <returns>a <see cref="Memory{T}"/> at the given id.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static Memory<T> GetMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
        {
            Debug.Assert(id >= 0 && id < count);

            return memory.Slice(id, 1);
        }

        /// <summary>
        /// Extract a ref T at the given index.
        /// </summary>
        /// <typeparam name="T">The data type.</typeparam>
        /// <param name="memory">The <see cref="Memory{T}"/> to extract the data from.</param>
        /// <param name="id">The id in the provided memory.</param>
        /// <param name="count">The max allowed count. (for bound checking of the id in debug mode)</param>
        /// <returns>a ref T at the given id.</returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static ref T GetFromMemory<T>(Memory<T> memory, int id, uint count) where T : unmanaged
        {
            return ref GetMemory(memory, id, count).Span[0];
        }
    }
}