github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/quic/gquic-go/buffer_pool.go (about)

     1  package gquic
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic/gquic-go/internal/protocol"
     7  )
     8  
     9  var bufferPool sync.Pool
    10  
    11  func getPacketBuffer() *[]byte {
    12  	return bufferPool.Get().(*[]byte)
    13  }
    14  
    15  func putPacketBuffer(buf *[]byte) {
    16  	if cap(*buf) != int(protocol.MaxReceivePacketSize) {
    17  		panic("putPacketBuffer called with packet of wrong size!")
    18  	}
    19  	bufferPool.Put(buf)
    20  }
    21  
    22  func init() {
    23  	bufferPool.New = func() interface{} {
    24  		b := make([]byte, 0, protocol.MaxReceivePacketSize)
    25  		return &b
    26  	}
    27  }