Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 75fecd5

Browse files
improve comments
1 parent e950f11 commit 75fecd5

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ impl InitMask {
583583

584584
#[inline]
585585
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.
586589
let bits = bits.bytes();
587590
let a = bits / InitMask::BLOCK_SIZE;
588591
let b = bits % InitMask::BLOCK_SIZE;
@@ -721,23 +724,23 @@ impl InitMask {
721724
is_init: bool,
722725
) -> Option<Size> {
723726
// For the following examples, assume this function was called with:
724-
// bits = 11011100
727+
// bits = 0b00111011
725728
// start_bit = 3
726729
// 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`.
729732

730733
// Invert bits so we're always looking for the first set bit.
731-
// ! 11011100
732-
// bits = 00100011
734+
// ! 0b00111011
735+
// bits = 0b11000100
733736
let bits = if is_init { bits } else { !bits };
734737
// Mask off unused start bits.
735-
// 00100011
736-
// & 00011111
737-
// bits = 00000011
738+
// 0b11000100
739+
// & 0b11111000
740+
// bits = 0b11000000
738741
let bits = bits & (!0 << start_bit);
739742
// Find set bit, if any.
740-
// bit = trailing_zeros(00000011)
743+
// bit = trailing_zeros(0b11000000)
741744
// bit = 6
742745
if bits == 0 {
743746
None
@@ -772,6 +775,7 @@ impl InitMask {
772775
// For (a), the block index of `end_inclusive` is 1, and for (b), it's 0.
773776
// This provides the desired behavior of searching blocks 0 and 1 for (a),
774777
// and searching only block 0 for (b).
778+
// There is no concern of overflows since we checked for `start >= end` above.
775779
let (start_block, start_bit) = InitMask::bit_index(start);
776780
let end_inclusive = Size::from_bytes(end.bytes() - 1);
777781
let (end_block_inclusive, _) = InitMask::bit_index(end_inclusive);
@@ -1046,6 +1050,7 @@ impl<Tag, Extra> Allocation<Tag, Extra> {
10461050
let mut ranges = smallvec::SmallVec::<[u64; 1]>::new();
10471051
let initial = self.init_mask.get(range.start);
10481052

1053+
// Here we rely on `range_as_init_chunks` to yield alternating init/uninit chunks.
10491054
for chunk in self.init_mask.range_as_init_chunks(range.start, range.end()) {
10501055
let len = chunk.range().end.bytes() - chunk.range().start.bytes();
10511056
ranges.push(len);

0 commit comments

Comments
 (0)