diff options
author | 2021-11-13 12:29:08 +0100 | |
---|---|---|
committer | 2021-11-13 12:29:08 +0100 | |
commit | 829a934d23ab221049b4d54926305d8d5d64c9ad (patch) | |
tree | f4e382b289c113d3ba8a3c7a183507a5609c46c0 /vendor/codeberg.org/gruf/go-pools/bytes.go | |
parent | smtp + email confirmation (#285) (diff) | |
download | gotosocial-829a934d23ab221049b4d54926305d8d5d64c9ad.tar.xz |
update dependencies (#296)
Diffstat (limited to 'vendor/codeberg.org/gruf/go-pools/bytes.go')
-rw-r--r-- | vendor/codeberg.org/gruf/go-pools/bytes.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/vendor/codeberg.org/gruf/go-pools/bytes.go b/vendor/codeberg.org/gruf/go-pools/bytes.go new file mode 100644 index 000000000..76fe18616 --- /dev/null +++ b/vendor/codeberg.org/gruf/go-pools/bytes.go @@ -0,0 +1,46 @@ +package pools + +import ( + "sync" + + "codeberg.org/gruf/go-bytes" +) + +// BufferPool is a pooled allocator for bytes.Buffer objects +type BufferPool interface { + // Get fetches a bytes.Buffer from pool + Get() *bytes.Buffer + + // Put places supplied bytes.Buffer in pool + Put(*bytes.Buffer) +} + +// NewBufferPool returns a newly instantiated bytes.Buffer pool +func NewBufferPool(size int) BufferPool { + return &bufferPool{ + pool: sync.Pool{ + New: func() interface{} { + return &bytes.Buffer{B: make([]byte, 0, size)} + }, + }, + size: size, + } +} + +// bufferPool is our implementation of BufferPool +type bufferPool struct { + pool sync.Pool + size int +} + +func (p *bufferPool) Get() *bytes.Buffer { + return p.pool.Get().(*bytes.Buffer) +} + +func (p *bufferPool) Put(buf *bytes.Buffer) { + if buf.Cap() < p.size { + return + } + buf.Reset() + p.pool.Put(buf) +} |