mirror of
https://github.com/netchx/netch.git
synced 2026-03-30 19:09:48 +08:00
done
This commit is contained in:
77
Netch/3rd/DnsClient.NET/Internal/PooledBytes.cs
Normal file
77
Netch/3rd/DnsClient.NET/Internal/PooledBytes.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace DnsClient.Internal
|
||||
{
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
public class PooledBytes : IDisposable
|
||||
{
|
||||
private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(4096 * 4, 100);
|
||||
private int _length;
|
||||
private ArraySegment<byte> _buffer;
|
||||
private bool _disposed = false;
|
||||
|
||||
public PooledBytes(int length)
|
||||
{
|
||||
if (length <= 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(length));
|
||||
}
|
||||
|
||||
_length = length;
|
||||
_buffer = new ArraySegment<byte>(_pool.Rent(length), 0, _length);
|
||||
}
|
||||
|
||||
public void Extend(int length)
|
||||
{
|
||||
var newBuffer = _pool.Rent(_length + length);
|
||||
|
||||
System.Buffer.BlockCopy(_buffer.Array, 0, newBuffer, 0, _length);
|
||||
_pool.Return(_buffer.Array);
|
||||
_length = _length + length;
|
||||
_buffer = new ArraySegment<byte>(newBuffer, 0, _length);
|
||||
}
|
||||
|
||||
public byte[] Buffer
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(PooledBytes));
|
||||
}
|
||||
|
||||
return _buffer.Array;
|
||||
}
|
||||
}
|
||||
|
||||
public ArraySegment<byte> BufferSegment
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(PooledBytes));
|
||||
}
|
||||
|
||||
return _buffer;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && !_disposed)
|
||||
{
|
||||
_disposed = true;
|
||||
_pool.Return(_buffer.Array);
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
||||
}
|
||||
77
Netch/3rd/DnsClient.NET/Internal/StringBuilderObjectPool.cs
Normal file
77
Netch/3rd/DnsClient.NET/Internal/StringBuilderObjectPool.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
|
||||
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace DnsClient.Internal
|
||||
{
|
||||
public class StringBuilderObjectPool
|
||||
{
|
||||
private readonly StringBuilder[] _items;
|
||||
private readonly int _initialCapacity;
|
||||
private readonly int _maxPooledCapacity;
|
||||
private StringBuilder _fastAccess;
|
||||
|
||||
public static StringBuilderObjectPool Default { get; } = new StringBuilderObjectPool();
|
||||
|
||||
public StringBuilderObjectPool(int pooledItems = 16, int initialCapacity = 200, int maxPooledCapacity = 1024 * 2)
|
||||
{
|
||||
_items = new StringBuilder[pooledItems];
|
||||
_initialCapacity = initialCapacity;
|
||||
_maxPooledCapacity = maxPooledCapacity;
|
||||
_fastAccess = Create();
|
||||
}
|
||||
|
||||
public StringBuilder Get()
|
||||
{
|
||||
//return Create();
|
||||
var found = _fastAccess;
|
||||
|
||||
if (found == null || Interlocked.CompareExchange(ref _fastAccess, null, found) != found)
|
||||
{
|
||||
for (var i = 0; i < _items.Length; i++)
|
||||
{
|
||||
found = _items[i];
|
||||
|
||||
if (found != null && Interlocked.CompareExchange(ref _items[i], null, found) == found)
|
||||
{
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return found ?? Create();
|
||||
}
|
||||
|
||||
public void Return(StringBuilder value)
|
||||
{
|
||||
//return;
|
||||
if (value == null || value.Capacity > _maxPooledCapacity)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
value.Clear();
|
||||
|
||||
if (_fastAccess == null && Interlocked.CompareExchange(ref _fastAccess, value, null) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < _items.Length; i++)
|
||||
{
|
||||
if (Interlocked.CompareExchange(ref _items[i], value, null) == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private StringBuilder Create()
|
||||
{
|
||||
return new StringBuilder(_initialCapacity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
|
||||
Reference in New Issue
Block a user