bufPool.go 533 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package utl
  2. import (
  3. "sync"
  4. )
  5. var (
  6. DefBufPool *BufPool
  7. )
  8. func init() {
  9. DefBufPool = NewBufPool(uint32(1024 * 4))
  10. }
  11. type BufPool struct {
  12. p *sync.Pool
  13. size uint32
  14. }
  15. func NewBufPool(size uint32) *BufPool {
  16. return &BufPool{
  17. p: &sync.Pool{
  18. New: func() interface{} {
  19. return make([]byte, size)
  20. },
  21. },
  22. size: size,
  23. }
  24. }
  25. func (self *BufPool) Get() []byte {
  26. return self.p.Get().([]byte)
  27. }
  28. func (self *BufPool) GetSize() uint32 {
  29. return self.size
  30. }
  31. func (self *BufPool) Put(data []byte) {
  32. self.p.Put(data)
  33. }