diff options
Diffstat (limited to 'vendor/git.iim.gay/grufwub/go-bytes/pool.go')
-rw-r--r-- | vendor/git.iim.gay/grufwub/go-bytes/pool.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/vendor/git.iim.gay/grufwub/go-bytes/pool.go b/vendor/git.iim.gay/grufwub/go-bytes/pool.go new file mode 100644 index 000000000..edd6859c1 --- /dev/null +++ b/vendor/git.iim.gay/grufwub/go-bytes/pool.go @@ -0,0 +1,39 @@ +package bytes + +import ( + "bytes" + "sync" +) + +type SizedBufferPool struct { + pool sync.Pool + len int + cap int +} + +func (p *SizedBufferPool) Init(len, cap int) { + p.pool.New = func() interface{} { + buf := NewBuffer(make([]byte, len, cap)) + return &buf + } + p.len = len + p.cap = cap +} + +func (p *SizedBufferPool) Acquire() *bytes.Buffer { + return p.pool.Get().(*bytes.Buffer) +} + +func (p *SizedBufferPool) Release(buf *bytes.Buffer) { + // If not enough cap, ignore + if buf.Cap() < p.cap { + return + } + + // Set length to expected + buf.Reset() + buf.Grow(p.len) + + // Place in pool + p.pool.Put(buf) +} |