aboutsummaryrefslogtreecommitdiff
path: root/internal/bufpool/borrow.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/borrow.go
parentdiff/lines: Split files (diff)
signatureNo signature
internal/bufpool: Split files
Diffstat (limited to 'internal/bufpool/borrow.go')
-rw-r--r--internal/bufpool/borrow.go31
1 files changed, 31 insertions, 0 deletions
diff --git a/internal/bufpool/borrow.go b/internal/bufpool/borrow.go
new file mode 100644
index 00000000..ff212a9b
--- /dev/null
+++ b/internal/bufpool/borrow.go
@@ -0,0 +1,31 @@
+package bufpool
+
+// Borrow retrieves a Buffer suitable for storing up to capHint bytes.
+// The returned Buffer may come from an internal sync.Pool.
+//
+// If capHint is smaller than DefaultBufferCap, it is automatically raised
+// to DefaultBufferCap. If no pooled buffer has sufficient capacity, a new
+// unpooled buffer is allocated.
+//
+// The caller must call Release() when finished using the returned Buffer.
+func Borrow(capHint int) Buffer {
+ if capHint < DefaultBufferCap {
+ capHint = DefaultBufferCap
+ }
+
+ classIdx, classCap, pooled := classFor(capHint)
+ if !pooled {
+ newBuf := make([]byte, 0, capHint)
+
+ return Buffer{buf: newBuf, pool: unpooled}
+ }
+ //nolint:forcetypeassert
+ buf := bufferPools[classIdx].Get().(*[]byte)
+ if cap(*buf) < classCap {
+ *buf = make([]byte, 0, classCap)
+ }
+
+ slice := (*buf)[:0]
+
+ return Buffer{buf: slice, pool: poolIndex(classIdx)} //#nosec G115
+}