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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Rust implementation of the `CPU_*` macro API.

#![allow(non_snake_case)]

use super::types::RawCpuSet;
use core::mem::size_of_val;

#[inline]
pub(crate) fn CPU_SET(cpu: usize, cpuset: &mut RawCpuSet) {
    let size_in_bits = 8 * size_of_val(&cpuset.bits[0]); // 32, 64 etc
    let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
    cpuset.bits[idx] |= 1 << offset
}

#[inline]
pub(crate) fn CPU_ZERO(cpuset: &mut RawCpuSet) {
    // TODO: With, Rust 1.50, use `cpuset.bits.fill(0)` instead.
    for element in &mut cpuset.bits {
        *element = 0;
    }
}

#[inline]
pub(crate) fn CPU_CLR(cpu: usize, cpuset: &mut RawCpuSet) {
    let size_in_bits = 8 * size_of_val(&cpuset.bits[0]); // 32, 64 etc
    let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
    cpuset.bits[idx] &= !(1 << offset)
}

#[inline]
pub(crate) fn CPU_ISSET(cpu: usize, cpuset: &RawCpuSet) -> bool {
    let size_in_bits = 8 * size_of_val(&cpuset.bits[0]);
    let (idx, offset) = (cpu / size_in_bits, cpu % size_in_bits);
    (cpuset.bits[idx] & (1 << offset)) != 0
}

#[inline]
pub(crate) fn CPU_COUNT_S(size_in_bytes: usize, cpuset: &RawCpuSet) -> u32 {
    let size_of_mask = size_of_val(&cpuset.bits[0]);
    let idx = size_in_bytes / size_of_mask;
    cpuset.bits[..idx]
        .iter()
        .fold(0, |acc, i| acc + i.count_ones())
}

#[inline]
pub(crate) fn CPU_COUNT(cpuset: &RawCpuSet) -> u32 {
    CPU_COUNT_S(core::mem::size_of::<RawCpuSet>(), cpuset)
}