system_memory/linux.rs
1//! Linux specific implementation.
2
3use core::{ffi::c_int, mem::MaybeUninit};
4use errno::Errno;
5use libc::sysinfo;
6
7/// Get information about the host machine using [`sysinfo`](fn@sysinfo) or return [`errno`] if it fails.
8pub fn populate_sysinfo() -> Result<sysinfo, Errno> {
9 let mut sys_info: MaybeUninit<sysinfo> = MaybeUninit::uninit();
10
11 // Call sysinfo syscall.
12 let return_code: c_int = unsafe { sysinfo(sys_info.as_mut_ptr()) };
13
14 if return_code < 0 {
15 Err(errno::errno())
16 } else {
17 // SAFETY: Assume that the syscall properly initialized the instance.
18 Ok(unsafe { sys_info.assume_init() })
19 }
20}
21
22/// Get a properly populated [`sysinfo`](struct@sysinfo) or panic.
23///
24/// # Panics
25/// - If the underlying [`sysinfo`](fn@sysinfo) call fails.
26pub fn get_sysinfo() -> sysinfo {
27 populate_sysinfo().expect("could not get sysinfo")
28}