aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Graphics.Vulkan/NativeArray.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Ryujinx.Graphics.Vulkan/NativeArray.cs')
-rw-r--r--src/Ryujinx.Graphics.Vulkan/NativeArray.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Ryujinx.Graphics.Vulkan/NativeArray.cs b/src/Ryujinx.Graphics.Vulkan/NativeArray.cs
new file mode 100644
index 00000000..3a851287
--- /dev/null
+++ b/src/Ryujinx.Graphics.Vulkan/NativeArray.cs
@@ -0,0 +1,48 @@
+using System;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+namespace Ryujinx.Graphics.Vulkan
+{
+ unsafe class NativeArray<T> : IDisposable where T : unmanaged
+ {
+ public T* Pointer { get; private set; }
+ public int Length { get; }
+
+ public ref T this[int index]
+ {
+ get => ref Pointer[Checked(index)];
+ }
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private int Checked(int index)
+ {
+ if ((uint)index >= (uint)Length)
+ {
+ throw new IndexOutOfRangeException();
+ }
+
+ return index;
+ }
+
+ public NativeArray(int length)
+ {
+ Pointer = (T*)Marshal.AllocHGlobal(checked(length * Unsafe.SizeOf<T>()));
+ Length = length;
+ }
+
+ public Span<T> AsSpan()
+ {
+ return new Span<T>(Pointer, Length);
+ }
+
+ public void Dispose()
+ {
+ if (Pointer != null)
+ {
+ Marshal.FreeHGlobal((IntPtr)Pointer);
+ Pointer = null;
+ }
+ }
+ }
+}