grow grows the buffer to guarantee space for n more bytes. It returns the index where bytes should be written. If the buffer can't grow it will panic with ErrTooLarge.
(n int)
| 124 | // It returns the index where bytes should be written. |
| 125 | // If the buffer can't grow it will panic with ErrTooLarge. |
| 126 | func (b *Buffer) grow(n int) int { |
| 127 | // If we have no buffer, get one from the pool |
| 128 | m := b.Len() |
| 129 | if m == 0 { |
| 130 | if b.buf == nil { |
| 131 | b.buf = makeSlice(2 * n) |
| 132 | b.off = 0 |
| 133 | } else if b.off != 0 { |
| 134 | // If buffer is empty, reset to recover space. |
| 135 | b.Truncate(0) |
| 136 | } |
| 137 | } |
| 138 | if len(b.buf)+n > cap(b.buf) { |
| 139 | var buf []byte |
| 140 | if m+n <= cap(b.buf)/2 { |
| 141 | // We can slide things down instead of allocating a new |
| 142 | // slice. We only need m+n <= cap(b.buf) to slide, but |
| 143 | // we instead let capacity get twice as large so we |
| 144 | // don't spend all our time copying. |
| 145 | copy(b.buf[:], b.buf[b.off:]) |
| 146 | buf = b.buf[:m] |
| 147 | } else { |
| 148 | // not enough space anywhere |
| 149 | buf = makeSlice(2*cap(b.buf) + n) |
| 150 | copy(buf, b.buf[b.off:]) |
| 151 | Pool(b.buf) |
| 152 | b.buf = buf |
| 153 | } |
| 154 | b.off = 0 |
| 155 | } |
| 156 | b.buf = b.buf[0 : b.off+m+n] |
| 157 | return b.off + m |
| 158 | } |
| 159 | |
| 160 | // Grow grows the buffer's capacity, if necessary, to guarantee space for |
| 161 | // another n bytes. After Grow(n), at least n bytes can be written to the |