You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
546 B
32 lines
546 B
package core
|
|
|
|
import "sync"
|
|
|
|
// A Pool is a type-safe wrapper around a sync.Pool.
|
|
type Pool struct {
|
|
p *sync.Pool
|
|
}
|
|
|
|
// NewPool constructs a new Pool.
|
|
func NewPool(size int) Pool {
|
|
if size == 0 {
|
|
size = _size
|
|
}
|
|
return Pool{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 Pool) Get() *Buffer {
|
|
buf := p.p.Get().(*Buffer)
|
|
buf.Reset()
|
|
buf.pool = p
|
|
return buf
|
|
}
|
|
|
|
func (p Pool) put(buf *Buffer) {
|
|
p.p.Put(buf)
|
|
}
|
|
|