mirror of
https://github.com/octoleo/restic.git
synced 2024-11-25 06:07:44 +00:00
0db1d11b2e
This isn't doing anything. Channels should get cleaned up by the GC when the last reference to them disappears, just like all other data structures. Also inlined BufferPool.Put in Buffer.Release, its only caller.
55 lines
1.1 KiB
Go
55 lines
1.1 KiB
Go
package archiver
|
|
|
|
// Buffer is a reusable buffer. After the buffer has been used, Release should
|
|
// be called so the underlying slice is put back into the pool.
|
|
type Buffer struct {
|
|
Data []byte
|
|
pool *BufferPool
|
|
}
|
|
|
|
// Release puts the buffer back into the pool it came from.
|
|
func (b *Buffer) Release() {
|
|
pool := b.pool
|
|
if pool == nil || cap(b.Data) > pool.defaultSize {
|
|
return
|
|
}
|
|
|
|
select {
|
|
case pool.ch <- b:
|
|
default:
|
|
}
|
|
}
|
|
|
|
// BufferPool implements a limited set of reusable buffers.
|
|
type BufferPool struct {
|
|
ch chan *Buffer
|
|
defaultSize int
|
|
}
|
|
|
|
// NewBufferPool initializes a new buffer pool. The pool stores at most max
|
|
// items. New buffers are created with defaultSize. Buffers that have grown
|
|
// larger are not put back.
|
|
func NewBufferPool(max int, defaultSize int) *BufferPool {
|
|
b := &BufferPool{
|
|
ch: make(chan *Buffer, max),
|
|
defaultSize: defaultSize,
|
|
}
|
|
return b
|
|
}
|
|
|
|
// Get returns a new buffer, either from the pool or newly allocated.
|
|
func (pool *BufferPool) Get() *Buffer {
|
|
select {
|
|
case buf := <-pool.ch:
|
|
return buf
|
|
default:
|
|
}
|
|
|
|
b := &Buffer{
|
|
Data: make([]byte, pool.defaultSize),
|
|
pool: pool,
|
|
}
|
|
|
|
return b
|
|
}
|