Show / Hide Table of Contents

Struct MemoryRental<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 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 Source

MemoryRental(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

pool is null.

| Improve this Doc View Source

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

pool is null.

ArgumentOutOfRangeException

minBufferSize is less than or equal to zero.

| Improve this Doc View Source

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

minBufferSize is less than or equal to zero.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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 Source

IsEmpty

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

Declaration
public bool IsEmpty { get; }
Property Value
Type Description
Boolean
| Improve this Doc View Source

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.

| Improve this Doc View Source

Length

Gets length of the rented memory.

Declaration
public int Length { get; }
Property Value
Type Description
Int32
| Improve this Doc View Source

Span

Gets the rented memory.

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

Methods

| Improve this Doc View Source

Dispose()

Returns the memory back to the pool.

Declaration
public void Dispose()
| Improve this Doc View Source

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
ValueType.ToString()

Operators

| Improve this Doc View Source

Implicit(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>
  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX