| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package utl
- import (
- "sync"
- )
- var (
- DefBufPool *BufPool
- )
- func init() {
- DefBufPool = NewBufPool(uint32(1024 * 2))
- }
- type BufPool struct {
- p *sync.Pool
- size uint32
- }
- func NewBufPool(size uint32) *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() uint32 {
- return self.size
- }
- func (self *BufPool) Put(data []byte) {
- self.p.Put(data)
- }
|