Table of Contents

Struct SafeBufferSpanProvider

Namespace
Zyl.ExSpans
Assembly
ExSpans.Core.dll

The span provider that manages the pointer acquire for SafeBuffer (管理 SafeBuffer 指针获取的跨度提供者). For example, it can provide span access for memory mapped files (例如它可以为内存映射文件提供跨度访问器).

public readonly struct SafeBufferSpanProvider : IDisposable, IExSpanBase<byte>, IReadOnlyExSpanBase<byte>, IExLength
Implements
Inherited Members
Extension Methods

Remarks

For example:
static void TestMemoryMappedFile(TextWriter writer) {
    try {
        const string MemoryMappedFilePath = "ExSpans.Sample.tmp";
        const string? MemoryMappedFileMapName = null; // If it is not null, MacOS will throw an exception. System.PlatformNotSupportedException: Named maps are not supported.
        const long MemoryMappedFileSize = 1 * 1024 * 1024; // 1MB
        using MemoryMappedFile mappedFile = MemoryMappedFile.CreateFromFile(MemoryMappedFilePath, FileMode.Create, MemoryMappedFileMapName, MemoryMappedFileSize);
        using MemoryMappedViewAccessor accessor = mappedFile.CreateViewAccessor();
        using SafeBufferSpanProvider spanProvider = accessor.SafeMemoryMappedViewHandle.CreateSpanProvider();
        // Write.
        ExSpan<int> spanInt = spanProvider.CreateExSpan<int>();
        spanInt.Fill(0x01020304);
        spanInt[0] = 0x12345678;
        // Read.
        writer.WriteLine(string.Format("Data[0]: {0} // 0x{0:X}", spanInt[0]));
        writer.WriteLine(string.Format("Data[1]: {0} // 0x{0:X}", spanInt[1]));
    } catch (Exception ex) {
        writer.WriteLine(string.Format("Run TestMemoryMappedFile fail! {0}", ex.ToString()));
    }
    // Output:
    // Data[0]: 305419896 // 0x12345678
    // Data[1]: 16909060 // 0x1020304
}

Constructors

SafeBufferSpanProvider(SafeBuffer)

Create SafeBufferSpanProvider (创建 SafeBufferSpanProvider).

Properties

Length

The number of items in the Ex span (扩展跨度中的项数).

Source

Get source SafeBuffer (取得源 SafeBuffer).

Methods

CreateExSpanSaturating<TTo>()

Create a ExSpan<T> using the specified type with saturating (饱和的使用指定类型来创建 ExSpan<T>).

CreateExSpan<TTo>()

Create a ExSpan<T> using the specified type (使用指定类型来创建 ExSpan<T>).

CreateReadOnlyExSpanSaturating<TTo>()

Create a ReadOnlyExSpan<T> using the specified type with saturating (饱和的使用指定类型来创建 ReadOnlyExSpan<T>).

CreateReadOnlyExSpan<TTo>()

Create a ReadOnlyExSpan<T> using the specified type (使用指定类型来创建 ReadOnlyExSpan<T>).

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

GetPinnableReadOnlyReference()

Returns a read only reference to the 0th element of the span. If the span is empty, returns null reference. It can be used for pinning and is required to support the use of span within a fixed statement (返回对只读跨度的第0个元素的引用。如果跨度为空,则返回null引用. 它可用于固定,并且需要支持在 fixed 语句中使用跨度).

GetPinnableReference()

Returns a reference to the 0th element of the span. If the span is empty, returns null reference. It can be used for pinning and is required to support the use of span within a fixed statement (返回对跨度的第0个元素的引用。如果跨度为空,则返回null引用. 它可用于固定,并且需要支持在 fixed 语句中使用跨度).