github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/runtime/mbitmap_allocheaders.go (about)

     1  // Copyright 2023 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.
     4  
     5  //go:build goexperiment.allocheaders
     6  
     7  // Garbage collector: type and heap bitmaps.
     8  //
     9  // Stack, data, and bss bitmaps
    10  //
    11  // Stack frames and global variables in the data and bss sections are
    12  // described by bitmaps with 1 bit per pointer-sized word. A "1" bit
    13  // means the word is a live pointer to be visited by the GC (referred to
    14  // as "pointer"). A "0" bit means the word should be ignored by GC
    15  // (referred to as "scalar", though it could be a dead pointer value).
    16  //
    17  // Heap bitmaps
    18  //
    19  // The heap bitmap comprises 1 bit for each pointer-sized word in the heap,
    20  // recording whether a pointer is stored in that word or not. This bitmap
    21  // is stored at the end of a span for small objects and is unrolled at
    22  // runtime from type metadata for all larger objects. Objects without
    23  // pointers have neither a bitmap nor associated type metadata.
    24  //
    25  // Bits in all cases correspond to words in little-endian order.
    26  //
    27  // For small objects, if s is the mspan for the span starting at "start",
    28  // then s.heapBits() returns a slice containing the bitmap for the whole span.
    29  // That is, s.heapBits()[0] holds the goarch.PtrSize*8 bits for the first
    30  // goarch.PtrSize*8 words from "start" through "start+63*ptrSize" in the span.
    31  // On a related note, small objects are always small enough that their bitmap
    32  // fits in goarch.PtrSize*8 bits, so writing out bitmap data takes two bitmap
    33  // writes at most (because object boundaries don't generally lie on
    34  // s.heapBits()[i] boundaries).
    35  //
    36  // For larger objects, if t is the type for the object starting at "start",
    37  // within some span whose mspan is s, then the bitmap at t.GCData is "tiled"
    38  // from "start" through "start+s.elemsize".
    39  // Specifically, the first bit of t.GCData corresponds to the word at "start",
    40  // the second to the word after "start", and so on up to t.PtrBytes. At t.PtrBytes,
    41  // we skip to "start+t.Size_" and begin again from there. This process is
    42  // repeated until we hit "start+s.elemsize".
    43  // This tiling algorithm supports array data, since the type always refers to
    44  // the element type of the array. Single objects are considered the same as
    45  // single-element arrays.
    46  // The tiling algorithm may scan data past the end of the compiler-recognized
    47  // object, but any unused data within the allocation slot (i.e. within s.elemsize)
    48  // is zeroed, so the GC just observes nil pointers.
    49  // Note that this "tiled" bitmap isn't stored anywhere; it is generated on-the-fly.
    50  //
    51  // For objects without their own span, the type metadata is stored in the first
    52  // word before the object at the beginning of the allocation slot. For objects
    53  // with their own span, the type metadata is stored in the mspan.
    54  //
    55  // The bitmap for small unallocated objects in scannable spans is not maintained
    56  // (can be junk).
    57  
    58  package runtime