github.com/igoogolx/clash@v1.19.8/common/pool/buffer.go (about)

     1  package pool
     2  
     3  import (
     4  	"bytes"
     5  	"sync"
     6  
     7  	"github.com/Dreamacro/protobytes"
     8  )
     9  
    10  var (
    11  	bufferPool      = sync.Pool{New: func() any { return &bytes.Buffer{} }}
    12  	bytesBufferPool = sync.Pool{New: func() any { return &protobytes.BytesWriter{} }}
    13  )
    14  
    15  func GetBuffer() *bytes.Buffer {
    16  	return bufferPool.Get().(*bytes.Buffer)
    17  }
    18  
    19  func PutBuffer(buf *bytes.Buffer) {
    20  	buf.Reset()
    21  	bufferPool.Put(buf)
    22  }
    23  
    24  func GetBytesBuffer() *protobytes.BytesWriter {
    25  	return bytesBufferPool.Get().(*protobytes.BytesWriter)
    26  }
    27  
    28  func PutBytesBuffer(buf *protobytes.BytesWriter) {
    29  	buf.Reset()
    30  	bytesBufferPool.Put(buf)
    31  }