onnxruntime/
memory.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use tracing::debug;

use onnxruntime_sys as sys;

use crate::{
    error::{status_to_result, OrtError, Result},
    g_ort, AllocatorType, MemType,
};

#[derive(Debug)]
pub(crate) struct MemoryInfo {
    pub ptr: *mut sys::OrtMemoryInfo,
}

impl MemoryInfo {
    #[tracing::instrument]
    pub fn new(allocator: AllocatorType, memory_type: MemType) -> Result<Self> {
        debug!("Creating new memory info.");
        let mut memory_info_ptr: *mut sys::OrtMemoryInfo = std::ptr::null_mut();
        let status = unsafe {
            g_ort().CreateCpuMemoryInfo.unwrap()(
                allocator.into(),
                memory_type.into(),
                &mut memory_info_ptr,
            )
        };
        status_to_result(status).map_err(OrtError::CreateCpuMemoryInfo)?;
        assert_ne!(memory_info_ptr, std::ptr::null_mut());

        Ok(Self {
            ptr: memory_info_ptr,
        })
    }
}

impl Drop for MemoryInfo {
    #[tracing::instrument]
    fn drop(&mut self) {
        debug!("Dropping the memory information.");
        assert_ne!(self.ptr, std::ptr::null_mut());

        unsafe { g_ort().ReleaseMemoryInfo.unwrap()(self.ptr) };

        self.ptr = std::ptr::null_mut();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use test_env_log::test;

    #[test]
    fn memory_info_constructor_destructor() {
        let memory_info = MemoryInfo::new(AllocatorType::Arena, MemType::Default).unwrap();
        std::mem::drop(memory_info);
    }
}