| 26 | } |
| 27 | |
| 28 | pub fn alloc(&self, block_device: &Arc<dyn BlockDevice>) -> Option<usize> { |
| 29 | for block_id in 0..self.blocks { |
| 30 | let pos = get_block_cache( |
| 31 | block_id + self.start_block_id as usize, |
| 32 | Arc::clone(block_device), |
| 33 | ) |
| 34 | .lock() |
| 35 | .modify(0, |bitmap_block: &mut BitmapBlock| { |
| 36 | if let Some((bits64_pos, inner_pos)) = bitmap_block |
| 37 | .iter() |
| 38 | .enumerate() |
| 39 | .find(|(_, bits64)| **bits64 != u64::MAX) |
| 40 | .map(|(bits64_pos, bits64)| (bits64_pos, bits64.trailing_ones() as usize)) |
| 41 | { |
| 42 | // modify cache |
| 43 | bitmap_block[bits64_pos] |= 1u64 << inner_pos; |
| 44 | Some(block_id * BLOCK_BITS + bits64_pos * 64 + inner_pos as usize) |
| 45 | } else { |
| 46 | None |
| 47 | } |
| 48 | }); |
| 49 | if pos.is_some() { |
| 50 | return pos; |
| 51 | } |
| 52 | } |
| 53 | None |
| 54 | } |
| 55 | |
| 56 | pub fn dealloc(&self, block_device: &Arc<dyn BlockDevice>, bit: usize) { |
| 57 | let (block_pos, bits64_pos, inner_pos) = decomposition(bit); |