PooledSliceAllocator

Namespace: SnowBank.Buffers · class

Implements: ISliceAllocator, IDisposable

Allocator that stores all data into slabs allocated from a pool (or the heap for large allocations)

Remarks

This instance MUST be disposed in order to return all slabs to the pool. Failing to do so will hinder the performance of the pool!

This buffer will allocate new slabs of memory as needed, and keep them alive until it is disposed or cleared.

Slice allocated from this writer MUST NOT be used after this instance has been disposed or cleared!

If you require all allocated data to survive this instance, use ArraySliceAllocator instead.

Constructors

PooledSliceAllocator

PooledSliceAllocator()

Creates an instance of a PooledSliceAllocator, in which data can be written to, with an initial capacity specified.

PooledSliceAllocator(int slabSize)

Creates an instance of a PooledSliceAllocator, in which data can be written to, with an initial capacity specified.

PooledSliceAllocator(ArrayPool<byte> pool)

Creates an instance of a PooledSliceAllocator, in which data can be written to, with an initial capacity specified.

PooledSliceAllocator(int slabSize, ArrayPool<byte> pool)

Creates an instance of a PooledSliceAllocator, in which data can be written to, with an initial capacity specified.

  • slabSize — The initial capacity with which to initialize the underlying buffer.
  • pool — If provided, used to rent the slabs. If null, slabs will be allocated from the heap.

Properties

FreeCapacity

int FreeCapacity { get; }

Returns the amount of space available that can still be written into without forcing the underlying buffer to grow.

MaxSlabSize

int MaxSlabSize { get; }

Maximum size of slabs allocated from the pool. Slabs with a size greater than this value will be allocated on the heap instead.

TotalAllocated

long TotalAllocated { get; }

Returns the amount of data written to the underlying buffer so far.

Methods

Allocate

ArraySegment<byte> Allocate(int size)

Returns a ArraySegment to write to that is exactly the requested size (specified by size) and advance the cursor.

  • size — The exact length of the returned Slice. If 0, a non-empty buffer is returned.

Returns: A ArraySegment of at exactly the size requested.

Clear

void Clear()

Clears the data written to the underlying buffers.

You must clear the before trying to re-use it.

Dispose

void Dispose()

Clears the writer, and returns all the slabs to the pool