github.com/yaling888/clash@v1.53.0/common/pool/pool.go (about)

     1  package pool
     2  
     3  import "sync"
     4  
     5  const (
     6  	NetBufferSize = 64 << 10
     7  )
     8  
     9  var netBufferPool = sync.Pool{
    10  	New: func() any {
    11  		b := make([]byte, NetBufferSize)
    12  		return &b
    13  	},
    14  }
    15  
    16  func GetNetBuf() *[]byte {
    17  	return netBufferPool.Get().(*[]byte)
    18  }
    19  
    20  func PutNetBuf(bufP *[]byte) {
    21  	if bufP == nil {
    22  		panic("bufP is nil")
    23  	}
    24  	if cap(*bufP) < NetBufferSize {
    25  		panic("invalid bufP capacity")
    26  	}
    27  	if len(*bufP) < NetBufferSize {
    28  		*bufP = (*bufP)[:NetBufferSize]
    29  	}
    30  	netBufferPool.Put(bufP)
    31  }