Source File
databuffer.go
Belonging Package
golang.org/x/net/http2
package http2
import (
)
var (
dataChunkSizeClasses = []int{
1 << 10,
2 << 10,
4 << 10,
8 << 10,
16 << 10,
}
dataChunkPools = [...]sync.Pool{
{New: func() interface{} { return make([]byte, 1<<10) }},
{New: func() interface{} { return make([]byte, 2<<10) }},
{New: func() interface{} { return make([]byte, 4<<10) }},
{New: func() interface{} { return make([]byte, 8<<10) }},
{New: func() interface{} { return make([]byte, 16<<10) }},
}
)
func ( int64) []byte {
:= 0
for ; < len(dataChunkSizeClasses)-1; ++ {
if <= int64(dataChunkSizeClasses[]) {
break
}
}
return dataChunkPools[].Get().([]byte)
}
func ( []byte) {
for , := range dataChunkSizeClasses {
if len() == {
dataChunkPools[].Put()
return
}
}
panic(fmt.Sprintf("unexpected buffer len=%v", len()))
}
type dataBuffer struct {
chunks [][]byte
r int // next byte to read is chunks[0][r]
w int // next byte to write is chunks[len(chunks)-1][w]
size int // total buffered bytes
expected int64 // we expect at least this many bytes in future Write calls (ignored if <= 0)
}
var errReadEmpty = errors.New("read from empty dataBuffer")
func ( *dataBuffer) ( []byte) (int, error) {
if .size == 0 {
return 0, errReadEmpty
}
var int
for len() > 0 && .size > 0 {
:= .bytesFromFirstChunk()
:= copy(, )
= [:]
+=
.r +=
func ( *dataBuffer) () int {
return .size
}
func ( *dataBuffer) ( []byte) (int, error) {
:= len()
:= int64(len())
if .expected > {
= .expected
}
:= .lastChunkOrAlloc()
:= copy([.w:], )
= [:]
.w +=
.size +=
.expected -= int64()
}
return , nil
}
func ( *dataBuffer) ( int64) []byte {
if len(.chunks) != 0 {
:= .chunks[len(.chunks)-1]
if .w < len() {
return
}
}
:= getDataBufferChunk()
.chunks = append(.chunks, )
.w = 0
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |