pool.go 439 B

123456789101112131415161718192021222324252627282930
  1. package buffer
  2. import (
  3. "sync"
  4. )
  5. type Pool struct {
  6. // A Pool must not be copied after first use.
  7. // https://golang.org/pkg/sync/#Pool
  8. buffers sync.Pool
  9. }
  10. func NewPool(bufferSize int) *Pool {
  11. return &Pool{
  12. buffers: sync.Pool{
  13. New: func() interface{} {
  14. return make([]byte, bufferSize)
  15. },
  16. },
  17. }
  18. }
  19. func (p *Pool) Get() []byte {
  20. return p.buffers.Get().([]byte)
  21. }
  22. func (p *Pool) Put(buf []byte) {
  23. p.buffers.Put(buf)
  24. }