1
0
Fork 0
forked from wry/wry

all: split reusable components into workspace crates

This commit is contained in:
kossLAN 2026-05-29 09:14:53 -04:00
parent 2a079ed800
commit 657e7ce2f7
No known key found for this signature in database
225 changed files with 7422 additions and 17602 deletions

20
utils/src/num_cpus.rs Normal file
View file

@ -0,0 +1,20 @@
use {
crate::oserror::{OsError, OsErrorExt},
smallvec::{SmallVec, smallvec_inline},
uapi::c,
};
pub fn num_cpus() -> Result<u32, OsError> {
let mut buf: SmallVec<[usize; 32]> = smallvec_inline![0; 32];
loop {
match uapi::sched_getaffinity(0, &mut buf).to_os_error() {
Ok(_) => return Ok(count(&buf)),
Err(OsError(c::EINVAL)) => buf.extend_from_slice(&[0; 32][..]),
Err(e) => return Err(e),
}
}
}
fn count(buf: &[usize]) -> u32 {
buf.iter().copied().map(|n| n.count_ones()).sum()
}