wireguard-go/misc.go

63 lines
931 B
Go
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
package main
2017-07-01 21:29:22 +00:00
import (
"sync/atomic"
2017-07-01 21:29:22 +00:00
)
2017-12-01 22:37:26 +00:00
/* Atomic Boolean */
2017-07-08 21:51:26 +00:00
const (
AtomicFalse = int32(iota)
2017-07-08 21:51:26 +00:00
AtomicTrue
)
type AtomicBool struct {
flag int32
}
func (a *AtomicBool) Get() bool {
return atomic.LoadInt32(&a.flag) == AtomicTrue
}
func (a *AtomicBool) Swap(val bool) bool {
flag := AtomicFalse
if val {
flag = AtomicTrue
}
return atomic.SwapInt32(&a.flag, flag) == AtomicTrue
}
func (a *AtomicBool) Set(val bool) {
flag := AtomicFalse
if val {
flag = AtomicTrue
}
atomic.StoreInt32(&a.flag, flag)
}
2017-12-01 22:37:26 +00:00
/* Integer manipulation */
func toInt32(n uint32) int32 {
mask := uint32(1 << 31)
return int32(-(n & mask) + (n & ^mask))
}
func min(a uint, b uint) uint {
if a > b {
return b
}
return a
}
2017-07-10 10:09:19 +00:00
func minUint64(a uint64, b uint64) uint64 {
if a > b {
return b
}
return a
}