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 //go:build goexperiment.allocheaders
7 // Malloc small size classes.
9 // See malloc.go for overview.
10 // See also mksizeclasses.go for how we decide what size classes to use.
14 // Returns size of the memory block that mallocgc will allocate if you ask for the size,
15 // minus any inline space for metadata.
16 func roundupsize(size uintptr, noscan bool) (reqSize uintptr) {
18 if reqSize <= maxSmallSize-mallocHeaderSize {
20 if !noscan && reqSize > minSizeForMallocHeader { // !noscan && !heapBitsInSpan(reqSize)
21 reqSize += mallocHeaderSize
23 // (reqSize - size) is either mallocHeaderSize or 0. We need to subtract mallocHeaderSize
24 // from the result if we have one, since mallocgc will add it back in.
25 if reqSize <= smallSizeMax-8 {
26 return uintptr(class_to_size[size_to_class8[divRoundUp(reqSize, smallSizeDiv)]]) - (reqSize - size)
28 return uintptr(class_to_size[size_to_class128[divRoundUp(reqSize-smallSizeMax, largeSizeDiv)]]) - (reqSize - size)
30 // Large object. Align reqSize up to the next page. Check for overflow.
31 reqSize += pageSize - 1
35 return reqSize &^ (pageSize - 1)