clash/common/pool/buffer.go

32 lines
593 B
Go
Raw Normal View History

2021-09-20 21:02:18 +08:00
package pool
import (
"bytes"
"sync"
"github.com/Dreamacro/protobytes"
2021-09-20 21:02:18 +08:00
)
var (
bufferPool = sync.Pool{New: func() any { return &bytes.Buffer{} }}
bytesBufferPool = sync.Pool{New: func() any { return &protobytes.BytesWriter{} }}
)
2021-09-20 21:02:18 +08:00
func GetBuffer() *bytes.Buffer {
return bufferPool.Get().(*bytes.Buffer)
}
func PutBuffer(buf *bytes.Buffer) {
buf.Reset()
bufferPool.Put(buf)
}
func GetBytesBuffer() *protobytes.BytesWriter {
return bytesBufferPool.Get().(*protobytes.BytesWriter)
}
func PutBytesBuffer(buf *protobytes.BytesWriter) {
buf.Reset()
2023-04-20 11:07:21 +08:00
bytesBufferPool.Put(buf)
}