Created
December 18, 2015 20:28
-
-
Save BBI-YggyKing/94f7960748d682cddb8d to your computer and use it in GitHub Desktop.
A simple object pool. #speedcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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