@@ -583,6 +583,9 @@ impl InitMask {
583
583
584
584
#[inline]
585
585
fn bit_index(bits: Size) -> (usize, usize) {
586
+ // BLOCK_SIZE is the number of bits that can fit in a `Block`.
587
+ // Each bit in a `Block` represents the initialization state of one byte of an allocation,
588
+ // so we use `.bytes()` here.
586
589
let bits = bits.bytes();
587
590
let a = bits / InitMask::BLOCK_SIZE;
588
591
let b = bits % InitMask::BLOCK_SIZE;
@@ -721,23 +724,23 @@ impl InitMask {
721
724
is_init: bool,
722
725
) -> Option<Size> {
723
726
// For the following examples, assume this function was called with:
724
- // bits = 11011100
727
+ // bits = 0b00111011
725
728
// start_bit = 3
726
729
// is_init = false
727
- // Note again that the least significant bit is written first,
728
- // which is backwards compared to how we normally write numbers .
730
+ // Note that, for the examples in this function, the most significant bit is written first,
731
+ // which is backwards compared to the comments in `find_bit`/`find_bit_fast` .
729
732
730
733
// Invert bits so we're always looking for the first set bit.
731
- // ! 11011100
732
- // bits = 00100011
734
+ // ! 0b00111011
735
+ // bits = 0b11000100
733
736
let bits = if is_init { bits } else { !bits };
734
737
// Mask off unused start bits.
735
- // 00100011
736
- // & 00011111
737
- // bits = 00000011
738
+ // 0b11000100
739
+ // & 0b11111000
740
+ // bits = 0b11000000
738
741
let bits = bits & (!0 << start_bit);
739
742
// Find set bit, if any.
740
- // bit = trailing_zeros(00000011 )
743
+ // bit = trailing_zeros(0b11000000 )
741
744
// bit = 6
742
745
if bits == 0 {
743
746
None
@@ -772,6 +775,7 @@ impl InitMask {
772
775
// For (a), the block index of `end_inclusive` is 1, and for (b), it's 0.
773
776
// This provides the desired behavior of searching blocks 0 and 1 for (a),
774
777
// and searching only block 0 for (b).
778
+ // There is no concern of overflows since we checked for `start >= end` above.
775
779
let (start_block, start_bit) = InitMask::bit_index(start);
776
780
let end_inclusive = Size::from_bytes(end.bytes() - 1);
777
781
let (end_block_inclusive, _) = InitMask::bit_index(end_inclusive);
@@ -1046,6 +1050,7 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
1046
1050
let mut ranges = smallvec::SmallVec::<[u64; 1]>::new();
1047
1051
let initial = self.init_mask.get(range.start);
1048
1052
1053
+ // Here we rely on `range_as_init_chunks` to yield alternating init/uninit chunks.
1049
1054
for chunk in self.init_mask.range_as_init_chunks(range.start, range.end()) {
1050
1055
let len = chunk.range().end.bytes() - chunk.range().start.bytes();
1051
1056
ranges.push(len);
0 commit comments