mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-21 03:39:57 -04:00
Import the base tree (MIT, see LICENSE)
This commit is contained in:
34
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/ObjectPool.cs
vendored
Normal file
34
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/ObjectPool.cs
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
17
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/SharedPools.cs
vendored
Normal file
17
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/SharedPools.cs
vendored
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/ThreadStaticArray.cs
vendored
Normal file
17
tools/vendor/ryujinx/src/Ryujinx.Common/Pools/ThreadStaticArray.cs
vendored
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user