package utils

import (
	"sync"
)

var (
	DefBufPool *BufPool
)

func init() {
	DefBufPool = NewBufPool(uint16(1024 * 4))
}

type BufPool struct {
	p    *sync.Pool
	size uint16
}

func NewBufPool(size uint16) *BufPool {
	return &BufPool{
		p: &sync.Pool{
			New: func() interface{} {
				return make([]byte, size)
			},
		},
		size: size,
	}
}

func (self *BufPool) Get() []byte {
	return self.p.Get().([]byte)
}

func (self *BufPool) GetSize() uint16 {
	return self.size
}

func (self *BufPool) Put(data []byte) {
	self.p.Put(data)
}
