PoolingBufferWriter<T>
Represents memory writer that uses pooled memory.
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
namespace DotNext.Buffers
{
[Nullable(new byte[] {
0,
1
})]
public sealed class PoolingBufferWriter<[Nullable(2)] T> : BufferWriter<T>, IMemoryOwner<T>, IDisposable
{
[CompilerGenerated]
private MemoryAllocator<T> <allocator>P = allocator;
private MemoryOwner<T> buffer;
public override int Capacity {
get {
return buffer.Length;
}
set {
if (value < 0)
throw new ArgumentOutOfRangeException("value");
if (value > 0)
buffer = Memory.AllocateAtLeast<T>(<allocator>P, value);
}
}
[Nullable(new byte[] {
0,
1
})]
public override ReadOnlyMemory<T> WrittenMemory {
[return: Nullable(new byte[] {
0,
1
})]
get {
return GetWrittenMemory();
}
}
[Nullable(new byte[] {
0,
1
})]
Memory<T> IMemoryOwner<T>.Memory {
get {
return GetWrittenMemory();
}
}
public PoolingBufferWriter([Nullable(new byte[] {
2,
1
})] MemoryAllocator<T> allocator = null)
{
}
private Memory<T> GetWrittenMemory()
{
ObjectDisposedException.ThrowIf(base.IsDisposed, (object)this);
return buffer.Memory.Slice(0, position);
}
public override void Clear(bool reuseBuffer = false)
{
ObjectDisposedException.ThrowIf(base.IsDisposed, (object)this);
if (!reuseBuffer)
buffer.Dispose();
else if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) {
buffer.Span.Clear();
}
position = 0;
}
[return: IsReadOnly]
private ref MemoryOwner<T> GetBuffer(int sizeHint)
{
ArgumentOutOfRangeException.ThrowIfNegative<int>(sizeHint, "sizeHint");
ObjectDisposedException.ThrowIf(base.IsDisposed, (object)this);
CheckAndResizeBuffer(sizeHint);
return ref buffer;
}
[return: Nullable(new byte[] {
0,
1
})]
public override Memory<T> GetMemory(int sizeHint = 0)
{
return GetBuffer(sizeHint).Memory.Slice(position);
}
[return: Nullable(new byte[] {
0,
1
})]
public override Span<T> GetSpan(int sizeHint = 0)
{
return GetBuffer(sizeHint).Span.Slice(position);
}
[return: Nullable(new byte[] {
0,
1
})]
public override MemoryOwner<T> DetachBuffer()
{
ObjectDisposedException.ThrowIf(base.IsDisposed, (object)this);
MemoryOwner<T> result;
if (position > 0) {
result = buffer;
buffer = default(MemoryOwner<T>);
result.Truncate(position);
position = 0;
} else
result = default(MemoryOwner<T>);
return result;
}
private protected override void Resize(int newSize)
{
Memory.Resize<T>(ref buffer, newSize, <allocator>P);
<PoolingBufferWriter>F537FA966C1BD3B9ABB8344285AB5B09713F3F23D0E6CE3CB4F822F79C622DE25__PoolingBufferWriter.AllocationMeter.Record(buffer.Length, ref measurementTags);
}
protected override void Dispose(bool disposing)
{
buffer.Dispose();
base.Dispose(disposing);
}
}
}