yggdrasil-go/src/yggdrasil/util.go

82 lines
1.3 KiB
Go
Raw Normal View History

2017-12-29 07:16:20 +03:00
package yggdrasil
// These are misc. utility functions that didn't really fit anywhere else
import "fmt"
import "runtime"
2018-01-05 01:37:51 +03:00
2017-12-29 07:16:20 +03:00
//import "sync"
func Util_testAddrIDMask() {
2018-01-05 01:37:51 +03:00
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)
}
}
2017-12-29 07:16:20 +03:00
}
func util_yield() {
2018-01-05 01:37:51 +03:00
runtime.Gosched()
2017-12-29 07:16:20 +03:00
}
func util_lockthread() {
2018-01-05 01:37:51 +03:00
runtime.LockOSThread()
2017-12-29 07:16:20 +03:00
}
func util_unlockthread() {
2018-01-05 01:37:51 +03:00
runtime.UnlockOSThread()
2017-12-29 07:16:20 +03:00
}
/*
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) // FIXME? The cast to interface{} allocates...
}
*/
var byteStore chan []byte
func util_initByteStore() {
2018-01-05 01:37:51 +03:00
if byteStore == nil {
byteStore = make(chan []byte, 32)
}
2017-12-29 07:16:20 +03:00
}
func util_getBytes() []byte {
2018-01-05 01:37:51 +03:00
select {
case bs := <-byteStore:
return bs[:0]
default:
return nil
}
2017-12-29 07:16:20 +03:00
}
func util_putBytes(bs []byte) {
2018-01-05 01:37:51 +03:00
select {
case byteStore <- bs:
default:
}
2017-12-29 07:16:20 +03:00
}