syncthing/buffers/buffers.go

46 lines
553 B
Go
Raw Normal View History

2013-12-15 03:43:31 -07:00
package buffers
2013-12-29 18:33:57 -07:00
const (
largeMin = 1024
)
var (
smallBuffers = make(chan []byte, 32)
largeBuffers = make(chan []byte, 32)
)
2013-12-15 03:43:31 -07:00
func Get(size int) []byte {
2013-12-29 18:33:57 -07:00
var ch = largeBuffers
if size < largeMin {
ch = smallBuffers
}
2013-12-15 03:43:31 -07:00
var buf []byte
select {
2013-12-29 18:33:57 -07:00
case buf = <-ch:
2013-12-15 03:43:31 -07:00
default:
}
2013-12-29 18:33:57 -07:00
2013-12-15 03:43:31 -07:00
if len(buf) < size {
return make([]byte, size)
}
return buf[:size]
}
func Put(buf []byte) {
2013-12-29 18:33:57 -07:00
buf = buf[:cap(buf)]
if len(buf) == 0 {
2013-12-15 03:43:31 -07:00
return
}
2013-12-29 18:33:57 -07:00
var ch = largeBuffers
if len(buf) < largeMin {
ch = smallBuffers
}
2013-12-15 03:43:31 -07:00
select {
2013-12-29 18:33:57 -07:00
case ch <- buf:
2013-12-15 03:43:31 -07:00
default:
}
}