1//! Rust implementation of the `CPU_*` macro API.
2
3#![allow(non_snake_case)]
4
5use super::types::RawCpuSet;
6use core::mem::size_of_val;
7
8#[inline]
9pub(crate) fn CPU_SET(cpu: usize, cpuset: &mut RawCpuSet) {
10 let size_in_bits: usize = 8 * size_of_val(&cpuset.bits[0]); // 32, 64 etc
11 let (idx: usize, offset: usize) = (cpu / size_in_bits, cpu % size_in_bits);
12 cpuset.bits[idx] |= 1 << offset
13}
14
15#[inline]
16pub(crate) fn CPU_ZERO(cpuset: &mut RawCpuSet) {
17 // TODO: With, Rust 1.50, use `cpuset.bits.fill(0)` instead.
18 for element: &mut u64 in &mut cpuset.bits {
19 *element = 0;
20 }
21}
22
23#[inline]
24pub(crate) fn CPU_CLR(cpu: usize, cpuset: &mut RawCpuSet) {
25 let size_in_bits: usize = 8 * size_of_val(&cpuset.bits[0]); // 32, 64 etc
26 let (idx: usize, offset: usize) = (cpu / size_in_bits, cpu % size_in_bits);
27 cpuset.bits[idx] &= !(1 << offset)
28}
29
30#[inline]
31pub(crate) fn CPU_ISSET(cpu: usize, cpuset: &RawCpuSet) -> bool {
32 let size_in_bits: usize = 8 * size_of_val(&cpuset.bits[0]);
33 let (idx: usize, offset: usize) = (cpu / size_in_bits, cpu % size_in_bits);
34 (cpuset.bits[idx] & (1 << offset)) != 0
35}
36
37#[inline]
38pub(crate) fn CPU_COUNT_S(size_in_bytes: usize, cpuset: &RawCpuSet) -> u32 {
39 let size_of_mask: usize = size_of_val(&cpuset.bits[0]);
40 let idx: usize = size_in_bytes / size_of_mask;
41 cpuset.bits[..idx]
42 .iter()
43 .fold(init:0, |acc: u32, i: &u64| acc + i.count_ones())
44}
45
46#[inline]
47pub(crate) fn CPU_COUNT(cpuset: &RawCpuSet) -> u32 {
48 CPU_COUNT_S(size_in_bytes:core::mem::size_of::<RawCpuSet>(), cpuset)
49}
50