Commit 0abb4be6 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Hide bufferpool implementations to reduce API clutter.

parent 41f74f69
...@@ -16,25 +16,29 @@ type BufferPool interface { ...@@ -16,25 +16,29 @@ type BufferPool interface {
String() string String() string
} }
type GcBufferPool struct { type gcBufferPool struct {
} }
// NewGcBufferPool is just a fallback to the standard allocation routines. // NewGcBufferPool is just a fallback to the standard allocation routines.
func NewGcBufferPool() *GcBufferPool { func NewGcBufferPool() BufferPool {
return &GcBufferPool{} return &gcBufferPool{}
} }
func (p *GcBufferPool) AllocBuffer(size uint32) []byte { func (p *gcBufferPool) String() string {
return "gc"
}
func (p *gcBufferPool) AllocBuffer(size uint32) []byte {
return make([]byte, size) return make([]byte, size)
} }
func (p *GcBufferPool) FreeBuffer(slice []byte) { func (p *gcBufferPool) FreeBuffer(slice []byte) {
} }
// BufferPool implements a pool of buffers that returns slices with // BufferPool implements a pool of buffers that returns slices with
// capacity of a multiple of PAGESIZE, which have possibly been used, // capacity of a multiple of PAGESIZE, which have possibly been used,
// and may contain random contents. // and may contain random contents.
type BufferPoolImpl struct { type bufferPoolImpl struct {
lock sync.Mutex lock sync.Mutex
// For each page size multiple a list of slice pointers. // For each page size multiple a list of slice pointers.
...@@ -48,14 +52,14 @@ type BufferPoolImpl struct { ...@@ -48,14 +52,14 @@ type BufferPoolImpl struct {
createdBuffers int createdBuffers int
} }
func NewBufferPool() *BufferPoolImpl { func NewBufferPool() BufferPool {
bp := new(BufferPoolImpl) bp := new(bufferPoolImpl)
bp.buffersBySize = make([][][]byte, 0, 32) bp.buffersBySize = make([][][]byte, 0, 32)
bp.outstandingBuffers = make(map[uintptr]bool) bp.outstandingBuffers = make(map[uintptr]bool)
return bp return bp
} }
func (p *BufferPoolImpl) String() string { func (p *bufferPoolImpl) String() string {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
...@@ -70,7 +74,7 @@ func (p *BufferPoolImpl) String() string { ...@@ -70,7 +74,7 @@ func (p *BufferPoolImpl) String() string {
strings.Join(result, ", ")) strings.Join(result, ", "))
} }
func (p *BufferPoolImpl) getBuffer(pageCount int) []byte { func (p *bufferPoolImpl) getBuffer(pageCount int) []byte {
for ; pageCount < len(p.buffersBySize); pageCount++ { for ; pageCount < len(p.buffersBySize); pageCount++ {
bufferList := p.buffersBySize[pageCount] bufferList := p.buffersBySize[pageCount]
if len(bufferList) > 0 { if len(bufferList) > 0 {
...@@ -83,7 +87,7 @@ func (p *BufferPoolImpl) getBuffer(pageCount int) []byte { ...@@ -83,7 +87,7 @@ func (p *BufferPoolImpl) getBuffer(pageCount int) []byte {
return nil return nil
} }
func (p *BufferPoolImpl) addBuffer(slice []byte, pages int) { func (p *bufferPoolImpl) addBuffer(slice []byte, pages int) {
for len(p.buffersBySize) <= int(pages) { for len(p.buffersBySize) <= int(pages) {
p.buffersBySize = append(p.buffersBySize, make([][]byte, 0)) p.buffersBySize = append(p.buffersBySize, make([][]byte, 0))
} }
...@@ -92,7 +96,7 @@ func (p *BufferPoolImpl) addBuffer(slice []byte, pages int) { ...@@ -92,7 +96,7 @@ func (p *BufferPoolImpl) addBuffer(slice []byte, pages int) {
// AllocBuffer creates a buffer of at least the given size. After use, // AllocBuffer creates a buffer of at least the given size. After use,
// it should be deallocated with FreeBuffer(). // it should be deallocated with FreeBuffer().
func (p *BufferPoolImpl) AllocBuffer(size uint32) []byte { func (p *bufferPoolImpl) AllocBuffer(size uint32) []byte {
sz := int(size) sz := int(size)
if sz < PAGESIZE { if sz < PAGESIZE {
sz = PAGESIZE sz = PAGESIZE
...@@ -128,7 +132,7 @@ func (p *BufferPoolImpl) AllocBuffer(size uint32) []byte { ...@@ -128,7 +132,7 @@ func (p *BufferPoolImpl) AllocBuffer(size uint32) []byte {
// FreeBuffer takes back a buffer if it was allocated through // FreeBuffer takes back a buffer if it was allocated through
// AllocBuffer. It is not an error to call FreeBuffer() on a slice // AllocBuffer. It is not an error to call FreeBuffer() on a slice
// obtained elsewhere. // obtained elsewhere.
func (p *BufferPoolImpl) FreeBuffer(slice []byte) { func (p *bufferPoolImpl) FreeBuffer(slice []byte) {
if slice == nil { if slice == nil {
return return
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment