github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/runtime/msize.go (about)

     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  // sizeToClass(0 <= n <= MaxSmallSize) returns the size class,
    13  //	1 <= sizeclass < NumSizeClasses, for n.
    14  //	Size class 0 is reserved to mean "not small".
    15  //
    16  // The sizeToClass lookup is implemented using two arrays,
    17  // one mapping sizes <= 1024 to their class and one mapping
    18  // sizes >= 1024 and <= MaxSmallSize to their class.
    19  // All objects are 8-aligned, so the first array is indexed by
    20  // the size divided by 8 (rounded up).  Objects >= 1024 bytes
    21  // are 128-aligned, so the second array is indexed by the
    22  // size divided by 128 (rounded up).  The arrays are constants
    23  // in sizeclass.go generated by mksizeclass.go.
    24  func sizeToClass(size uint32) uint32 {
    25  	if size > _MaxSmallSize {
    26  		throw("invalid size")
    27  	}
    28  	if size > smallSizeMax-8 {
    29  		return uint32(size_to_class128[(size-smallSizeMax+largeSizeDiv-1)/largeSizeDiv])
    30  	}
    31  	return uint32(size_to_class8[(size+smallSizeDiv-1)/smallSizeDiv])
    32  }
    33  
    34  // Returns size of the memory block that mallocgc will allocate if you ask for the size.
    35  func roundupsize(size uintptr) uintptr {
    36  	if size < _MaxSmallSize {
    37  		if size <= smallSizeMax-8 {
    38  			return uintptr(class_to_size[size_to_class8[(size+smallSizeDiv-1)/smallSizeDiv]])
    39  		} else {
    40  			return uintptr(class_to_size[size_to_class128[(size-smallSizeMax+largeSizeDiv-1)/largeSizeDiv]])
    41  		}
    42  	}
    43  	if size+_PageSize < size {
    44  		return size
    45  	}
    46  	return round(size, _PageSize)
    47  }