Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add {Advise, UncheckedAdvice}::is_supported() #122

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,41 @@ pub enum UncheckedAdvice {
// MADV_KEEPONFORK (since Linux 4.14)
// MADV_COLD (since Linux 5.4)
// MADV_PAGEOUT (since Linux 5.4)

#[cfg(target_os = "linux")]
impl Advice {
/// Performs a runtime check if this advice is supported by the kernel.
/// Only supported on Linux. See the [`madvise(2)`] man page.
///
/// [`madvise(2)`]: https://man7.org/linux/man-pages/man2/madvise.2.html#VERSIONS
pub fn is_supported(self) -> bool {
(unsafe { libc::madvise(std::ptr::null_mut(), 0, self as libc::c_int) }) == 0
}
}

#[cfg(target_os = "linux")]
impl UncheckedAdvice {
/// Performs a runtime check if this advice is supported by the kernel.
/// Only supported on Linux. See the [`madvise(2)`] man page.
///
/// [`madvise(2)`]: https://man7.org/linux/man-pages/man2/madvise.2.html#VERSIONS
pub fn is_supported(self) -> bool {
(unsafe { libc::madvise(std::ptr::null_mut(), 0, self as libc::c_int) }) == 0
}
}

#[cfg(test)]
mod tests {
use super::*;

#[cfg(target_os = "linux")]
#[test]
fn test_is_supported() {
assert!(Advice::Normal.is_supported());
assert!(Advice::Random.is_supported());
assert!(Advice::Sequential.is_supported());
assert!(Advice::WillNeed.is_supported());

assert!(UncheckedAdvice::DontNeed.is_supported());
}
}
Loading