diff options
Diffstat (limited to 'internal/bufpool/capacity.go')
| -rw-r--r-- | internal/bufpool/capacity.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/internal/bufpool/capacity.go b/internal/bufpool/capacity.go new file mode 100644 index 00000000..ecbd7d76 --- /dev/null +++ b/internal/bufpool/capacity.go @@ -0,0 +1,37 @@ +package bufpool + +// ensureCapacity grows the underlying buffer to accommodate the requested +// number of bytes. Growth doubles the capacity by default unless a larger +// expansion is needed. If the previous storage was pooled and not oversized, +// it is returned to the pool. +func (buf *Buffer) ensureCapacity(needed int) { + if cap(buf.buf) >= needed { + return + } + + classIdx, classCap, pooled := classFor(needed) + + var newBuf []byte + + if pooled { + //nolint:forcetypeassert + raw := bufferPools[classIdx].Get().(*[]byte) + if cap(*raw) < classCap { + *raw = make([]byte, 0, classCap) + } + + newBuf = (*raw)[:len(buf.buf)] + } else { + newBuf = make([]byte, len(buf.buf), classCap) + } + + copy(newBuf, buf.buf) + buf.returnToPool() + + buf.buf = newBuf + if pooled { + buf.pool = poolIndex(classIdx) //#nosec G115 + } else { + buf.pool = unpooled + } +} |
