package bufferpool: 1. global rename to default 2. new struct SharedBuffer

pull/2/head
q191201771 5 years ago
parent 709aa1a157
commit 13e1825dcb

@ -10,22 +10,22 @@ package bufferpool
import "bytes"
var global BufferPool
var defaultPool BufferPool
func Get(size int) *bytes.Buffer {
return global.Get(size)
return defaultPool.Get(size)
}
func Put(buf *bytes.Buffer) {
global.Put(buf)
defaultPool.Put(buf)
}
func RetrieveStatus() Status {
return global.RetrieveStatus()
return defaultPool.RetrieveStatus()
}
func Init(strategy Strategy) {
global = NewBufferPool(strategy)
defaultPool = NewBufferPool(strategy)
}
func init() {

@ -0,0 +1,43 @@
// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package bufferpool
import (
"bytes"
"github.com/q191201771/naza/pkg/nazaatomic"
)
type SharedBuffer struct {
*bytes.Buffer
pool BufferPool
count *nazaatomic.Uint32
}
func NewSharedBufferDefault(size int) *SharedBuffer {
return NewSharedBuffer(defaultPool, size)
}
func NewSharedBuffer(pool BufferPool, size int) *SharedBuffer {
return &SharedBuffer{
Buffer: pool.Get(size),
count: new(nazaatomic.Uint32),
}
}
func (sb *SharedBuffer) Ref() *SharedBuffer {
sb.count.Increment()
return sb
}
func (sb *SharedBuffer) ReleaseIfNeeded() {
if sb.count.Decrement() == 0 {
sb.pool.Put(sb.Buffer)
}
}
Loading…
Cancel
Save