5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-10 06:20:26 +00:00
yggdrasil-go/src/yggdrasil/util.go
2018-01-26 17:30:51 -06:00

82 lines
1.4 KiB
Go

package yggdrasil
// These are misc. utility functions that didn't really fit anywhere else
import "fmt"
import "runtime"
//import "sync"
func Util_testAddrIDMask() {
for idx := 0; idx < 16; idx++ {
var orig NodeID
orig[8] = 42
for bidx := 0; bidx < idx; bidx++ {
orig[bidx/8] |= (0x80 >> uint8(bidx%8))
}
addr := address_addrForNodeID(&orig)
nid, mask := addr.getNodeIDandMask()
for b := 0; b < len(mask); b++ {
nid[b] &= mask[b]
orig[b] &= mask[b]
}
if *nid != orig {
fmt.Println(orig)
fmt.Println(*addr)
fmt.Println(*nid)
fmt.Println(*mask)
panic(idx)
}
}
}
func util_yield() {
runtime.Gosched()
}
func util_lockthread() {
runtime.LockOSThread()
}
func util_unlockthread() {
runtime.UnlockOSThread()
}
/* Used previously, but removed because casting to an interface{} allocates...
var byteStore sync.Pool = sync.Pool{
New: func () interface{} { return []byte(nil) },
}
func util_getBytes() []byte {
return byteStore.Get().([]byte)[:0]
}
func util_putBytes(bs []byte) {
byteStore.Put(bs) // This is the part that allocates
}
*/
var byteStore chan []byte
func util_initByteStore() {
if byteStore == nil {
byteStore = make(chan []byte, 32)
}
}
func util_getBytes() []byte {
select {
case bs := <-byteStore:
return bs[:0]
default:
return nil
}
}
func util_putBytes(bs []byte) {
select {
case byteStore <- bs:
default:
}
}