ファイル
YMM4-ProxyForge/ProxyForge/Memory/BufferPool.cs
T
routersys 5ef1222518 ZeroDiskProxyからProxyForgeへのプロジェクト全体のリブランディング
プロジェクト名をZeroDiskProxyからProxyForgeへ全面的にリブランディングしました。
2026-03-25 19:58:39 +09:00

38 行
1.1 KiB
C#

using System.Buffers;
namespace ProxyForge.Memory;
internal static class BufferPool
{
private const int MaxPoolableSize = 1024 * 1024;
internal static byte[] Rent(int minimumLength) => ArrayPool<byte>.Shared.Rent(minimumLength);
internal static void Return(byte[] buffer, bool clearArray = false)
{
if (buffer.Length <= MaxPoolableSize)
ArrayPool<byte>.Shared.Return(buffer, clearArray);
}
}
internal struct PooledBuffer : IDisposable
{
private byte[]? _buffer;
private readonly int _length;
internal PooledBuffer(int length)
{
_length = length;
_buffer = BufferPool.Rent(length);
}
internal readonly byte[] Array => _buffer ?? throw new ObjectDisposedException(nameof(PooledBuffer));
internal readonly int Length => _length;
internal readonly Span<byte> Span => _buffer is not null ? _buffer.AsSpan(0, _length) : throw new ObjectDisposedException(nameof(PooledBuffer));
public void Dispose()
{
var buf = Interlocked.Exchange(ref _buffer, null);
if (buf is not null)
BufferPool.Return(buf);
}
}