@@ -32,7 +32,6 @@ package runtime
3232
3333import (
3434 "internal/task"
35- "math/bits"
3635 "runtime/interrupt"
3736 "unsafe"
3837)
@@ -125,29 +124,24 @@ func (b gcBlock) address() uintptr {
125124 return addr
126125}
127126
128- // findHead returns the head (first block) of an object, assuming the block
129- // points to an allocated object. It returns the same block if this block
130- // already points to the head.
131- func (b gcBlock ) findHead () gcBlock {
132- stateBytePtr := (* uint8 )(unsafe .Add (metadataStart , b / blocksPerStateByte ))
127+ //const blockStateByteAllTails = uint8(blockStateTail<<(stateBits*3) | blockStateTail<<(stateBits*2) | blockStateTail<<(stateBits*1) | blockStateTail<<(stateBits*0))
133128
134- // XOR the stateByte with byte containing all tails to turn tail bits to 0 and
135- // mask out the bits that are not part of the object
136- otherObjectBlocks := int (blocksPerStateByte - (b % blocksPerStateByte + 1 ))
137- stateByte := ((* stateBytePtr ) ^ blockStateByteAllTails ) & (uint8 (1 << (8 - (otherObjectBlocks * stateBits ))) - 1 )
129+ func (b gcBlock ) findHead () gcBlock {
130+ for {
131+ rem := b % blocksPerStateByte
132+ stateByte := * (* uint8 )(unsafe .Add (metadataStart , b / blocksPerStateByte ))
133+ if stateByte == blockStateByteAllTails {
134+ b -= rem + 1
135+ continue
136+ }
138137
139- // loop until state byte is not all tails
140- for stateByte == 0 {
141- stateBytePtr = ( * uint8 )( unsafe . Add ( unsafe . Pointer ( stateBytePtr ), - 1 ))
142- stateByte = ( * stateBytePtr ) ^ blockStateByteAllTails
143- b -= blocksPerStateByte
138+ state := blockState ( stateByte >> ( rem * stateBits )) & blockStateMask
139+ if state != blockStateTail {
140+ break
141+ }
142+ b --
144143 }
145144
146- // in the first state byte which is not all tails, count the number of leading bits that are 0 and
147- // divide it by stateBits to get the number of tail blocks. Subtract otherObjectBlocks to exclude
148- // blocks that are not part of the object
149- b -= gcBlock ((bits .LeadingZeros8 (stateByte ) / stateBits ) - otherObjectBlocks )
150-
151145 if gcAsserts {
152146 if b .state () != blockStateHead && b .state () != blockStateMark {
153147 runtimePanic ("gc: found tail without head" )
0 commit comments