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