/ src / Ryujinx.Graphics.Vulkan / NativeArray.cs
NativeArray.cs
 1  using System;
 2  using System.Runtime.CompilerServices;
 3  using System.Runtime.InteropServices;
 4  
 5  namespace Ryujinx.Graphics.Vulkan
 6  {
 7      unsafe class NativeArray<T> : IDisposable where T : unmanaged
 8      {
 9          public T* Pointer { get; private set; }
10          public int Length { get; }
11  
12          public ref T this[int index]
13          {
14              get => ref Pointer[Checked(index)];
15          }
16  
17          [MethodImpl(MethodImplOptions.AggressiveInlining)]
18          private int Checked(int index)
19          {
20              if ((uint)index >= (uint)Length)
21              {
22                  throw new IndexOutOfRangeException();
23              }
24  
25              return index;
26          }
27  
28          public NativeArray(int length)
29          {
30              Pointer = (T*)Marshal.AllocHGlobal(checked(length * Unsafe.SizeOf<T>()));
31              Length = length;
32          }
33  
34          public Span<T> AsSpan()
35          {
36              return new Span<T>(Pointer, Length);
37          }
38  
39          public void Dispose()
40          {
41              if (Pointer != null)
42              {
43                  Marshal.FreeHGlobal((IntPtr)Pointer);
44                  Pointer = null;
45              }
46          }
47      }
48  }