Source File
buffer_ext.go
Belonging Package
github.com/vmihailenco/bufpool
package bufpool
import
// 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 ( *Buffer) () {
if .off > cap(.buf) {
panic("Buffer is used after Put")
}
.buf = .buf[:0]
.off = 0
.lastRead = opInvalid
}
func ( *Buffer) ( []byte) {
if .off > cap(.buf) {
panic("Buffer is used after Put")
}
.buf = [:0]
.off = 0
.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 ( *Buffer) ( int) int {
if .off > cap(.buf) {
panic("Buffer is used after Put")
}
:= .Len()
// If buffer is empty, reset to recover space.
if == 0 && .off != 0 {
.Reset()
}
// Try to grow by means of a reslice.
if , := .tryGrowByReslice(); {
return
}
if .buf == nil && <= smallBufferSize {
.buf = make([]byte, , smallBufferSize)
return 0
}
:= cap(.buf)
if <= /2- {
// 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(.buf, .buf[.off:])
} else if > maxInt-- {
panic(bytes.ErrTooLarge)
} else {
// Not enough space anywhere, we need to allocate.
:= Get(2* + )
copy(.buf, .buf[.off:])
.buf, .buf = .buf, .buf
Put()
}
// Restore b.off and len(b.buf).
.off = 0
.buf = .buf[:+]
return
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)