Skip to main content

NDAllocator

Trait NDAllocator 

Source
pub unsafe trait NDAllocator:
    Send
    + Sync
    + 'static {
    const MIN_ALIGN: usize;

    // Required methods
    unsafe fn alloc_data(&mut self, prototype: &DLTensor) -> *mut c_void;
    unsafe fn free_data(&mut self, tensor: &DLTensor);
}
Expand description

Allocates the data buffer of a tensor made by Tensor::from_nd_alloc.

§Safety

alloc_data must return a pointer aligned to MIN_ALIGN and valid for the prototype’s data, numel() * bits * lanes bits rounded up to whole bytes (sub-byte types are packed), and free_data must release exactly what alloc_data returned. free_data may be called for a tensor whose data is null, when alloc_data panicked, and must then do nothing.

Send + Sync: the tensor frees its data through the allocator when the last reference drops, which may be on any thread.

Required Associated Constants§

Source

const MIN_ALIGN: usize

The minimum alignment of the allocated data, in bytes.

Required Methods§

Source

unsafe fn alloc_data(&mut self, prototype: &DLTensor) -> *mut c_void

Allocate the data of a tensor.

§Arguments
  • prototype - The tensor to allocate for; its data pointer is still null.
§Returns

The data pointer, which the tensor stores.

§Safety

prototype must have a valid shape and dtype.

Source

unsafe fn free_data(&mut self, tensor: &DLTensor)

Free the data of a tensor, when the tensor is dropped.

§Arguments
  • tensor - The tensor whose data to free.
§Safety

tensor.data must be null or come from alloc_data on this allocator with the same shape and dtype, and must not be used afterwards.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§