Skip to content

Commit

Permalink
Auto merge of #2842 - rtzoeller:freebsd-cpu-macros, r=Amanieu
Browse files Browse the repository at this point in the history
Fix FreeBSD CPU_ macros

The current definitions conflate bits and bytes, leading to panics.

Found while trying to add support for `nix::sched::sched_setaffinity` to FreeBSD.
  • Loading branch information
bors committed Jul 12, 2022
2 parents b01a39a + 57dff10 commit fb8d512
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/unix/bsd/freebsdlike/freebsd/mod.rs
Expand Up @@ -3781,31 +3781,31 @@ f! {
}

pub fn CPU_SET(cpu: usize, cpuset: &mut cpuset_t) -> () {
let bitset_bits = ::mem::size_of::<::c_long>();
let bitset_bits = 8 * ::mem::size_of::<::c_long>();
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
cpuset.__bits[idx] |= 1 << offset;
()
}

pub fn CPU_CLR(cpu: usize, cpuset: &mut cpuset_t) -> () {
let bitset_bits = ::mem::size_of::<::c_long>();
let bitset_bits = 8 * ::mem::size_of::<::c_long>();
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
cpuset.__bits[idx] &= !(1 << offset);
()
}

pub fn CPU_ISSET(cpu: usize, cpuset: &cpuset_t) -> bool {
let bitset_bits = ::mem::size_of::<::c_long>();
let bitset_bits = 8 * ::mem::size_of::<::c_long>();
let (idx, offset) = (cpu / bitset_bits, cpu % bitset_bits);
0 != cpuset.__bits[idx] & (1 << offset)
}

pub fn CPU_COUNT(cpuset: &cpuset_t) -> ::c_int {
let mut s: u32 = 0;
let cpuset_size = ::mem::size_of::<cpuset_t>();
let bitset_bits = ::mem::size_of::<::c_long>();
let bitset_size = ::mem::size_of::<::c_long>();

for i in cpuset.__bits[..(cpuset_size / bitset_bits)].iter() {
for i in cpuset.__bits[..(cpuset_size / bitset_size)].iter() {
s += i.count_ones();
};
s as ::c_int
Expand Down

0 comments on commit fb8d512

Please sign in to comment.