Source file src/runtime/malloc.go

     1  // Copyright 2014 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  // Memory allocator.
     6  //
     7  // This was originally based on tcmalloc, but has diverged quite a bit.
     8  // http://goog-perftools.sourceforge.net/doc/tcmalloc.html
     9  
    10  // The main allocator works in runs of pages.
    11  // Small allocation sizes (up to and including 32 kB) are
    12  // rounded to one of about 70 size classes, each of which
    13  // has its own free set of objects of exactly that size.
    14  // Any free page of memory can be split into a set of objects
    15  // of one size class, which are then managed using a free bitmap.
    16  //
    17  // The allocator's data structures are:
    18  //
    19  //	fixalloc: a free-list allocator for fixed-size off-heap objects,
    20  //		used to manage storage used by the allocator.
    21  //	mheap: the malloc heap, managed at page (8192-byte) granularity.
    22  //	mspan: a run of in-use pages managed by the mheap.
    23  //	mcentral: collects all spans of a given size class.
    24  //	mcache: a per-P cache of mspans with free space.
    25  //	mstats: allocation statistics.
    26  //
    27  // Allocating a small object proceeds up a hierarchy of caches:
    28  //
    29  //	1. Round the size up to one of the small size classes
    30  //	   and look in the corresponding mspan in this P's mcache.
    31  //	   Scan the mspan's free bitmap to find a free slot.
    32  //	   If there is a free slot, allocate it.
    33  //	   This can all be done without acquiring a lock.
    34  //
    35  //	2. If the mspan has no free slots, obtain a new mspan
    36  //	   from the mcentral's list of mspans of the required size
    37  //	   class that have free space.
    38  //	   Obtaining a whole span amortizes the cost of locking
    39  //	   the mcentral.
    40  //
    41  //	3. If the mcentral's mspan list is empty, obtain a run
    42  //	   of pages from the mheap to use for the mspan.
    43  //
    44  //	4. If the mheap is empty or has no page runs large enough,
    45  //	   allocate a new group of pages (at least 1MB) from the
    46  //	   operating system. Allocating a large run of pages
    47  //	   amortizes the cost of talking to the operating system.
    48  //
    49  // Sweeping an mspan and freeing objects on it proceeds up a similar
    50  // hierarchy:
    51  //
    52  //	1. If the mspan is being swept in response to allocation, it
    53  //	   is returned to the mcache to satisfy the allocation.
    54  //
    55  //	2. Otherwise, if the mspan still has allocated objects in it,
    56  //	   it is placed on the mcentral free list for the mspan's size
    57  //	   class.
    58  //
    59  //	3. Otherwise, if all objects in the mspan are free, the mspan's
    60  //	   pages are returned to the mheap and the mspan is now dead.
    61  //
    62  // Allocating and freeing a large object uses the mheap
    63  // directly, bypassing the mcache and mcentral.
    64  //
    65  // If mspan.needzero is false, then free object slots in the mspan are
    66  // already zeroed. Otherwise if needzero is true, objects are zeroed as
    67  // they are allocated. There are various benefits to delaying zeroing
    68  // this way:
    69  //
    70  //	1. Stack frame allocation can avoid zeroing altogether.
    71  //
    72  //	2. It exhibits better temporal locality, since the program is
    73  //	   probably about to write to the memory.
    74  //
    75  //	3. We don't zero pages that never get reused.
    76  
    77  // Virtual memory layout
    78  //
    79  // The heap consists of a set of arenas, which are 64MB on 64-bit and
    80  // 4MB on 32-bit (heapArenaBytes). Each arena's start address is also
    81  // aligned to the arena size.
    82  //
    83  // Each arena has an associated heapArena object that stores the
    84  // metadata for that arena: the heap bitmap for all words in the arena
    85  // and the span map for all pages in the arena. heapArena objects are
    86  // themselves allocated off-heap.
    87  //
    88  // Since arenas are aligned, the address space can be viewed as a
    89  // series of arena frames. The arena map (mheap_.arenas) maps from
    90  // arena frame number to *heapArena, or nil for parts of the address
    91  // space not backed by the Go heap. The arena map is structured as a
    92  // two-level array consisting of a "L1" arena map and many "L2" arena
    93  // maps; however, since arenas are large, on many architectures, the
    94  // arena map consists of a single, large L2 map.
    95  //
    96  // The arena map covers the entire possible address space, allowing
    97  // the Go heap to use any part of the address space. The allocator
    98  // attempts to keep arenas contiguous so that large spans (and hence
    99  // large objects) can cross arenas.
   100  
   101  package runtime
   102  
   103  import (
   104  	"internal/goarch"
   105  	"internal/goos"
   106  	"internal/runtime/atomic"
   107  	"runtime/internal/math"
   108  	"runtime/internal/sys"
   109  	"unsafe"
   110  )
   111  
   112  const (
   113  	maxTinySize   = _TinySize
   114  	tinySizeClass = _TinySizeClass
   115  	maxSmallSize  = _MaxSmallSize
   116  
   117  	pageShift = _PageShift
   118  	pageSize  = _PageSize
   119  
   120  	_PageSize = 1 << _PageShift
   121  	_PageMask = _PageSize - 1
   122  
   123  	// _64bit = 1 on 64-bit systems, 0 on 32-bit systems
   124  	_64bit = 1 << (^uintptr(0) >> 63) / 2
   125  
   126  	// Tiny allocator parameters, see "Tiny allocator" comment in malloc.go.
   127  	_TinySize      = 16
   128  	_TinySizeClass = int8(2)
   129  
   130  	_FixAllocChunk = 16 << 10 // Chunk size for FixAlloc
   131  
   132  	// Per-P, per order stack segment cache size.
   133  	_StackCacheSize = 32 * 1024
   134  
   135  	// Number of orders that get caching. Order 0 is FixedStack
   136  	// and each successive order is twice as large.
   137  	// We want to cache 2KB, 4KB, 8KB, and 16KB stacks. Larger stacks
   138  	// will be allocated directly.
   139  	// Since FixedStack is different on different systems, we
   140  	// must vary NumStackOrders to keep the same maximum cached size.
   141  	//   OS               | FixedStack | NumStackOrders
   142  	//   -----------------+------------+---------------
   143  	//   linux/darwin/bsd | 2KB        | 4
   144  	//   windows/32       | 4KB        | 3
   145  	//   windows/64       | 8KB        | 2
   146  	//   plan9            | 4KB        | 3
   147  	_NumStackOrders = 4 - goarch.PtrSize/4*goos.IsWindows - 1*goos.IsPlan9
   148  
   149  	// heapAddrBits is the number of bits in a heap address. On
   150  	// amd64, addresses are sign-extended beyond heapAddrBits. On
   151  	// other arches, they are zero-extended.
   152  	//
   153  	// On most 64-bit platforms, we limit this to 48 bits based on a
   154  	// combination of hardware and OS limitations.
   155  	//
   156  	// amd64 hardware limits addresses to 48 bits, sign-extended
   157  	// to 64 bits. Addresses where the top 16 bits are not either
   158  	// all 0 or all 1 are "non-canonical" and invalid. Because of
   159  	// these "negative" addresses, we offset addresses by 1<<47
   160  	// (arenaBaseOffset) on amd64 before computing indexes into
   161  	// the heap arenas index. In 2017, amd64 hardware added
   162  	// support for 57 bit addresses; however, currently only Linux
   163  	// supports this extension and the kernel will never choose an
   164  	// address above 1<<47 unless mmap is called with a hint
   165  	// address above 1<<47 (which we never do).
   166  	//
   167  	// arm64 hardware (as of ARMv8) limits user addresses to 48
   168  	// bits, in the range [0, 1<<48).
   169  	//
   170  	// ppc64, mips64, and s390x support arbitrary 64 bit addresses
   171  	// in hardware. On Linux, Go leans on stricter OS limits. Based
   172  	// on Linux's processor.h, the user address space is limited as
   173  	// follows on 64-bit architectures:
   174  	//
   175  	// Architecture  Name              Maximum Value (exclusive)
   176  	// ---------------------------------------------------------------------
   177  	// amd64         TASK_SIZE_MAX     0x007ffffffff000 (47 bit addresses)
   178  	// arm64         TASK_SIZE_64      0x01000000000000 (48 bit addresses)
   179  	// ppc64{,le}    TASK_SIZE_USER64  0x00400000000000 (46 bit addresses)
   180  	// mips64{,le}   TASK_SIZE64       0x00010000000000 (40 bit addresses)
   181  	// s390x         TASK_SIZE         1<<64 (64 bit addresses)
   182  	//
   183  	// These limits may increase over time, but are currently at
   184  	// most 48 bits except on s390x. On all architectures, Linux
   185  	// starts placing mmap'd regions at addresses that are
   186  	// significantly below 48 bits, so even if it's possible to
   187  	// exceed Go's 48 bit limit, it's extremely unlikely in
   188  	// practice.
   189  	//
   190  	// On 32-bit platforms, we accept the full 32-bit address
   191  	// space because doing so is cheap.
   192  	// mips32 only has access to the low 2GB of virtual memory, so
   193  	// we further limit it to 31 bits.
   194  	//
   195  	// On ios/arm64, although 64-bit pointers are presumably
   196  	// available, pointers are truncated to 33 bits in iOS <14.
   197  	// Furthermore, only the top 4 GiB of the address space are
   198  	// actually available to the application. In iOS >=14, more
   199  	// of the address space is available, and the OS can now
   200  	// provide addresses outside of those 33 bits. Pick 40 bits
   201  	// as a reasonable balance between address space usage by the
   202  	// page allocator, and flexibility for what mmap'd regions
   203  	// we'll accept for the heap. We can't just move to the full
   204  	// 48 bits because this uses too much address space for older
   205  	// iOS versions.
   206  	// TODO(mknyszek): Once iOS <14 is deprecated, promote ios/arm64
   207  	// to a 48-bit address space like every other arm64 platform.
   208  	//
   209  	// WebAssembly currently has a limit of 4GB linear memory.
   210  	heapAddrBits = (_64bit*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64))*48 + (1-_64bit+goarch.IsWasm)*(32-(goarch.IsMips+goarch.IsMipsle)) + 40*goos.IsIos*goarch.IsArm64
   211  
   212  	// maxAlloc is the maximum size of an allocation. On 64-bit,
   213  	// it's theoretically possible to allocate 1<<heapAddrBits bytes. On
   214  	// 32-bit, however, this is one less than 1<<32 because the
   215  	// number of bytes in the address space doesn't actually fit
   216  	// in a uintptr.
   217  	maxAlloc = (1 << heapAddrBits) - (1-_64bit)*1
   218  
   219  	// The number of bits in a heap address, the size of heap
   220  	// arenas, and the L1 and L2 arena map sizes are related by
   221  	//
   222  	//   (1 << addr bits) = arena size * L1 entries * L2 entries
   223  	//
   224  	// Currently, we balance these as follows:
   225  	//
   226  	//       Platform  Addr bits  Arena size  L1 entries   L2 entries
   227  	// --------------  ---------  ----------  ----------  -----------
   228  	//       */64-bit         48        64MB           1    4M (32MB)
   229  	// windows/64-bit         48         4MB          64    1M  (8MB)
   230  	//      ios/arm64         33         4MB           1  2048  (8KB)
   231  	//       */32-bit         32         4MB           1  1024  (4KB)
   232  	//     */mips(le)         31         4MB           1   512  (2KB)
   233  
   234  	// heapArenaBytes is the size of a heap arena. The heap
   235  	// consists of mappings of size heapArenaBytes, aligned to
   236  	// heapArenaBytes. The initial heap mapping is one arena.
   237  	//
   238  	// This is currently 64MB on 64-bit non-Windows and 4MB on
   239  	// 32-bit and on Windows. We use smaller arenas on Windows
   240  	// because all committed memory is charged to the process,
   241  	// even if it's not touched. Hence, for processes with small
   242  	// heaps, the mapped arena space needs to be commensurate.
   243  	// This is particularly important with the race detector,
   244  	// since it significantly amplifies the cost of committed
   245  	// memory.
   246  	heapArenaBytes = 1 << logHeapArenaBytes
   247  
   248  	heapArenaWords = heapArenaBytes / goarch.PtrSize
   249  
   250  	// logHeapArenaBytes is log_2 of heapArenaBytes. For clarity,
   251  	// prefer using heapArenaBytes where possible (we need the
   252  	// constant to compute some other constants).
   253  	logHeapArenaBytes = (6+20)*(_64bit*(1-goos.IsWindows)*(1-goarch.IsWasm)*(1-goos.IsIos*goarch.IsArm64)) + (2+20)*(_64bit*goos.IsWindows) + (2+20)*(1-_64bit) + (2+20)*goarch.IsWasm + (2+20)*goos.IsIos*goarch.IsArm64
   254  
   255  	// heapArenaBitmapWords is the size of each heap arena's bitmap in uintptrs.
   256  	heapArenaBitmapWords = heapArenaWords / (8 * goarch.PtrSize)
   257  
   258  	pagesPerArena = heapArenaBytes / pageSize
   259  
   260  	// arenaL1Bits is the number of bits of the arena number
   261  	// covered by the first level arena map.
   262  	//
   263  	// This number should be small, since the first level arena
   264  	// map requires PtrSize*(1<<arenaL1Bits) of space in the
   265  	// binary's BSS. It can be zero, in which case the first level
   266  	// index is effectively unused. There is a performance benefit
   267  	// to this, since the generated code can be more efficient,
   268  	// but comes at the cost of having a large L2 mapping.
   269  	//
   270  	// We use the L1 map on 64-bit Windows because the arena size
   271  	// is small, but the address space is still 48 bits, and
   272  	// there's a high cost to having a large L2.
   273  	arenaL1Bits = 6 * (_64bit * goos.IsWindows)
   274  
   275  	// arenaL2Bits is the number of bits of the arena number
   276  	// covered by the second level arena index.
   277  	//
   278  	// The size of each arena map allocation is proportional to
   279  	// 1<<arenaL2Bits, so it's important that this not be too
   280  	// large. 48 bits leads to 32MB arena index allocations, which
   281  	// is about the practical threshold.
   282  	arenaL2Bits = heapAddrBits - logHeapArenaBytes - arenaL1Bits
   283  
   284  	// arenaL1Shift is the number of bits to shift an arena frame
   285  	// number by to compute an index into the first level arena map.
   286  	arenaL1Shift = arenaL2Bits
   287  
   288  	// arenaBits is the total bits in a combined arena map index.
   289  	// This is split between the index into the L1 arena map and
   290  	// the L2 arena map.
   291  	arenaBits = arenaL1Bits + arenaL2Bits
   292  
   293  	// arenaBaseOffset is the pointer value that corresponds to
   294  	// index 0 in the heap arena map.
   295  	//
   296  	// On amd64, the address space is 48 bits, sign extended to 64
   297  	// bits. This offset lets us handle "negative" addresses (or
   298  	// high addresses if viewed as unsigned).
   299  	//
   300  	// On aix/ppc64, this offset allows to keep the heapAddrBits to
   301  	// 48. Otherwise, it would be 60 in order to handle mmap addresses
   302  	// (in range 0x0a00000000000000 - 0x0afffffffffffff). But in this
   303  	// case, the memory reserved in (s *pageAlloc).init for chunks
   304  	// is causing important slowdowns.
   305  	//
   306  	// On other platforms, the user address space is contiguous
   307  	// and starts at 0, so no offset is necessary.
   308  	arenaBaseOffset = 0xffff800000000000*goarch.IsAmd64 + 0x0a00000000000000*goos.IsAix
   309  	// A typed version of this constant that will make it into DWARF (for viewcore).
   310  	arenaBaseOffsetUintptr = uintptr(arenaBaseOffset)
   311  
   312  	// Max number of threads to run garbage collection.
   313  	// 2, 3, and 4 are all plausible maximums depending
   314  	// on the hardware details of the machine. The garbage
   315  	// collector scales well to 32 cpus.
   316  	_MaxGcproc = 32
   317  
   318  	// minLegalPointer is the smallest possible legal pointer.
   319  	// This is the smallest possible architectural page size,
   320  	// since we assume that the first page is never mapped.
   321  	//
   322  	// This should agree with minZeroPage in the compiler.
   323  	minLegalPointer uintptr = 4096
   324  
   325  	// minHeapForMetadataHugePages sets a threshold on when certain kinds of
   326  	// heap metadata, currently the arenas map L2 entries and page alloc bitmap
   327  	// mappings, are allowed to be backed by huge pages. If the heap goal ever
   328  	// exceeds this threshold, then huge pages are enabled.
   329  	//
   330  	// These numbers are chosen with the assumption that huge pages are on the
   331  	// order of a few MiB in size.
   332  	//
   333  	// The kind of metadata this applies to has a very low overhead when compared
   334  	// to address space used, but their constant overheads for small heaps would
   335  	// be very high if they were to be backed by huge pages (e.g. a few MiB makes
   336  	// a huge difference for an 8 MiB heap, but barely any difference for a 1 GiB
   337  	// heap). The benefit of huge pages is also not worth it for small heaps,
   338  	// because only a very, very small part of the metadata is used for small heaps.
   339  	//
   340  	// N.B. If the heap goal exceeds the threshold then shrinks to a very small size
   341  	// again, then huge pages will still be enabled for this mapping. The reason is that
   342  	// there's no point unless we're also returning the physical memory for these
   343  	// metadata mappings back to the OS. That would be quite complex to do in general
   344  	// as the heap is likely fragmented after a reduction in heap size.
   345  	minHeapForMetadataHugePages = 1 << 30
   346  )
   347  
   348  // physPageSize is the size in bytes of the OS's physical pages.
   349  // Mapping and unmapping operations must be done at multiples of
   350  // physPageSize.
   351  //
   352  // This must be set by the OS init code (typically in osinit) before
   353  // mallocinit.
   354  var physPageSize uintptr
   355  
   356  // physHugePageSize is the size in bytes of the OS's default physical huge
   357  // page size whose allocation is opaque to the application. It is assumed
   358  // and verified to be a power of two.
   359  //
   360  // If set, this must be set by the OS init code (typically in osinit) before
   361  // mallocinit. However, setting it at all is optional, and leaving the default
   362  // value is always safe (though potentially less efficient).
   363  //
   364  // Since physHugePageSize is always assumed to be a power of two,
   365  // physHugePageShift is defined as physHugePageSize == 1 << physHugePageShift.
   366  // The purpose of physHugePageShift is to avoid doing divisions in
   367  // performance critical functions.
   368  var (
   369  	physHugePageSize  uintptr
   370  	physHugePageShift uint
   371  )
   372  
   373  func mallocinit() {
   374  	if class_to_size[_TinySizeClass] != _TinySize {
   375  		throw("bad TinySizeClass")
   376  	}
   377  
   378  	if heapArenaBitmapWords&(heapArenaBitmapWords-1) != 0 {
   379  		// heapBits expects modular arithmetic on bitmap
   380  		// addresses to work.
   381  		throw("heapArenaBitmapWords not a power of 2")
   382  	}
   383  
   384  	// Check physPageSize.
   385  	if physPageSize == 0 {
   386  		// The OS init code failed to fetch the physical page size.
   387  		throw("failed to get system page size")
   388  	}
   389  	if physPageSize > maxPhysPageSize {
   390  		print("system page size (", physPageSize, ") is larger than maximum page size (", maxPhysPageSize, ")\n")
   391  		throw("bad system page size")
   392  	}
   393  	if physPageSize < minPhysPageSize {
   394  		print("system page size (", physPageSize, ") is smaller than minimum page size (", minPhysPageSize, ")\n")
   395  		throw("bad system page size")
   396  	}
   397  	if physPageSize&(physPageSize-1) != 0 {
   398  		print("system page size (", physPageSize, ") must be a power of 2\n")
   399  		throw("bad system page size")
   400  	}
   401  	if physHugePageSize&(physHugePageSize-1) != 0 {
   402  		print("system huge page size (", physHugePageSize, ") must be a power of 2\n")
   403  		throw("bad system huge page size")
   404  	}
   405  	if physHugePageSize > maxPhysHugePageSize {
   406  		// physHugePageSize is greater than the maximum supported huge page size.
   407  		// Don't throw here, like in the other cases, since a system configured
   408  		// in this way isn't wrong, we just don't have the code to support them.
   409  		// Instead, silently set the huge page size to zero.
   410  		physHugePageSize = 0
   411  	}
   412  	if physHugePageSize != 0 {
   413  		// Since physHugePageSize is a power of 2, it suffices to increase
   414  		// physHugePageShift until 1<<physHugePageShift == physHugePageSize.
   415  		for 1<<physHugePageShift != physHugePageSize {
   416  			physHugePageShift++
   417  		}
   418  	}
   419  	if pagesPerArena%pagesPerSpanRoot != 0 {
   420  		print("pagesPerArena (", pagesPerArena, ") is not divisible by pagesPerSpanRoot (", pagesPerSpanRoot, ")\n")
   421  		throw("bad pagesPerSpanRoot")
   422  	}
   423  	if pagesPerArena%pagesPerReclaimerChunk != 0 {
   424  		print("pagesPerArena (", pagesPerArena, ") is not divisible by pagesPerReclaimerChunk (", pagesPerReclaimerChunk, ")\n")
   425  		throw("bad pagesPerReclaimerChunk")
   426  	}
   427  	// Check that the minimum size (exclusive) for a malloc header is also
   428  	// a size class boundary. This is important to making sure checks align
   429  	// across different parts of the runtime.
   430  	minSizeForMallocHeaderIsSizeClass := false
   431  	for i := 0; i < len(class_to_size); i++ {
   432  		if minSizeForMallocHeader == uintptr(class_to_size[i]) {
   433  			minSizeForMallocHeaderIsSizeClass = true
   434  			break
   435  		}
   436  	}
   437  	if !minSizeForMallocHeaderIsSizeClass {
   438  		throw("min size of malloc header is not a size class boundary")
   439  	}
   440  	// Check that the pointer bitmap for all small sizes without a malloc header
   441  	// fits in a word.
   442  	if minSizeForMallocHeader/goarch.PtrSize > 8*goarch.PtrSize {
   443  		throw("max pointer/scan bitmap size for headerless objects is too large")
   444  	}
   445  
   446  	if minTagBits > taggedPointerBits {
   447  		throw("taggedPointerbits too small")
   448  	}
   449  
   450  	// Initialize the heap.
   451  	mheap_.init()
   452  	mcache0 = allocmcache()
   453  	lockInit(&gcBitsArenas.lock, lockRankGcBitsArenas)
   454  	lockInit(&profInsertLock, lockRankProfInsert)
   455  	lockInit(&profBlockLock, lockRankProfBlock)
   456  	lockInit(&profMemActiveLock, lockRankProfMemActive)
   457  	for i := range profMemFutureLock {
   458  		lockInit(&profMemFutureLock[i], lockRankProfMemFuture)
   459  	}
   460  	lockInit(&globalAlloc.mutex, lockRankGlobalAlloc)
   461  
   462  	// Create initial arena growth hints.
   463  	if goarch.PtrSize == 8 {
   464  		// On a 64-bit machine, we pick the following hints
   465  		// because:
   466  		//
   467  		// 1. Starting from the middle of the address space
   468  		// makes it easier to grow out a contiguous range
   469  		// without running in to some other mapping.
   470  		//
   471  		// 2. This makes Go heap addresses more easily
   472  		// recognizable when debugging.
   473  		//
   474  		// 3. Stack scanning in gccgo is still conservative,
   475  		// so it's important that addresses be distinguishable
   476  		// from other data.
   477  		//
   478  		// Starting at 0x00c0 means that the valid memory addresses
   479  		// will begin 0x00c0, 0x00c1, ...
   480  		// In little-endian, that's c0 00, c1 00, ... None of those are valid
   481  		// UTF-8 sequences, and they are otherwise as far away from
   482  		// ff (likely a common byte) as possible. If that fails, we try other 0xXXc0
   483  		// addresses. An earlier attempt to use 0x11f8 caused out of memory errors
   484  		// on OS X during thread allocations.  0x00c0 causes conflicts with
   485  		// AddressSanitizer which reserves all memory up to 0x0100.
   486  		// These choices reduce the odds of a conservative garbage collector
   487  		// not collecting memory because some non-pointer block of memory
   488  		// had a bit pattern that matched a memory address.
   489  		//
   490  		// However, on arm64, we ignore all this advice above and slam the
   491  		// allocation at 0x40 << 32 because when using 4k pages with 3-level
   492  		// translation buffers, the user address space is limited to 39 bits
   493  		// On ios/arm64, the address space is even smaller.
   494  		//
   495  		// On AIX, mmaps starts at 0x0A00000000000000 for 64-bit.
   496  		// processes.
   497  		//
   498  		// Space mapped for user arenas comes immediately after the range
   499  		// originally reserved for the regular heap when race mode is not
   500  		// enabled because user arena chunks can never be used for regular heap
   501  		// allocations and we want to avoid fragmenting the address space.
   502  		//
   503  		// In race mode we have no choice but to just use the same hints because
   504  		// the race detector requires that the heap be mapped contiguously.
   505  		for i := 0x7f; i >= 0; i-- {
   506  			var p uintptr
   507  			switch {
   508  			case raceenabled:
   509  				// The TSAN runtime requires the heap
   510  				// to be in the range [0x00c000000000,
   511  				// 0x00e000000000).
   512  				p = uintptr(i)<<32 | uintptrMask&(0x00c0<<32)
   513  				if p >= uintptrMask&0x00e000000000 {
   514  					continue
   515  				}
   516  			case GOARCH == "arm64" && GOOS == "ios":
   517  				p = uintptr(i)<<40 | uintptrMask&(0x0013<<28)
   518  			case GOARCH == "arm64":
   519  				p = uintptr(i)<<40 | uintptrMask&(0x0040<<32)
   520  			case GOOS == "aix":
   521  				if i == 0 {
   522  					// We don't use addresses directly after 0x0A00000000000000
   523  					// to avoid collisions with others mmaps done by non-go programs.
   524  					continue
   525  				}
   526  				p = uintptr(i)<<40 | uintptrMask&(0xa0<<52)
   527  			default:
   528  				p = uintptr(i)<<40 | uintptrMask&(0x00c0<<32)
   529  			}
   530  			// Switch to generating hints for user arenas if we've gone
   531  			// through about half the hints. In race mode, take only about
   532  			// a quarter; we don't have very much space to work with.
   533  			hintList := &mheap_.arenaHints
   534  			if (!raceenabled && i > 0x3f) || (raceenabled && i > 0x5f) {
   535  				hintList = &mheap_.userArena.arenaHints
   536  			}
   537  			hint := (*arenaHint)(mheap_.arenaHintAlloc.alloc())
   538  			hint.addr = p
   539  			hint.next, *hintList = *hintList, hint
   540  		}
   541  	} else {
   542  		// On a 32-bit machine, we're much more concerned
   543  		// about keeping the usable heap contiguous.
   544  		// Hence:
   545  		//
   546  		// 1. We reserve space for all heapArenas up front so
   547  		// they don't get interleaved with the heap. They're
   548  		// ~258MB, so this isn't too bad. (We could reserve a
   549  		// smaller amount of space up front if this is a
   550  		// problem.)
   551  		//
   552  		// 2. We hint the heap to start right above the end of
   553  		// the binary so we have the best chance of keeping it
   554  		// contiguous.
   555  		//
   556  		// 3. We try to stake out a reasonably large initial
   557  		// heap reservation.
   558  
   559  		const arenaMetaSize = (1 << arenaBits) * unsafe.Sizeof(heapArena{})
   560  		meta := uintptr(sysReserve(nil, arenaMetaSize))
   561  		if meta != 0 {
   562  			mheap_.heapArenaAlloc.init(meta, arenaMetaSize, true)
   563  		}
   564  
   565  		// We want to start the arena low, but if we're linked
   566  		// against C code, it's possible global constructors
   567  		// have called malloc and adjusted the process' brk.
   568  		// Query the brk so we can avoid trying to map the
   569  		// region over it (which will cause the kernel to put
   570  		// the region somewhere else, likely at a high
   571  		// address).
   572  		procBrk := sbrk0()
   573  
   574  		// If we ask for the end of the data segment but the
   575  		// operating system requires a little more space
   576  		// before we can start allocating, it will give out a
   577  		// slightly higher pointer. Except QEMU, which is
   578  		// buggy, as usual: it won't adjust the pointer
   579  		// upward. So adjust it upward a little bit ourselves:
   580  		// 1/4 MB to get away from the running binary image.
   581  		p := firstmoduledata.end
   582  		if p < procBrk {
   583  			p = procBrk
   584  		}
   585  		if mheap_.heapArenaAlloc.next <= p && p < mheap_.heapArenaAlloc.end {
   586  			p = mheap_.heapArenaAlloc.end
   587  		}
   588  		p = alignUp(p+(256<<10), heapArenaBytes)
   589  		// Because we're worried about fragmentation on
   590  		// 32-bit, we try to make a large initial reservation.
   591  		arenaSizes := []uintptr{
   592  			512 << 20,
   593  			256 << 20,
   594  			128 << 20,
   595  		}
   596  		for _, arenaSize := range arenaSizes {
   597  			a, size := sysReserveAligned(unsafe.Pointer(p), arenaSize, heapArenaBytes)
   598  			if a != nil {
   599  				mheap_.arena.init(uintptr(a), size, false)
   600  				p = mheap_.arena.end // For hint below
   601  				break
   602  			}
   603  		}
   604  		hint := (*arenaHint)(mheap_.arenaHintAlloc.alloc())
   605  		hint.addr = p
   606  		hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
   607  
   608  		// Place the hint for user arenas just after the large reservation.
   609  		//
   610  		// While this potentially competes with the hint above, in practice we probably
   611  		// aren't going to be getting this far anyway on 32-bit platforms.
   612  		userArenaHint := (*arenaHint)(mheap_.arenaHintAlloc.alloc())
   613  		userArenaHint.addr = p
   614  		userArenaHint.next, mheap_.userArena.arenaHints = mheap_.userArena.arenaHints, userArenaHint
   615  	}
   616  	// Initialize the memory limit here because the allocator is going to look at it
   617  	// but we haven't called gcinit yet and we're definitely going to allocate memory before then.
   618  	gcController.memoryLimit.Store(maxInt64)
   619  }
   620  
   621  // sysAlloc allocates heap arena space for at least n bytes. The
   622  // returned pointer is always heapArenaBytes-aligned and backed by
   623  // h.arenas metadata. The returned size is always a multiple of
   624  // heapArenaBytes. sysAlloc returns nil on failure.
   625  // There is no corresponding free function.
   626  //
   627  // hintList is a list of hint addresses for where to allocate new
   628  // heap arenas. It must be non-nil.
   629  //
   630  // register indicates whether the heap arena should be registered
   631  // in allArenas.
   632  //
   633  // sysAlloc returns a memory region in the Reserved state. This region must
   634  // be transitioned to Prepared and then Ready before use.
   635  //
   636  // h must be locked.
   637  func (h *mheap) sysAlloc(n uintptr, hintList **arenaHint, register bool) (v unsafe.Pointer, size uintptr) {
   638  	assertLockHeld(&h.lock)
   639  
   640  	n = alignUp(n, heapArenaBytes)
   641  
   642  	if hintList == &h.arenaHints {
   643  		// First, try the arena pre-reservation.
   644  		// Newly-used mappings are considered released.
   645  		//
   646  		// Only do this if we're using the regular heap arena hints.
   647  		// This behavior is only for the heap.
   648  		v = h.arena.alloc(n, heapArenaBytes, &gcController.heapReleased)
   649  		if v != nil {
   650  			size = n
   651  			goto mapped
   652  		}
   653  	}
   654  
   655  	// Try to grow the heap at a hint address.
   656  	for *hintList != nil {
   657  		hint := *hintList
   658  		p := hint.addr
   659  		if hint.down {
   660  			p -= n
   661  		}
   662  		if p+n < p {
   663  			// We can't use this, so don't ask.
   664  			v = nil
   665  		} else if arenaIndex(p+n-1) >= 1<<arenaBits {
   666  			// Outside addressable heap. Can't use.
   667  			v = nil
   668  		} else {
   669  			v = sysReserve(unsafe.Pointer(p), n)
   670  		}
   671  		if p == uintptr(v) {
   672  			// Success. Update the hint.
   673  			if !hint.down {
   674  				p += n
   675  			}
   676  			hint.addr = p
   677  			size = n
   678  			break
   679  		}
   680  		// Failed. Discard this hint and try the next.
   681  		//
   682  		// TODO: This would be cleaner if sysReserve could be
   683  		// told to only return the requested address. In
   684  		// particular, this is already how Windows behaves, so
   685  		// it would simplify things there.
   686  		if v != nil {
   687  			sysFreeOS(v, n)
   688  		}
   689  		*hintList = hint.next
   690  		h.arenaHintAlloc.free(unsafe.Pointer(hint))
   691  	}
   692  
   693  	if size == 0 {
   694  		if raceenabled {
   695  			// The race detector assumes the heap lives in
   696  			// [0x00c000000000, 0x00e000000000), but we
   697  			// just ran out of hints in this region. Give
   698  			// a nice failure.
   699  			throw("too many address space collisions for -race mode")
   700  		}
   701  
   702  		// All of the hints failed, so we'll take any
   703  		// (sufficiently aligned) address the kernel will give
   704  		// us.
   705  		v, size = sysReserveAligned(nil, n, heapArenaBytes)
   706  		if v == nil {
   707  			return nil, 0
   708  		}
   709  
   710  		// Create new hints for extending this region.
   711  		hint := (*arenaHint)(h.arenaHintAlloc.alloc())
   712  		hint.addr, hint.down = uintptr(v), true
   713  		hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
   714  		hint = (*arenaHint)(h.arenaHintAlloc.alloc())
   715  		hint.addr = uintptr(v) + size
   716  		hint.next, mheap_.arenaHints = mheap_.arenaHints, hint
   717  	}
   718  
   719  	// Check for bad pointers or pointers we can't use.
   720  	{
   721  		var bad string
   722  		p := uintptr(v)
   723  		if p+size < p {
   724  			bad = "region exceeds uintptr range"
   725  		} else if arenaIndex(p) >= 1<<arenaBits {
   726  			bad = "base outside usable address space"
   727  		} else if arenaIndex(p+size-1) >= 1<<arenaBits {
   728  			bad = "end outside usable address space"
   729  		}
   730  		if bad != "" {
   731  			// This should be impossible on most architectures,
   732  			// but it would be really confusing to debug.
   733  			print("runtime: memory allocated by OS [", hex(p), ", ", hex(p+size), ") not in usable address space: ", bad, "\n")
   734  			throw("memory reservation exceeds address space limit")
   735  		}
   736  	}
   737  
   738  	if uintptr(v)&(heapArenaBytes-1) != 0 {
   739  		throw("misrounded allocation in sysAlloc")
   740  	}
   741  
   742  mapped:
   743  	// Create arena metadata.
   744  	for ri := arenaIndex(uintptr(v)); ri <= arenaIndex(uintptr(v)+size-1); ri++ {
   745  		l2 := h.arenas[ri.l1()]
   746  		if l2 == nil {
   747  			// Allocate an L2 arena map.
   748  			//
   749  			// Use sysAllocOS instead of sysAlloc or persistentalloc because there's no
   750  			// statistic we can comfortably account for this space in. With this structure,
   751  			// we rely on demand paging to avoid large overheads, but tracking which memory
   752  			// is paged in is too expensive. Trying to account for the whole region means
   753  			// that it will appear like an enormous memory overhead in statistics, even though
   754  			// it is not.
   755  			l2 = (*[1 << arenaL2Bits]*heapArena)(sysAllocOS(unsafe.Sizeof(*l2)))
   756  			if l2 == nil {
   757  				throw("out of memory allocating heap arena map")
   758  			}
   759  			if h.arenasHugePages {
   760  				sysHugePage(unsafe.Pointer(l2), unsafe.Sizeof(*l2))
   761  			} else {
   762  				sysNoHugePage(unsafe.Pointer(l2), unsafe.Sizeof(*l2))
   763  			}
   764  			atomic.StorepNoWB(unsafe.Pointer(&h.arenas[ri.l1()]), unsafe.Pointer(l2))
   765  		}
   766  
   767  		if l2[ri.l2()] != nil {
   768  			throw("arena already initialized")
   769  		}
   770  		var r *heapArena
   771  		r = (*heapArena)(h.heapArenaAlloc.alloc(unsafe.Sizeof(*r), goarch.PtrSize, &memstats.gcMiscSys))
   772  		if r == nil {
   773  			r = (*heapArena)(persistentalloc(unsafe.Sizeof(*r), goarch.PtrSize, &memstats.gcMiscSys))
   774  			if r == nil {
   775  				throw("out of memory allocating heap arena metadata")
   776  			}
   777  		}
   778  
   779  		// Register the arena in allArenas if requested.
   780  		if register {
   781  			if len(h.allArenas) == cap(h.allArenas) {
   782  				size := 2 * uintptr(cap(h.allArenas)) * goarch.PtrSize
   783  				if size == 0 {
   784  					size = physPageSize
   785  				}
   786  				newArray := (*notInHeap)(persistentalloc(size, goarch.PtrSize, &memstats.gcMiscSys))
   787  				if newArray == nil {
   788  					throw("out of memory allocating allArenas")
   789  				}
   790  				oldSlice := h.allArenas
   791  				*(*notInHeapSlice)(unsafe.Pointer(&h.allArenas)) = notInHeapSlice{newArray, len(h.allArenas), int(size / goarch.PtrSize)}
   792  				copy(h.allArenas, oldSlice)
   793  				// Do not free the old backing array because
   794  				// there may be concurrent readers. Since we
   795  				// double the array each time, this can lead
   796  				// to at most 2x waste.
   797  			}
   798  			h.allArenas = h.allArenas[:len(h.allArenas)+1]
   799  			h.allArenas[len(h.allArenas)-1] = ri
   800  		}
   801  
   802  		// Store atomically just in case an object from the
   803  		// new heap arena becomes visible before the heap lock
   804  		// is released (which shouldn't happen, but there's
   805  		// little downside to this).
   806  		atomic.StorepNoWB(unsafe.Pointer(&l2[ri.l2()]), unsafe.Pointer(r))
   807  	}
   808  
   809  	// Tell the race detector about the new heap memory.
   810  	if raceenabled {
   811  		racemapshadow(v, size)
   812  	}
   813  
   814  	return
   815  }
   816  
   817  // sysReserveAligned is like sysReserve, but the returned pointer is
   818  // aligned to align bytes. It may reserve either n or n+align bytes,
   819  // so it returns the size that was reserved.
   820  func sysReserveAligned(v unsafe.Pointer, size, align uintptr) (unsafe.Pointer, uintptr) {
   821  	// Since the alignment is rather large in uses of this
   822  	// function, we're not likely to get it by chance, so we ask
   823  	// for a larger region and remove the parts we don't need.
   824  	retries := 0
   825  retry:
   826  	p := uintptr(sysReserve(v, size+align))
   827  	switch {
   828  	case p == 0:
   829  		return nil, 0
   830  	case p&(align-1) == 0:
   831  		return unsafe.Pointer(p), size + align
   832  	case GOOS == "windows":
   833  		// On Windows we can't release pieces of a
   834  		// reservation, so we release the whole thing and
   835  		// re-reserve the aligned sub-region. This may race,
   836  		// so we may have to try again.
   837  		sysFreeOS(unsafe.Pointer(p), size+align)
   838  		p = alignUp(p, align)
   839  		p2 := sysReserve(unsafe.Pointer(p), size)
   840  		if p != uintptr(p2) {
   841  			// Must have raced. Try again.
   842  			sysFreeOS(p2, size)
   843  			if retries++; retries == 100 {
   844  				throw("failed to allocate aligned heap memory; too many retries")
   845  			}
   846  			goto retry
   847  		}
   848  		// Success.
   849  		return p2, size
   850  	default:
   851  		// Trim off the unaligned parts.
   852  		pAligned := alignUp(p, align)
   853  		sysFreeOS(unsafe.Pointer(p), pAligned-p)
   854  		end := pAligned + size
   855  		endLen := (p + size + align) - end
   856  		if endLen > 0 {
   857  			sysFreeOS(unsafe.Pointer(end), endLen)
   858  		}
   859  		return unsafe.Pointer(pAligned), size
   860  	}
   861  }
   862  
   863  // enableMetadataHugePages enables huge pages for various sources of heap metadata.
   864  //
   865  // A note on latency: for sufficiently small heaps (<10s of GiB) this function will take constant
   866  // time, but may take time proportional to the size of the mapped heap beyond that.
   867  //
   868  // This function is idempotent.
   869  //
   870  // The heap lock must not be held over this operation, since it will briefly acquire
   871  // the heap lock.
   872  //
   873  // Must be called on the system stack because it acquires the heap lock.
   874  //
   875  //go:systemstack
   876  func (h *mheap) enableMetadataHugePages() {
   877  	// Enable huge pages for page structure.
   878  	h.pages.enableChunkHugePages()
   879  
   880  	// Grab the lock and set arenasHugePages if it's not.
   881  	//
   882  	// Once arenasHugePages is set, all new L2 entries will be eligible for
   883  	// huge pages. We'll set all the old entries after we release the lock.
   884  	lock(&h.lock)
   885  	if h.arenasHugePages {
   886  		unlock(&h.lock)
   887  		return
   888  	}
   889  	h.arenasHugePages = true
   890  	unlock(&h.lock)
   891  
   892  	// N.B. The arenas L1 map is quite small on all platforms, so it's fine to
   893  	// just iterate over the whole thing.
   894  	for i := range h.arenas {
   895  		l2 := (*[1 << arenaL2Bits]*heapArena)(atomic.Loadp(unsafe.Pointer(&h.arenas[i])))
   896  		if l2 == nil {
   897  			continue
   898  		}
   899  		sysHugePage(unsafe.Pointer(l2), unsafe.Sizeof(*l2))
   900  	}
   901  }
   902  
   903  // base address for all 0-byte allocations
   904  var zerobase uintptr
   905  
   906  // nextFreeFast returns the next free object if one is quickly available.
   907  // Otherwise it returns 0.
   908  func nextFreeFast(s *mspan) gclinkptr {
   909  	theBit := sys.TrailingZeros64(s.allocCache) // Is there a free object in the allocCache?
   910  	if theBit < 64 {
   911  		result := s.freeindex + uint16(theBit)
   912  		if result < s.nelems {
   913  			freeidx := result + 1
   914  			if freeidx%64 == 0 && freeidx != s.nelems {
   915  				return 0
   916  			}
   917  			s.allocCache >>= uint(theBit + 1)
   918  			s.freeindex = freeidx
   919  			s.allocCount++
   920  			return gclinkptr(uintptr(result)*s.elemsize + s.base())
   921  		}
   922  	}
   923  	return 0
   924  }
   925  
   926  // nextFree returns the next free object from the cached span if one is available.
   927  // Otherwise it refills the cache with a span with an available object and
   928  // returns that object along with a flag indicating that this was a heavy
   929  // weight allocation. If it is a heavy weight allocation the caller must
   930  // determine whether a new GC cycle needs to be started or if the GC is active
   931  // whether this goroutine needs to assist the GC.
   932  //
   933  // Must run in a non-preemptible context since otherwise the owner of
   934  // c could change.
   935  func (c *mcache) nextFree(spc spanClass) (v gclinkptr, s *mspan, shouldhelpgc bool) {
   936  	s = c.alloc[spc]
   937  	shouldhelpgc = false
   938  	freeIndex := s.nextFreeIndex()
   939  	if freeIndex == s.nelems {
   940  		// The span is full.
   941  		if s.allocCount != s.nelems {
   942  			println("runtime: s.allocCount=", s.allocCount, "s.nelems=", s.nelems)
   943  			throw("s.allocCount != s.nelems && freeIndex == s.nelems")
   944  		}
   945  		c.refill(spc)
   946  		shouldhelpgc = true
   947  		s = c.alloc[spc]
   948  
   949  		freeIndex = s.nextFreeIndex()
   950  	}
   951  
   952  	if freeIndex >= s.nelems {
   953  		throw("freeIndex is not valid")
   954  	}
   955  
   956  	v = gclinkptr(uintptr(freeIndex)*s.elemsize + s.base())
   957  	s.allocCount++
   958  	if s.allocCount > s.nelems {
   959  		println("s.allocCount=", s.allocCount, "s.nelems=", s.nelems)
   960  		throw("s.allocCount > s.nelems")
   961  	}
   962  	return
   963  }
   964  
   965  // Allocate an object of size bytes.
   966  // Small objects are allocated from the per-P cache's free lists.
   967  // Large objects (> 32 kB) are allocated straight from the heap.
   968  func mallocgc(size uintptr, typ *_type, needzero bool) unsafe.Pointer {
   969  	if gcphase == _GCmarktermination {
   970  		throw("mallocgc called with gcphase == _GCmarktermination")
   971  	}
   972  
   973  	if size == 0 {
   974  		return unsafe.Pointer(&zerobase)
   975  	}
   976  
   977  	// It's possible for any malloc to trigger sweeping, which may in
   978  	// turn queue finalizers. Record this dynamic lock edge.
   979  	lockRankMayQueueFinalizer()
   980  
   981  	userSize := size
   982  	if asanenabled {
   983  		// Refer to ASAN runtime library, the malloc() function allocates extra memory,
   984  		// the redzone, around the user requested memory region. And the redzones are marked
   985  		// as unaddressable. We perform the same operations in Go to detect the overflows or
   986  		// underflows.
   987  		size += computeRZlog(size)
   988  	}
   989  
   990  	if debug.malloc {
   991  		if debug.sbrk != 0 {
   992  			align := uintptr(16)
   993  			if typ != nil {
   994  				// TODO(austin): This should be just
   995  				//   align = uintptr(typ.align)
   996  				// but that's only 4 on 32-bit platforms,
   997  				// even if there's a uint64 field in typ (see #599).
   998  				// This causes 64-bit atomic accesses to panic.
   999  				// Hence, we use stricter alignment that matches
  1000  				// the normal allocator better.
  1001  				if size&7 == 0 {
  1002  					align = 8
  1003  				} else if size&3 == 0 {
  1004  					align = 4
  1005  				} else if size&1 == 0 {
  1006  					align = 2
  1007  				} else {
  1008  					align = 1
  1009  				}
  1010  			}
  1011  			return persistentalloc(size, align, &memstats.other_sys)
  1012  		}
  1013  
  1014  		if inittrace.active && inittrace.id == getg().goid {
  1015  			// Init functions are executed sequentially in a single goroutine.
  1016  			inittrace.allocs += 1
  1017  		}
  1018  	}
  1019  
  1020  	// assistG is the G to charge for this allocation, or nil if
  1021  	// GC is not currently active.
  1022  	assistG := deductAssistCredit(size)
  1023  
  1024  	// Set mp.mallocing to keep from being preempted by GC.
  1025  	mp := acquirem()
  1026  	if mp.mallocing != 0 {
  1027  		throw("malloc deadlock")
  1028  	}
  1029  	if mp.gsignal == getg() {
  1030  		throw("malloc during signal")
  1031  	}
  1032  	mp.mallocing = 1
  1033  
  1034  	shouldhelpgc := false
  1035  	dataSize := userSize
  1036  	c := getMCache(mp)
  1037  	if c == nil {
  1038  		throw("mallocgc called without a P or outside bootstrapping")
  1039  	}
  1040  	var span *mspan
  1041  	var header **_type
  1042  	var x unsafe.Pointer
  1043  	noscan := typ == nil || !typ.Pointers()
  1044  	// In some cases block zeroing can profitably (for latency reduction purposes)
  1045  	// be delayed till preemption is possible; delayedZeroing tracks that state.
  1046  	delayedZeroing := false
  1047  	// Determine if it's a 'small' object that goes into a size-classed span.
  1048  	//
  1049  	// Note: This comparison looks a little strange, but it exists to smooth out
  1050  	// the crossover between the largest size class and large objects that have
  1051  	// their own spans. The small window of object sizes between maxSmallSize-mallocHeaderSize
  1052  	// and maxSmallSize will be considered large, even though they might fit in
  1053  	// a size class. In practice this is completely fine, since the largest small
  1054  	// size class has a single object in it already, precisely to make the transition
  1055  	// to large objects smooth.
  1056  	if size <= maxSmallSize-mallocHeaderSize {
  1057  		if noscan && size < maxTinySize {
  1058  			// Tiny allocator.
  1059  			//
  1060  			// Tiny allocator combines several tiny allocation requests
  1061  			// into a single memory block. The resulting memory block
  1062  			// is freed when all subobjects are unreachable. The subobjects
  1063  			// must be noscan (don't have pointers), this ensures that
  1064  			// the amount of potentially wasted memory is bounded.
  1065  			//
  1066  			// Size of the memory block used for combining (maxTinySize) is tunable.
  1067  			// Current setting is 16 bytes, which relates to 2x worst case memory
  1068  			// wastage (when all but one subobjects are unreachable).
  1069  			// 8 bytes would result in no wastage at all, but provides less
  1070  			// opportunities for combining.
  1071  			// 32 bytes provides more opportunities for combining,
  1072  			// but can lead to 4x worst case wastage.
  1073  			// The best case winning is 8x regardless of block size.
  1074  			//
  1075  			// Objects obtained from tiny allocator must not be freed explicitly.
  1076  			// So when an object will be freed explicitly, we ensure that
  1077  			// its size >= maxTinySize.
  1078  			//
  1079  			// SetFinalizer has a special case for objects potentially coming
  1080  			// from tiny allocator, it such case it allows to set finalizers
  1081  			// for an inner byte of a memory block.
  1082  			//
  1083  			// The main targets of tiny allocator are small strings and
  1084  			// standalone escaping variables. On a json benchmark
  1085  			// the allocator reduces number of allocations by ~12% and
  1086  			// reduces heap size by ~20%.
  1087  			off := c.tinyoffset
  1088  			// Align tiny pointer for required (conservative) alignment.
  1089  			if size&7 == 0 {
  1090  				off = alignUp(off, 8)
  1091  			} else if goarch.PtrSize == 4 && size == 12 {
  1092  				// Conservatively align 12-byte objects to 8 bytes on 32-bit
  1093  				// systems so that objects whose first field is a 64-bit
  1094  				// value is aligned to 8 bytes and does not cause a fault on
  1095  				// atomic access. See issue 37262.
  1096  				// TODO(mknyszek): Remove this workaround if/when issue 36606
  1097  				// is resolved.
  1098  				off = alignUp(off, 8)
  1099  			} else if size&3 == 0 {
  1100  				off = alignUp(off, 4)
  1101  			} else if size&1 == 0 {
  1102  				off = alignUp(off, 2)
  1103  			}
  1104  			if off+size <= maxTinySize && c.tiny != 0 {
  1105  				// The object fits into existing tiny block.
  1106  				x = unsafe.Pointer(c.tiny + off)
  1107  				c.tinyoffset = off + size
  1108  				c.tinyAllocs++
  1109  				mp.mallocing = 0
  1110  				releasem(mp)
  1111  				return x
  1112  			}
  1113  			// Allocate a new maxTinySize block.
  1114  			span = c.alloc[tinySpanClass]
  1115  			v := nextFreeFast(span)
  1116  			if v == 0 {
  1117  				v, span, shouldhelpgc = c.nextFree(tinySpanClass)
  1118  			}
  1119  			x = unsafe.Pointer(v)
  1120  			(*[2]uint64)(x)[0] = 0
  1121  			(*[2]uint64)(x)[1] = 0
  1122  			// See if we need to replace the existing tiny block with the new one
  1123  			// based on amount of remaining free space.
  1124  			if !raceenabled && (size < c.tinyoffset || c.tiny == 0) {
  1125  				// Note: disabled when race detector is on, see comment near end of this function.
  1126  				c.tiny = uintptr(x)
  1127  				c.tinyoffset = size
  1128  			}
  1129  			size = maxTinySize
  1130  		} else {
  1131  			hasHeader := !noscan && !heapBitsInSpan(size)
  1132  			if hasHeader {
  1133  				size += mallocHeaderSize
  1134  			}
  1135  			var sizeclass uint8
  1136  			if size <= smallSizeMax-8 {
  1137  				sizeclass = size_to_class8[divRoundUp(size, smallSizeDiv)]
  1138  			} else {
  1139  				sizeclass = size_to_class128[divRoundUp(size-smallSizeMax, largeSizeDiv)]
  1140  			}
  1141  			size = uintptr(class_to_size[sizeclass])
  1142  			spc := makeSpanClass(sizeclass, noscan)
  1143  			span = c.alloc[spc]
  1144  			v := nextFreeFast(span)
  1145  			if v == 0 {
  1146  				v, span, shouldhelpgc = c.nextFree(spc)
  1147  			}
  1148  			x = unsafe.Pointer(v)
  1149  			if needzero && span.needzero != 0 {
  1150  				memclrNoHeapPointers(x, size)
  1151  			}
  1152  			if hasHeader {
  1153  				header = (**_type)(x)
  1154  				x = add(x, mallocHeaderSize)
  1155  				size -= mallocHeaderSize
  1156  			}
  1157  		}
  1158  	} else {
  1159  		shouldhelpgc = true
  1160  		// For large allocations, keep track of zeroed state so that
  1161  		// bulk zeroing can be happen later in a preemptible context.
  1162  		span = c.allocLarge(size, noscan)
  1163  		span.freeindex = 1
  1164  		span.allocCount = 1
  1165  		size = span.elemsize
  1166  		x = unsafe.Pointer(span.base())
  1167  		if needzero && span.needzero != 0 {
  1168  			delayedZeroing = true
  1169  		}
  1170  		if !noscan {
  1171  			// Tell the GC not to look at this yet.
  1172  			span.largeType = nil
  1173  			header = &span.largeType
  1174  		}
  1175  	}
  1176  	if !noscan && !delayedZeroing {
  1177  		c.scanAlloc += heapSetType(uintptr(x), dataSize, typ, header, span)
  1178  	}
  1179  
  1180  	// Ensure that the stores above that initialize x to
  1181  	// type-safe memory and set the heap bits occur before
  1182  	// the caller can make x observable to the garbage
  1183  	// collector. Otherwise, on weakly ordered machines,
  1184  	// the garbage collector could follow a pointer to x,
  1185  	// but see uninitialized memory or stale heap bits.
  1186  	publicationBarrier()
  1187  	// As x and the heap bits are initialized, update
  1188  	// freeIndexForScan now so x is seen by the GC
  1189  	// (including conservative scan) as an allocated object.
  1190  	// While this pointer can't escape into user code as a
  1191  	// _live_ pointer until we return, conservative scanning
  1192  	// may find a dead pointer that happens to point into this
  1193  	// object. Delaying this update until now ensures that
  1194  	// conservative scanning considers this pointer dead until
  1195  	// this point.
  1196  	span.freeIndexForScan = span.freeindex
  1197  
  1198  	// Allocate black during GC.
  1199  	// All slots hold nil so no scanning is needed.
  1200  	// This may be racing with GC so do it atomically if there can be
  1201  	// a race marking the bit.
  1202  	if gcphase != _GCoff {
  1203  		gcmarknewobject(span, uintptr(x))
  1204  	}
  1205  
  1206  	if raceenabled {
  1207  		racemalloc(x, size)
  1208  	}
  1209  
  1210  	if msanenabled {
  1211  		msanmalloc(x, size)
  1212  	}
  1213  
  1214  	if asanenabled {
  1215  		// We should only read/write the memory with the size asked by the user.
  1216  		// The rest of the allocated memory should be poisoned, so that we can report
  1217  		// errors when accessing poisoned memory.
  1218  		// The allocated memory is larger than required userSize, it will also include
  1219  		// redzone and some other padding bytes.
  1220  		rzBeg := unsafe.Add(x, userSize)
  1221  		asanpoison(rzBeg, size-userSize)
  1222  		asanunpoison(x, userSize)
  1223  	}
  1224  
  1225  	// TODO(mknyszek): We should really count the header as part
  1226  	// of gc_sys or something. The code below just pretends it is
  1227  	// internal fragmentation and matches the GC's accounting by
  1228  	// using the whole allocation slot.
  1229  	fullSize := span.elemsize
  1230  	if rate := MemProfileRate; rate > 0 {
  1231  		// Note cache c only valid while m acquired; see #47302
  1232  		//
  1233  		// N.B. Use the full size because that matches how the GC
  1234  		// will update the mem profile on the "free" side.
  1235  		if rate != 1 && fullSize < c.nextSample {
  1236  			c.nextSample -= fullSize
  1237  		} else {
  1238  			profilealloc(mp, x, fullSize)
  1239  		}
  1240  	}
  1241  	mp.mallocing = 0
  1242  	releasem(mp)
  1243  
  1244  	// Objects can be zeroed late in a context where preemption can occur.
  1245  	// If the object contains pointers, its pointer data must be cleared
  1246  	// or otherwise indicate that the GC shouldn't scan it.
  1247  	// x will keep the memory alive.
  1248  	if delayedZeroing {
  1249  		// N.B. size == fullSize always in this case.
  1250  		memclrNoHeapPointersChunked(size, x) // This is a possible preemption point: see #47302
  1251  
  1252  		// Finish storing the type information for this case.
  1253  		if !noscan {
  1254  			mp := acquirem()
  1255  			getMCache(mp).scanAlloc += heapSetType(uintptr(x), dataSize, typ, header, span)
  1256  
  1257  			// Publish the type information with the zeroed memory.
  1258  			publicationBarrier()
  1259  			releasem(mp)
  1260  		}
  1261  	}
  1262  
  1263  	if debug.malloc {
  1264  		if debug.allocfreetrace != 0 {
  1265  			tracealloc(x, size, typ)
  1266  		}
  1267  
  1268  		if inittrace.active && inittrace.id == getg().goid {
  1269  			// Init functions are executed sequentially in a single goroutine.
  1270  			inittrace.bytes += uint64(fullSize)
  1271  		}
  1272  	}
  1273  
  1274  	if assistG != nil {
  1275  		// Account for internal fragmentation in the assist
  1276  		// debt now that we know it.
  1277  		//
  1278  		// N.B. Use the full size because that's how the rest
  1279  		// of the GC accounts for bytes marked.
  1280  		assistG.gcAssistBytes -= int64(fullSize - dataSize)
  1281  	}
  1282  
  1283  	if shouldhelpgc {
  1284  		if t := (gcTrigger{kind: gcTriggerHeap}); t.test() {
  1285  			gcStart(t)
  1286  		}
  1287  	}
  1288  
  1289  	if raceenabled && noscan && dataSize < maxTinySize {
  1290  		// Pad tinysize allocations so they are aligned with the end
  1291  		// of the tinyalloc region. This ensures that any arithmetic
  1292  		// that goes off the top end of the object will be detectable
  1293  		// by checkptr (issue 38872).
  1294  		// Note that we disable tinyalloc when raceenabled for this to work.
  1295  		// TODO: This padding is only performed when the race detector
  1296  		// is enabled. It would be nice to enable it if any package
  1297  		// was compiled with checkptr, but there's no easy way to
  1298  		// detect that (especially at compile time).
  1299  		// TODO: enable this padding for all allocations, not just
  1300  		// tinyalloc ones. It's tricky because of pointer maps.
  1301  		// Maybe just all noscan objects?
  1302  		x = add(x, size-dataSize)
  1303  	}
  1304  
  1305  	return x
  1306  }
  1307  
  1308  // deductAssistCredit reduces the current G's assist credit
  1309  // by size bytes, and assists the GC if necessary.
  1310  //
  1311  // Caller must be preemptible.
  1312  //
  1313  // Returns the G for which the assist credit was accounted.
  1314  func deductAssistCredit(size uintptr) *g {
  1315  	var assistG *g
  1316  	if gcBlackenEnabled != 0 {
  1317  		// Charge the current user G for this allocation.
  1318  		assistG = getg()
  1319  		if assistG.m.curg != nil {
  1320  			assistG = assistG.m.curg
  1321  		}
  1322  		// Charge the allocation against the G. We'll account
  1323  		// for internal fragmentation at the end of mallocgc.
  1324  		assistG.gcAssistBytes -= int64(size)
  1325  
  1326  		if assistG.gcAssistBytes < 0 {
  1327  			// This G is in debt. Assist the GC to correct
  1328  			// this before allocating. This must happen
  1329  			// before disabling preemption.
  1330  			gcAssistAlloc(assistG)
  1331  		}
  1332  	}
  1333  	return assistG
  1334  }
  1335  
  1336  // memclrNoHeapPointersChunked repeatedly calls memclrNoHeapPointers
  1337  // on chunks of the buffer to be zeroed, with opportunities for preemption
  1338  // along the way.  memclrNoHeapPointers contains no safepoints and also
  1339  // cannot be preemptively scheduled, so this provides a still-efficient
  1340  // block copy that can also be preempted on a reasonable granularity.
  1341  //
  1342  // Use this with care; if the data being cleared is tagged to contain
  1343  // pointers, this allows the GC to run before it is all cleared.
  1344  func memclrNoHeapPointersChunked(size uintptr, x unsafe.Pointer) {
  1345  	v := uintptr(x)
  1346  	// got this from benchmarking. 128k is too small, 512k is too large.
  1347  	const chunkBytes = 256 * 1024
  1348  	vsize := v + size
  1349  	for voff := v; voff < vsize; voff = voff + chunkBytes {
  1350  		if getg().preempt {
  1351  			// may hold locks, e.g., profiling
  1352  			goschedguarded()
  1353  		}
  1354  		// clear min(avail, lump) bytes
  1355  		n := vsize - voff
  1356  		if n > chunkBytes {
  1357  			n = chunkBytes
  1358  		}
  1359  		memclrNoHeapPointers(unsafe.Pointer(voff), n)
  1360  	}
  1361  }
  1362  
  1363  // implementation of new builtin
  1364  // compiler (both frontend and SSA backend) knows the signature
  1365  // of this function.
  1366  func newobject(typ *_type) unsafe.Pointer {
  1367  	return mallocgc(typ.Size_, typ, true)
  1368  }
  1369  
  1370  //go:linkname reflect_unsafe_New reflect.unsafe_New
  1371  func reflect_unsafe_New(typ *_type) unsafe.Pointer {
  1372  	return mallocgc(typ.Size_, typ, true)
  1373  }
  1374  
  1375  //go:linkname reflectlite_unsafe_New internal/reflectlite.unsafe_New
  1376  func reflectlite_unsafe_New(typ *_type) unsafe.Pointer {
  1377  	return mallocgc(typ.Size_, typ, true)
  1378  }
  1379  
  1380  // newarray allocates an array of n elements of type typ.
  1381  func newarray(typ *_type, n int) unsafe.Pointer {
  1382  	if n == 1 {
  1383  		return mallocgc(typ.Size_, typ, true)
  1384  	}
  1385  	mem, overflow := math.MulUintptr(typ.Size_, uintptr(n))
  1386  	if overflow || mem > maxAlloc || n < 0 {
  1387  		panic(plainError("runtime: allocation size out of range"))
  1388  	}
  1389  	return mallocgc(mem, typ, true)
  1390  }
  1391  
  1392  //go:linkname reflect_unsafe_NewArray reflect.unsafe_NewArray
  1393  func reflect_unsafe_NewArray(typ *_type, n int) unsafe.Pointer {
  1394  	return newarray(typ, n)
  1395  }
  1396  
  1397  func profilealloc(mp *m, x unsafe.Pointer, size uintptr) {
  1398  	c := getMCache(mp)
  1399  	if c == nil {
  1400  		throw("profilealloc called without a P or outside bootstrapping")
  1401  	}
  1402  	c.nextSample = nextSample()
  1403  	mProf_Malloc(x, size)
  1404  }
  1405  
  1406  // nextSample returns the next sampling point for heap profiling. The goal is
  1407  // to sample allocations on average every MemProfileRate bytes, but with a
  1408  // completely random distribution over the allocation timeline; this
  1409  // corresponds to a Poisson process with parameter MemProfileRate. In Poisson
  1410  // processes, the distance between two samples follows the exponential
  1411  // distribution (exp(MemProfileRate)), so the best return value is a random
  1412  // number taken from an exponential distribution whose mean is MemProfileRate.
  1413  func nextSample() uintptr {
  1414  	if MemProfileRate == 1 {
  1415  		// Callers assign our return value to
  1416  		// mcache.next_sample, but next_sample is not used
  1417  		// when the rate is 1. So avoid the math below and
  1418  		// just return something.
  1419  		return 0
  1420  	}
  1421  	if GOOS == "plan9" {
  1422  		// Plan 9 doesn't support floating point in note handler.
  1423  		if gp := getg(); gp == gp.m.gsignal {
  1424  			return nextSampleNoFP()
  1425  		}
  1426  	}
  1427  
  1428  	return uintptr(fastexprand(MemProfileRate))
  1429  }
  1430  
  1431  // fastexprand returns a random number from an exponential distribution with
  1432  // the specified mean.
  1433  func fastexprand(mean int) int32 {
  1434  	// Avoid overflow. Maximum possible step is
  1435  	// -ln(1/(1<<randomBitCount)) * mean, approximately 20 * mean.
  1436  	switch {
  1437  	case mean > 0x7000000:
  1438  		mean = 0x7000000
  1439  	case mean == 0:
  1440  		return 0
  1441  	}
  1442  
  1443  	// Take a random sample of the exponential distribution exp(-mean*x).
  1444  	// The probability distribution function is mean*exp(-mean*x), so the CDF is
  1445  	// p = 1 - exp(-mean*x), so
  1446  	// q = 1 - p == exp(-mean*x)
  1447  	// log_e(q) = -mean*x
  1448  	// -log_e(q)/mean = x
  1449  	// x = -log_e(q) * mean
  1450  	// x = log_2(q) * (-log_e(2)) * mean    ; Using log_2 for efficiency
  1451  	const randomBitCount = 26
  1452  	q := cheaprandn(1<<randomBitCount) + 1
  1453  	qlog := fastlog2(float64(q)) - randomBitCount
  1454  	if qlog > 0 {
  1455  		qlog = 0
  1456  	}
  1457  	const minusLog2 = -0.6931471805599453 // -ln(2)
  1458  	return int32(qlog*(minusLog2*float64(mean))) + 1
  1459  }
  1460  
  1461  // nextSampleNoFP is similar to nextSample, but uses older,
  1462  // simpler code to avoid floating point.
  1463  func nextSampleNoFP() uintptr {
  1464  	// Set first allocation sample size.
  1465  	rate := MemProfileRate
  1466  	if rate > 0x3fffffff { // make 2*rate not overflow
  1467  		rate = 0x3fffffff
  1468  	}
  1469  	if rate != 0 {
  1470  		return uintptr(cheaprandn(uint32(2 * rate)))
  1471  	}
  1472  	return 0
  1473  }
  1474  
  1475  type persistentAlloc struct {
  1476  	base *notInHeap
  1477  	off  uintptr
  1478  }
  1479  
  1480  var globalAlloc struct {
  1481  	mutex
  1482  	persistentAlloc
  1483  }
  1484  
  1485  // persistentChunkSize is the number of bytes we allocate when we grow
  1486  // a persistentAlloc.
  1487  const persistentChunkSize = 256 << 10
  1488  
  1489  // persistentChunks is a list of all the persistent chunks we have
  1490  // allocated. The list is maintained through the first word in the
  1491  // persistent chunk. This is updated atomically.
  1492  var persistentChunks *notInHeap
  1493  
  1494  // Wrapper around sysAlloc that can allocate small chunks.
  1495  // There is no associated free operation.
  1496  // Intended for things like function/type/debug-related persistent data.
  1497  // If align is 0, uses default align (currently 8).
  1498  // The returned memory will be zeroed.
  1499  // sysStat must be non-nil.
  1500  //
  1501  // Consider marking persistentalloc'd types not in heap by embedding
  1502  // runtime/internal/sys.NotInHeap.
  1503  func persistentalloc(size, align uintptr, sysStat *sysMemStat) unsafe.Pointer {
  1504  	var p *notInHeap
  1505  	systemstack(func() {
  1506  		p = persistentalloc1(size, align, sysStat)
  1507  	})
  1508  	return unsafe.Pointer(p)
  1509  }
  1510  
  1511  // Must run on system stack because stack growth can (re)invoke it.
  1512  // See issue 9174.
  1513  //
  1514  //go:systemstack
  1515  func persistentalloc1(size, align uintptr, sysStat *sysMemStat) *notInHeap {
  1516  	const (
  1517  		maxBlock = 64 << 10 // VM reservation granularity is 64K on windows
  1518  	)
  1519  
  1520  	if size == 0 {
  1521  		throw("persistentalloc: size == 0")
  1522  	}
  1523  	if align != 0 {
  1524  		if align&(align-1) != 0 {
  1525  			throw("persistentalloc: align is not a power of 2")
  1526  		}
  1527  		if align > _PageSize {
  1528  			throw("persistentalloc: align is too large")
  1529  		}
  1530  	} else {
  1531  		align = 8
  1532  	}
  1533  
  1534  	if size >= maxBlock {
  1535  		return (*notInHeap)(sysAlloc(size, sysStat))
  1536  	}
  1537  
  1538  	mp := acquirem()
  1539  	var persistent *persistentAlloc
  1540  	if mp != nil && mp.p != 0 {
  1541  		persistent = &mp.p.ptr().palloc
  1542  	} else {
  1543  		lock(&globalAlloc.mutex)
  1544  		persistent = &globalAlloc.persistentAlloc
  1545  	}
  1546  	persistent.off = alignUp(persistent.off, align)
  1547  	if persistent.off+size > persistentChunkSize || persistent.base == nil {
  1548  		persistent.base = (*notInHeap)(sysAlloc(persistentChunkSize, &memstats.other_sys))
  1549  		if persistent.base == nil {
  1550  			if persistent == &globalAlloc.persistentAlloc {
  1551  				unlock(&globalAlloc.mutex)
  1552  			}
  1553  			throw("runtime: cannot allocate memory")
  1554  		}
  1555  
  1556  		// Add the new chunk to the persistentChunks list.
  1557  		for {
  1558  			chunks := uintptr(unsafe.Pointer(persistentChunks))
  1559  			*(*uintptr)(unsafe.Pointer(persistent.base)) = chunks
  1560  			if atomic.Casuintptr((*uintptr)(unsafe.Pointer(&persistentChunks)), chunks, uintptr(unsafe.Pointer(persistent.base))) {
  1561  				break
  1562  			}
  1563  		}
  1564  		persistent.off = alignUp(goarch.PtrSize, align)
  1565  	}
  1566  	p := persistent.base.add(persistent.off)
  1567  	persistent.off += size
  1568  	releasem(mp)
  1569  	if persistent == &globalAlloc.persistentAlloc {
  1570  		unlock(&globalAlloc.mutex)
  1571  	}
  1572  
  1573  	if sysStat != &memstats.other_sys {
  1574  		sysStat.add(int64(size))
  1575  		memstats.other_sys.add(-int64(size))
  1576  	}
  1577  	return p
  1578  }
  1579  
  1580  // inPersistentAlloc reports whether p points to memory allocated by
  1581  // persistentalloc. This must be nosplit because it is called by the
  1582  // cgo checker code, which is called by the write barrier code.
  1583  //
  1584  //go:nosplit
  1585  func inPersistentAlloc(p uintptr) bool {
  1586  	chunk := atomic.Loaduintptr((*uintptr)(unsafe.Pointer(&persistentChunks)))
  1587  	for chunk != 0 {
  1588  		if p >= chunk && p < chunk+persistentChunkSize {
  1589  			return true
  1590  		}
  1591  		chunk = *(*uintptr)(unsafe.Pointer(chunk))
  1592  	}
  1593  	return false
  1594  }
  1595  
  1596  // linearAlloc is a simple linear allocator that pre-reserves a region
  1597  // of memory and then optionally maps that region into the Ready state
  1598  // as needed.
  1599  //
  1600  // The caller is responsible for locking.
  1601  type linearAlloc struct {
  1602  	next   uintptr // next free byte
  1603  	mapped uintptr // one byte past end of mapped space
  1604  	end    uintptr // end of reserved space
  1605  
  1606  	mapMemory bool // transition memory from Reserved to Ready if true
  1607  }
  1608  
  1609  func (l *linearAlloc) init(base, size uintptr, mapMemory bool) {
  1610  	if base+size < base {
  1611  		// Chop off the last byte. The runtime isn't prepared
  1612  		// to deal with situations where the bounds could overflow.
  1613  		// Leave that memory reserved, though, so we don't map it
  1614  		// later.
  1615  		size -= 1
  1616  	}
  1617  	l.next, l.mapped = base, base
  1618  	l.end = base + size
  1619  	l.mapMemory = mapMemory
  1620  }
  1621  
  1622  func (l *linearAlloc) alloc(size, align uintptr, sysStat *sysMemStat) unsafe.Pointer {
  1623  	p := alignUp(l.next, align)
  1624  	if p+size > l.end {
  1625  		return nil
  1626  	}
  1627  	l.next = p + size
  1628  	if pEnd := alignUp(l.next-1, physPageSize); pEnd > l.mapped {
  1629  		if l.mapMemory {
  1630  			// Transition from Reserved to Prepared to Ready.
  1631  			n := pEnd - l.mapped
  1632  			sysMap(unsafe.Pointer(l.mapped), n, sysStat)
  1633  			sysUsed(unsafe.Pointer(l.mapped), n, n)
  1634  		}
  1635  		l.mapped = pEnd
  1636  	}
  1637  	return unsafe.Pointer(p)
  1638  }
  1639  
  1640  // notInHeap is off-heap memory allocated by a lower-level allocator
  1641  // like sysAlloc or persistentAlloc.
  1642  //
  1643  // In general, it's better to use real types which embed
  1644  // runtime/internal/sys.NotInHeap, but this serves as a generic type
  1645  // for situations where that isn't possible (like in the allocators).
  1646  //
  1647  // TODO: Use this as the return type of sysAlloc, persistentAlloc, etc?
  1648  type notInHeap struct{ _ sys.NotInHeap }
  1649  
  1650  func (p *notInHeap) add(bytes uintptr) *notInHeap {
  1651  	return (*notInHeap)(unsafe.Pointer(uintptr(unsafe.Pointer(p)) + bytes))
  1652  }
  1653  
  1654  // computeRZlog computes the size of the redzone.
  1655  // Refer to the implementation of the compiler-rt.
  1656  func computeRZlog(userSize uintptr) uintptr {
  1657  	switch {
  1658  	case userSize <= (64 - 16):
  1659  		return 16 << 0
  1660  	case userSize <= (128 - 32):
  1661  		return 16 << 1
  1662  	case userSize <= (512 - 64):
  1663  		return 16 << 2
  1664  	case userSize <= (4096 - 128):
  1665  		return 16 << 3
  1666  	case userSize <= (1<<14)-256:
  1667  		return 16 << 4
  1668  	case userSize <= (1<<15)-512:
  1669  		return 16 << 5
  1670  	case userSize <= (1<<16)-1024:
  1671  		return 16 << 6
  1672  	default:
  1673  		return 16 << 7
  1674  	}
  1675  }
  1676  

View as plain text