mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
47 lines
612 B
Go
47 lines
612 B
Go
// Package buffers manages a set of reusable byte buffers.
|
|
package buffers
|
|
|
|
const (
|
|
largeMin = 1024
|
|
)
|
|
|
|
var (
|
|
smallBuffers = make(chan []byte, 32)
|
|
largeBuffers = make(chan []byte, 32)
|
|
)
|
|
|
|
func Get(size int) []byte {
|
|
var ch = largeBuffers
|
|
if size < largeMin {
|
|
ch = smallBuffers
|
|
}
|
|
|
|
var buf []byte
|
|
select {
|
|
case buf = <-ch:
|
|
default:
|
|
}
|
|
|
|
if len(buf) < size {
|
|
return make([]byte, size)
|
|
}
|
|
return buf[:size]
|
|
}
|
|
|
|
func Put(buf []byte) {
|
|
buf = buf[:cap(buf)]
|
|
if len(buf) == 0 {
|
|
return
|
|
}
|
|
|
|
var ch = largeBuffers
|
|
if len(buf) < largeMin {
|
|
ch = smallBuffers
|
|
}
|
|
|
|
select {
|
|
case ch <- buf:
|
|
default:
|
|
}
|
|
}
|