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