github.com/pidato/unsafe@v0.1.4/memory/heap/class.go (about)

     1  package heap
     2  
     3  import "math/bits"
     4  
     5  type Class struct {
     6  	Index uint16
     7  	Shift uint16
     8  	Size  uint32
     9  }
    10  
    11  func (cls Class) Set(bitmap uint32) uint32 {
    12  	return bitmap | (1 << cls.Shift)
    13  }
    14  
    15  func (cls Class) Set64(bitmap uint64) uint64 {
    16  	return bitmap | (1 << cls.Shift)
    17  }
    18  
    19  func (cls Class) Unset(bitmap uint32) uint32 {
    20  	return bitmap & ^(1 << cls.Shift)
    21  }
    22  
    23  func (cls Class) Unset64(bitmap uint64) uint64 {
    24  	return bitmap & ^(1 << cls.Shift)
    25  }
    26  
    27  func (cls Class) Search(bitmap uint32) int {
    28  	return bits.LeadingZeros32(bitmap<<cls.Index) + int(cls.Index)
    29  }
    30  
    31  func (cls Class) Search64(bitmap uint64) int {
    32  	return bits.LeadingZeros64(bitmap<<cls.Index) + int(cls.Index)
    33  }
    34  
    35  // alignUp rounds n up to a multiple of a. a must be a power of 2.
    36  func alignUp(n, a uintptr) uintptr {
    37  	return (n + a - 1) &^ (a - 1)
    38  }
    39  
    40  // alignDown rounds n down to a multiple of a. a must be a power of 2.
    41  func alignDown(n, a uintptr) uintptr {
    42  	return n &^ (a - 1)
    43  }
    44  
    45  // divRoundUp returns ceil(n / a).
    46  func divRoundUp(n, a uintptr) uintptr {
    47  	// a is generally a power of two. This will get inlined and
    48  	// the compiler will optimize the division.
    49  	return (n + a - 1) / a
    50  }