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
| Edit this page 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 | The memory allocator. |
int | length | The number of items in the rented memory. |
Returns
Type | Description |
---|---|
MemoryOwner<T> | The allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
AllocateAtLeast<T>(int)
Rents a block of memory of at least
length
size from Shared pool.
Declaration
public static MemoryOwner<T> AllocateAtLeast<T>(int length)
Parameters
Type | Name | Description |
---|---|---|
int | length | The number of items in the rented memory. |
Returns
Type | Description |
---|---|
MemoryOwner<T> | The allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
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 | The memory allocator. |
int | length | The number of items in the rented memory. |
Returns
Type | Description |
---|---|
MemoryOwner<T> | The allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
AllocateExactly<T>(int)
Rents a block of memory of the specified size from Shared pool.
Declaration
public static MemoryOwner<T> AllocateExactly<T>(int length)
Parameters
Type | Name | Description |
---|---|---|
int | length | The number of items in the rented memory. |
Returns
Type | Description |
---|---|
MemoryOwner<T> | The allocated memory. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
Concat<T>(ReadOnlyMemory<T>, ReadOnlyMemory<T>)
Converts two memory blocks to ReadOnlySequence<T> data type.
Declaration
public static ReadOnlySequence<T> Concat<T>(this ReadOnlyMemory<T> first, ReadOnlyMemory<T> second)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlyMemory<T> | first | The first memory block. |
ReadOnlyMemory<T> | second | The second memory block. |
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. |
CopyTo<T>(in ReadOnlySequence<T>, scoped Span<T>, out int)
Copies the contents from the source sequence into a destination span.
Declaration
public static void CopyTo<T>(this in ReadOnlySequence<T> source, scoped Span<T> destination, out int writtenCount)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySequence<T> | source | Source sequence. |
Span<T> | destination | Destination memory. |
int | writtenCount | The number of copied elements. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the sequence. |
GetArrayAllocator<T>()
Returns array allocator.
Declaration
public static MemoryAllocator<T> GetArrayAllocator<T>()
Returns
Type | Description |
---|---|
MemoryAllocator<T> | The array allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the array. |
GetPinnedArrayAllocator<T>()
Returns an allocator of pinned arrays.
Declaration
public static MemoryAllocator<T> GetPinnedArrayAllocator<T>() where T : unmanaged
Returns
Type | Description |
---|---|
MemoryAllocator<T> | The array allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the array. |
GetReference<T>(in MemoryOwner<T>)
Gets managed pointer to the first element in the rented memory block.
Declaration
public static ref T GetReference<T>(in MemoryOwner<T> owner)
Parameters
Type | Name | Description |
---|---|---|
MemoryOwner<T> | owner | The rented memory block. |
Returns
Type | Description |
---|---|
T | A managed pointer to the first element; or NullRef<T>() if memory block is empty. |
Type Parameters
Name | Description |
---|---|
T | The type of the elements in the memory block. |
ReleaseAll<T>(ref MemoryOwner<T>)
Releases all resources encapsulated by the container.
Declaration
public static void ReleaseAll<T>(this ref MemoryOwner<T> owner) where T : notnull, IDisposable
Parameters
Type | Name | Description |
---|---|---|
MemoryOwner<T> | owner | The rented memory. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the rented memory. |
Remarks
This method calls Dispose() for each object in the rented block.
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 = null)
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 |
|
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 | The array pool. |
Returns
Type | Description |
---|---|
MemoryAllocator<T> | The array allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
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 | The memory pool. |
Returns
Type | Description |
---|---|
MemoryAllocator<T> | The memory allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
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 | The memory provider. |
Returns
Type | Description |
---|---|
MemoryAllocator<T> | The memory allocator. |
Type Parameters
Name | Description |
---|---|
T | The type of the items in the memory pool. |
ToReadOnlySequence(IEnumerable<string?>)
Constructs a sequence of characters from a collection of strings.
Declaration
public static ReadOnlySequence<char> ToReadOnlySequence(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. |
ToReadOnlySequence(StringBuilder)
Gets a sequence of characters written to the builder.
Declaration
public static ReadOnlySequence<char> ToReadOnlySequence(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. |
ToReadOnlySequence<T>(IEnumerable<ReadOnlyMemory<T>>)
Converts the sequence of memory blocks to ReadOnlySequence<T> data type.
Declaration
public static ReadOnlySequence<T> ToReadOnlySequence<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. |
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 | A contiguous region of arbitrary memory. |
int | maxLength | Maximum length. |
Returns
Type | Description |
---|---|
Memory<T> | Trimmed memory block. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the span. |
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 | A contiguous region of arbitrary memory. |
int | maxLength | Maximum length. |
Returns
Type | Description |
---|---|
ReadOnlyMemory<T> | Trimmed memory block. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the span. |
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> sequence, int count, out ReadOnlyMemory<T> span)
Parameters
Type | Name | Description |
---|---|---|
ReadOnlySequence<T> | sequence | The sequence of elements to read from. |
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 | The type of the elements in the block. |