This commit is contained in:
ffdfgdfg
2019-08-23 18:53:36 +08:00
parent 7178b33807
commit 00900c1315
7 changed files with 324 additions and 128 deletions

View File

@@ -1,6 +1,7 @@
package pool
import (
"bytes"
"sync"
)
@@ -36,6 +37,7 @@ var BufPoolCopy = sync.Pool{
return &buf
},
}
func PutBufPoolUdp(buf []byte) {
if cap(buf) == PoolSizeUdp {
BufPoolUdp.Put(buf[:PoolSizeUdp])
@@ -48,7 +50,7 @@ func PutBufPoolCopy(buf []byte) {
}
}
func GetBufPoolCopy() ([]byte) {
func GetBufPoolCopy() []byte {
return (*BufPoolCopy.Get().(*[]byte))[:PoolSizeCopy]
}
@@ -57,3 +59,31 @@ func PutBufPoolMax(buf []byte) {
BufPoolMax.Put(buf[:PoolSize])
}
}
type BufferPool struct {
pool sync.Pool
}
func (Self *BufferPool) New() {
Self.pool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
}
func (Self *BufferPool) Get() *bytes.Buffer {
return Self.pool.Get().(*bytes.Buffer)
}
func (Self *BufferPool) Put(x *bytes.Buffer) {
x.Reset()
Self.pool.Put(x)
}
var once = sync.Once{}
var BuffPool = BufferPool{}
func init() {
once.Do(BuffPool.New)
}