1
0
Fork 0
forked from wry/wry

autocommit 2022-03-02 14:24:07 CET

This commit is contained in:
Julian Orth 2022-03-02 14:24:07 +01:00
parent 0e9afcbfa5
commit aa0cb94143
30 changed files with 1059 additions and 123 deletions

28
src/utils/bitfield.rs Normal file
View file

@ -0,0 +1,28 @@
use std::mem;
const SEG_SIZE: usize = 8 * mem::size_of::<usize>();
#[derive(Default)]
pub struct Bitfield {
vals: Vec<usize>,
}
impl Bitfield {
pub fn acquire(&mut self) -> u32 {
for (idx, n) in self.vals.iter_mut().enumerate() {
if *n != 0 {
let pos = n.trailing_zeros();
*n &= !(1 << pos);
return (idx * SEG_SIZE) as u32 + pos;
}
}
self.vals.push(!1);
((self.vals.len() - 1) * SEG_SIZE) as u32
}
pub fn release(&mut self, val: u32) {
let idx = val as usize / SEG_SIZE;
let pos = val as usize % SEG_SIZE;
self.vals[idx] |= 1 << pos;
}
}