|
| 1 | +// Copyright 2025 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 | +package gc |
| 6 | + |
| 7 | +const ( |
| 8 | + ptrBits = 8 * PtrSize |
| 9 | + |
| 10 | + // A malloc header is functionally a single type pointer, but |
| 11 | + // we need to use 8 here to ensure 8-byte alignment of allocations |
| 12 | + // on 32-bit platforms. It's wasteful, but a lot of code relies on |
| 13 | + // 8-byte alignment for 8-byte atomics. |
| 14 | + MallocHeaderSize = 8 |
| 15 | + |
| 16 | + // The minimum object size that has a malloc header, exclusive. |
| 17 | + // |
| 18 | + // The size of this value controls overheads from the malloc header. |
| 19 | + // The minimum size is bound by writeHeapBitsSmall, which assumes that the |
| 20 | + // pointer bitmap for objects of a size smaller than this doesn't cross |
| 21 | + // more than one pointer-word boundary. This sets an upper-bound on this |
| 22 | + // value at the number of bits in a uintptr, multiplied by the pointer |
| 23 | + // size in bytes. |
| 24 | + // |
| 25 | + // We choose a value here that has a natural cutover point in terms of memory |
| 26 | + // overheads. This value just happens to be the maximum possible value this |
| 27 | + // can be. |
| 28 | + // |
| 29 | + // A span with heap bits in it will have 128 bytes of heap bits on 64-bit |
| 30 | + // platforms, and 256 bytes of heap bits on 32-bit platforms. The first size |
| 31 | + // class where malloc headers match this overhead for 64-bit platforms is |
| 32 | + // 512 bytes (8 KiB / 512 bytes * 8 bytes-per-header = 128 bytes of overhead). |
| 33 | + // On 32-bit platforms, this same point is the 256 byte size class |
| 34 | + // (8 KiB / 256 bytes * 8 bytes-per-header = 256 bytes of overhead). |
| 35 | + // |
| 36 | + // Guaranteed to be exactly at a size class boundary. The reason this value is |
| 37 | + // an exclusive minimum is subtle. Suppose we're allocating a 504-byte object |
| 38 | + // and its rounded up to 512 bytes for the size class. If minSizeForMallocHeader |
| 39 | + // is 512 and an inclusive minimum, then a comparison against minSizeForMallocHeader |
| 40 | + // by the two values would produce different results. In other words, the comparison |
| 41 | + // would not be invariant to size-class rounding. Eschewing this property means a |
| 42 | + // more complex check or possibly storing additional state to determine whether a |
| 43 | + // span has malloc headers. |
| 44 | + MinSizeForMallocHeader = PtrSize * ptrBits |
| 45 | + |
| 46 | + // PageSize is the increment in which spans are managed. |
| 47 | + PageSize = 1 << PageShift |
| 48 | + |
| 49 | + PtrSize = 4 << (^uintptr(0) >> 63) |
| 50 | +) |
0 commit comments