1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use libc::sysconf;
use libc::_SC_IOV_MAX;
use libc::_SC_NPROCESSORS_CONF;
use libc::_SC_PAGESIZE;

use crate::Result;

/// Safe wrapper for `sysconf(_SC_IOV_MAX)`.
#[inline(always)]
pub fn iov_max() -> usize {
    // SAFETY:
    // Trivially safe
    unsafe { sysconf(_SC_IOV_MAX) as usize }
}

/// Safe wrapper for `sysconf(_SC_PAGESIZE)`.
#[inline(always)]
pub fn pagesize() -> usize {
    // SAFETY:
    // Trivially safe
    unsafe { sysconf(_SC_PAGESIZE) as usize }
}

/// Returns the number of online logical cores on the system.
#[inline(always)]
pub fn number_of_logical_cores() -> Result<usize> {
    // SAFETY:
    // Safe because we pass a flag for this call and the host supports this system call
    Ok(unsafe { sysconf(_SC_NPROCESSORS_CONF) } as usize)
}