mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2025-12-12 07:36:59 +00:00
35 lines
754 B
C#
35 lines
754 B
C#
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
|
|
namespace Ryujinx.Common
|
|
{
|
|
public class ObjectPool<T>(Func<T> factory, int size = -1)
|
|
where T : class
|
|
{
|
|
private int _size = size;
|
|
private readonly ConcurrentStack<T> _items = new();
|
|
|
|
public T Allocate()
|
|
{
|
|
bool success = _items.TryPop(out T instance);
|
|
|
|
if (!success)
|
|
{
|
|
instance = factory();
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
public void Release(T obj)
|
|
{
|
|
if (_size < 0 || _items.Count < _size)
|
|
{
|
|
_items.Push(obj);
|
|
}
|
|
}
|
|
|
|
public void Clear() => _items.Clear();
|
|
}
|
|
}
|