@@ -123,35 +123,25 @@ func (b gcBlock) address() uintptr {
123123 return addr
124124}
125125
126- // findHead returns the head (first block) of an object, assuming the block
127- // points to an allocated object. It returns the same block if this block
128- // already points to the head.
129126func (b gcBlock ) findHead () gcBlock {
130127 stateBytePtr := (* uint8 )(unsafe .Add (metadataStart , b / blocksPerStateByte ))
131128
132- // XOR the stateByte with byte containing all tails to turn tail bits to 0
133- // and shift out the bits that are not part of the object
134- stateByte := ((* stateBytePtr ) ^ blockStateByteAllTails ) << ((blocksPerStateByte - (b % blocksPerStateByte + 1 )) * stateBits )
135- // if stateByte is 0 that means all blocks are tails so we loop trough subsequent states,
136- // byte at a time to find the first byte that is not all tails
137- if stateByte == 0 {
138- // subtract the number of object blocks that were in the first byte
139- b -= (b % blocksPerStateByte + 1 )
140- // skip to next byte
129+ // XOR the stateByte with byte containing all tails to turn tail bits to 0 and
130+ // mask out the bits that are not part of the object
131+ otherObjectBlocks := int (blocksPerStateByte - (b % blocksPerStateByte + 1 ))
132+ stateByte := ((* stateBytePtr ) ^ blockStateByteAllTails ) & (uint8 (1 << (8 - (otherObjectBlocks * stateBits ))) - 1 )
133+
134+ // loop until state byte is not all tails
135+ for stateByte == 0 {
141136 stateBytePtr = (* uint8 )(unsafe .Add (unsafe .Pointer (stateBytePtr ), - 1 ))
142- // loop until state byte is not all tails
143- for (* stateBytePtr )^ blockStateByteAllTails == 0 {
144- stateBytePtr = (* uint8 )(unsafe .Add (unsafe .Pointer (stateBytePtr ), - 1 ))
145- b -= blocksPerStateByte
146- }
147- // set stateByte variable to the first byte that is not all tails and turn all tail bits to zeroes
148137 stateByte = (* stateBytePtr ) ^ blockStateByteAllTails
138+ b -= blocksPerStateByte
149139 }
150140
151- // at this point stateByte is set to the first state byte of the object that we encountered which is not all tails
152- // and all tail bits in it are turned to zero. We count number of bytes that are 0 (tail) using LeadingZeros8
153- // and divide it by stateBits to get the number of tail blocks in state bits.
154- b -= gcBlock (bits .LeadingZeros8 (stateByte ) / stateBits )
141+ // in the first state byte which is not all tails, count the number of leading bits that are 0 and
142+ // divide it by stateBits to get the number of tail blocks. Subtract otherObjectBlocks to exclude
143+ // blocks that are not part of the object
144+ b -= gcBlock (( bits .LeadingZeros8 (stateByte ) / stateBits ) - otherObjectBlocks )
155145
156146 if gcAsserts {
157147 if b .state () != blockStateHead && b .state () != blockStateMark {
0 commit comments