aboutsummaryrefslogtreecommitdiff
path: root/internal/bufpool/capacity.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-03-06 11:22:29 +0800
committerGravatar Runxi Yu2026-03-06 11:22:29 +0800
commit374ca2159407c6f3ec786bc19e25da44ded62fcf (patch)
tree4af6ccdb5057f84f0b55a2151231a8f33ed4b41e /internal/bufpool/capacity.go
parentdiff/lines: Split files (diff)
signatureNo signature
internal/bufpool: Split files
Diffstat (limited to 'internal/bufpool/capacity.go')
-rw-r--r--internal/bufpool/capacity.go37
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
+ }
+}