Inital Commit
This commit is contained in:
@@ -1,108 +1,98 @@
|
||||
#region References
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
#endregion
|
||||
|
||||
namespace Server.Network
|
||||
{
|
||||
public class BufferPool
|
||||
{
|
||||
public static List<BufferPool> Pools { get; private set; }
|
||||
public class BufferPool
|
||||
{
|
||||
private static readonly ConcurrentDictionary<BufferPool, string> m_Pools = new ConcurrentDictionary<BufferPool, string>();
|
||||
|
||||
static BufferPool()
|
||||
{
|
||||
Pools = new List<BufferPool>();
|
||||
}
|
||||
public static ReadOnlyCollection<BufferPool> Pools => (ReadOnlyCollection<BufferPool>)m_Pools.Keys;
|
||||
|
||||
private readonly string m_Name;
|
||||
private readonly string m_Name;
|
||||
|
||||
private readonly int m_InitialCapacity;
|
||||
private readonly int m_BufferSize;
|
||||
private readonly int m_InitialCapacity;
|
||||
private readonly int m_BufferSize;
|
||||
|
||||
private int m_Misses;
|
||||
private volatile int m_Misses;
|
||||
|
||||
private readonly Queue<byte[]> m_FreeBuffers;
|
||||
private readonly ConcurrentQueue<byte[]> m_FreeBuffers = new ConcurrentQueue<byte[]>();
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (this)
|
||||
return m_FreeBuffers.Count;
|
||||
}
|
||||
}
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_FreeBuffers.Count;
|
||||
}
|
||||
}
|
||||
|
||||
public void GetInfo(
|
||||
out string name,
|
||||
out int freeCount,
|
||||
out int initialCapacity,
|
||||
out int currentCapacity,
|
||||
out int bufferSize,
|
||||
out int misses)
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
name = m_Name;
|
||||
freeCount = m_FreeBuffers.Count;
|
||||
initialCapacity = m_InitialCapacity;
|
||||
currentCapacity = m_InitialCapacity * (1 + m_Misses);
|
||||
bufferSize = m_BufferSize;
|
||||
misses = m_Misses;
|
||||
}
|
||||
}
|
||||
public void GetInfo(
|
||||
out string name,
|
||||
out int freeCount,
|
||||
out int initialCapacity,
|
||||
out int currentCapacity,
|
||||
out int bufferSize,
|
||||
out int misses)
|
||||
{
|
||||
name = m_Name;
|
||||
freeCount = m_FreeBuffers.Count;
|
||||
initialCapacity = m_InitialCapacity;
|
||||
currentCapacity = m_InitialCapacity * (1 + m_Misses);
|
||||
bufferSize = m_BufferSize;
|
||||
misses = m_Misses;
|
||||
}
|
||||
|
||||
public BufferPool(string name, int initialCapacity, int bufferSize)
|
||||
{
|
||||
m_Name = name;
|
||||
public BufferPool(string name, int initialCapacity, int bufferSize)
|
||||
{
|
||||
m_Name = name;
|
||||
|
||||
m_InitialCapacity = initialCapacity;
|
||||
m_BufferSize = bufferSize;
|
||||
m_InitialCapacity = initialCapacity;
|
||||
m_BufferSize = bufferSize;
|
||||
|
||||
m_FreeBuffers = new Queue<byte[]>(initialCapacity);
|
||||
while (m_FreeBuffers.Count < initialCapacity)
|
||||
{
|
||||
m_FreeBuffers.Enqueue(new byte[bufferSize]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < initialCapacity; ++i)
|
||||
{
|
||||
m_FreeBuffers.Enqueue(new byte[bufferSize]);
|
||||
}
|
||||
m_Pools[this] = name;
|
||||
}
|
||||
|
||||
lock (Pools)
|
||||
Pools.Add(this);
|
||||
}
|
||||
public byte[] AcquireBuffer()
|
||||
{
|
||||
if (m_FreeBuffers.TryDequeue(out var buffer))
|
||||
{
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public byte[] AcquireBuffer()
|
||||
{
|
||||
lock (this)
|
||||
{
|
||||
if (m_FreeBuffers.Count > 0)
|
||||
{
|
||||
return m_FreeBuffers.Dequeue();
|
||||
}
|
||||
++m_Misses;
|
||||
|
||||
++m_Misses;
|
||||
while (m_FreeBuffers.Count < m_InitialCapacity - 1)
|
||||
{
|
||||
m_FreeBuffers.Enqueue(new byte[m_BufferSize]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m_InitialCapacity; ++i)
|
||||
{
|
||||
m_FreeBuffers.Enqueue(new byte[m_BufferSize]);
|
||||
}
|
||||
return new byte[m_BufferSize];
|
||||
}
|
||||
|
||||
return m_FreeBuffers.Dequeue();
|
||||
}
|
||||
}
|
||||
public void ReleaseBuffer(byte[] buffer)
|
||||
{
|
||||
if (buffer == null || buffer.Length != m_BufferSize)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
public void ReleaseBuffer(byte[] buffer)
|
||||
{
|
||||
if (buffer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Array.Clear(buffer, 0, buffer.Length);
|
||||
|
||||
lock (this)
|
||||
m_FreeBuffers.Enqueue(buffer);
|
||||
}
|
||||
m_FreeBuffers.Enqueue(buffer);
|
||||
}
|
||||
|
||||
public void Free()
|
||||
{
|
||||
lock (Pools)
|
||||
Pools.Remove(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
public void Free()
|
||||
{
|
||||
m_Pools.TryRemove(this, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user