Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
utils: Add memory unit abstraction
Browse files Browse the repository at this point in the history
Add MemUnit to help to manage memory, this will handle memory
units internally and provide proper methods to convert to different
units.

Signed-off-by: Jose Carlos Venegas Munoz <[email protected]>
  • Loading branch information
jcvenegas committed Mar 25, 2020
1 parent 5e7d253 commit b6a7d8d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions virtcontainers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,38 @@ func SupportsVsocks() bool {
var StartCmd = func(c *exec.Cmd) error {
return c.Start()
}

// AlignMem align memory provided to a block size
func (m MemUnit) AlignMem(blockSize MemUnit) MemUnit {
memSize := m
if m < blockSize {
memSize = blockSize

}

remainder := memSize % blockSize

if remainder != 0 {
// Align memory to memoryBlockSizeMB
memSize += blockSize - remainder

}
return memSize
}

type MemUnit uint64

func (m MemUnit) ToMiB() uint64 {
return m.ToBytes() / (1 * MiB).ToBytes()
}

func (m MemUnit) ToBytes() uint64 {
return uint64(m)
}

const (
Byte MemUnit = 1
KiB = Byte << 10
MiB = KiB << 10
GiB = MiB << 10
)

0 comments on commit b6a7d8d

Please sign in to comment.