From acb3dc88819b482bbae419a3a36332db30d629f5 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 21 Nov 2013 12:55:05 -0500 Subject: [PATCH] Add double-free detection code to the pools --- .../nvstream/av/AvByteBufferPool.java | 18 +++++++++++++++++- .../limelight/nvstream/av/AvObjectPool.java | 17 ++++++++++++++++- .../nvstream/av/AvShortBufferPool.java | 18 +++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/com/limelight/nvstream/av/AvByteBufferPool.java b/src/com/limelight/nvstream/av/AvByteBufferPool.java index 1da5e6ad..0fe652af 100644 --- a/src/com/limelight/nvstream/av/AvByteBufferPool.java +++ b/src/com/limelight/nvstream/av/AvByteBufferPool.java @@ -6,6 +6,8 @@ public class AvByteBufferPool { private ConcurrentLinkedQueue bufferList = new ConcurrentLinkedQueue(); private int bufferSize; + private static final boolean doubleFreeDebug = true; + public AvByteBufferPool(int size) { this.bufferSize = size; @@ -18,7 +20,13 @@ public class AvByteBufferPool { public byte[] allocate() { - byte[] buff = bufferList.poll(); + byte[] buff; + if (doubleFreeDebug) { + buff = null; + } + else { + buff = bufferList.poll(); + } if (buff == null) { buff = new byte[bufferSize]; } @@ -27,6 +35,14 @@ public class AvByteBufferPool { public void free(byte[] buffer) { + if (doubleFreeDebug) { + for (byte[] buf : bufferList) { + if (buf == buffer) { + throw new IllegalStateException("Double free detected"); + } + } + } + bufferList.add(buffer); } } diff --git a/src/com/limelight/nvstream/av/AvObjectPool.java b/src/com/limelight/nvstream/av/AvObjectPool.java index 97de612c..d751be98 100644 --- a/src/com/limelight/nvstream/av/AvObjectPool.java +++ b/src/com/limelight/nvstream/av/AvObjectPool.java @@ -5,6 +5,8 @@ import java.util.concurrent.ConcurrentLinkedQueue; public class AvObjectPool { private ConcurrentLinkedQueue objectList = new ConcurrentLinkedQueue(); + private static final boolean doubleFreeDebug = true; + public void purge() { objectList.clear(); @@ -12,11 +14,24 @@ public class AvObjectPool { public T tryAllocate() { - return objectList.poll(); + if (doubleFreeDebug) { + return null; + } + else { + return objectList.poll(); + } } public void free(T object) { + if (doubleFreeDebug) { + for (T obj : objectList) { + if (obj == object) { + throw new IllegalStateException("Double free detected"); + } + } + } + objectList.add(object); } } diff --git a/src/com/limelight/nvstream/av/AvShortBufferPool.java b/src/com/limelight/nvstream/av/AvShortBufferPool.java index 012383f8..9f7a956c 100644 --- a/src/com/limelight/nvstream/av/AvShortBufferPool.java +++ b/src/com/limelight/nvstream/av/AvShortBufferPool.java @@ -6,6 +6,8 @@ public class AvShortBufferPool { private ConcurrentLinkedQueue bufferList = new ConcurrentLinkedQueue(); 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); } }