JsSharedDataBlock Class

A Shared Data Block (https://tc39.es/ecma262/#sec-data-blocks): the memory of a SharedArrayBuffer. It can be shared by several engines (agents) running on different threads, e.g. by the structured clone of a SharedArrayBuffer sent to a worker. The memory is a managed array allocated once for the maximum byte length on the pinned object heap: a growable block never moves, its byte length grows atomically. The bytes are 16-byte aligned (the atomic operations of 64-bit values are aligned). The spans, references and memories of the block reference the array, so they keep the memory alive by themselves, even once no SharedArrayBuffer references the block.

public sealed class JsSharedDataBlock

Properties

NameDescription
ByteLengthGets the byte length ([[ArrayBufferByteLengthData]], read with the seq-cst order).
IsGrowableGets whether the block is growable.
MaxByteLengthGets the maximum byte length of a growable block, or null for a fixed-length block.

Gets the byte length ([[ArrayBufferByteLengthData]], read with the seq-cst order).

public int ByteLength { get; }

Property value

int

Gets whether the block is growable.

public bool IsGrowable { get; }

Property value

bool

Gets the maximum byte length of a growable block, or null for a fixed-length block.

public int? MaxByteLength { get; }

Property value

int?

Methods

NameDescription
GetMemory()Gets the bytes of the block (its current byte length) as a Memory<T>, which keeps the memory alive.
GetReference(int)Gets a reference to a byte of the block (for the atomic operations); the index is less than the byte length.
GetSpan()Gets the bytes of the block (its current byte length).
TryCreate(int, int?)CreateSharedByteDataBlock(size): a zeroed block, or null when the memory cannot be allocated.
TryGrow(int)Grows the block (SharedArrayBuffer.prototype.grow): false when the new length is less than the current length or greater than the maximum byte length. The new bytes are zeroed before the new length is published.

Gets the bytes of the block (its current byte length) as a Memory<T>, which keeps the memory alive.

public Memory<byte> GetMemory()

GetReference(int)

JsSharedDataBlock.cs

Gets a reference to a byte of the block (for the atomic operations); the index is less than the byte length.

public ref byte GetReference(int byteIndex)

Parameters

byteIndex int

Returns

byte

Gets the bytes of the block (its current byte length).

public Span<byte> GetSpan()

Returns

Span<byte>

TryCreate(int, int?)

JsSharedDataBlock.cs

CreateSharedByteDataBlock(size): a zeroed block, or null when the memory cannot be allocated.

public static JsSharedDataBlock? TryCreate(int byteLength, int? maxByteLength)

Parameters

byteLength int
The initial byte length.
maxByteLength int?
The maximum byte length of a growable block, or null for a fixed-length block.

Returns

JsSharedDataBlock?

Grows the block (SharedArrayBuffer.prototype.grow): false when the new length is less than the current length or greater than the maximum byte length. The new bytes are zeroed before the new length is published.

public bool TryGrow(int newByteLength)

Parameters

newByteLength int

Returns

bool