Reserve ensures buffer has at least n bytes available for writing from current position. Call this before multiple unsafe writes to avoid repeated grow() calls.
(n int)
| 515 | // Reserve ensures buffer has at least n bytes available for writing from current position. |
| 516 | // Call this before multiple unsafe writes to avoid repeated grow() calls. |
| 517 | func (b *ByteBuffer) Reserve(n int) { |
| 518 | needed := b.writerIndex + n |
| 519 | if needed <= len(b.data) { |
| 520 | return // Already have enough space |
| 521 | } |
| 522 | // Need to expand - calculate new size |
| 523 | if needed <= cap(b.data) { |
| 524 | b.data = b.data[:cap(b.data)] |
| 525 | } else { |
| 526 | newCap := 2 * needed |
| 527 | newBuf := make([]byte, newCap, newCap) |
| 528 | copy(newBuf, b.data) |
| 529 | b.data = newBuf |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // UnsafeWriteVarint32 writes a varint32 without grow check. |
| 534 | // Caller must have called Reserve(5) beforehand. |
no outgoing calls