Files
ryujinx/src/Ryujinx.Graphics.Nvdec.Vp9/Common/MemoryAllocator.cs
TSRBerry 6aa8d71588 [Ryujinx.Graphics.Nvdec.Vp9] Address dotnet-format issues (#5371)
* dotnet format style --severity info

Some changes were manually reverted.

* dotnet format analyzers --serverity info

Some changes have been minimally adapted.

* Restore a few unused methods and variables

* Silence dotnet format IDE0060 warnings

* Address or silence dotnet format IDE1006 warnings

* Address most dotnet format whitespace warnings

* Apply dotnet format whitespace formatting

A few of them have been manually reverted and the corresponding warning was silenced

* Add comments to disabled warnings

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Address IDE0251 warnings

* Address a few disabled IDE0060 warnings

* Silence IDE0060 in .editorconfig

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* Fix empty lines before return

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Add trailing commas, remove redundant code and remove static modifier from Surface.HighBd

* Fix naming rule violations

* Fix naming rule violations

* Fix empty line before return

* Fix comment style

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Remove comment alignment

* Address review feedback

* Separate comments by 2 spaces and fix other formatting issues

* Make HighBd an auto-property

* Replace if-chain with if-else-chain

* Fix new naming rule violations

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
2023-06-28 09:26:39 +02:00

95 lines
2.5 KiB
C#

using Ryujinx.Common.Memory;
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Graphics.Nvdec.Vp9.Common
{
internal class MemoryAllocator : IDisposable
{
private const int PoolEntries = 10;
private struct PoolItem
{
public IntPtr Pointer;
public int Length;
public bool InUse;
}
private readonly PoolItem[] _pool = new PoolItem[PoolEntries];
public ArrayPtr<T> Allocate<T>(int length) where T : unmanaged
{
int lengthInBytes = Unsafe.SizeOf<T>() * length;
IntPtr ptr = IntPtr.Zero;
for (int i = 0; i < PoolEntries; i++)
{
ref PoolItem item = ref _pool[i];
if (!item.InUse && item.Length == lengthInBytes)
{
item.InUse = true;
ptr = item.Pointer;
break;
}
}
if (ptr == IntPtr.Zero)
{
ptr = Marshal.AllocHGlobal(lengthInBytes);
for (int i = 0; i < PoolEntries; i++)
{
ref PoolItem item = ref _pool[i];
if (!item.InUse)
{
item.InUse = true;
if (item.Pointer != IntPtr.Zero)
{
Marshal.FreeHGlobal(item.Pointer);
}
item.Pointer = ptr;
item.Length = lengthInBytes;
break;
}
}
}
return new ArrayPtr<T>(ptr, length);
}
public unsafe void Free<T>(ArrayPtr<T> arr) where T : unmanaged
{
IntPtr ptr = (IntPtr)arr.ToPointer();
for (int i = 0; i < PoolEntries; i++)
{
ref PoolItem item = ref _pool[i];
if (item.Pointer == ptr)
{
item.InUse = false;
break;
}
}
}
public void Dispose()
{
for (int i = 0; i < PoolEntries; i++)
{
ref PoolItem item = ref _pool[i];
if (item.Pointer != IntPtr.Zero)
{
Marshal.FreeHGlobal(item.Pointer);
item.Pointer = IntPtr.Zero;
}
}
}
}
}