Table of Contents

Struct MemoryPoolToken<T>

Namespace
LfrlAnvil.Memory
Assembly
LfrlAnvil.Core.dll

A lightweight container for an underlying MemoryPool<T> node.

public readonly struct MemoryPoolToken<T> : IDisposable

Type Parameters

T

Element type.

Implements
Inherited Members
Extension Methods

Properties

Clear

Specifies whether the underlying buffer will be additionally cleared during this token's disposal.

public bool Clear { get; }

Property Value

bool

Empty

An empty token.

public static MemoryPoolToken<T> Empty { get; }

Property Value

MemoryPoolToken<T>

Owner

MemoryPool<T> instance that owns this token.

public MemoryPool<T>? Owner { get; }

Property Value

MemoryPool<T>

Methods

AsMemory()

Creates a new Memory<T> instance from the underlying buffer of this token.

[Pure]
public Memory<T> AsMemory()

Returns

Memory<T>

New Memory<T> instance.

Remarks

Returns an Empty instance for disposed tokens.

AsSpan()

Creates a new Span<T> instance from the underlying buffer of this token.

[Pure]
public Span<T> AsSpan()

Returns

Span<T>

New Span<T> instance.

Remarks

Returns an Empty instance for disposed tokens.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Remarks

Frees the underlying node and returns it to the pool.

EnableClearing(bool)

Creates a new MemoryPoolToken<T> from this token with a new Clear value.

[Pure]
public MemoryPoolToken<T> EnableClearing(bool enabled = true)

Parameters

enabled bool

Clear value to set.

Returns

MemoryPoolToken<T>

New MemoryPoolToken<T> instance.

SetLength(int, bool)

Resizes the underlying buffer. Length change will invalidate exposed underlying buffer's Memory<T> or Span<T> instances.

public void SetLength(int length, bool trimStart = false)

Parameters

length int

New underlying buffer length.

trimStart bool

Specifies whether elements at the start of the buffer should be trimmed instead of elements at the end, when new length is less than current length. Equal to false by default.

Remarks

When new length is less than the current length, then elements at the end of the buffer will be discarded, unless trimStart is equal to true, in which case elements at the start of the buffer will be discarded. Additionally, the Clear value will determine whether discarded elements will be cleared.

Exceptions

ArgumentOutOfRangeException

When length is less than or equal to 0.

Split(int)

Splits this token in-place into two tokens, after which this token will contain elements at the end of the original buffer and returned token will contain elements at the start of the original buffer.

public MemoryPoolToken<T> Split(int length)

Parameters

length int

Number of elements to move to the returned token.

Returns

MemoryPoolToken<T>

New MemoryPoolToken<T> instance that contains the first length elements from the original buffer.

Remarks

Returns an Empty instance for disposed tokens or when provided length is less than or equal to 0. Returns this token when the provided length is greater than or equal to its buffer's length. Clear value is copied from this token.

ToString()

Returns a string representation of this MemoryPoolToken<T> instance.

[Pure]
public override string ToString()

Returns

string

String representation.

TryGetInfo()

Returns a MemoryPool<T>.ReportInfo.Node instance that represents this token, if node exists.

[Pure]
public MemoryPool<T>.ReportInfo.Node? TryGetInfo()

Returns

MemoryPool<T>.ReportInfo.Node?

New MemoryPool<T>.ReportInfo.Node instance or null, if node does not exist.