| 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 |
|
| 10 |
package runtime |
| 11 |
|
| 12 |
import "internal/runtime/gc" |
| 13 |
|
| 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) { |
| 17 |
reqSize = size |
| 18 |
if reqSize <= maxSmallSize-gc.MallocHeaderSize { |
| 19 |
// Small object. |
| 20 |
if !noscan && reqSize > gc.MinSizeForMallocHeader { // !noscan && !heapBitsInSpan(reqSize) |
| 21 |
reqSize += gc.MallocHeaderSize |
| 22 |
} |
| 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 <= gc.SmallSizeMax-8 { |
| 26 |
return uintptr(gc.SizeClassToSize[gc.SizeToSizeClass8[divRoundUp(reqSize, gc.SmallSizeDiv)]]) - (reqSize - size) |
| 27 |
} |
| 28 |
return uintptr(gc.SizeClassToSize[gc.SizeToSizeClass128[divRoundUp(reqSize-gc.SmallSizeMax, gc.LargeSizeDiv)]]) - (reqSize - size) |
| 29 |
} |
| 30 |
// Large object. Align reqSize up to the next page. Check for overflow. |
| 31 |
reqSize += pageSize - 1 |
| 32 |
if reqSize < size { |
| 33 |
return size |
| 34 |
} |
| 35 |
return reqSize &^ (pageSize - 1) |
| 36 |
} |