Table of Contents

Class UnmanagedMemorySpanHolder<T>

Namespace
LibSodium
Assembly
LibSodium.Net.dll

Holds the pointer and length of an unmanaged memory span for later recreation. Use only with unmanaged memory.

public class UnmanagedMemorySpanHolder<T> where T : unmanaged

Type Parameters

T

The unmanaged type of the span elements.

Inheritance
UnmanagedMemorySpanHolder<T>
Inherited Members

Remarks

This class stores the pointer and length of an unmanaged memory span, allowing for its later reconstruction. It is critical to use this class only with spans pointing to unmanaged memory. Using it with managed or stack-allocated spans will result in undefined behavior and potential memory corruption. This holder is particularly useful when a span needs to be accessed in asynchronous operations, lambda expressions, anonymous methods or other contexts with limited scope, such as after awaiting a task.

Constructors

UnmanagedMemorySpanHolder(ReadOnlySpan<T>)

Initializes a new instance of the UnmanagedMemorySpanHolder<T> class from a read-only span. Use only with unmanaged memory.

public UnmanagedMemorySpanHolder(ReadOnlySpan<T> span)

Parameters

span ReadOnlySpan<T>

The read-only span pointing to unmanaged memory.

Exceptions

ArgumentException

Thrown when the span is empty.

UnmanagedMemorySpanHolder(Span<T>)

Initializes a new instance of the UnmanagedMemorySpanHolder<T> class from a writable span. Use only with unmanaged memory.

public UnmanagedMemorySpanHolder(Span<T> span)

Parameters

span Span<T>

The read-only span pointing to unmanaged memory.

Exceptions

ArgumentException

Thrown when the span is empty.

Methods

GetOriginalReadOnlySpan()

Creates a new ReadOnlySpan<T> from the held unmanaged memory representing the original ReadOnlySpan<T>.

public ReadOnlySpan<T> GetOriginalReadOnlySpan()

Returns

ReadOnlySpan<T>

A ReadOnlySpan<T> representing the original ReadOnlySpan<T>

GetOriginalSpan()

Creates a new Span<T> from the held unmanaged memory representing the original Span<T>

public Span<T> GetOriginalSpan()

Returns

Span<T>

A Span<T> representing the original Span<T>

Exceptions

InvalidOperationException

The original span was read-only, you cannot get a writable span.