cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
buffer/pool.go
29lines · modeblame
464bb530Rueian6 years ago | 1 | package buffer |
| 2 | | |
| 3 | import ( | |
| 4 | "sync" | |
| 5 | ) | |
| 6 | | |
| 7 | type Pool struct { | |
| 8 | // A Pool must not be copied after first use. | |
| 9 | // https://golang.org/pkg/sync/#Pool | |
| 10 | buffers sync.Pool | |
| 11 | } | |
| 12 | | |
| 13 | func NewPool(bufferSize int) *Pool { | |
| 14 | return &Pool{ | |
| 15 | buffers: sync.Pool{ | |
| 16 | New: func() interface{} { | |
| 17 | return make([]byte, bufferSize) | |
| 18 | }, | |
| 19 | }, | |
| 20 | } | |
| 21 | } | |
| 22 | | |
| 23 | func (p *Pool) Get() []byte { | |
| 24 | return p.buffers.Get().([]byte) | |
| 25 | } | |
| 26 | | |
| 27 | func (p *Pool) Put(buf []byte) { | |
| 28 | p.buffers.Put(buf) | |
| 29 | } |