Show / Hide Table of Contents

Struct SpanOwner<T>

Represents the memory obtained from the pool or allocated on the stack or heap.

Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: DotNext.Buffers
Assembly: DotNext.dll
Syntax
public ref struct SpanOwner<T>
Type Parameters
Name Description
T

The type of the elements in the rented memory.

Remarks

This type is aimed to be compatible with memory allocated using stackalloc operator. If stack allocation threshold is reached (e.g. DotNext.Buffers.SpanOwner<T>.StackallocThreshold) then it's possible to use pooled memory from arbitrary MemoryPool<T> or Shared. Custom ArrayPool<T> is not supported because default Shared is optimized for per-CPU core allocation which is perfect when the same thread is responsible for renting and releasing the array.

Examples
const int stackallocThreshold = 20;
var memory = size <=stackallocThreshold ? new SpanOwner<byte>(stackalloc byte[stackallocThreshold], size) : new SpanOwner<byte>(size);

Constructors

| Edit this page View Source

SpanOwner(MemoryPool<T>)

Rents the memory from the pool.

Declaration
public SpanOwner(MemoryPool<T> pool)
Parameters
Type Name Description
MemoryPool<T> pool

The memory pool.

Exceptions
Type Condition
ArgumentNullException

pool is null.

| Edit this page View Source

SpanOwner(MemoryPool<T>, int, bool)

Rents the memory from the pool.

Declaration
public SpanOwner(MemoryPool<T> pool, int minBufferSize, bool exactSize = true)
Parameters
Type Name Description
MemoryPool<T> pool

The memory pool.

int minBufferSize

The minimum size of the memory to rent.

bool exactSize

true to return the buffer of minBufferSize length; otherwise, the returned buffer is at least of minBufferSize.

Exceptions
Type Condition
ArgumentNullException

pool is null.

ArgumentOutOfRangeException

minBufferSize is less than or equal to zero.

| Edit this page View Source

SpanOwner(int, bool)

Rents the memory from Shared, if T contains at least one field of reference type; or use NativeMemory.

Declaration
public SpanOwner(int minBufferSize, bool exactSize = true)
Parameters
Type Name Description
int minBufferSize

The minimum size of the memory to rent.

bool exactSize

true to return the buffer of minBufferSize length; otherwise, the returned buffer is at least of minBufferSize.

Exceptions
Type Condition
ArgumentOutOfRangeException

minBufferSize is less than or equal to zero.

| Edit this page View Source

SpanOwner(Span<T>)

Rents the memory referenced by the span.

Declaration
public SpanOwner(Span<T> span)
Parameters
Type Name Description
Span<T> span

The span that references the memory to rent.

| Edit this page View Source

SpanOwner(Span<T>, int)

Rents the memory referenced by the span.

Declaration
public SpanOwner(Span<T> span, int length)
Parameters
Type Name Description
Span<T> span

The span that references the memory to rent.

int length

The actual length of the data.

Properties

| Edit this page View Source

IsEmpty

Gets a value indicating that this object doesn't reference rented memory.

Declaration
public readonly bool IsEmpty { get; }
Property Value
Type Description
bool
| Edit this page View Source

this[int]

Gets the memory element by its index.

Declaration
public readonly ref T this[int index] { get; }
Parameters
Type Name Description
int index

The index of the memory element.

Property Value
Type Description
T

The managed pointer to the memory element.

| Edit this page View Source

Length

Gets length of the rented memory.

Declaration
public readonly int Length { get; }
Property Value
Type Description
int
| Edit this page View Source

Span

Gets the rented memory.

Declaration
public readonly Span<T> Span { get; }
Property Value
Type Description
Span<T>

Methods

| Edit this page View Source

Dispose()

Returns the memory back to the pool.

Declaration
public void Dispose()
| Edit this page View Source

ToString()

Gets textual representation of the rented memory.

Declaration
public override readonly string ToString()
Returns
Type Description
string

The textual representation of the rented memory.

Overrides
ValueType.ToString()

Operators

| Edit this page View Source

implicit operator SpanOwner<T>(Span<T>)

Converts the reference to the already allocated memory into the rental object.

Declaration
public static implicit operator SpanOwner<T>(Span<T> span)
Parameters
Type Name Description
Span<T> span

The allocated memory to convert.

Returns
Type Description
SpanOwner<T>
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾