1 Star 0 Fork 0

sy_183 / go-common

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
buffer-pool.go 695 Bytes
一键复制 编辑 原始数据 按行查看 历史
sy_183 提交于 2023-02-27 19:27 . 纵横视频存储服务v1.1
package bufferpool
import "sync"
// A BufferPool is a type-safe wrapper around a sync.Pool.
type BufferPool struct {
p *sync.Pool
}
// NewBufferPool constructs a new Pool.
func NewBufferPool(size uint) *BufferPool {
return &BufferPool{p: &sync.Pool{
New: func() interface{} {
return &Buffer{bs: make([]byte, 0, size)}
},
}}
}
// Get retrieves a Buffer from the pool, creating one if necessary.
func (p *BufferPool) Get() *Buffer {
buf := p.p.Get().(*Buffer)
buf.Reset()
buf.pool = p
return buf
}
func (p *BufferPool) put(buf *Buffer) {
p.p.Put(buf)
}
var (
_pool = NewBufferPool(1024)
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
1
https://gitee.com/sy_183/go-common.git
git@gitee.com:sy_183/go-common.git
sy_183
go-common
go-common
v1.0.4

搜索帮助