Struct MemoryPoolToken<T>
A lightweight container for an underlying MemoryPool<T> node.
public readonly struct MemoryPoolToken<T> : IDisposable
Type Parameters
TElement 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
Empty
An empty token.
public static MemoryPoolToken<T> Empty { get; }
Property Value
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
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
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
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
lengthintNew underlying buffer length.
trimStartboolSpecifies whether elements at the start of the buffer should be trimmed instead of elements at the end, when new
lengthis 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
lengthis 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
lengthintNumber of elements to move to the returned token.
Returns
- MemoryPoolToken<T>
New MemoryPoolToken<T> instance that contains the first
lengthelements 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.