Struct SpanOwner<T>
Represents the memory obtained from the pool or allocated on the stack or heap.
Inherited Members
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 SourceSpanOwner(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 |
|
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 |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentOutOfRangeException |
|
SpanOwner(int, bool)
Rents the memory from Shared, if T
contains at least one field of the 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 |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
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. |
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 SourceIsEmpty
Gets a value indicating that this object doesn't reference rented memory.
Declaration
public readonly bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| bool |
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. |
Length
Gets length of the rented memory.
Declaration
public readonly int Length { get; }
Property Value
| Type | Description |
|---|---|
| int |
Span
Gets the rented memory.
Declaration
public readonly Span<T> Span { get; }
Property Value
| Type | Description |
|---|---|
| Span<T> |
Methods
| Edit this page View SourceDispose()
Returns the memory to the pool.
Declaration
public void Dispose()
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
Operators
| Edit this page View Sourceimplicit 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> |