utils: Add memory unit abstraction

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 <jose.carlos.venegas.munoz@intel.com>
This commit is contained in:
Jose Carlos Venegas Munoz 2020-03-24 23:27:47 +00:00
parent 5e7d253859
commit b6a7d8d63a

View File

@ -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
)