Skip to content
fn

wrap_existing

→MetalBuffer
MetalBuffer wrap_existing(void * cpu_ptr, int nbytes)

Wrap an externally-owned, page-aligned CPU pointer in a Metal buffer.

Calls newBufferWithBytesNoCopy: on the shared MTLDevice, which requires the source memory to be page-aligned and at least page-size bytes. No copy is performed — GPU kernels read/write the caller's buffer in place.

See Also

allocate_shared — fresh allocation, no aliasing.

Parameters

cpu_ptrvoid*
CPU pointer to wrap. Must be page-aligned (typically obtained from posix_memalign or another Metal allocation).
nbytesstd::size_t
Byte length of the region. Must be ≥ system page size.

Returns

MetalBuffer

Descriptor referring to cpu_ptr with mtl_handle set to the freshly created id<MTLBuffer>.

Raises

:runtime_error
If Metal rejects the wrap (e.g. unaligned pointer, missing MTLDevice).