Skip to main content

leodos_libcfs/os/
heap.rs

1//
2//! Safe, idiomatic wrapper for querying OSAL heap statistics.
3
4use crate::error::Result;
5use crate::ffi;
6use crate::status::check;
7use core::mem::MaybeUninit;
8
9/// A snapshot of statistics for the system's dynamic memory heap.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct HeapInfo {
12    /// Total number of free bytes available in the heap.
13    pub free_bytes: usize,
14    /// Total number of free blocks in the heap.
15    pub free_blocks: usize,
16    /// The size of the largest contiguous free block available.
17    pub largest_free_block: usize,
18}
19
20impl From<ffi::OS_heap_prop_t> for HeapInfo {
21    fn from(prop: ffi::OS_heap_prop_t) -> Self {
22        Self {
23            free_bytes: prop.free_bytes,
24            free_blocks: prop.free_blocks,
25            largest_free_block: prop.largest_free_block,
26        }
27    }
28}
29
30impl HeapInfo {
31    /// Retrieves statistics about the current state of the system heap.
32    ///
33    /// This function is a safe wrapper around `OS_HeapGetInfo`.
34    pub fn query() -> Result<HeapInfo> {
35        let mut prop = MaybeUninit::uninit();
36        check(unsafe { ffi::OS_HeapGetInfo(prop.as_mut_ptr()) })?;
37        Ok(HeapInfo::from(unsafe { prop.assume_init() }))
38    }
39}