mirror of
https://github.com/moonlight-stream/moonlight-android.git
synced 2025-07-20 03:23:07 +00:00
Add double-free detection code to the pools
This commit is contained in:
parent
7951c0fa13
commit
acb3dc8881
@ -6,6 +6,8 @@ public class AvByteBufferPool {
|
|||||||
private ConcurrentLinkedQueue<byte[]> bufferList = new ConcurrentLinkedQueue<byte[]>();
|
private ConcurrentLinkedQueue<byte[]> bufferList = new ConcurrentLinkedQueue<byte[]>();
|
||||||
private int bufferSize;
|
private int bufferSize;
|
||||||
|
|
||||||
|
private static final boolean doubleFreeDebug = true;
|
||||||
|
|
||||||
public AvByteBufferPool(int size)
|
public AvByteBufferPool(int size)
|
||||||
{
|
{
|
||||||
this.bufferSize = size;
|
this.bufferSize = size;
|
||||||
@ -18,7 +20,13 @@ public class AvByteBufferPool {
|
|||||||
|
|
||||||
public byte[] allocate()
|
public byte[] allocate()
|
||||||
{
|
{
|
||||||
byte[] buff = bufferList.poll();
|
byte[] buff;
|
||||||
|
if (doubleFreeDebug) {
|
||||||
|
buff = null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
buff = bufferList.poll();
|
||||||
|
}
|
||||||
if (buff == null) {
|
if (buff == null) {
|
||||||
buff = new byte[bufferSize];
|
buff = new byte[bufferSize];
|
||||||
}
|
}
|
||||||
@ -27,6 +35,14 @@ public class AvByteBufferPool {
|
|||||||
|
|
||||||
public void free(byte[] buffer)
|
public void free(byte[] buffer)
|
||||||
{
|
{
|
||||||
|
if (doubleFreeDebug) {
|
||||||
|
for (byte[] buf : bufferList) {
|
||||||
|
if (buf == buffer) {
|
||||||
|
throw new IllegalStateException("Double free detected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bufferList.add(buffer);
|
bufferList.add(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import java.util.concurrent.ConcurrentLinkedQueue;
|
|||||||
public class AvObjectPool<T> {
|
public class AvObjectPool<T> {
|
||||||
private ConcurrentLinkedQueue<T> objectList = new ConcurrentLinkedQueue<T>();
|
private ConcurrentLinkedQueue<T> objectList = new ConcurrentLinkedQueue<T>();
|
||||||
|
|
||||||
|
private static final boolean doubleFreeDebug = true;
|
||||||
|
|
||||||
public void purge()
|
public void purge()
|
||||||
{
|
{
|
||||||
objectList.clear();
|
objectList.clear();
|
||||||
@ -12,11 +14,24 @@ public class AvObjectPool<T> {
|
|||||||
|
|
||||||
public T tryAllocate()
|
public T tryAllocate()
|
||||||
{
|
{
|
||||||
return objectList.poll();
|
if (doubleFreeDebug) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return objectList.poll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void free(T object)
|
public void free(T object)
|
||||||
{
|
{
|
||||||
|
if (doubleFreeDebug) {
|
||||||
|
for (T obj : objectList) {
|
||||||
|
if (obj == object) {
|
||||||
|
throw new IllegalStateException("Double free detected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
objectList.add(object);
|
objectList.add(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,8 @@ public class AvShortBufferPool {
|
|||||||
private ConcurrentLinkedQueue<short[]> bufferList = new ConcurrentLinkedQueue<short[]>();
|
private ConcurrentLinkedQueue<short[]> bufferList = new ConcurrentLinkedQueue<short[]>();
|
||||||
private int bufferSize;
|
private int bufferSize;
|
||||||
|
|
||||||
|
private static final boolean doubleFreeDebug = true;
|
||||||
|
|
||||||
public AvShortBufferPool(int size)
|
public AvShortBufferPool(int size)
|
||||||
{
|
{
|
||||||
this.bufferSize = size;
|
this.bufferSize = size;
|
||||||
@ -18,7 +20,13 @@ public class AvShortBufferPool {
|
|||||||
|
|
||||||
public short[] allocate()
|
public short[] allocate()
|
||||||
{
|
{
|
||||||
short[] buff = bufferList.poll();
|
short[] buff;
|
||||||
|
if (doubleFreeDebug) {
|
||||||
|
buff = null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
buff = bufferList.poll();
|
||||||
|
}
|
||||||
if (buff == null) {
|
if (buff == null) {
|
||||||
buff = new short[bufferSize];
|
buff = new short[bufferSize];
|
||||||
}
|
}
|
||||||
@ -27,6 +35,14 @@ public class AvShortBufferPool {
|
|||||||
|
|
||||||
public void free(short[] buffer)
|
public void free(short[] buffer)
|
||||||
{
|
{
|
||||||
|
if (doubleFreeDebug) {
|
||||||
|
for (short[] buf : bufferList) {
|
||||||
|
if (buf == buffer) {
|
||||||
|
throw new IllegalStateException("Double free detected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bufferList.add(buffer);
|
bufferList.add(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user