Skip to content

Instantly share code, notes, and snippets.

@BBI-YggyKing
Created December 18, 2015 20:28
Show Gist options
  • Save BBI-YggyKing/94f7960748d682cddb8d to your computer and use it in GitHub Desktop.
Save BBI-YggyKing/94f7960748d682cddb8d to your computer and use it in GitHub Desktop.
A simple object pool. #speedcode
public class Pool<T> where T : new()
{
private readonly T[] mPool;
private int mNextAvailable;
public Pool(int capacity)
{
mPool = new T[capacity];
for (int i = 0; i < capacity; ++i)
{
mPool[i] = new T();
}
mNextAvailable = 0;
}
public T Reserve()
{
if (mNextAvailable < mPool.Length)
{
T obj = mPool[mNextAvailable];
mPool[mNextAvailable++] = default(T);
return obj;
}
else
{
return default(T);
}
}
public void Release(T obj)
{
if (mNextAvailable > 0)
{
mPool[--mNextAvailable] = obj;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment