#![deny(missing_copy_implementations, missing_debug_implementations)]
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(missing_docs)]
#![no_std]
#![cfg_attr(all(doc, CHANNEL_NIGHTLY), feature(doc_auto_cfg))]
#[cfg(windows)]
pub mod windows;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub mod macos;
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(any(windows, target_os = "linux", target_os = "macos", target_os = "ios"))]
#[allow(unreachable_code)]
pub fn total() -> u64 {
#[cfg(windows)]
return windows::mem_status().ullTotalPhys;
#[cfg(target_os = "linux")]
return linux::get_sysinfo().totalram as u64;
#[cfg(any(target_os = "macos", target_os = "ios"))]
return macos::total();
unreachable!("This function should have already hit a CFG and returned");
}
#[cfg(any(windows, target_os = "linux", target_os = "macos", target_os = "ios"))]
#[allow(unreachable_code)]
pub fn available() -> u64 {
#[cfg(windows)]
return windows::mem_status().ullAvailPhys;
#[cfg(target_os = "linux")]
return linux::get_sysinfo().freeram as u64;
#[cfg(any(target_os = "macos", target_os = "ios"))]
return macos::calculate_available_memory();
unreachable!("This function should have already hit a CFG and returned");
}
#[cfg(any(windows, target_os = "linux", target_os = "macos", target_os = "ios"))]
pub fn used() -> u64 {
total() - available()
}
#[cfg(test)]
mod tests {
extern crate std;
use std::println;
#[test]
fn get_total_system_memory() {
println!(
"Total system memory: {:.2} GiB",
super::total() as f64 / 1024f64 / 1024f64 / 1024f64
);
println!(
"Available system memory: {:.2} GiB",
super::available() as f64 / 1024f64 / 1024f64 / 1024f64
);
assert_eq!(super::used(), super::total() - super::available());
}
}