autocommit 2022-03-02 14:24:07 CET
This commit is contained in:
parent
0e9afcbfa5
commit
aa0cb94143
30 changed files with 1059 additions and 123 deletions
28
src/utils/bitfield.rs
Normal file
28
src/utils/bitfield.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue