Add double-free detection code to the pools

This commit is contained in:
Cameron Gutman
2013-11-21 12:55:05 -05:00
parent 7951c0fa13
commit acb3dc8881
3 changed files with 50 additions and 3 deletions
@@ -6,6 +6,8 @@ public class AvShortBufferPool {
private ConcurrentLinkedQueue<short[]> bufferList = new ConcurrentLinkedQueue<short[]>();
private int bufferSize;
private static final boolean doubleFreeDebug = true;
public AvShortBufferPool(int size)
{
this.bufferSize = size;
@@ -18,7 +20,13 @@ public class AvShortBufferPool {
public short[] allocate()
{
short[] buff = bufferList.poll();
short[] buff;
if (doubleFreeDebug) {
buff = null;
}
else {
buff = bufferList.poll();
}
if (buff == null) {
buff = new short[bufferSize];
}
@@ -27,6 +35,14 @@ public class AvShortBufferPool {
public void free(short[] buffer)
{
if (doubleFreeDebug) {
for (short[] buf : bufferList) {
if (buf == buffer) {
throw new IllegalStateException("Double free detected");
}
}
}
bufferList.add(buffer);
}
}