51 lines
638 B
C#
51 lines
638 B
C#
using System.Collections.Generic;
|
|
//
|
|
public class ViMemoryAllocator<T> where T : class, new()
|
|
{
|
|
public ViMemoryAllocator(int capacity)
|
|
{
|
|
_capacity = capacity;
|
|
_pool = new Stack<T>(capacity);
|
|
}
|
|
//
|
|
public void Start()
|
|
{
|
|
_pool.Clear();
|
|
//
|
|
for (int iter = 0; iter < _capacity; ++iter)
|
|
{
|
|
_pool.Push(new T());
|
|
}
|
|
}
|
|
//
|
|
public T New()
|
|
{
|
|
if (_pool.Count > 0)
|
|
{
|
|
return _pool.Pop();
|
|
}
|
|
else
|
|
{
|
|
return new T();
|
|
}
|
|
}
|
|
//
|
|
public void Delete(T node)
|
|
{
|
|
if (node == null)
|
|
{
|
|
return;
|
|
}
|
|
//
|
|
_pool.Push(node);
|
|
}
|
|
//
|
|
public void End()
|
|
{
|
|
_pool.Clear();
|
|
}
|
|
//
|
|
Stack<T> _pool;
|
|
int _capacity = 10;
|
|
}
|