Skip to content
fn

allocate_cpu

→CpuStorage
CpuStorage allocate_cpu(const int & shape, Dtype dt)

Allocates a zero-initialised CPU buffer sized for shape elements of dtype dt and wraps it as a CpuStorage.

See Also

allocate_aligned_bytes — the underlying aligned allocator. helpers::fresh — wraps a Storage into a TensorImpl.

Parameters

shapeconst Shape&
Logical shape of the tensor that will use the buffer. Only the element count (shape_numel) influences allocation size.
dtDtype
Element dtype. Determines per-element size via dtype_size.

Returns

CpuStorage

Newly allocated storage with dtype = dt, nbytes = shape_numel(shape) * dtype_size(dt), and a 64-byte aligned (kCpuAlignment) pointer to a zeroed buffer. nbytes == 0 (empty shape or any dim of 0) yields a storage whose ptr is null — callers must check for this case.

Notes

The buffer is zeroed via std::memset; allocation uses allocate_aligned_bytes so the pointer satisfies SIMD-friendly alignment requirements expected by Apple Accelerate kernels.

Zero-byte allocations short-circuit the memset and return a storage with a null ptr and nbytes == 0. This matches the convention used elsewhere in the engine for empty tensors.

Examples

Allocate a ``(3, 4)`` ``float32`` buffer::
    auto storage = helpers::allocate_cpu({3, 4}, Dtype::Float32);
    // storage.nbytes == 3 * 4 * 4 == 48; ptr is 64-byte aligned and
    // memset-zeroed.