Source File
buffer_pool.go
Belonging Package
github.com/quic-go/quic-go
package quicimport ()type packetBuffer struct {Data []byte// refCount counts how many packets Data is used in.// It doesn't support concurrent use.// It is > 1 when used for coalesced packet.refCount int}// Split increases the refCount.// It must be called when a packet buffer is used for more than one packet,// e.g. when splitting coalesced packets.func ( *packetBuffer) () {.refCount++}// Decrement decrements the reference counter.// It doesn't put the buffer back into the pool.func ( *packetBuffer) () {.refCount--if .refCount < 0 {panic("negative packetBuffer refCount")}}// MaybeRelease puts the packet buffer back into the pool,// if the reference counter already reached 0.func ( *packetBuffer) () {// only put the packetBuffer back if it's not used any moreif .refCount == 0 {.putBack()}}// Release puts back the packet buffer into the pool.// It should be called when processing is definitely finished.func ( *packetBuffer) () {.Decrement()if .refCount != 0 {panic("packetBuffer refCount not zero")}.putBack()}// Len returns the length of Datafunc ( *packetBuffer) () protocol.ByteCount { return protocol.ByteCount(len(.Data)) }func ( *packetBuffer) () protocol.ByteCount { return protocol.ByteCount(cap(.Data)) }func ( *packetBuffer) () {if cap(.Data) == protocol.MaxPacketBufferSize {bufferPool.Put()return}if cap(.Data) == protocol.MaxLargePacketBufferSize {largeBufferPool.Put()return}panic("putPacketBuffer called with packet of wrong size!")}var bufferPool, largeBufferPool sync.Poolfunc getPacketBuffer() *packetBuffer {:= bufferPool.Get().(*packetBuffer).refCount = 1.Data = .Data[:0]return}func getLargePacketBuffer() *packetBuffer {:= largeBufferPool.Get().(*packetBuffer).refCount = 1.Data = .Data[:0]return}func init() {bufferPool.New = func() any {return &packetBuffer{Data: make([]byte, 0, protocol.MaxPacketBufferSize)}}largeBufferPool.New = func() any {return &packetBuffer{Data: make([]byte, 0, protocol.MaxLargePacketBufferSize)}}}
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux 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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |