Use MaybeUninit when calling getrusage (musl compatibility) (#7086)

This commit is contained in:
Koute
2023-04-17 15:44:08 +09:00
committed by GitHub
parent 0211c4b2f7
commit 59054bd466
@@ -197,33 +197,21 @@ pub mod memory_tracker {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub mod max_rss_stat { pub mod max_rss_stat {
use crate::LOG_TARGET; use crate::LOG_TARGET;
use libc::{getrusage, rusage, timeval, RUSAGE_THREAD}; use core::mem::MaybeUninit;
use libc::{getrusage, rusage, RUSAGE_THREAD};
use std::io; use std::io;
/// Get the rusage stats for the current thread. /// Get the rusage stats for the current thread.
fn getrusage_thread() -> io::Result<rusage> { fn getrusage_thread() -> io::Result<rusage> {
let mut result = rusage { let mut result: MaybeUninit<rusage> = MaybeUninit::zeroed();
ru_utime: timeval { tv_sec: 0, tv_usec: 0 },
ru_stime: timeval { tv_sec: 0, tv_usec: 0 }, // SAFETY: `result` is a valid pointer, so calling this is safe.
ru_maxrss: 0, if unsafe { getrusage(RUSAGE_THREAD, result.as_mut_ptr()) } == -1 {
ru_ixrss: 0,
ru_idrss: 0,
ru_isrss: 0,
ru_minflt: 0,
ru_majflt: 0,
ru_nswap: 0,
ru_inblock: 0,
ru_oublock: 0,
ru_msgsnd: 0,
ru_msgrcv: 0,
ru_nsignals: 0,
ru_nvcsw: 0,
ru_nivcsw: 0,
};
if unsafe { getrusage(RUSAGE_THREAD, &mut result) } == -1 {
return Err(io::Error::last_os_error()) return Err(io::Error::last_os_error())
} }
Ok(result)
// SAFETY: `result` was successfully initialized by `getrusage`.
unsafe { Ok(result.assume_init()) }
} }
/// Gets the `ru_maxrss` for the current thread. /// Gets the `ru_maxrss` for the current thread.