1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-08-31 00:34:14 -04:00
v2fly/external/github.com/lucas-clemente/quic-go/buffer_pool.go

57 lines
1.3 KiB
Go
Raw Normal View History

2018-11-20 17:51:25 -05:00
package quic
import (
"sync"
2019-01-17 09:33:18 -05:00
"v2ray.com/core/external/github.com/lucas-clemente/quic-go/internal/protocol"
2019-01-02 07:01:06 -05:00
"v2ray.com/core/common/bytespool"
2018-11-20 17:51:25 -05:00
)
2019-01-02 07:01:06 -05:00
type packetBuffer struct {
Slice []byte
2018-11-20 17:51:25 -05:00
2019-01-02 07:01:06 -05:00
// refCount counts how many packets the Slice is used in.
// It doesn't support concurrent use.
// It is > 1 when used for coalesced packet.
refCount int
2018-11-20 17:51:25 -05:00
}
2019-01-02 07:01:06 -05:00
// 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 (b *packetBuffer) Split() {
b.refCount++
}
// Release decreases the refCount.
// It should be called when processing the packet is finished.
// When the refCount reaches 0, the packet buffer is put back into the pool.
func (b *packetBuffer) Release() {
if cap(b.Slice) < 2048 {
2018-11-21 05:03:47 -05:00
return
2018-11-20 17:51:25 -05:00
}
2019-01-02 07:01:06 -05:00
b.refCount--
if b.refCount < 0 {
panic("negative packetBuffer refCount")
}
// only put the packetBuffer back if it's not used any more
if b.refCount == 0 {
2019-01-02 07:13:50 -05:00
buffer := b.Slice[0:cap(b.Slice)]
bufferPool.Put(buffer)
2019-01-02 07:01:06 -05:00
}
}
var bufferPool *sync.Pool
func getPacketBuffer() *packetBuffer {
buffer := bufferPool.Get().([]byte)
return &packetBuffer{
refCount: 1,
Slice: buffer[:protocol.MaxReceivePacketSize],
}
2018-11-20 17:51:25 -05:00
}
func init() {
2018-11-21 04:52:07 -05:00
bufferPool = bytespool.GetPool(int32(protocol.MaxReceivePacketSize))
2018-11-20 17:51:25 -05:00
}