Files
mirror-processhacker/trunk/ProcessHacker.Common/FreeList.cs
T
wj32 249345130d added basic FreeList
git-svn-id: svn://svn.code.sf.net/p/processhacker/code@1383 21ef857c-d57f-4fe0-8362-d861dc6d29cd
2009-06-07 06:50:14 +00:00

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);
}
}
}
}