Show / Hide Table of Contents

Class Memory

Represents methods to work with memory pools and buffers.

Inheritance
object
Memory
Inherited Members
object.GetType()
object.MemberwiseClone()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: DotNext.Buffers
Assembly: DotNext.dll
Syntax
public static class Memory

Methods

View Source

AllocateAtLeast<T>(MemoryAllocator<T>, int)

Allocates memory of at least length size.

Declaration
public static MemoryOwner<T> AllocateAtLeast<T>(this MemoryAllocator<T> allocator, int length)
Parameters
Type Name Description
MemoryAllocator<T> allocator
int length

The number of items in the rented memory.

Returns
Type Description
MemoryOwner<T>

The allocated memory.

Type Parameters
Name Description
T
View Source

AllocateExactly<T>(MemoryAllocator<T>, int)

Allocates memory of length size.

Declaration
public static MemoryOwner<T> AllocateExactly<T>(this MemoryAllocator<T> allocator, int length)
Parameters
Type Name Description
MemoryAllocator<T> allocator
int length

The number of items in the rented memory.

Returns
Type Description
MemoryOwner<T>

The allocated memory.

Type Parameters
Name Description
T
View Source

Concat(IEnumerable<string?>?)

Constructs a sequence of characters from a collection of strings.

Declaration
public static ReadOnlySequence<char> Concat(this IEnumerable<string?>? strings)
Parameters
Type Name Description
IEnumerable<string> strings

A collection of strings.

Returns
Type Description
ReadOnlySequence<char>

A sequence of characters representing concatenated strings.

View Source

Concat<T>(IEnumerable<ReadOnlyMemory<T>>?)

Converts the sequence of memory blocks to ReadOnlySequence<T> data type.

Declaration
public static ReadOnlySequence<T> Concat<T>(this IEnumerable<ReadOnlyMemory<T>>? chunks)
Parameters
Type Name Description
IEnumerable<ReadOnlyMemory<T>> chunks

The sequence of memory blocks.

Returns
Type Description
ReadOnlySequence<T>

The constructed ReadOnlySequence<T> instance containing memory blocks.

Type Parameters
Name Description
T

The type of elements in the memory blocks.

View Source

Concat<T>(params ReadOnlySpan<ReadOnlyMemory<T>>)

Converts the sequence of memory blocks to ReadOnlySequence<T> data type.

Declaration
public static ReadOnlySequence<T> Concat<T>(params ReadOnlySpan<ReadOnlyMemory<T>> chunks)
Parameters
Type Name Description
ReadOnlySpan<ReadOnlyMemory<T>> chunks

The sequence of memory blocks.

Returns
Type Description
ReadOnlySequence<T>

The constructed ReadOnlySequence<T> instance containing memory blocks.

Type Parameters
Name Description
T
View Source

CopyTo<T>(in ReadOnlySequence<T>, Span<T>, out SequencePosition)

Copies the contents from the source sequence into a destination span.

Declaration
public static int CopyTo<T>(this in ReadOnlySequence<T> source, Span<T> destination, out SequencePosition consumed)
Parameters
Type Name Description
ReadOnlySequence<T> source
Span<T> destination

Destination memory.

SequencePosition consumed

The position within the receiver that represents the end of destination.

Returns
Type Description
int

The number of copied elements.

Type Parameters
Name Description
T
View Source

Resize<T>(ref MemoryOwner<T>, int, MemoryAllocator<T>)

Resizes the buffer.

Declaration
public static void Resize<T>(this ref MemoryOwner<T> owner, int newLength, MemoryAllocator<T> allocator)
Parameters
Type Name Description
MemoryOwner<T> owner

The buffer owner to resize.

int newLength

A new length of the buffer.

MemoryAllocator<T> allocator

The allocator to be called if the requested length is larger than the requested length.

Type Parameters
Name Description
T

The type of the elements in the buffer.

Exceptions
Type Condition
ArgumentOutOfRangeException

newLength is less than zero.

View Source

ToAllocator<T>(ArrayPool<T>)

Converts array pool to the memory allocator.

Declaration
public static MemoryAllocator<T> ToAllocator<T>(this ArrayPool<T> pool)
Parameters
Type Name Description
ArrayPool<T> pool
Returns
Type Description
MemoryAllocator<T>

The array allocator.

Type Parameters
Name Description
T
View Source

ToAllocator<T>(MemoryPool<T>)

Converts memory pool to the memory allocator.

Declaration
public static MemoryAllocator<T> ToAllocator<T>(this MemoryPool<T> pool)
Parameters
Type Name Description
MemoryPool<T> pool
Returns
Type Description
MemoryAllocator<T>

The memory allocator.

Type Parameters
Name Description
T
View Source

ToAllocator<T>(Func<int, IMemoryOwner<T>>)

Converts memory provider to the memory allocator.

Declaration
public static MemoryAllocator<T> ToAllocator<T>(this Func<int, IMemoryOwner<T>> provider)
Parameters
Type Name Description
Func<int, IMemoryOwner<T>> provider
Returns
Type Description
MemoryAllocator<T>

The memory allocator.

Type Parameters
Name Description
T
View Source

ToSequence(StringBuilder)

Gets a sequence of characters written to the builder.

Declaration
public static ReadOnlySequence<char> ToSequence(this StringBuilder builder)
Parameters
Type Name Description
StringBuilder builder

A string builder.

Returns
Type Description
ReadOnlySequence<char>

A sequence of characters written to the builder.

View Source

TrimLength<T>(Memory<T>, int)

Trims the memory block to specified length if it exceeds it. If length is less that maxLength then the original block returned.

Declaration
public static Memory<T> TrimLength<T>(this Memory<T> memory, int maxLength)
Parameters
Type Name Description
Memory<T> memory
int maxLength

Maximum length.

Returns
Type Description
Memory<T>

Trimmed memory block.

Type Parameters
Name Description
T
View Source

TrimLength<T>(ReadOnlyMemory<T>, int)

Trims the memory block to specified length if it exceeds it. If length is less that maxLength then the original block returned.

Declaration
public static ReadOnlyMemory<T> TrimLength<T>(this ReadOnlyMemory<T> memory, int maxLength)
Parameters
Type Name Description
ReadOnlyMemory<T> memory
int maxLength

Maximum length.

Returns
Type Description
ReadOnlyMemory<T>

Trimmed memory block.

Type Parameters
Name Description
T
View Source

TryGetBlock<T>(in ReadOnlySequence<T>, int, out ReadOnlyMemory<T>)

Tries to get a contiguous block of memory from the specified sequence of elements.

Declaration
public static bool TryGetBlock<T>(this in ReadOnlySequence<T> source, int count, out ReadOnlyMemory<T> span)
Parameters
Type Name Description
ReadOnlySequence<T> source
int count

The size of contiguous block.

ReadOnlyMemory<T> span

The contiguous block of elements.

Returns
Type Description
bool

true if contiguous block of elements is obtained successfully; otherwise, false.

Type Parameters
Name Description
T
View Source

get_Array<T>()

Gets the array allocator.

Declaration
public static MemoryAllocator<T> get_Array<T>()
Returns
Type Description
MemoryAllocator<T>

The array allocator.

Type Parameters
Name Description
T
View Source

get_DefaultIfNull<T>(MemoryAllocator<T>?)

Gets get_Default<T>() allocator if the current is null.

Declaration
public static MemoryAllocator<T> get_DefaultIfNull<T>(MemoryAllocator<T>? allocator)
Parameters
Type Name Description
MemoryAllocator<T> allocator
Returns
Type Description
MemoryAllocator<T>
Type Parameters
Name Description
T
View Source

get_Default<T>()

Gets the default allocator that uses Shared pool.

Declaration
public static MemoryAllocator<T> get_Default<T>()
Returns
Type Description
MemoryAllocator<T>
Type Parameters
Name Description
T
View Source

get_Pinned<T>()

Returns an allocator of pinned arrays.

Declaration
public static MemoryAllocator<T> get_Pinned<T>() where T : unmanaged
Returns
Type Description
MemoryAllocator<T>

The array allocator.

Type Parameters
Name Description
T
View Source

op_Modulus<T>(Memory<T>, int)

Trims the memory block to specified length if it exceeds it. If length is less that maxLength then the original block returned.

Declaration
public static Memory<T> op_Modulus<T>(Memory<T> x, int maxLength)
Parameters
Type Name Description
Memory<T> x

The memory to trim.

int maxLength

Maximum length.

Returns
Type Description
Memory<T>

Trimmed memory block.

Type Parameters
Name Description
T
View Source

op_Modulus<T>(ReadOnlyMemory<T>, int)

Trims the memory block to specified length if it exceeds it. If length is less that maxLength then the original block returned.

Declaration
public static ReadOnlyMemory<T> op_Modulus<T>(ReadOnlyMemory<T> x, int maxLength)
Parameters
Type Name Description
ReadOnlyMemory<T> x

The memory to trim.

int maxLength

Maximum length.

Returns
Type Description
ReadOnlyMemory<T>

Trimmed memory block.

Type Parameters
Name Description
T
View Source

op_UnsignedRightShift<T>(in ReadOnlySequence<T>, Span<T>)

Copies the contents from the source sequence into a destination span.

Declaration
public static int op_UnsignedRightShift<T>(in ReadOnlySequence<T> src, Span<T> dest)
Parameters
Type Name Description
ReadOnlySequence<T> src

The sequence to copy from.

Span<T> dest

Destination memory.

Returns
Type Description
int

The number of copied elements.

Type Parameters
Name Description
T
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾