storage

Macro storage 

Source
macro_rules! storage {
    ($t:ty) => { ... };
    ($t:ty, [$n:expr]) => { ... };
    ($t:ty, ($n:expr)) => { ... };
}
Expand description

A macro to create a pinned Storage object(s).

Some types in the library required dedicated memory management, either to avoid heap allocations, to prevent moving the object, etc. The Storage struct helper function used to allocate memory for these types, see its description for more information. The macro is a convenient way to create a pinned Storage object(s):

  • storage!(T) creates a single storage for type T, Pin<&mut Storage<T>>.
  • storage!(T, [N]) creates an array on the stack, Pin<&mut [Storage<T>, N]>.
  • storage!(T, (N)) creates a vector in the heap, Pin<Box<[Storage<T>]>>. Usually converted to Pin<&mut [Storage<T>, N]> with as_mut().
let tensor_impl: TensorImpl = ...;
let storage: Pin<&mut Storage<Tensor<f32>>> = executorch::storage!(Tensor<f32>);
let tensor = Tensor::new_in_storage(&tensor_impl, storage);

let (evalue1, evalue2, evalue3) = (EValue::new(42), EValue::new(17), EValue::new(6));
let wrapped_vals = EValuePtrList::new([&evalue1, &evalue2, &evalue3]);
let mut unwrapped_vals: Pin<&mut [Storage<i64>, 3]> = storage!(i64, [3]); // an array allocation on the stack
let list = BoxedEvalueList::new(&wrapped_vals, unwrapped_vals).unwrap();

let evalues = vec![EValue::new(42), EValue::new(17), EValue::new(6)];
let wrapped_vals = EValuePtrList::new(evalues.iter());
let mut unwrapped_vals: Pin<Box<[Storage<i64>]>> = storage!(i64, (evalues.len())); // a vector allocation on the heap
let list = BoxedEvalueList::new(&wrapped_vals, unwrapped_vals.as_mut()).unwrap(); // .as_mut()