Import the base tree (MIT, see LICENSE)

This commit is contained in:
Valiantsin Dzerakh
2026-11-04 22:16:41 +03:00
committed by chrg6
commit 0478c13e45
734 changed files with 233953 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Concurrent;
namespace Ryujinx.Common
{
public class ObjectPool<T>(Func<T> factory, int size = -1)
where T : class
{
private int _size = size;
private readonly ConcurrentBag<T> _items = new();
public T Allocate()
{
bool success = _items.TryTake(out T instance);
if (!success)
{
instance = factory();
}
return instance;
}
public void Release(T obj)
{
if (_size < 0 || _items.Count < _size)
{
_items.Add(obj);
}
}
public void Clear() => _items.Clear();
}
}

View File

@@ -0,0 +1,17 @@
namespace Ryujinx.Common
{
public static class SharedPools
{
private static class DefaultPool<T>
where T : class, new()
{
public static readonly ObjectPool<T> Instance = new(() => new T(), 20);
}
public static ObjectPool<T> Default<T>()
where T : class, new()
{
return DefaultPool<T>.Instance;
}
}
}

View File

@@ -0,0 +1,17 @@
using System;
namespace Ryujinx.Common.Pools
{
public static class ThreadStaticArray<T>
{
[ThreadStatic]
private static T[] _array;
public static ref T[] Get()
{
_array ??= new T[1];
return ref _array;
}
}
}