Table of Contents

Class MetalMemoryBuffer

Namespace
DotCompute.Backends.Metal.Memory
Assembly
DotCompute.Backends.Metal.dll

Metal-specific memory buffer implementation with actual Metal API integration.

public sealed class MetalMemoryBuffer : IUnifiedMemoryBuffer, IAsyncDisposable, IDisposable
Inheritance
MetalMemoryBuffer
Implements
Inherited Members
Extension Methods

Constructors

MetalMemoryBuffer(long, MemoryOptions, nint, MetalStorageMode)

Initializes a new instance of the MetalMemoryBuffer class.

public MetalMemoryBuffer(long sizeInBytes, MemoryOptions options, nint device = 0, MetalStorageMode storageMode = MetalStorageMode.Shared)

Parameters

sizeInBytes long

The size in bytes.

options MemoryOptions

Memory options.

device nint

The Metal device to use for buffer allocation.

storageMode MetalStorageMode

The Metal storage mode (defaults to Shared).

Properties

Buffer

Gets the native Metal buffer handle.

public nint Buffer { get; }

Property Value

nint

Device

Gets the Metal device used for this buffer.

public nint Device { get; }

Property Value

nint

IsDisposed

Gets whether the buffer has been disposed.

public bool IsDisposed { get; }

Property Value

bool

NativeHandle

Gets the native handle (alias for Buffer for consistency).

public nint NativeHandle { get; }

Property Value

nint

Options

Gets the memory allocation options.

public MemoryOptions Options { get; }

Property Value

MemoryOptions

SizeInBytes

Gets the size of the buffer in bytes.

public long SizeInBytes { get; }

Property Value

long

State

Gets the current state of the buffer.

public BufferState State { get; }

Property Value

BufferState

StorageMode

Gets the storage mode used for this buffer.

public MetalStorageMode StorageMode { get; }

Property Value

MetalStorageMode

Methods

CloneAsync(CancellationToken)

Creates a copy of this buffer with the same data.

public ValueTask<MetalMemoryBuffer> CloneAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask<MetalMemoryBuffer>

CopyFromAsync<T>(ReadOnlyMemory<T>, long, CancellationToken)

Copies data from a source memory into this buffer.

public ValueTask CopyFromAsync<T>(ReadOnlyMemory<T> source, long offset = 0, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>

The source memory to copy from.

offset long

The offset in bytes where to start copying.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyFromHostAsync<T>(ReadOnlyMemory<T>, long, CancellationToken)

Legacy support method (calls CopyFromAsync).

public ValueTask CopyFromHostAsync<T>(ReadOnlyMemory<T> source, long offset = 0, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

source ReadOnlyMemory<T>
offset long
cancellationToken CancellationToken

Returns

ValueTask

Type Parameters

T

CopyToAsync<T>(Memory<T>, long, CancellationToken)

Copies data from this buffer to a destination memory.

public ValueTask CopyToAsync<T>(Memory<T> destination, long offset = 0, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

destination Memory<T>

The destination memory to copy to.

offset long

The offset in bytes where to start copying from.

cancellationToken CancellationToken

Cancellation token.

Returns

ValueTask

A task representing the copy operation.

Type Parameters

T

The element type.

CopyToHostAsync<T>(Memory<T>, long, CancellationToken)

Legacy support method (calls CopyToAsync).

public ValueTask CopyToHostAsync<T>(Memory<T> destination, long offset = 0, CancellationToken cancellationToken = default) where T : unmanaged

Parameters

destination Memory<T>
offset long
cancellationToken CancellationToken

Returns

ValueTask

Type Parameters

T

Dispose()

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

public void Dispose()

DisposeAsync()

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

public ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

GetActualLength()

Gets the actual buffer length as reported by Metal.

public long GetActualLength()

Returns

long

InitializeAsync(CancellationToken)

Initializes the Metal buffer (async version for enhanced memory manager).

public ValueTask InitializeAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

Returns

ValueTask

IsZeroCopyUnifiedMemory()

Gets whether this buffer uses zero-copy unified memory.

public bool IsZeroCopyUnifiedMemory()

Returns

bool