Skip to content

Commit

Permalink
rename to PooledMemoryBufferWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
itn3000 committed Feb 9, 2022
1 parent 0cdf67e commit aeedfc7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions PooledStream.Test/TestPooledBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class TestPooledBufferWriter
[Fact]
public void Write()
{
using var bw = new PooledBufferWriter<byte>(ArrayPool<byte>.Shared, 1024);
using var bw = new PooledMemoryBufferWriter<byte>(ArrayPool<byte>.Shared, 1024);
var data = new byte[128];
for (int i = 0; i < 16; i++)
{
Expand All @@ -28,7 +28,7 @@ public void Write()
[Fact]
public void Reset()
{
using var bw = new PooledBufferWriter<byte>();
using var bw = new PooledMemoryBufferWriter<byte>();
var sp = bw.GetSpan(128);
sp.Fill(1);
bw.Advance(128);
Expand Down
36 changes: 19 additions & 17 deletions PooledStream/PooledBufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,36 @@

namespace PooledStream
{
public class PooledBufferWriter<T> : IDisposable, IBufferWriter<T> where T : struct
public class PooledMemoryBufferWriter<T> : IDisposable, IBufferWriter<T> where T : struct
{
ArrayPool<T> _Pool;
T[] _currentBuffer;
int _Position;
int _Length;
const int DefaultSize = 1024;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void Reallocate(int sizeHint)
{
var nar = _Pool.Rent(sizeHint);
if(_currentBuffer != null)
if (_currentBuffer != null)
{
Buffer.BlockCopy(_currentBuffer, 0, nar, 0, _currentBuffer.Length < nar.Length ? _currentBuffer.Length : nar.Length);
_Pool.Return(_currentBuffer);
}
_currentBuffer = nar;
}
public PooledBufferWriter(): this(ArrayPool<T>.Shared)
public PooledMemoryBufferWriter() : this(ArrayPool<T>.Shared)
{

}
public PooledMemoryBufferWriter(int preallocateSize) : this(ArrayPool<T>.Shared, preallocateSize)
{

}
public PooledBufferWriter(ArrayPool<T> pool): this(pool, DefaultSize)
public PooledMemoryBufferWriter(ArrayPool<T> pool) : this(pool, DefaultSize)
{
}
public PooledBufferWriter(ArrayPool<T> pool, int preallocateSize)
public PooledMemoryBufferWriter(ArrayPool<T> pool, int preallocateSize)
{
_Pool = pool;
_currentBuffer = null;
Expand All @@ -39,12 +44,12 @@ public PooledBufferWriter(ArrayPool<T> pool, int preallocateSize)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Advance(int count)
{
if(_Position + count > _currentBuffer.Length)
if (_Position + count > _currentBuffer.Length)
{
throw new IndexOutOfRangeException("advance too many(" + count.ToString() + ")");
}
_Position += count;
if(_Length < _Position)
if (_Length < _Position)
{
_Length = _Position;
}
Expand All @@ -53,7 +58,7 @@ public void Advance(int count)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
if(_currentBuffer != null)
if (_currentBuffer != null)
{
_Pool.Return(_currentBuffer);
_currentBuffer = null;
Expand All @@ -65,11 +70,11 @@ public void Dispose()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Memory<T> GetMemory(int sizeHint = 0)
{
if(sizeHint == 0)
if (sizeHint == 0)
{
sizeHint = DefaultSize;
}
if(_Position + sizeHint > _currentBuffer.Length)
if (_Position + sizeHint > _currentBuffer.Length)
{
Reallocate(_Position + sizeHint);
}
Expand All @@ -79,11 +84,11 @@ public Memory<T> GetMemory(int sizeHint = 0)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<T> GetSpan(int sizeHint = 0)
{
if(sizeHint == 0)
if (sizeHint == 0)
{
sizeHint = DefaultSize;
}
if(_Position + sizeHint > _currentBuffer.Length)
if (_Position + sizeHint > _currentBuffer.Length)
{
Reallocate(_Position + sizeHint);
}
Expand All @@ -102,11 +107,8 @@ public ReadOnlyMemory<T> ToMemoryUnsafe()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(int preallocateSize = DefaultSize)
{
if(preallocateSize > _currentBuffer.Length)
{
_Pool.Return(_currentBuffer);
_currentBuffer = _Pool.Rent(preallocateSize);
}
_Pool.Return(_currentBuffer);
_currentBuffer = _Pool.Rent(preallocateSize);
_Length = 0;
_Position = 0;
}
Expand Down

0 comments on commit aeedfc7

Please sign in to comment.