kornia_rs/tensor/
allocator.rs1use std::alloc::{GlobalAlloc, Layout, System};
2
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum TensorAllocatorError {
7 #[error("Invalid tensor layout {0}")]
8 LayoutError(core::alloc::LayoutError),
9
10 #[error("Null pointer")]
11 NullPointer,
12}
13
14pub trait TensorAllocator: Clone {
25 fn alloc(&self, layout: Layout) -> Result<*mut u8, TensorAllocatorError>;
26 fn dealloc(&self, ptr: *mut u8, layout: Layout);
27}
28
29#[derive(Clone)]
30pub struct CpuAllocator;
32
33impl Default for CpuAllocator {
35 fn default() -> Self {
36 Self
37 }
38}
39
40impl TensorAllocator for CpuAllocator {
42 fn alloc(&self, layout: Layout) -> Result<*mut u8, TensorAllocatorError> {
52 let ptr = unsafe { System.alloc(layout) };
53 if ptr.is_null() {
54 Err(TensorAllocatorError::NullPointer)?
55 }
56 Ok(ptr)
57 }
58
59 #[allow(clippy::not_unsafe_ptr_arg_deref)]
70 fn dealloc(&self, ptr: *mut u8, layout: Layout) {
71 unsafe { System.dealloc(ptr, layout) }
72 }
73}
74
75#[cfg(test)]
76mod tests {
77 use super::*;
78
79 #[test]
80 fn test_cpu_allocator() -> Result<(), TensorAllocatorError> {
81 let allocator = CpuAllocator;
82 let layout = Layout::from_size_align(1024, 64).unwrap();
83 let ptr = allocator.alloc(layout)?;
84 allocator.dealloc(ptr, layout);
85 Ok(())
86 }
87}