Class Memory
Represents methods to work with memory pools and buffers.
Inherited Members
Namespace: DotNext.Buffers
Assembly: DotNext.dll
Syntax
public static class Memory
Methods
View SourceAllocateAtLeast<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 |
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 |
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. |
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. |
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 |
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 |
Returns
| Type | Description |
|---|---|
| int | The number of copied elements. |
Type Parameters
| Name | Description |
|---|---|
| T |
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 |
|
SequenceCompareTo<T>(in ReadOnlySequence<T>, in ReadOnlySequence<T>, IComparer<T>?)
Determines the relative order of the sequences being compared by comparing the elements.
Declaration
public static int SequenceCompareTo<T>(this in ReadOnlySequence<T> source, in ReadOnlySequence<T> other, IComparer<T>? comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySequence<T> | source | |
| ReadOnlySequence<T> | other | The second sequence to compare. |
| IComparer<T> | comparer | The comparer to use when comparing elements; or null to use the Default. |
Returns
| Type | Description |
|---|---|
| int | A signed integer that indicates the relative order. |
Type Parameters
| Name | Description |
|---|---|
| T |
SequenceCompareTo<T>(in ReadOnlySequence<T>, ReadOnlySpan<T>, IComparer<T>?)
Determines the relative order of the sequences being compared by comparing the elements.
Declaration
public static int SequenceCompareTo<T>(this in ReadOnlySequence<T> source, ReadOnlySpan<T> other, IComparer<T>? comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySequence<T> | source | |
| ReadOnlySpan<T> | other | The second sequence to compare. |
| IComparer<T> | comparer | The comparer to use when comparing elements; or null to use the Default. |
Returns
| Type | Description |
|---|---|
| int | A signed integer that indicates the relative order. |
Type Parameters
| Name | Description |
|---|---|
| T |
SequenceEqual<T>(in ReadOnlySequence<T>, in ReadOnlySequence<T>, IEqualityComparer<T>?)
Determines whether two sequences are equal by comparing the elements.
Declaration
public static bool SequenceEqual<T>(this in ReadOnlySequence<T> source, in ReadOnlySequence<T> other, IEqualityComparer<T>? comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySequence<T> | source | |
| ReadOnlySequence<T> | other | The second sequence to compare. |
| IEqualityComparer<T> | comparer | The comparer to use when comparing elements; or null to use the Default. |
Returns
| Type | Description |
|---|---|
| bool | true if both sequences have the same elements in the same order; otherwise, false. |
Type Parameters
| Name | Description |
|---|---|
| T |
SequenceEqual<T>(in ReadOnlySequence<T>, ReadOnlySpan<T>, IEqualityComparer<T>?)
Determines whether two sequences are equal by comparing the elements.
Declaration
public static bool SequenceEqual<T>(this in ReadOnlySequence<T> source, ReadOnlySpan<T> other, IEqualityComparer<T>? comparer = null)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySequence<T> | source | |
| ReadOnlySpan<T> | other | The second sequence to compare. |
| IEqualityComparer<T> | comparer | The comparer to use when comparing elements; or null to use the Default. |
Returns
| Type | Description |
|---|---|
| bool | true if both sequences have the same elements in the same order; otherwise, false. |
Type Parameters
| Name | Description |
|---|---|
| T |
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 |
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 |
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 |
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. |
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 |
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 |
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 |
get_ArrayPool<T>()
Gets the allocator which uses Shared pool.
Declaration
public static MemoryAllocator<T> get_ArrayPool<T>()
Returns
| Type | Description |
|---|---|
| MemoryAllocator<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
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 |
get_ByteArrayPool<T>()
Gets the allocator that rents memory from ArrayPool<byte>.Shared
and reinterprets it as elements of type T.
Declaration
public static MemoryAllocator<T> get_ByteArrayPool<T>()
Returns
| Type | Description |
|---|---|
| MemoryAllocator<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
All element types share a single pool of byte arrays instead of keeping a separate pool per type. This applies to types that contain no references and require alignment no larger than the pointer size, such as char, int, double, or structs composed of them. For other types, as well as for small or very large buffers, the allocator falls back to Shared.
Memory rented from the byte pool is backed by MemoryManager<T> rather than an array: each rental allocates a small wrapper object, and TryGetArray<T>(ReadOnlyMemory<T>, out ArraySegment<T>) returns false for such memory.
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 |
get_Default<T>()
Gets the allocator, which is get_ArrayPool<T>() by default.
Declaration
public static MemoryAllocator<T> get_Default<T>()
Returns
| Type | Description |
|---|---|
| MemoryAllocator<T> |
Type Parameters
| Name | Description |
|---|---|
| T |
Remarks
The default allocator can be changed to get_Array<T>() by
turning off DotNext.Buffers.MemoryAllocator.Default.UseArrayPool switch.
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 |
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 |
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 |
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 |