mirror of
https://github.com/mirror/processhacker
synced 2026-06-08 16:03:24 +00:00
249345130d
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1383 21ef857c-d57f-4fe0-8362-d861dc6d29cd
55 lines
1.3 KiB
C#
55 lines
1.3 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace ProcessHacker.Common
|
|
{
|
|
/// <summary>
|
|
/// Manages a list of free objects that can be re-used.
|
|
/// </summary>
|
|
public class FreeList<T>
|
|
where T : IResettable, new()
|
|
{
|
|
private LinkedList<T> _list = new LinkedList<T>();
|
|
private int _maximumCount = 0;
|
|
|
|
public int MaximumCount
|
|
{
|
|
get { return _maximumCount; }
|
|
set { _maximumCount = value; }
|
|
}
|
|
|
|
public T Allocate()
|
|
{
|
|
lock (_list)
|
|
{
|
|
if (_list.Count == 0)
|
|
return this.AllocateNew();
|
|
T obj = _list.First.Value;
|
|
_list.RemoveFirst();
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
private T AllocateNew()
|
|
{
|
|
T obj = new T();
|
|
obj.ResetObject();
|
|
return obj;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
lock (_list)
|
|
_list.Clear();
|
|
}
|
|
|
|
public void Free(T obj)
|
|
{
|
|
lock (_list)
|
|
{
|
|
if (_list.Count < _maximumCount || _maximumCount == 0)
|
|
_list.AddFirst(obj);
|
|
}
|
|
}
|
|
}
|
|
}
|