SliceOwner

Namespace: System · struct

Implements: ISliceSerializable, ISpanEncodable, IDisposable

A container for rented Slice that can be returned into a ArrayPool after it is not needed anymore.

Remarks

Users of this type MUST call Dispose after the data is no longer needed, or the buffers will not be returned to the pool.

Users of this type MUST NOT use any data from this instance after calling Dispose

This type mirrors IMemoryOwner but without allocations and with further optimizations.

Properties

Count

int Count { get; }

Size (in bytes) of the content

Returns once the container is disposed.

Data

Slice Data { get; }

Returns the content as a Slice

Empty

static SliceOwner Empty { get; }

Rented buffer that is equivalent to the Empty slice

IsPooled

bool IsPooled { get; }

Returns true if the buffer is rented from a pool and is temporary, or false if it is allocated from the heap and can be exposed

IsValid

bool IsValid { get; }

Returns true if the buffer is still usable, or false if Dispose has been called at least once.

Memory

ReadOnlyMemory<byte> Memory { get; }

Returns the content as a ReadOnlyMemory

Nil

static SliceOwner Nil { get; }

Rented buffer that is equivalent to the Nil slice

Pool

ArrayPool<byte> Pool { get; }

Pool used to allocate the buffer.

Span

ReadOnlySpan<byte> Span { get; }

Returns the content as a ReadOnlySpan

Methods

Copy

Slice Copy()

Returns a copy the original content (whether it was pooled or not)

SliceOwner Copy(ArrayPool<byte> pool, bool clearAfterUse = false)

Returns a copy of the original content, stored using the specified pool

static SliceOwner Copy(Slice data, ArrayPool<byte> pool, bool clearAfterUse = false)

Returns a SliceOwner with a copy of Slice, using a buffer allocated from a pool

  • data — Slice to copy
  • pool — Pool that will be used to allocate the content of data.
  • clearAfterUse — If true, the buffer will be cleared when Dispose is called, before optionally being returned to the pool

Returns: SliceOwner that will return the buffer to the pool once disposed.

static SliceOwner Copy(ReadOnlySpan<byte> data, ArrayPool<byte> pool, bool clearAfterUse = false)

Returns a SliceOwner with a copy of a span of bytes, stored in a slice allocated from a pool

  • data — Span of bytes to copy
  • pool — Pool that will be used to allocate the content of data.
  • clearAfterUse — If true, the buffer will be cleared when Dispose is called, before optionally being returned to the pool

Returns: SliceOwner that will return the buffer to the pool once disposed.

CopyTo

int CopyTo(Span<byte> destination)

Copies the content to a destination Span

Create

static SliceOwner Create(Slice data, ArrayPool<byte> pool = null, bool clearAfterUse = false)

Returns a SliceOwner that will dispose the content of a slice allocated from a pool

  • data — Slice of data, that is either Nil, Empty, or uses an array rented from pool
  • pool — Pool (optional) that was used to allocate the content of data, or null if the content was allocated on the heap or must not be returned to any pool.
  • clearAfterUse — If true, the buffer will be cleared when Dispose is called, before optionally being returned to the pool

Returns: SliceOwner that will return the buffer to the pool once disposed (if one was provided).

Dispose

void Dispose()

GetOrCopy

Slice GetOrCopy()

Returns either the original content (if it is not pooled), or a copy of the content (if it is pooled)

Returns: Slice that is safe to use, even after this instance has been disposed

ToArray

byte[] ToArray()

Returns a copy of the content

TryCopyTo

bool TryCopyTo(Span<byte> destination)

Copies the content to a destination Span, if it is large enough.

bool TryCopyTo(Span<byte> destination, out int bytesWritten)

Copies the content to a destination Span, if it is large enough.

TryGetMemory

bool TryGetMemory(out ReadOnlyMemory<byte> data)

Returns the content as a ReadOnlyMemory, unless the container has been disposed.

TryGetSlice

bool TryGetSlice(out Slice data)

Returns the content as a Slice, unless the container has been disposed.

TryGetSpan

bool TryGetSpan(out ReadOnlySpan<byte> data)

Returns the content as a ReadOnlySpan, unless the container has been disposed.

Wrap

static SliceOwner Wrap(Slice data)

Returns a SliceOwner that wraps an existing Slice that is not allocated from a pool

  • data — Slice of data, that is either Nil, Empty

Returns: SliceOwner that will do nothing when disposed.

static SliceOwner Wrap(Slice data, bool clearAfterUse)

Returns a SliceOwner that wraps an existing Slice that is not allocated from a pool

  • data — Slice of data, that is either Nil, Empty
  • clearAfterUse — If true, the buffer will be cleared when Dispose is called, before optionally being returned to the pool

Returns: SliceOwner that will do nothing when disposed.