sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use super::*;

impl SystemInner {
    pub(crate) fn refresh_memory_specifics(&mut self, refresh_kind: MemoryRefreshKind) {
        let mut mib = [libc::CTL_VM as _, libc::VM_SWAPUSAGE as _];

        unsafe {
            if refresh_kind.swap() {
                // Retrieve system values.
                let mut xs: libc::xsw_usage = mem::zeroed::<libc::xsw_usage>();
                if get_sys_value(
                    mem::size_of::<libc::xsw_usage>(),
                    &mut xs as *mut _ as *mut c_void,
                    &mut mib,
                ) {
                    self.swap_total = xs.xsu_total;
                    self.swap_free = xs.xsu_avail;
                }
            }
            if refresh_kind.ram() {
                mib[0] = libc::CTL_HW as _;
                mib[1] = libc::HW_MEMSIZE as _;
                // Retrieve RAM information.
                if self.mem_total < 1 {
                    get_sys_value(
                        mem::size_of::<u64>(),
                        &mut self.mem_total as *mut u64 as *mut c_void,
                        &mut mib,
                    );
                }
                let mut count: u32 = libc::HOST_VM_INFO64_COUNT as _;
                let mut stat = mem::zeroed::<vm_statistics64>();
                if host_statistics64(
                    self.port,
                    libc::HOST_VM_INFO64,
                    &mut stat as *mut vm_statistics64 as *mut _,
                    &mut count,
                ) == libc::KERN_SUCCESS
                {
                    // From the Apple XNU documentation: https://github.com/apple-oss-distributions/xnu/blob/main/doc/vm/memorystatus_notify.md#available-memory
                    self.mem_available = u64::from(stat.active_count)
                        .saturating_add(u64::from(stat.inactive_count))
                        .saturating_add(u64::from(stat.free_count))
                        .saturating_mul(self.page_size_b);
                    self.mem_used = u64::from(stat.internal_page_count)
                        .saturating_sub(u64::from(stat.purgeable_count))
                        .saturating_add(u64::from(stat.wire_count))
                        .saturating_add(u64::from(stat.compressor_page_count))
                        .saturating_mul(self.page_size_b);
                    self.mem_free = u64::from(stat.free_count)
                        .saturating_sub(u64::from(stat.speculative_count))
                        .saturating_mul(self.page_size_b);
                }
            }
        }
    }

    pub(crate) fn total_memory(&self) -> u64 {
        self.mem_total
    }

    pub(crate) fn free_memory(&self) -> u64 {
        self.mem_free
    }

    pub(crate) fn available_memory(&self) -> u64 {
        self.mem_available
    }

    pub(crate) fn used_memory(&self) -> u64 {
        self.mem_used
    }

    pub(crate) fn total_swap(&self) -> u64 {
        self.swap_total
    }

    pub(crate) fn free_swap(&self) -> u64 {
        self.swap_free
    }

    // TODO: verify this computation.
    pub(crate) fn used_swap(&self) -> u64 {
        self.swap_total - self.swap_free
    }
}