2024-08-28 21:40:52 -04:00
|
|
|
// Copyright (c) 2015-2024 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
package bpool
|
|
|
|
|
2024-08-28 21:40:52 -04:00
|
|
|
import (
|
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
2023-12-21 11:59:38 -05:00
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// BytePoolCap implements a leaky pool of []byte in the form of a bounded channel.
|
|
|
|
type BytePoolCap struct {
|
|
|
|
c chan []byte
|
|
|
|
w int
|
|
|
|
wcap int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBytePoolCap creates a new BytePool bounded to the given maxSize, with new
|
|
|
|
// byte arrays sized based on width.
|
2024-01-30 14:13:27 -05:00
|
|
|
func NewBytePoolCap(maxSize uint64, width int, capwidth int) (bp *BytePoolCap) {
|
2024-08-28 21:40:52 -04:00
|
|
|
if capwidth <= 0 {
|
|
|
|
panic("total buffer capacity must be provided")
|
|
|
|
}
|
|
|
|
if capwidth < 64 {
|
2023-12-21 11:59:38 -05:00
|
|
|
panic("buffer capped with smaller than 64 bytes is not supported")
|
|
|
|
}
|
2024-08-28 21:40:52 -04:00
|
|
|
if width > capwidth {
|
|
|
|
panic("minimum buffer length cannot be > capacity of the buffer")
|
2023-12-21 11:59:38 -05:00
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
return &BytePoolCap{
|
|
|
|
c: make(chan []byte, maxSize),
|
|
|
|
w: width,
|
|
|
|
wcap: capwidth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-21 11:59:38 -05:00
|
|
|
// Populate - populates and pre-warms the byte pool, this function is non-blocking.
|
|
|
|
func (bp *BytePoolCap) Populate() {
|
|
|
|
for _, buf := range reedsolomon.AllocAligned(cap(bp.c), bp.wcap) {
|
|
|
|
bp.Put(buf[:bp.w])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// Get gets a []byte from the BytePool, or creates a new one if none are
|
|
|
|
// available in the pool.
|
|
|
|
func (bp *BytePoolCap) Get() (b []byte) {
|
2024-04-19 12:44:59 -04:00
|
|
|
if bp == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
select {
|
|
|
|
case b = <-bp.c:
|
2023-12-21 11:59:38 -05:00
|
|
|
// reuse existing buffer
|
2018-02-15 20:45:57 -05:00
|
|
|
default:
|
2023-12-21 11:59:38 -05:00
|
|
|
// create new aligned buffer
|
2024-08-28 21:40:52 -04:00
|
|
|
b = reedsolomon.AllocAligned(1, bp.wcap)[0][:bp.w]
|
2018-02-15 20:45:57 -05:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put returns the given Buffer to the BytePool.
|
|
|
|
func (bp *BytePoolCap) Put(b []byte) {
|
2024-04-19 12:44:59 -04:00
|
|
|
if bp == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-08-28 21:40:52 -04:00
|
|
|
|
|
|
|
if cap(b) != bp.wcap {
|
|
|
|
// someone tried to put back buffer which is not part of this buffer pool
|
|
|
|
// we simply don't put this back into pool, a modified buffer provided
|
|
|
|
// by this package is no more usable, callers make sure to not modify
|
|
|
|
// the capacity of the buffer.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
select {
|
2024-08-28 21:40:52 -04:00
|
|
|
case bp.c <- b[:bp.w]:
|
2018-02-15 20:45:57 -05:00
|
|
|
// buffer went back into pool
|
|
|
|
default:
|
|
|
|
// buffer didn't go back into pool, just discard
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Width returns the width of the byte arrays in this pool.
|
|
|
|
func (bp *BytePoolCap) Width() (n int) {
|
2024-04-19 12:44:59 -04:00
|
|
|
if bp == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
return bp.w
|
|
|
|
}
|
|
|
|
|
|
|
|
// WidthCap returns the cap width of the byte arrays in this pool.
|
|
|
|
func (bp *BytePoolCap) WidthCap() (n int) {
|
2024-04-19 12:44:59 -04:00
|
|
|
if bp == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2018-02-15 20:45:57 -05:00
|
|
|
return bp.wcap
|
|
|
|
}
|
2024-08-28 21:40:52 -04:00
|
|
|
|
|
|
|
// CurrentSize returns current size of buffer pool
|
|
|
|
func (bp *BytePoolCap) CurrentSize() int {
|
|
|
|
if bp == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return len(bp.c) * bp.w
|
|
|
|
}
|