initial import
This commit is contained in:
34
core/iox/bufferpool.go
Normal file
34
core/iox/bufferpool.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package iox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type BufferPool struct {
|
||||
capability int
|
||||
pool *sync.Pool
|
||||
}
|
||||
|
||||
func NewBufferPool(capability int) *BufferPool {
|
||||
return &BufferPool{
|
||||
capability: capability,
|
||||
pool: &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (bp *BufferPool) Get() *bytes.Buffer {
|
||||
buf := bp.pool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
return buf
|
||||
}
|
||||
|
||||
func (bp *BufferPool) Put(buf *bytes.Buffer) {
|
||||
if buf.Cap() < bp.capability {
|
||||
bp.pool.Put(buf)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user