buffer_ext.go
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package bufpool
import "bytes"
// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as Truncate(0).
func (b *Buffer) Reset() {
if b.off > cap(b.buf) {
panic("Buffer is used after Put")
}
b.buf = b.buf[:0]
b.off = 0
b.lastRead = opInvalid
}
func (b *Buffer) ResetBuf(buf []byte) {
if b.off > cap(b.buf) {
panic("Buffer is used after Put")
}
b.buf = buf[:0]
b.off = 0
b.lastRead = opInvalid
}
// 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.
func (b *Buffer) grow(n int) int {
if b.off > cap(b.buf) {
panic("Buffer is used after Put")
}
m := b.Len()
// If buffer is empty, reset to recover space.
if m == 0 && b.off != 0 {
b.Reset()
}
// Try to grow by means of a reslice.
if i, ok := b.tryGrowByReslice(n); ok {
return i
}
if b.buf == nil && n <= smallBufferSize {
b.buf = make([]byte, n, smallBufferSize)
return 0
}
c := cap(b.buf)
if n <= c/2-m {
// We can slide things down instead of allocating a new
// slice. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we
// don't spend all our time copying.
copy(b.buf, b.buf[b.off:])
} else if c > maxInt-c-n {
panic(bytes.ErrTooLarge)
} else {
// Not enough space anywhere, we need to allocate.
tmp := Get(2*c + n)
copy(tmp.buf, b.buf[b.off:])
b.buf, tmp.buf = tmp.buf, b.buf
Put(tmp)
}
// Restore b.off and len(b.buf).
b.off = 0
b.buf = b.buf[:m+n]
return m
}