Struct MemoryRental<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 struct MemoryRental<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.MemoryRental`1.StackallocThreshold) then it's possible to use pooled memory from
arbitrary MemoryPool<T> or Shared. Arbitrary
ArrayPool<T> is not supported because default Shared
is optimized for per-CPU core allocation which is perfectly for situation when the same
thread is responsible for renting and releasing array. Otherwise, it's recommended to
use ArrayRental<T>.
Examples
const int stackallocThreshold = 20;
var memory = size <=stackallocThreshold ? new MemoryRental<byte>(stackalloc byte[stackallocThreshold], size) : new MemoryRental<byte>(size);
Constructors
| Improve this Doc View SourceMemoryRental(MemoryPool<T>)
Rents the memory from the pool.
Declaration
public MemoryRental(MemoryPool<T> pool)
Parameters
Type | Name | Description |
---|---|---|
MemoryPool<T> | pool | The memory pool. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
MemoryRental(MemoryPool<T>, Int32)
Rents the memory from the pool.
Declaration
public MemoryRental(MemoryPool<T> pool, int minBufferSize)
Parameters
Type | Name | Description |
---|---|---|
MemoryPool<T> | pool | The memory pool. |
Int32 | minBufferSize | The minimum size of the memory to rent. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
MemoryRental(Int32)
Rents the memory from Shared.
Declaration
public MemoryRental(int minBufferSize)
Parameters
Type | Name | Description |
---|---|---|
Int32 | minBufferSize | The minimum size of the memory to rent. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
MemoryRental(Span<T>)
Rents the memory referenced by the span.
Declaration
public MemoryRental(Span<T> span)
Parameters
Type | Name | Description |
---|---|---|
Span<T> | span | The span that references the memory to rent. |
MemoryRental(Span<T>, Int32)
Rents the memory referenced by the span.
Declaration
public MemoryRental(Span<T> span, int length)
Parameters
Type | Name | Description |
---|---|---|
Span<T> | span | The span that references the memory to rent. |
Int32 | length | The actual length of the data. |
Properties
| Improve this Doc View SourceIsEmpty
Gets a value indicating that this object doesn't reference rented memory.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
Boolean |
Item[Int32]
Gets the memory element by its index.
Declaration
public T this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | 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 int Length { get; }
Property Value
Type | Description |
---|---|
Int32 |
Span
Gets the rented memory.
Declaration
public Span<T> Span { get; }
Property Value
Type | Description |
---|---|
Span<T> |
Methods
| Improve this Doc View SourceDispose()
Returns the memory back to the pool.
Declaration
public void Dispose()
ToString()
Gets textual representation of the rented memory.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String | The textual representation of the rented memory. |
Overrides
Operators
| Improve this Doc View SourceImplicit(Span<T> to MemoryRental<T>)
Converts the reference to the already allocated memory into the rental object.
Declaration
public static implicit operator MemoryRental<T>(Span<T> span)
Parameters
Type | Name | Description |
---|---|---|
Span<T> | span | The allocated memory to convert. |
Returns
Type | Description |
---|---|
MemoryRental<T> |