1
0
Fork 0
mirror of https://github.com/Ryujinx/Ryujinx.git synced 2024-12-28 19:06:02 +00:00
Ryujinx/Ryujinx.Graphics.Vulkan/NativeArray.cs
2022-08-11 18:07:37 -03:00

45 lines
1.1 KiB
C#

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()
{
Marshal.FreeHGlobal((IntPtr)Pointer);
Pointer = null;
}
}
}