SliceBuffer

Namespace: SnowBank.Buffers · class

Implements: IBufferWriter<byte>

Buffer that can be used to efficiently store multiple slices into as few chunks as possible

Remarks

This class is useful to centralize a lot of temporary slices whose lifetime is linked to a specific operation. Dropping the reference to the buffer will automatically reclaim all the slices that were stored with it. This class is not thread safe.

Constructors

SliceBuffer

SliceBuffer()

Create a new slice buffer with the default page size

SliceBuffer(int pageSize, ArrayPool<byte> pool = null)

Create a new slice buffer with the specified page size

  • pageSize — Initial page size

Properties

Allocated

int Allocated { get; }

Gets the total memory size allocated to store all the slices in this buffer

PageCount

int PageCount { get; }

Number of memory pages used by this buffer

Size

int Size { get; }

Gets the number of bytes used by all the slice allocated in this buffer

Methods

Advance

Slice Advance(int count)

Notifies SliceBuffer that count amount of data was written to the output Span returned by a previous call to GetSpan

  • count — Number of bytes written

Returns: Slice with the data that was written

AllocateSpan

Span<byte> AllocateSpan(int count, bool aligned = false)

Allocate an empty space in the buffer

  • count — Number of bytes to allocate
  • aligned — If true, align the start of the slice with the default padding size.

Returns: Slice pointing to a space in the buffer

There is NO guarantees that the allocated slice will be pre-filled with zeroes.

GetMemory

Memory<byte> GetMemory(int sizeHint = 0)

Returns a Memory<byte> to write to that is at least the requested length (specified by sizeHint). If no sizeHint is provided (or it's equal to 0), some non-empty buffer is returned.

  • sizeHint — Minimum size required, or 0 to return all the remaining allocated space

Returns: Memory that can hold at least sizeHint bytes (if sizeHint is greater than 0), or all the space remaining in the buffer (if sizeHint is 0)

GetPages

Slice[] GetPages()

Return the list of all the pages used by this buffer

Returns: Array of pages used by the buffer

GetSpan

Span<byte> GetSpan(int sizeHint = 0)

Returns a Span<byte> to write to that is at least the requested length (specified by sizeHint). If no sizeHint is provided (or it's equal to 0), some non-empty buffer is returned.

  • sizeHint — Minimum size required, or 0 to return all the remaining allocated space

Returns: Span that can hold at least sizeHint bytes (if sizeHint is greater than 0), or all the space remaining in the buffer (if sizeHint is 0)

Intern

Slice Intern(ReadOnlySpan<byte> data, bool aligned = false)

Copy a slice into the buffer, with optional alignment, and return a new identical slice.

  • data — Data to copy to the buffer
  • aligned — If true, align the index of first byte of the slice with a multiple of 8 bytes

Returns: Slice that is the equivalent of data, backed by the buffer.

Slice Intern(Slice data, bool aligned = false)

Copy a slice into the buffer, with optional alignment, and return a new identical slice.

  • data — Data to copy to the buffer
  • aligned — If true, align the index of first byte of the slice with a multiple of 8 bytes

Returns: Slice that is the equivalent of data, backed by the buffer.

Slice[] Intern(ReadOnlySpan<Slice> data, bool aligned = false)

Copy a list of slices into the buffer, with optional alignment, and return a new array with the identical slices.

  • data — List of data to copy to the buffer
  • aligned — If true, align the index of first byte of the slice with a multiple of 8 bytes

Returns: Array of slices that are the equivalent of each slice in data, backed by the buffer.

Slice Intern(Slice data, Slice suffix, bool aligned = false)

Copy a slice into the buffer, immediately followed by a suffix, and return a new slice that is the concatenation of the two.

  • data — Data to copy to the buffer
  • suffix — Suffix to copy immediately after data.
  • aligned — If true, align the index of first byte of the slice with a multiple of 8 bytes

Returns: Slice that is the equivalent of data plus suffix, backed by the buffer.

When is empty, is returned without being copied to the buffer itself.

Slice Intern(ReadOnlySpan<byte> data, Slice suffix, bool aligned = false)

Copy a slice into the buffer, immediately followed by a suffix, and return a new slice that is the concatenation of the two.

  • data — Data to copy to the buffer
  • suffix — Suffix to copy immediately after data.
  • aligned — If true, align the index of first byte of the slice with a multiple of 8 bytes

Returns: Slice that is the equivalent of data plus suffix, backed by the buffer.

When is empty, is returned without being copied to the buffer itself.

Pin

Pinned Pin()

This method is not supported anymore

ReleaseMemoryUnsafe

void ReleaseMemoryUnsafe(bool clear = false)

Reset the buffer to its initial state and allow reuse of previously allocated memory

If there is a pool attached, all pages are returned to the pool and could be reused later. IMPORTANT: all slices allocated from this buffer CANNOT be used after a call to Reset!!!