using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetcodeIO.NET.Utils
{
///
/// Helper methods for allocating temporary buffers
///
public static class BufferPool
{
private static Dictionary> bufferPool = new Dictionary>();
///
/// Retrieve a buffer of the given size
///
public static byte[] GetBuffer(int size)
{
lock(bufferPool)
{
if (bufferPool.ContainsKey(size))
{
if (bufferPool[size].Count > 0)
return bufferPool[size].Dequeue();
}
}
return new byte[size];
}
///
/// Return a buffer to the pool
///
public static void ReturnBuffer(byte[] buffer)
{
lock(bufferPool)
{
if (!bufferPool.ContainsKey(buffer.Length))
bufferPool.Add(buffer.Length, new Queue());
System.Array.Clear(buffer, 0, buffer.Length);
bufferPool[buffer.Length].Enqueue(buffer);
}
}
}
}