1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Garbage collector: type and heap bitmaps.
7 // Stack, data, and bss bitmaps
9 // Stack frames and global variables in the data and bss sections are
10 // described by bitmaps with 1 bit per pointer-sized word. A "1" bit
11 // means the word is a live pointer to be visited by the GC (referred to
12 // as "pointer"). A "0" bit means the word should be ignored by GC
13 // (referred to as "scalar", though it could be a dead pointer value).
17 // The heap bitmap comprises 2 bits for each pointer-sized word in the heap,
18 // stored in the heapArena metadata backing each heap arena.
19 // That is, if ha is the heapArena for the arena starting a start,
20 // then ha.bitmap[0] holds the 2-bit entries for the four words start
21 // through start+3*ptrSize, ha.bitmap[1] holds the entries for
22 // start+4*ptrSize through start+7*ptrSize, and so on.
24 // In each 2-bit entry, the lower bit is a pointer/scalar bit, just
25 // like in the stack/data bitmaps described above. The upper bit
26 // indicates scan/dead: a "1" value ("scan") indicates that there may
27 // be pointers in later words of the allocation, and a "0" value
28 // ("dead") indicates there are no more pointers in the allocation. If
29 // the upper bit is 0, the lower bit must also be 0, and this
30 // indicates scanning can ignore the rest of the allocation.
32 // The 2-bit entries are split when written into the byte, so that the top half
33 // of the byte contains 4 high (scan) bits and the bottom half contains 4 low
34 // (pointer) bits. This form allows a copy from the 1-bit to the 4-bit form to
35 // keep the pointer bits contiguous, instead of having to space them out.
37 // The code makes use of the fact that the zero value for a heap
38 // bitmap means scalar/dead. This property must be preserved when
39 // modifying the encoding.
41 // The bitmap for noscan spans is not maintained. Code must ensure
42 // that an object is scannable before consulting its bitmap by
43 // checking either the noscan bit in the span or by consulting its
44 // type's information.
49 "runtime/internal/atomic"
50 "runtime/internal/sys"
58 heapBitsShift = 1 // shift offset between successive bitPointer or bitScan entries
59 wordsPerBitmapByte = 8 / 2 // heap words described by one bitmap byte
61 // all scan/pointer bits in a byte
62 bitScanAll = bitScan | bitScan<<heapBitsShift | bitScan<<(2*heapBitsShift) | bitScan<<(3*heapBitsShift)
63 bitPointerAll = bitPointer | bitPointer<<heapBitsShift | bitPointer<<(2*heapBitsShift) | bitPointer<<(3*heapBitsShift)
66 // addb returns the byte pointer p+n.
69 func addb(p *byte, n uintptr) *byte {
70 // Note: wrote out full expression instead of calling add(p, n)
71 // to reduce the number of temporaries generated by the
72 // compiler for this trivial expression during inlining.
73 return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + n))
76 // subtractb returns the byte pointer p-n.
79 func subtractb(p *byte, n uintptr) *byte {
80 // Note: wrote out full expression instead of calling add(p, -n)
81 // to reduce the number of temporaries generated by the
82 // compiler for this trivial expression during inlining.
83 return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) - n))
86 // add1 returns the byte pointer p+1.
89 func add1(p *byte) *byte {
90 // Note: wrote out full expression instead of calling addb(p, 1)
91 // to reduce the number of temporaries generated by the
92 // compiler for this trivial expression during inlining.
93 return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + 1))
96 // subtract1 returns the byte pointer p-1.
99 // nosplit because it is used during write barriers and must not be preempted.
101 func subtract1(p *byte) *byte {
102 // Note: wrote out full expression instead of calling subtractb(p, 1)
103 // to reduce the number of temporaries generated by the
104 // compiler for this trivial expression during inlining.
105 return (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) - 1))
108 // heapBits provides access to the bitmap bits for a single heap word.
109 // The methods on heapBits take value receivers so that the compiler
110 // can more easily inline calls to those methods and registerize the
111 // struct fields independently.
112 type heapBits struct {
115 arena uint32 // Index of heap arena containing bitp
116 last *uint8 // Last byte arena's bitmap
119 // Make the compiler check that heapBits.arena is large enough to hold
120 // the maximum arena frame number.
121 var _ = heapBits{arena: (1<<heapAddrBits)/heapArenaBytes - 1}
123 // markBits provides access to the mark bit for an object in the heap.
124 // bytep points to the byte holding the mark bit.
125 // mask is a byte with a single bit set that can be &ed with *bytep
126 // to see if the bit has been set.
127 // *m.byte&m.mask != 0 indicates the mark bit is set.
128 // index can be used along with span information to generate
129 // the address of the object in the heap.
130 // We maintain one set of mark bits for allocation and one for
132 type markBits struct {
139 func (s *mspan) allocBitsForIndex(allocBitIndex uintptr) markBits {
140 bytep, mask := s.allocBits.bitp(allocBitIndex)
141 return markBits{bytep, mask, allocBitIndex}
144 // refillAllocCache takes 8 bytes s.allocBits starting at whichByte
145 // and negates them so that ctz (count trailing zeros) instructions
146 // can be used. It then places these 8 bytes into the cached 64 bit
148 func (s *mspan) refillAllocCache(whichByte uintptr) {
149 bytes := (*[8]uint8)(unsafe.Pointer(s.allocBits.bytep(whichByte)))
151 aCache |= uint64(bytes[0])
152 aCache |= uint64(bytes[1]) << (1 * 8)
153 aCache |= uint64(bytes[2]) << (2 * 8)
154 aCache |= uint64(bytes[3]) << (3 * 8)
155 aCache |= uint64(bytes[4]) << (4 * 8)
156 aCache |= uint64(bytes[5]) << (5 * 8)
157 aCache |= uint64(bytes[6]) << (6 * 8)
158 aCache |= uint64(bytes[7]) << (7 * 8)
159 s.allocCache = ^aCache
162 // nextFreeIndex returns the index of the next free object in s at
163 // or after s.freeindex.
164 // There are hardware instructions that can be used to make this
165 // faster if profiling warrants it.
166 func (s *mspan) nextFreeIndex() uintptr {
167 sfreeindex := s.freeindex
169 if sfreeindex == snelems {
172 if sfreeindex > snelems {
173 throw("s.freeindex > s.nelems")
176 aCache := s.allocCache
178 bitIndex := sys.Ctz64(aCache)
180 // Move index to start of next cached bits.
181 sfreeindex = (sfreeindex + 64) &^ (64 - 1)
182 if sfreeindex >= snelems {
183 s.freeindex = snelems
186 whichByte := sfreeindex / 8
187 // Refill s.allocCache with the next 64 alloc bits.
188 s.refillAllocCache(whichByte)
189 aCache = s.allocCache
190 bitIndex = sys.Ctz64(aCache)
191 // nothing available in cached bits
192 // grab the next 8 bytes and try again.
194 result := sfreeindex + uintptr(bitIndex)
195 if result >= snelems {
196 s.freeindex = snelems
200 s.allocCache >>= uint(bitIndex + 1)
201 sfreeindex = result + 1
203 if sfreeindex%64 == 0 && sfreeindex != snelems {
204 // We just incremented s.freeindex so it isn't 0.
205 // As each 1 in s.allocCache was encountered and used for allocation
206 // it was shifted away. At this point s.allocCache contains all 0s.
207 // Refill s.allocCache so that it corresponds
208 // to the bits at s.allocBits starting at s.freeindex.
209 whichByte := sfreeindex / 8
210 s.refillAllocCache(whichByte)
212 s.freeindex = sfreeindex
216 // isFree reports whether the index'th object in s is unallocated.
218 // The caller must ensure s.state is mSpanInUse, and there must have
219 // been no preemption points since ensuring this (which could allow a
220 // GC transition, which would allow the state to change).
221 func (s *mspan) isFree(index uintptr) bool {
222 if index < s.freeindex {
225 bytep, mask := s.allocBits.bitp(index)
226 return *bytep&mask == 0
229 func (s *mspan) objIndex(p uintptr) uintptr {
230 byteOffset := p - s.base()
235 // s.baseMask is non-0, elemsize is a power of two, so shift by s.divShift
236 return byteOffset >> s.divShift
238 return uintptr(((uint64(byteOffset) >> s.divShift) * uint64(s.divMul)) >> s.divShift2)
241 func markBitsForAddr(p uintptr) markBits {
243 objIndex := s.objIndex(p)
244 return s.markBitsForIndex(objIndex)
247 func (s *mspan) markBitsForIndex(objIndex uintptr) markBits {
248 bytep, mask := s.gcmarkBits.bitp(objIndex)
249 return markBits{bytep, mask, objIndex}
252 func (s *mspan) markBitsForBase() markBits {
253 return markBits{(*uint8)(s.gcmarkBits), uint8(1), 0}
256 // isMarked reports whether mark bit m is set.
257 func (m markBits) isMarked() bool {
258 return *m.bytep&m.mask != 0
261 // setMarked sets the marked bit in the markbits, atomically.
262 func (m markBits) setMarked() {
263 // Might be racing with other updates, so use atomic update always.
264 // We used to be clever here and use a non-atomic update in certain
265 // cases, but it's not worth the risk.
266 atomic.Or8(m.bytep, m.mask)
269 // setMarkedNonAtomic sets the marked bit in the markbits, non-atomically.
270 func (m markBits) setMarkedNonAtomic() {
274 // clearMarked clears the marked bit in the markbits, atomically.
275 func (m markBits) clearMarked() {
276 // Might be racing with other updates, so use atomic update always.
277 // We used to be clever here and use a non-atomic update in certain
278 // cases, but it's not worth the risk.
279 atomic.And8(m.bytep, ^m.mask)
282 // markBitsForSpan returns the markBits for the span base address base.
283 func markBitsForSpan(base uintptr) (mbits markBits) {
284 mbits = markBitsForAddr(base)
286 throw("markBitsForSpan: unaligned start")
291 // advance advances the markBits to the next object in the span.
292 func (m *markBits) advance() {
294 m.bytep = (*uint8)(unsafe.Pointer(uintptr(unsafe.Pointer(m.bytep)) + 1))
302 // heapBitsForAddr returns the heapBits for the address addr.
303 // The caller must ensure addr is in an allocated span.
304 // In particular, be careful not to point past the end of an object.
306 // nosplit because it is used during write barriers and must not be preempted.
308 func heapBitsForAddr(addr uintptr) (h heapBits) {
309 // 2 bits per word, 4 pairs per byte, and a mask is hard coded.
310 arena := arenaIndex(addr)
311 ha := mheap_.arenas[arena.l1()][arena.l2()]
312 // The compiler uses a load for nil checking ha, but in this
313 // case we'll almost never hit that cache line again, so it
314 // makes more sense to do a value check.
316 // addr is not in the heap. Return nil heapBits, which
317 // we expect to crash in the caller.
320 h.bitp = &ha.bitmap[(addr/(sys.PtrSize*4))%heapArenaBitmapBytes]
321 h.shift = uint32((addr / sys.PtrSize) & 3)
322 h.arena = uint32(arena)
323 h.last = &ha.bitmap[len(ha.bitmap)-1]
327 // badPointer throws bad pointer in heap panic.
328 func badPointer(s *mspan, p, refBase, refOff uintptr) {
329 // Typically this indicates an incorrect use
330 // of unsafe or cgo to store a bad pointer in
331 // the Go heap. It may also indicate a runtime
334 // TODO(austin): We could be more aggressive
335 // and detect pointers to unallocated objects
336 // in allocated spans.
338 print("runtime: pointer ", hex(p))
339 state := s.state.get()
340 if state != mSpanInUse {
341 print(" to unallocated span")
343 print(" to unused region of span")
345 print(" span.base()=", hex(s.base()), " span.limit=", hex(s.limit), " span.state=", state, "\n")
347 print("runtime: found in object at *(", hex(refBase), "+", hex(refOff), ")\n")
348 gcDumpObject("object", refBase, refOff)
350 getg().m.traceback = 2
351 throw("found bad pointer in Go heap (incorrect use of unsafe or cgo?)")
354 // findObject returns the base address for the heap object containing
355 // the address p, the object's span, and the index of the object in s.
356 // If p does not point into a heap object, it returns base == 0.
358 // If p points is an invalid heap pointer and debug.invalidptr != 0,
359 // findObject panics.
361 // refBase and refOff optionally give the base address of the object
362 // in which the pointer p was found and the byte offset at which it
363 // was found. These are used for error reporting.
365 // It is nosplit so it is safe for p to be a pointer to the current goroutine's stack.
366 // Since p is a uintptr, it would not be adjusted if the stack were to move.
368 func findObject(p, refBase, refOff uintptr) (base uintptr, s *mspan, objIndex uintptr) {
370 // If s is nil, the virtual address has never been part of the heap.
371 // This pointer may be to some mmap'd region, so we allow it.
375 // If p is a bad pointer, it may not be in s's bounds.
377 // Check s.state to synchronize with span initialization
378 // before checking other fields. See also spanOfHeap.
379 if state := s.state.get(); state != mSpanInUse || p < s.base() || p >= s.limit {
380 // Pointers into stacks are also ok, the runtime manages these explicitly.
381 if state == mSpanManual {
384 // The following ensures that we are rigorous about what data
385 // structures hold valid pointers.
386 if debug.invalidptr != 0 {
387 badPointer(s, p, refBase, refOff)
391 // If this span holds object of a power of 2 size, just mask off the bits to
392 // the interior of the object. Otherwise use the size to get the base.
394 // optimize for power of 2 sized objects.
396 base = base + (p-base)&uintptr(s.baseMask)
397 objIndex = (base - s.base()) >> s.divShift
398 // base = p & s.baseMask is faster for small spans,
399 // but doesn't work for large spans.
400 // Overall, it's faster to use the more general computation above.
403 if p-base >= s.elemsize {
404 // n := (p - base) / s.elemsize, using division by multiplication
405 objIndex = uintptr(p-base) >> s.divShift * uintptr(s.divMul) >> s.divShift2
406 base += objIndex * s.elemsize
412 // next returns the heapBits describing the next pointer-sized word in memory.
413 // That is, if h describes address p, h.next() describes p+ptrSize.
414 // Note that next does not modify h. The caller must record the result.
416 // nosplit because it is used during write barriers and must not be preempted.
418 func (h heapBits) next() heapBits {
419 if h.shift < 3*heapBitsShift {
420 h.shift += heapBitsShift
421 } else if h.bitp != h.last {
422 h.bitp, h.shift = add1(h.bitp), 0
424 // Move to the next arena.
430 // nextArena advances h to the beginning of the next heap arena.
432 // This is a slow-path helper to next. gc's inliner knows that
433 // heapBits.next can be inlined even though it calls this. This is
434 // marked noinline so it doesn't get inlined into next and cause next
435 // to be too big to inline.
439 func (h heapBits) nextArena() heapBits {
441 ai := arenaIdx(h.arena)
442 l2 := mheap_.arenas[ai.l1()]
444 // We just passed the end of the object, which
445 // was also the end of the heap. Poison h. It
446 // should never be dereferenced at this point.
453 h.bitp, h.shift = &ha.bitmap[0], 0
454 h.last = &ha.bitmap[len(ha.bitmap)-1]
458 // forward returns the heapBits describing n pointer-sized words ahead of h in memory.
459 // That is, if h describes address p, h.forward(n) describes p+n*ptrSize.
460 // h.forward(1) is equivalent to h.next(), just slower.
461 // Note that forward does not modify h. The caller must record the result.
462 // bits returns the heap bits for the current word.
464 func (h heapBits) forward(n uintptr) heapBits {
465 n += uintptr(h.shift) / heapBitsShift
466 nbitp := uintptr(unsafe.Pointer(h.bitp)) + n/4
467 h.shift = uint32(n%4) * heapBitsShift
468 if nbitp <= uintptr(unsafe.Pointer(h.last)) {
469 h.bitp = (*uint8)(unsafe.Pointer(nbitp))
473 // We're in a new heap arena.
474 past := nbitp - (uintptr(unsafe.Pointer(h.last)) + 1)
475 h.arena += 1 + uint32(past/heapArenaBitmapBytes)
476 ai := arenaIdx(h.arena)
477 if l2 := mheap_.arenas[ai.l1()]; l2 != nil && l2[ai.l2()] != nil {
479 h.bitp = &a.bitmap[past%heapArenaBitmapBytes]
480 h.last = &a.bitmap[len(a.bitmap)-1]
482 h.bitp, h.last = nil, nil
487 // forwardOrBoundary is like forward, but stops at boundaries between
488 // contiguous sections of the bitmap. It returns the number of words
489 // advanced over, which will be <= n.
490 func (h heapBits) forwardOrBoundary(n uintptr) (heapBits, uintptr) {
491 maxn := 4 * ((uintptr(unsafe.Pointer(h.last)) + 1) - uintptr(unsafe.Pointer(h.bitp)))
495 return h.forward(n), n
498 // The caller can test morePointers and isPointer by &-ing with bitScan and bitPointer.
499 // The result includes in its higher bits the bits for subsequent words
500 // described by the same bitmap byte.
502 // nosplit because it is used during write barriers and must not be preempted.
504 func (h heapBits) bits() uint32 {
505 // The (shift & 31) eliminates a test and conditional branch
506 // from the generated code.
507 return uint32(*h.bitp) >> (h.shift & 31)
510 // morePointers reports whether this word and all remaining words in this object
512 // h must not describe the second word of the object.
513 func (h heapBits) morePointers() bool {
514 return h.bits()&bitScan != 0
517 // isPointer reports whether the heap bits describe a pointer word.
519 // nosplit because it is used during write barriers and must not be preempted.
521 func (h heapBits) isPointer() bool {
522 return h.bits()&bitPointer != 0
525 // bulkBarrierPreWrite executes a write barrier
526 // for every pointer slot in the memory range [src, src+size),
527 // using pointer/scalar information from [dst, dst+size).
528 // This executes the write barriers necessary before a memmove.
529 // src, dst, and size must be pointer-aligned.
530 // The range [dst, dst+size) must lie within a single object.
531 // It does not perform the actual writes.
533 // As a special case, src == 0 indicates that this is being used for a
534 // memclr. bulkBarrierPreWrite will pass 0 for the src of each write
537 // Callers should call bulkBarrierPreWrite immediately before
538 // calling memmove(dst, src, size). This function is marked nosplit
539 // to avoid being preempted; the GC must not stop the goroutine
540 // between the memmove and the execution of the barriers.
541 // The caller is also responsible for cgo pointer checks if this
542 // may be writing Go pointers into non-Go memory.
544 // The pointer bitmap is not maintained for allocations containing
545 // no pointers at all; any caller of bulkBarrierPreWrite must first
546 // make sure the underlying allocation contains pointers, usually
547 // by checking typ.ptrdata.
549 // Callers must perform cgo checks if writeBarrier.cgo.
552 func bulkBarrierPreWrite(dst, src, size uintptr) {
553 if (dst|src|size)&(sys.PtrSize-1) != 0 {
554 throw("bulkBarrierPreWrite: unaligned arguments")
556 if !writeBarrier.needed {
559 if s := spanOf(dst); s == nil {
560 // If dst is a global, use the data or BSS bitmaps to
561 // execute write barriers.
562 for _, datap := range activeModules() {
563 if datap.data <= dst && dst < datap.edata {
564 bulkBarrierBitmap(dst, src, size, dst-datap.data, datap.gcdatamask.bytedata)
568 for _, datap := range activeModules() {
569 if datap.bss <= dst && dst < datap.ebss {
570 bulkBarrierBitmap(dst, src, size, dst-datap.bss, datap.gcbssmask.bytedata)
575 } else if s.state.get() != mSpanInUse || dst < s.base() || s.limit <= dst {
576 // dst was heap memory at some point, but isn't now.
577 // It can't be a global. It must be either our stack,
578 // or in the case of direct channel sends, it could be
579 // another stack. Either way, no need for barriers.
580 // This will also catch if dst is in a freed span,
581 // though that should never have.
585 buf := &getg().m.p.ptr().wbBuf
586 h := heapBitsForAddr(dst)
588 for i := uintptr(0); i < size; i += sys.PtrSize {
590 dstx := (*uintptr)(unsafe.Pointer(dst + i))
591 if !buf.putFast(*dstx, 0) {
598 for i := uintptr(0); i < size; i += sys.PtrSize {
600 dstx := (*uintptr)(unsafe.Pointer(dst + i))
601 srcx := (*uintptr)(unsafe.Pointer(src + i))
602 if !buf.putFast(*dstx, *srcx) {
611 // bulkBarrierPreWriteSrcOnly is like bulkBarrierPreWrite but
612 // does not execute write barriers for [dst, dst+size).
614 // In addition to the requirements of bulkBarrierPreWrite
615 // callers need to ensure [dst, dst+size) is zeroed.
617 // This is used for special cases where e.g. dst was just
618 // created and zeroed with malloc.
620 func bulkBarrierPreWriteSrcOnly(dst, src, size uintptr) {
621 if (dst|src|size)&(sys.PtrSize-1) != 0 {
622 throw("bulkBarrierPreWrite: unaligned arguments")
624 if !writeBarrier.needed {
627 buf := &getg().m.p.ptr().wbBuf
628 h := heapBitsForAddr(dst)
629 for i := uintptr(0); i < size; i += sys.PtrSize {
631 srcx := (*uintptr)(unsafe.Pointer(src + i))
632 if !buf.putFast(0, *srcx) {
640 // bulkBarrierBitmap executes write barriers for copying from [src,
641 // src+size) to [dst, dst+size) using a 1-bit pointer bitmap. src is
642 // assumed to start maskOffset bytes into the data covered by the
643 // bitmap in bits (which may not be a multiple of 8).
645 // This is used by bulkBarrierPreWrite for writes to data and BSS.
648 func bulkBarrierBitmap(dst, src, size, maskOffset uintptr, bits *uint8) {
649 word := maskOffset / sys.PtrSize
650 bits = addb(bits, word/8)
651 mask := uint8(1) << (word % 8)
653 buf := &getg().m.p.ptr().wbBuf
654 for i := uintptr(0); i < size; i += sys.PtrSize {
665 dstx := (*uintptr)(unsafe.Pointer(dst + i))
667 if !buf.putFast(*dstx, 0) {
671 srcx := (*uintptr)(unsafe.Pointer(src + i))
672 if !buf.putFast(*dstx, *srcx) {
681 // typeBitsBulkBarrier executes a write barrier for every
682 // pointer that would be copied from [src, src+size) to [dst,
683 // dst+size) by a memmove using the type bitmap to locate those
686 // The type typ must correspond exactly to [src, src+size) and [dst, dst+size).
687 // dst, src, and size must be pointer-aligned.
688 // The type typ must have a plain bitmap, not a GC program.
689 // The only use of this function is in channel sends, and the
690 // 64 kB channel element limit takes care of this for us.
692 // Must not be preempted because it typically runs right before memmove,
693 // and the GC must observe them as an atomic action.
695 // Callers must perform cgo checks if writeBarrier.cgo.
698 func typeBitsBulkBarrier(typ *_type, dst, src, size uintptr) {
700 throw("runtime: typeBitsBulkBarrier without type")
702 if typ.size != size {
703 println("runtime: typeBitsBulkBarrier with type ", typ.string(), " of size ", typ.size, " but memory size", size)
704 throw("runtime: invalid typeBitsBulkBarrier")
706 if typ.kind&kindGCProg != 0 {
707 println("runtime: typeBitsBulkBarrier with type ", typ.string(), " with GC prog")
708 throw("runtime: invalid typeBitsBulkBarrier")
710 if !writeBarrier.needed {
713 ptrmask := typ.gcdata
714 buf := &getg().m.p.ptr().wbBuf
716 for i := uintptr(0); i < typ.ptrdata; i += sys.PtrSize {
717 if i&(sys.PtrSize*8-1) == 0 {
718 bits = uint32(*ptrmask)
719 ptrmask = addb(ptrmask, 1)
724 dstx := (*uintptr)(unsafe.Pointer(dst + i))
725 srcx := (*uintptr)(unsafe.Pointer(src + i))
726 if !buf.putFast(*dstx, *srcx) {
733 // The methods operating on spans all require that h has been returned
734 // by heapBitsForSpan and that size, n, total are the span layout description
735 // returned by the mspan's layout method.
736 // If total > size*n, it means that there is extra leftover memory in the span,
737 // usually due to rounding.
739 // TODO(rsc): Perhaps introduce a different heapBitsSpan type.
741 // initSpan initializes the heap bitmap for a span.
742 // If this is a span of pointer-sized objects, it initializes all
743 // words to pointer/scan.
744 // Otherwise, it initializes all words to scalar/dead.
745 func (h heapBits) initSpan(s *mspan) {
746 // Clear bits corresponding to objects.
747 nw := (s.npages << _PageShift) / sys.PtrSize
748 if nw%wordsPerBitmapByte != 0 {
749 throw("initSpan: unaligned length")
752 throw("initSpan: unaligned base")
754 isPtrs := sys.PtrSize == 8 && s.elemsize == sys.PtrSize
756 hNext, anw := h.forwardOrBoundary(nw)
757 nbyte := anw / wordsPerBitmapByte
760 for i := uintptr(0); i < nbyte; i++ {
761 *bitp = bitPointerAll | bitScanAll
765 memclrNoHeapPointers(unsafe.Pointer(h.bitp), nbyte)
772 // countAlloc returns the number of objects allocated in span s by
773 // scanning the allocation bitmap.
774 func (s *mspan) countAlloc() int {
776 bytes := divRoundUp(s.nelems, 8)
777 // Iterate over each 8-byte chunk and count allocations
778 // with an intrinsic. Note that newMarkBits guarantees that
779 // gcmarkBits will be 8-byte aligned, so we don't have to
780 // worry about edge cases, irrelevant bits will simply be zero.
781 for i := uintptr(0); i < bytes; i += 8 {
782 // Extract 64 bits from the byte pointer and get a OnesCount.
783 // Note that the unsafe cast here doesn't preserve endianness,
784 // but that's OK. We only care about how many bits are 1, not
785 // about the order we discover them in.
786 mrkBits := *(*uint64)(unsafe.Pointer(s.gcmarkBits.bytep(i)))
787 count += sys.OnesCount64(mrkBits)
792 // heapBitsSetType records that the new allocation [x, x+size)
793 // holds in [x, x+dataSize) one or more values of type typ.
794 // (The number of values is given by dataSize / typ.size.)
795 // If dataSize < size, the fragment [x+dataSize, x+size) is
796 // recorded as non-pointer data.
797 // It is known that the type has pointers somewhere;
798 // malloc does not call heapBitsSetType when there are no pointers,
799 // because all free objects are marked as noscan during
800 // heapBitsSweepSpan.
802 // There can only be one allocation from a given span active at a time,
803 // and the bitmap for a span always falls on byte boundaries,
804 // so there are no write-write races for access to the heap bitmap.
805 // Hence, heapBitsSetType can access the bitmap without atomics.
807 // There can be read-write races between heapBitsSetType and things
808 // that read the heap bitmap like scanobject. However, since
809 // heapBitsSetType is only used for objects that have not yet been
810 // made reachable, readers will ignore bits being modified by this
811 // function. This does mean this function cannot transiently modify
812 // bits that belong to neighboring objects. Also, on weakly-ordered
813 // machines, callers must execute a store/store (publication) barrier
814 // between calling this function and making the object reachable.
815 func heapBitsSetType(x, size, dataSize uintptr, typ *_type) {
816 const doubleCheck = false // slow but helpful; enable to test modifications to this code
819 mask1 = bitPointer | bitScan // 00010001
820 mask2 = bitPointer | bitScan | mask1<<heapBitsShift // 00110011
821 mask3 = bitPointer | bitScan | mask2<<heapBitsShift // 01110111
824 // dataSize is always size rounded up to the next malloc size class,
825 // except in the case of allocating a defer block, in which case
826 // size is sizeof(_defer{}) (at least 6 words) and dataSize may be
827 // arbitrarily larger.
829 // The checks for size == sys.PtrSize and size == 2*sys.PtrSize can therefore
830 // assume that dataSize == size without checking it explicitly.
832 if sys.PtrSize == 8 && size == sys.PtrSize {
833 // It's one word and it has pointers, it must be a pointer.
834 // Since all allocated one-word objects are pointers
835 // (non-pointers are aggregated into tinySize allocations),
836 // initSpan sets the pointer bits for us. Nothing to do here.
838 h := heapBitsForAddr(x)
840 throw("heapBitsSetType: pointer bit missing")
842 if !h.morePointers() {
843 throw("heapBitsSetType: scan bit missing")
849 h := heapBitsForAddr(x)
850 ptrmask := typ.gcdata // start of 1-bit pointer mask (or GC program, handled below)
852 // 2-word objects only have 4 bitmap bits and 3-word objects only have 6 bitmap bits.
853 // Therefore, these objects share a heap bitmap byte with the objects next to them.
854 // These are called out as a special case primarily so the code below can assume all
855 // objects are at least 4 words long and that their bitmaps start either at the beginning
856 // of a bitmap byte, or half-way in (h.shift of 0 and 2 respectively).
858 if size == 2*sys.PtrSize {
859 if typ.size == sys.PtrSize {
860 // We're allocating a block big enough to hold two pointers.
861 // On 64-bit, that means the actual object must be two pointers,
862 // or else we'd have used the one-pointer-sized block.
863 // On 32-bit, however, this is the 8-byte block, the smallest one.
864 // So it could be that we're allocating one pointer and this was
865 // just the smallest block available. Distinguish by checking dataSize.
866 // (In general the number of instances of typ being allocated is
867 // dataSize/typ.size.)
868 if sys.PtrSize == 4 && dataSize == sys.PtrSize {
869 // 1 pointer object. On 32-bit machines clear the bit for the
870 // unused second word.
871 *h.bitp &^= (bitPointer | bitScan | (bitPointer|bitScan)<<heapBitsShift) << h.shift
872 *h.bitp |= (bitPointer | bitScan) << h.shift
874 // 2-element array of pointer.
875 *h.bitp |= (bitPointer | bitScan | (bitPointer|bitScan)<<heapBitsShift) << h.shift
879 // Otherwise typ.size must be 2*sys.PtrSize,
880 // and typ.kind&kindGCProg == 0.
882 if typ.size != 2*sys.PtrSize || typ.kind&kindGCProg != 0 {
883 print("runtime: heapBitsSetType size=", size, " but typ.size=", typ.size, " gcprog=", typ.kind&kindGCProg != 0, "\n")
884 throw("heapBitsSetType")
887 b := uint32(*ptrmask)
889 hb |= bitScanAll & ((bitScan << (typ.ptrdata / sys.PtrSize)) - 1)
890 // Clear the bits for this object so we can set the
892 *h.bitp &^= (bitPointer | bitScan | ((bitPointer | bitScan) << heapBitsShift)) << h.shift
893 *h.bitp |= uint8(hb << h.shift)
895 } else if size == 3*sys.PtrSize {
899 println("runtime: invalid type ", typ.string())
900 throw("heapBitsSetType: called with non-pointer type")
902 if sys.PtrSize != 8 {
903 throw("heapBitsSetType: unexpected 3 pointer wide size class on 32 bit")
905 if typ.kind&kindGCProg != 0 {
906 throw("heapBitsSetType: unexpected GC prog for 3 pointer wide size class")
908 if typ.size == 2*sys.PtrSize {
909 print("runtime: heapBitsSetType size=", size, " but typ.size=", typ.size, "\n")
910 throw("heapBitsSetType: inconsistent object sizes")
913 if typ.size == sys.PtrSize {
914 // The type contains a pointer otherwise heapBitsSetType wouldn't have been called.
915 // Since the type is only 1 pointer wide and contains a pointer, its gcdata must be exactly 1.
916 if doubleCheck && *typ.gcdata != 1 {
917 print("runtime: heapBitsSetType size=", size, " typ.size=", typ.size, "but *typ.gcdata", *typ.gcdata, "\n")
918 throw("heapBitsSetType: unexpected gcdata for 1 pointer wide type size in 3 pointer wide size class")
920 // 3 element array of pointers. Unrolling ptrmask 3 times into p yields 00000111.
925 // Set bitScan bits for all pointers.
926 hb |= hb << wordsPerBitmapByte
927 // First bitScan bit is always set since the type contains pointers.
929 // Second bitScan bit needs to also be set if the third bitScan bit is set.
930 hb |= hb & (bitScan << (2 * heapBitsShift)) >> 1
932 // For h.shift > 1 heap bits cross a byte boundary and need to be written part
933 // to h.bitp and part to the next h.bitp.
936 *h.bitp &^= mask3 << 0
939 *h.bitp &^= mask3 << 1
942 *h.bitp &^= mask2 << 2
943 *h.bitp |= (hb & mask2) << 2
944 // Two words written to the first byte.
945 // Advance two words to get to the next byte.
948 *h.bitp |= (hb >> 2) & mask1
950 *h.bitp &^= mask1 << 3
951 *h.bitp |= (hb & mask1) << 3
952 // One word written to the first byte.
953 // Advance one word to get to the next byte.
956 *h.bitp |= (hb >> 1) & mask2
961 // Copy from 1-bit ptrmask into 2-bit bitmap.
962 // The basic approach is to use a single uintptr as a bit buffer,
963 // alternating between reloading the buffer and writing bitmap bytes.
964 // In general, one load can supply two bitmap byte writes.
965 // This is a lot of lines of code, but it compiles into relatively few
966 // machine instructions.
969 if arenaIndex(x+size-1) != arenaIdx(h.arena) || (doubleCheck && fastrand()%2 == 0) {
970 // This object spans heap arenas, so the bitmap may be
971 // discontiguous. Unroll it into the object instead
972 // and then copy it out.
974 // In doubleCheck mode, we randomly do this anyway to
975 // stress test the bitmap copying path.
977 h.bitp = (*uint8)(unsafe.Pointer(x))
983 p *byte // last ptrmask byte read
984 b uintptr // ptrmask bits already loaded
985 nb uintptr // number of bits in b at next read
986 endp *byte // final ptrmask byte to read (then repeat)
987 endnb uintptr // number of valid bits in *endp
988 pbits uintptr // alternate source of bits
990 // Heap bitmap output.
991 w uintptr // words processed
992 nw uintptr // number of words to process
993 hbitp *byte // next heap bitmap byte to write
994 hb uintptr // bits being prepared for *hbitp
999 // Handle GC program. Delayed until this part of the code
1000 // so that we can use the same double-checking mechanism
1001 // as the 1-bit case. Nothing above could have encountered
1002 // GC programs: the cases were all too small.
1003 if typ.kind&kindGCProg != 0 {
1004 heapBitsSetTypeGCProg(h, typ.ptrdata, typ.size, dataSize, size, addb(typ.gcdata, 4))
1006 // Double-check the heap bits written by GC program
1007 // by running the GC program to create a 1-bit pointer mask
1008 // and then jumping to the double-check code below.
1009 // This doesn't catch bugs shared between the 1-bit and 4-bit
1010 // GC program execution, but it does catch mistakes specific
1011 // to just one of those and bugs in heapBitsSetTypeGCProg's
1012 // implementation of arrays.
1013 lock(&debugPtrmask.lock)
1014 if debugPtrmask.data == nil {
1015 debugPtrmask.data = (*byte)(persistentalloc(1<<20, 1, &memstats.other_sys))
1017 ptrmask = debugPtrmask.data
1018 runGCProg(addb(typ.gcdata, 4), nil, ptrmask, 1)
1023 // Note about sizes:
1025 // typ.size is the number of words in the object,
1026 // and typ.ptrdata is the number of words in the prefix
1027 // of the object that contains pointers. That is, the final
1028 // typ.size - typ.ptrdata words contain no pointers.
1029 // This allows optimization of a common pattern where
1030 // an object has a small header followed by a large scalar
1031 // buffer. If we know the pointers are over, we don't have
1032 // to scan the buffer's heap bitmap at all.
1033 // The 1-bit ptrmasks are sized to contain only bits for
1034 // the typ.ptrdata prefix, zero padded out to a full byte
1035 // of bitmap. This code sets nw (below) so that heap bitmap
1036 // bits are only written for the typ.ptrdata prefix; if there is
1037 // more room in the allocated object, the next heap bitmap
1038 // entry is a 00, indicating that there are no more pointers
1039 // to scan. So only the ptrmask for the ptrdata bytes is needed.
1041 // Replicated copies are not as nice: if there is an array of
1042 // objects with scalar tails, all but the last tail does have to
1043 // be initialized, because there is no way to say "skip forward".
1044 // However, because of the possibility of a repeated type with
1045 // size not a multiple of 4 pointers (one heap bitmap byte),
1046 // the code already must handle the last ptrmask byte specially
1047 // by treating it as containing only the bits for endnb pointers,
1048 // where endnb <= 4. We represent large scalar tails that must
1049 // be expanded in the replication by setting endnb larger than 4.
1050 // This will have the effect of reading many bits out of b,
1051 // but once the real bits are shifted out, b will supply as many
1052 // zero bits as we try to read, which is exactly what we need.
1055 if typ.size < dataSize {
1056 // Filling in bits for an array of typ.
1057 // Set up for repetition of ptrmask during main loop.
1058 // Note that ptrmask describes only a prefix of
1059 const maxBits = sys.PtrSize*8 - 7
1060 if typ.ptrdata/sys.PtrSize <= maxBits {
1061 // Entire ptrmask fits in uintptr with room for a byte fragment.
1062 // Load into pbits and never read from ptrmask again.
1063 // This is especially important when the ptrmask has
1064 // fewer than 8 bits in it; otherwise the reload in the middle
1065 // of the Phase 2 loop would itself need to loop to gather
1068 // Accumulate ptrmask into b.
1069 // ptrmask is sized to describe only typ.ptrdata, but we record
1070 // it as describing typ.size bytes, since all the high bits are zero.
1071 nb = typ.ptrdata / sys.PtrSize
1072 for i := uintptr(0); i < nb; i += 8 {
1073 b |= uintptr(*p) << i
1076 nb = typ.size / sys.PtrSize
1078 // Replicate ptrmask to fill entire pbits uintptr.
1079 // Doubling and truncating is fewer steps than
1080 // iterating by nb each time. (nb could be 1.)
1081 // Since we loaded typ.ptrdata/sys.PtrSize bits
1082 // but are pretending to have typ.size/sys.PtrSize,
1083 // there might be no replication necessary/possible.
1086 if nb+nb <= maxBits {
1087 for endnb <= sys.PtrSize*8 {
1088 pbits |= pbits << endnb
1091 // Truncate to a multiple of original ptrmask.
1092 // Because nb+nb <= maxBits, nb fits in a byte.
1093 // Byte division is cheaper than uintptr division.
1094 endnb = uintptr(maxBits/byte(nb)) * nb
1095 pbits &= 1<<endnb - 1
1100 // Clear p and endp as sentinel for using pbits.
1101 // Checked during Phase 2 loop.
1105 // Ptrmask is larger. Read it multiple times.
1106 n := (typ.ptrdata/sys.PtrSize+7)/8 - 1
1107 endp = addb(ptrmask, n)
1108 endnb = typ.size/sys.PtrSize - n*8
1117 if typ.size == dataSize {
1118 // Single entry: can stop once we reach the non-pointer data.
1119 nw = typ.ptrdata / sys.PtrSize
1121 // Repeated instances of typ in an array.
1122 // Have to process first N-1 entries in full, but can stop
1123 // once we reach the non-pointer data in the final entry.
1124 nw = ((dataSize/typ.size-1)*typ.size + typ.ptrdata) / sys.PtrSize
1127 // No pointers! Caller was supposed to check.
1128 println("runtime: invalid type ", typ.string())
1129 throw("heapBitsSetType: called with non-pointer type")
1133 // Phase 1: Special case for leading byte (shift==0) or half-byte (shift==2).
1134 // The leading byte is special because it contains the bits for word 1,
1135 // which does not have the scan bit set.
1136 // The leading half-byte is special because it's a half a byte,
1137 // so we have to be careful with the bits already there.
1140 throw("heapBitsSetType: unexpected shift")
1143 // Ptrmask and heap bitmap are aligned.
1145 // This is a fast path for small objects.
1147 // The first byte we write out covers the first four
1148 // words of the object. The scan/dead bit on the first
1149 // word must be set to scan since there are pointers
1150 // somewhere in the object.
1151 // In all following words, we set the scan/dead
1152 // appropriately to indicate that the object continues
1153 // to the next 2-bit entry in the bitmap.
1155 // We set four bits at a time here, but if the object
1156 // is fewer than four words, phase 3 will clear
1157 // unnecessary bits.
1158 hb = b & bitPointerAll
1160 if w += 4; w >= nw {
1169 // Ptrmask and heap bitmap are misaligned.
1171 // On 32 bit architectures only the 6-word object that corresponds
1172 // to a 24 bytes size class can start with h.shift of 2 here since
1173 // all other non 16 byte aligned size classes have been handled by
1174 // special code paths at the beginning of heapBitsSetType on 32 bit.
1176 // Many size classes are only 16 byte aligned. On 64 bit architectures
1177 // this results in a heap bitmap position starting with a h.shift of 2.
1179 // The bits for the first two words are in a byte shared
1180 // with another object, so we must be careful with the bits
1183 // We took care of 1-word, 2-word, and 3-word objects above,
1184 // so this is at least a 6-word object.
1185 hb = (b & (bitPointer | bitPointer<<heapBitsShift)) << (2 * heapBitsShift)
1186 hb |= bitScan << (2 * heapBitsShift)
1188 hb |= bitScan << (3 * heapBitsShift)
1192 *hbitp &^= uint8((bitPointer | bitScan | ((bitPointer | bitScan) << heapBitsShift)) << (2 * heapBitsShift))
1195 if w += 2; w >= nw {
1196 // We know that there is more data, because we handled 2-word and 3-word objects above.
1197 // This must be at least a 6-word object. If we're out of pointer words,
1198 // mark no scan in next bitmap byte and finish.
1205 // Phase 2: Full bytes in bitmap, up to but not including write to last byte (full or partial) in bitmap.
1206 // The loop computes the bits for that last write but does not execute the write;
1207 // it leaves the bits in hb for processing by phase 3.
1208 // To avoid repeated adjustment of nb, we subtract out the 4 bits we're going to
1209 // use in the first half of the loop right now, and then we only adjust nb explicitly
1210 // if the 8 bits used by each iteration isn't balanced by 8 bits loaded mid-loop.
1213 // Emit bitmap byte.
1214 // b has at least nb+4 bits, with one exception:
1215 // if w+4 >= nw, then b has only nw-w bits,
1216 // but we'll stop at the break and then truncate
1217 // appropriately in Phase 3.
1218 hb = b & bitPointerAll
1220 if w += 4; w >= nw {
1227 // Load more bits. b has nb right now.
1229 // Fast path: keep reading from ptrmask.
1230 // nb unmodified: we just loaded 8 bits,
1231 // and the next iteration will consume 8 bits,
1232 // leaving us with the same nb the next time we're here.
1234 b |= uintptr(*p) << nb
1237 // Reduce the number of bits in b.
1238 // This is important if we skipped
1239 // over a scalar tail, since nb could
1240 // be larger than the bit width of b.
1243 } else if p == nil {
1244 // Almost as fast path: track bit count and refill from pbits.
1245 // For short repetitions.
1250 nb -= 8 // for next iteration
1252 // Slow path: reached end of ptrmask.
1253 // Process final partial byte and rewind to start.
1254 b |= uintptr(*p) << nb
1257 b |= uintptr(*ptrmask) << nb
1265 // Emit bitmap byte.
1266 hb = b & bitPointerAll
1268 if w += 4; w >= nw {
1277 // Phase 3: Write last byte or partial byte and zero the rest of the bitmap entries.
1279 // Counting the 4 entries in hb not yet written to memory,
1280 // there are more entries than possible pointer slots.
1281 // Discard the excess entries (can't be more than 3).
1282 mask := uintptr(1)<<(4-(w-nw)) - 1
1283 hb &= mask | mask<<4 // apply mask to both pointer bits and scan bits
1286 // Change nw from counting possibly-pointer words to total words in allocation.
1287 nw = size / sys.PtrSize
1289 // Write whole bitmap bytes.
1290 // The first is hb, the rest are zero.
1294 hb = 0 // for possible final half-byte below
1295 for w += 4; w <= nw; w += 4 {
1301 // Write final partial bitmap byte if any.
1302 // We know w > nw, or else we'd still be in the loop above.
1303 // It can be bigger only due to the 4 entries in hb that it counts.
1304 // If w == nw+4 then there's nothing left to do: we wrote all nw entries
1305 // and can discard the 4 sitting in hb.
1306 // But if w == nw+2, we need to write first two in hb.
1307 // The byte is shared with the next object, so be careful with
1310 *hbitp = *hbitp&^(bitPointer|bitScan|(bitPointer|bitScan)<<heapBitsShift) | uint8(hb)
1314 // Phase 4: Copy unrolled bitmap to per-arena bitmaps, if necessary.
1316 // TODO: We could probably make this faster by
1317 // handling [x+dataSize, x+size) specially.
1318 h := heapBitsForAddr(x)
1319 // cnw is the number of heap words, or bit pairs
1320 // remaining (like nw above).
1321 cnw := size / sys.PtrSize
1322 src := (*uint8)(unsafe.Pointer(x))
1323 // We know the first and last byte of the bitmap are
1324 // not the same, but it's still possible for small
1325 // objects span arenas, so it may share bitmap bytes
1326 // with neighboring objects.
1328 // Handle the first byte specially if it's shared. See
1329 // Phase 1 for why this is the only special case we need.
1331 if !(h.shift == 0 || h.shift == 2) {
1332 print("x=", x, " size=", size, " cnw=", h.shift, "\n")
1333 throw("bad start shift")
1337 *h.bitp = *h.bitp&^((bitPointer|bitScan|(bitPointer|bitScan)<<heapBitsShift)<<(2*heapBitsShift)) | *src
1342 // We're now byte aligned. Copy out to per-arena
1343 // bitmaps until the last byte (which may again be
1346 // This loop processes four words at a time,
1347 // so round cnw down accordingly.
1348 hNext, words := h.forwardOrBoundary(cnw / 4 * 4)
1350 // n is the number of bitmap bytes to copy.
1352 memmove(unsafe.Pointer(h.bitp), unsafe.Pointer(src), n)
1357 if doubleCheck && h.shift != 0 {
1358 print("cnw=", cnw, " h.shift=", h.shift, "\n")
1359 throw("bad shift after block copy")
1361 // Handle the last byte if it's shared.
1363 *h.bitp = *h.bitp&^(bitPointer|bitScan|(bitPointer|bitScan)<<heapBitsShift) | *src
1368 if uintptr(unsafe.Pointer(src)) > x+size {
1369 throw("copy exceeded object size")
1371 if !(cnw == 0 || cnw == 2) {
1372 print("x=", x, " size=", size, " cnw=", cnw, "\n")
1373 throw("bad number of remaining words")
1375 // Set up hbitp so doubleCheck code below can check it.
1378 // Zero the object where we wrote the bitmap.
1379 memclrNoHeapPointers(unsafe.Pointer(x), uintptr(unsafe.Pointer(src))-x)
1382 // Double check the whole bitmap.
1384 // x+size may not point to the heap, so back up one
1385 // word and then advance it the way we do above.
1386 end := heapBitsForAddr(x + size - sys.PtrSize)
1388 // In out-of-place copying, we just advance
1392 // Don't use next because that may advance to
1393 // the next arena and the in-place logic
1395 end.shift += heapBitsShift
1396 if end.shift == 4*heapBitsShift {
1397 end.bitp, end.shift = add1(end.bitp), 0
1400 if typ.kind&kindGCProg == 0 && (hbitp != end.bitp || (w == nw+2) != (end.shift == 2)) {
1401 println("ended at wrong bitmap byte for", typ.string(), "x", dataSize/typ.size)
1402 print("typ.size=", typ.size, " typ.ptrdata=", typ.ptrdata, " dataSize=", dataSize, " size=", size, "\n")
1403 print("w=", w, " nw=", nw, " b=", hex(b), " nb=", nb, " hb=", hex(hb), "\n")
1404 h0 := heapBitsForAddr(x)
1405 print("initial bits h0.bitp=", h0.bitp, " h0.shift=", h0.shift, "\n")
1406 print("ended at hbitp=", hbitp, " but next starts at bitp=", end.bitp, " shift=", end.shift, "\n")
1407 throw("bad heapBitsSetType")
1410 // Double-check that bits to be written were written correctly.
1411 // Does not check that other bits were not written, unfortunately.
1412 h := heapBitsForAddr(x)
1413 nptr := typ.ptrdata / sys.PtrSize
1414 ndata := typ.size / sys.PtrSize
1415 count := dataSize / typ.size
1416 totalptr := ((count-1)*typ.size + typ.ptrdata) / sys.PtrSize
1417 for i := uintptr(0); i < size/sys.PtrSize; i++ {
1419 var have, want uint8
1420 have = (*h.bitp >> h.shift) & (bitPointer | bitScan)
1422 if typ.kind&kindGCProg != 0 && i < (totalptr+3)/4*4 {
1423 // heapBitsSetTypeGCProg always fills
1424 // in full nibbles of bitScan.
1428 if j < nptr && (*addb(ptrmask, j/8)>>(j%8))&1 != 0 {
1434 println("mismatch writing bits for", typ.string(), "x", dataSize/typ.size)
1435 print("typ.size=", typ.size, " typ.ptrdata=", typ.ptrdata, " dataSize=", dataSize, " size=", size, "\n")
1436 print("kindGCProg=", typ.kind&kindGCProg != 0, " outOfPlace=", outOfPlace, "\n")
1437 print("w=", w, " nw=", nw, " b=", hex(b), " nb=", nb, " hb=", hex(hb), "\n")
1438 h0 := heapBitsForAddr(x)
1439 print("initial bits h0.bitp=", h0.bitp, " h0.shift=", h0.shift, "\n")
1440 print("current bits h.bitp=", h.bitp, " h.shift=", h.shift, " *h.bitp=", hex(*h.bitp), "\n")
1441 print("ptrmask=", ptrmask, " p=", p, " endp=", endp, " endnb=", endnb, " pbits=", hex(pbits), " b=", hex(b), " nb=", nb, "\n")
1442 println("at word", i, "offset", i*sys.PtrSize, "have", hex(have), "want", hex(want))
1443 if typ.kind&kindGCProg != 0 {
1444 println("GC program:")
1445 dumpGCProg(addb(typ.gcdata, 4))
1447 throw("bad heapBitsSetType")
1451 if ptrmask == debugPtrmask.data {
1452 unlock(&debugPtrmask.lock)
1457 var debugPtrmask struct {
1462 // heapBitsSetTypeGCProg implements heapBitsSetType using a GC program.
1463 // progSize is the size of the memory described by the program.
1464 // elemSize is the size of the element that the GC program describes (a prefix of).
1465 // dataSize is the total size of the intended data, a multiple of elemSize.
1466 // allocSize is the total size of the allocated memory.
1468 // GC programs are only used for large allocations.
1469 // heapBitsSetType requires that allocSize is a multiple of 4 words,
1470 // so that the relevant bitmap bytes are not shared with surrounding
1472 func heapBitsSetTypeGCProg(h heapBits, progSize, elemSize, dataSize, allocSize uintptr, prog *byte) {
1473 if sys.PtrSize == 8 && allocSize%(4*sys.PtrSize) != 0 {
1474 // Alignment will be wrong.
1475 throw("heapBitsSetTypeGCProg: small allocation")
1477 var totalBits uintptr
1478 if elemSize == dataSize {
1479 totalBits = runGCProg(prog, nil, h.bitp, 2)
1480 if totalBits*sys.PtrSize != progSize {
1481 println("runtime: heapBitsSetTypeGCProg: total bits", totalBits, "but progSize", progSize)
1482 throw("heapBitsSetTypeGCProg: unexpected bit count")
1485 count := dataSize / elemSize
1487 // Piece together program trailer to run after prog that does:
1489 // repeat(1, elemSize-progSize-1) // zeros to fill element size
1490 // repeat(elemSize, count-1) // repeat that element for count
1491 // This zero-pads the data remaining in the first element and then
1492 // repeats that first element to fill the array.
1493 var trailer [40]byte // 3 varints (max 10 each) + some bytes
1495 if n := elemSize/sys.PtrSize - progSize/sys.PtrSize; n > 0 {
1506 for ; n >= 0x80; n >>= 7 {
1507 trailer[i] = byte(n | 0x80)
1510 trailer[i] = byte(n)
1514 // repeat(elemSize/ptrSize, count-1)
1517 n := elemSize / sys.PtrSize
1518 for ; n >= 0x80; n >>= 7 {
1519 trailer[i] = byte(n | 0x80)
1522 trailer[i] = byte(n)
1525 for ; n >= 0x80; n >>= 7 {
1526 trailer[i] = byte(n | 0x80)
1529 trailer[i] = byte(n)
1534 runGCProg(prog, &trailer[0], h.bitp, 2)
1536 // Even though we filled in the full array just now,
1537 // record that we only filled in up to the ptrdata of the
1538 // last element. This will cause the code below to
1539 // memclr the dead section of the final array element,
1540 // so that scanobject can stop early in the final element.
1541 totalBits = (elemSize*(count-1) + progSize) / sys.PtrSize
1543 endProg := unsafe.Pointer(addb(h.bitp, (totalBits+3)/4))
1544 endAlloc := unsafe.Pointer(addb(h.bitp, allocSize/sys.PtrSize/wordsPerBitmapByte))
1545 memclrNoHeapPointers(endProg, uintptr(endAlloc)-uintptr(endProg))
1548 // progToPointerMask returns the 1-bit pointer mask output by the GC program prog.
1549 // size the size of the region described by prog, in bytes.
1550 // The resulting bitvector will have no more than size/sys.PtrSize bits.
1551 func progToPointerMask(prog *byte, size uintptr) bitvector {
1552 n := (size/sys.PtrSize + 7) / 8
1553 x := (*[1 << 30]byte)(persistentalloc(n+1, 1, &memstats.buckhash_sys))[:n+1]
1554 x[len(x)-1] = 0xa1 // overflow check sentinel
1555 n = runGCProg(prog, nil, &x[0], 1)
1556 if x[len(x)-1] != 0xa1 {
1557 throw("progToPointerMask: overflow")
1559 return bitvector{int32(n), &x[0]}
1562 // Packed GC pointer bitmaps, aka GC programs.
1564 // For large types containing arrays, the type information has a
1565 // natural repetition that can be encoded to save space in the
1566 // binary and in the memory representation of the type information.
1568 // The encoding is a simple Lempel-Ziv style bytecode machine
1569 // with the following instructions:
1572 // 0nnnnnnn: emit n bits copied from the next (n+7)/8 bytes
1573 // 10000000 n c: repeat the previous n bits c times; n, c are varints
1574 // 1nnnnnnn c: repeat the previous n bits c times; c is a varint
1576 // runGCProg executes the GC program prog, and then trailer if non-nil,
1577 // writing to dst with entries of the given size.
1578 // If size == 1, dst is a 1-bit pointer mask laid out moving forward from dst.
1579 // If size == 2, dst is the 2-bit heap bitmap, and writes move backward
1580 // starting at dst (because the heap bitmap does). In this case, the caller guarantees
1581 // that only whole bytes in dst need to be written.
1583 // runGCProg returns the number of 1- or 2-bit entries written to memory.
1584 func runGCProg(prog, trailer, dst *byte, size int) uintptr {
1587 // Bits waiting to be written to memory.
1594 // Flush accumulated full bytes.
1595 // The rest of the loop assumes that nbits <= 7.
1596 for ; nbits >= 8; nbits -= 8 {
1602 v := bits&bitPointerAll | bitScanAll
1606 v = bits&bitPointerAll | bitScanAll
1613 // Process one instruction.
1618 // Literal bits; n == 0 means end of program.
1620 // Program is over; continue in trailer if present.
1629 for i := uintptr(0); i < nbyte; i++ {
1630 bits |= uintptr(*p) << nbits
1637 v := bits&0xf | bitScanAll
1641 v = bits&0xf | bitScanAll
1648 bits |= uintptr(*p) << nbits
1655 // Repeat. If n == 0, it is encoded in a varint in the next bytes.
1657 for off := uint(0); ; off += 7 {
1660 n |= (x & 0x7F) << off
1667 // Count is encoded in a varint in the next bytes.
1669 for off := uint(0); ; off += 7 {
1672 c |= (x & 0x7F) << off
1677 c *= n // now total number of bits to copy
1679 // If the number of bits being repeated is small, load them
1680 // into a register and use that register for the entire loop
1681 // instead of repeatedly reading from memory.
1682 // Handling fewer than 8 bits here makes the general loop simpler.
1683 // The cutoff is sys.PtrSize*8 - 7 to guarantee that when we add
1684 // the pattern to a bit buffer holding at most 7 bits (a partial byte)
1685 // it will not overflow.
1687 const maxBits = sys.PtrSize*8 - 7
1689 // Start with bits in output buffer.
1693 // If we need more bits, fetch them from memory.
1695 src = subtract1(src)
1698 pattern |= uintptr(*src)
1699 src = subtract1(src)
1703 src = subtract1(src)
1706 pattern |= uintptr(*src) & 0xf
1707 src = subtract1(src)
1712 // We started with the whole bit output buffer,
1713 // and then we loaded bits from whole bytes.
1714 // Either way, we might now have too many instead of too few.
1715 // Discard the extra.
1717 pattern >>= npattern - n
1721 // Replicate pattern to at most maxBits.
1723 // One bit being repeated.
1724 // If the bit is 1, make the pattern all 1s.
1725 // If the bit is 0, the pattern is already all 0s,
1726 // but we can claim that the number of bits
1727 // in the word is equal to the number we need (c),
1728 // because right shift of bits will zero fill.
1730 pattern = 1<<maxBits - 1
1738 if nb+nb <= maxBits {
1739 // Double pattern until the whole uintptr is filled.
1740 for nb <= sys.PtrSize*8 {
1744 // Trim away incomplete copy of original pattern in high bits.
1745 // TODO(rsc): Replace with table lookup or loop on systems without divide?
1746 nb = maxBits / npattern * npattern
1753 // Add pattern to bit buffer and flush bit buffer, c/npattern times.
1754 // Since pattern contains >8 bits, there will be full bytes to flush
1755 // on each iteration.
1756 for ; c >= npattern; c -= npattern {
1757 bits |= pattern << nbits
1768 *dst = uint8(bits&0xf | bitScanAll)
1776 // Add final fragment to bit buffer.
1779 bits |= pattern << nbits
1785 // Repeat; n too large to fit in a register.
1786 // Since nbits <= 7, we know the first few bytes of repeated data
1787 // are already written to memory.
1788 off := n - nbits // n > nbits because n > maxBits and nbits <= 7
1790 // Leading src fragment.
1791 src = subtractb(src, (off+7)/8)
1792 if frag := off & 7; frag != 0 {
1793 bits |= uintptr(*src) >> (8 - frag) << nbits
1798 // Main loop: load one byte, write another.
1799 // The bits are rotating through the bit buffer.
1800 for i := c / 8; i > 0; i-- {
1801 bits |= uintptr(*src) << nbits
1807 // Final src fragment.
1809 bits |= (uintptr(*src) & (1<<c - 1)) << nbits
1813 // Leading src fragment.
1814 src = subtractb(src, (off+3)/4)
1815 if frag := off & 3; frag != 0 {
1816 bits |= (uintptr(*src) & 0xf) >> (4 - frag) << nbits
1821 // Main loop: load one byte, write another.
1822 // The bits are rotating through the bit buffer.
1823 for i := c / 4; i > 0; i-- {
1824 bits |= (uintptr(*src) & 0xf) << nbits
1826 *dst = uint8(bits&0xf | bitScanAll)
1830 // Final src fragment.
1832 bits |= (uintptr(*src) & (1<<c - 1)) << nbits
1838 // Write any final bits out, using full-byte writes, even for the final byte.
1839 var totalBits uintptr
1841 totalBits = (uintptr(unsafe.Pointer(dst))-uintptr(unsafe.Pointer(dstStart)))*8 + nbits
1843 for ; nbits > 0; nbits -= 8 {
1849 totalBits = (uintptr(unsafe.Pointer(dst))-uintptr(unsafe.Pointer(dstStart)))*4 + nbits
1851 for ; nbits > 0; nbits -= 4 {
1852 v := bits&0xf | bitScanAll
1861 // materializeGCProg allocates space for the (1-bit) pointer bitmask
1862 // for an object of size ptrdata. Then it fills that space with the
1863 // pointer bitmask specified by the program prog.
1864 // The bitmask starts at s.startAddr.
1865 // The result must be deallocated with dematerializeGCProg.
1866 func materializeGCProg(ptrdata uintptr, prog *byte) *mspan {
1867 // Each word of ptrdata needs one bit in the bitmap.
1868 bitmapBytes := divRoundUp(ptrdata, 8*sys.PtrSize)
1869 // Compute the number of pages needed for bitmapBytes.
1870 pages := divRoundUp(bitmapBytes, pageSize)
1871 s := mheap_.allocManual(pages, &memstats.gc_sys)
1872 runGCProg(addb(prog, 4), nil, (*byte)(unsafe.Pointer(s.startAddr)), 1)
1875 func dematerializeGCProg(s *mspan) {
1876 mheap_.freeManual(s, &memstats.gc_sys)
1879 func dumpGCProg(p *byte) {
1885 print("\t", nptr, " end\n")
1889 print("\t", nptr, " lit ", x, ":")
1891 for i := 0; i < n; i++ {
1898 nbit := int(x &^ 0x80)
1900 for nb := uint(0); ; nb += 7 {
1903 nbit |= int(x&0x7f) << nb
1910 for nb := uint(0); ; nb += 7 {
1913 count |= int(x&0x7f) << nb
1918 print("\t", nptr, " repeat ", nbit, " × ", count, "\n")
1919 nptr += nbit * count
1926 func getgcmaskcb(frame *stkframe, ctxt unsafe.Pointer) bool {
1927 target := (*stkframe)(ctxt)
1928 if frame.sp <= target.sp && target.sp < frame.varp {
1935 // gcbits returns the GC type info for x, for testing.
1936 // The result is the bitmap entries (0 or 1), one entry per byte.
1937 //go:linkname reflect_gcbits reflect.gcbits
1938 func reflect_gcbits(x interface{}) []byte {
1940 typ := (*ptrtype)(unsafe.Pointer(efaceOf(&x)._type)).elem
1941 nptr := typ.ptrdata / sys.PtrSize
1942 for uintptr(len(ret)) > nptr && ret[len(ret)-1] == 0 {
1943 ret = ret[:len(ret)-1]
1948 // Returns GC type info for the pointer stored in ep for testing.
1949 // If ep points to the stack, only static live information will be returned
1950 // (i.e. not for objects which are only dynamically live stack objects).
1951 func getgcmask(ep interface{}) (mask []byte) {
1956 for _, datap := range activeModules() {
1958 if datap.data <= uintptr(p) && uintptr(p) < datap.edata {
1959 bitmap := datap.gcdatamask.bytedata
1960 n := (*ptrtype)(unsafe.Pointer(t)).elem.size
1961 mask = make([]byte, n/sys.PtrSize)
1962 for i := uintptr(0); i < n; i += sys.PtrSize {
1963 off := (uintptr(p) + i - datap.data) / sys.PtrSize
1964 mask[i/sys.PtrSize] = (*addb(bitmap, off/8) >> (off % 8)) & 1
1970 if datap.bss <= uintptr(p) && uintptr(p) < datap.ebss {
1971 bitmap := datap.gcbssmask.bytedata
1972 n := (*ptrtype)(unsafe.Pointer(t)).elem.size
1973 mask = make([]byte, n/sys.PtrSize)
1974 for i := uintptr(0); i < n; i += sys.PtrSize {
1975 off := (uintptr(p) + i - datap.bss) / sys.PtrSize
1976 mask[i/sys.PtrSize] = (*addb(bitmap, off/8) >> (off % 8)) & 1
1983 if base, s, _ := findObject(uintptr(p), 0, 0); base != 0 {
1984 hbits := heapBitsForAddr(base)
1986 mask = make([]byte, n/sys.PtrSize)
1987 for i := uintptr(0); i < n; i += sys.PtrSize {
1988 if hbits.isPointer() {
1989 mask[i/sys.PtrSize] = 1
1991 if !hbits.morePointers() {
1992 mask = mask[:i/sys.PtrSize]
1995 hbits = hbits.next()
2001 if _g_ := getg(); _g_.m.curg.stack.lo <= uintptr(p) && uintptr(p) < _g_.m.curg.stack.hi {
2003 frame.sp = uintptr(p)
2005 gentraceback(_g_.m.curg.sched.pc, _g_.m.curg.sched.sp, 0, _g_.m.curg, 0, nil, 1000, getgcmaskcb, noescape(unsafe.Pointer(&frame)), 0)
2006 if frame.fn.valid() {
2007 locals, _, _ := getStackMap(&frame, nil, false)
2011 size := uintptr(locals.n) * sys.PtrSize
2012 n := (*ptrtype)(unsafe.Pointer(t)).elem.size
2013 mask = make([]byte, n/sys.PtrSize)
2014 for i := uintptr(0); i < n; i += sys.PtrSize {
2015 off := (uintptr(p) + i - frame.varp + size) / sys.PtrSize
2016 mask[i/sys.PtrSize] = locals.ptrbit(off)
2022 // otherwise, not something the GC knows about.
2023 // possibly read-only data, like malloc(0).
2024 // must not have pointers