Ryujinx/src/Ryujinx.Graphics.Vulkan/NativeArray.cs
TSRBerry 2989c163a8
editorconfig: Set default encoding to UTF-8 (#5793)
* editorconfig: Add default charset

* Change file encoding from UTF-8-BOM to UTF-8
2023-12-04 14:17:13 +01:00

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