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

56 lines
1.2 KiB
Go
Raw Normal View History

2018-11-20 17:51:25 -05:00
package quic
import (
"sync"
"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 {
bufferPool.Put(b.Slice)
}
}
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
}