Make GetMemory use MaxBufferSize for MemoryPool (#7143)

This commit is contained in:
Justin Kotalik 2019-02-04 13:55:48 -08:00 committed by GitHub
parent 1d2838ea2e
commit 9251dfe7c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 10 deletions

View File

@ -278,7 +278,8 @@ namespace System.IO.Pipelines
}
// Get a new buffer using the minimum segment size, unless the size hint is larger than a single segment.
_currentSegmentOwner = _pool.Rent(Math.Max(_minimumSegmentSize, sizeHint));
// Also, the size cannot be larger than the MaxBufferSize of the MemoryPool
_currentSegmentOwner = _pool.Rent(Math.Clamp(sizeHint, _minimumSegmentSize, _pool.MaxBufferSize));
_currentSegment = _currentSegmentOwner.Memory;
_position = 0;
}

View File

@ -89,14 +89,14 @@ namespace System.IO.Pipelines.Tests
Assert.Equal(memory, secondMemory);
}
[Fact]
public void CanGetNewSpanWhenNoAdvanceWhenSizeTooLarge()
[Theory]
[InlineData(0)]
[InlineData(2048)]
public void GetSpanWithZeroSizeHintReturnsMaxBufferSizeOfPool(int sizeHint)
{
var span = Writer.GetSpan(0);
var span = Writer.GetSpan(sizeHint);
var secondSpan = Writer.GetSpan(10000);
Assert.False(span.SequenceEqual(secondSpan));
Assert.Equal(4096, span.Length);
}
[Fact]

View File

@ -32,6 +32,7 @@ namespace System.IO.Pipelines.Tests
[InlineData(8000, 8000)]
public async Task CanAdvanceWithPartialConsumptionOfFirstSegment(int firstWriteLength, int secondWriteLength)
{
Writer = new StreamPipeWriter(MemoryStream, MinimumSegmentSize, new TestMemoryPool(maxBufferSize: 20000));
await Writer.WriteAsync(Encoding.ASCII.GetBytes("a"));
var expectedLength = firstWriteLength + secondWriteLength + 1;

View File

@ -14,12 +14,14 @@ namespace System.IO.Pipelines.Tests
public class TestMemoryPool : MemoryPool<byte>
{
private MemoryPool<byte> _pool;
private int _maxBufferSize;
private bool _disposed;
private int _rentCount;
public TestMemoryPool()
public TestMemoryPool(int maxBufferSize = 4096)
{
_pool = new CustomMemoryPool<byte>();
_maxBufferSize = maxBufferSize;
}
public override IMemoryOwner<byte> Rent(int minBufferSize = -1)
@ -39,7 +41,7 @@ namespace System.IO.Pipelines.Tests
_disposed = true;
}
public override int MaxBufferSize => 4096;
public override int MaxBufferSize => _maxBufferSize;
internal void CheckDisposed()
{