mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-27 20:45:28 -05:00
Merge branch 'master' of https://github.com/v2ray/v2ray-core
This commit is contained in:
@@ -6,15 +6,20 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Pool provides functionality to generate and recycle buffers on demand.
|
||||
type Pool interface {
|
||||
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
|
||||
Allocate() *Buffer
|
||||
// Free recycles the given buffer.
|
||||
Free(*Buffer)
|
||||
}
|
||||
|
||||
// SyncPool is a buffer pool based on sync.Pool
|
||||
type SyncPool struct {
|
||||
allocator *sync.Pool
|
||||
}
|
||||
|
||||
// NewSyncPool creates a SyncPool with given buffer size.
|
||||
func NewSyncPool(bufferSize uint32) *SyncPool {
|
||||
pool := &SyncPool{
|
||||
allocator: &sync.Pool{
|
||||
@@ -24,10 +29,12 @@ func NewSyncPool(bufferSize uint32) *SyncPool {
|
||||
return pool
|
||||
}
|
||||
|
||||
// Allocate implements Pool.Allocate().
|
||||
func (p *SyncPool) Allocate() *Buffer {
|
||||
return CreateBuffer(p.allocator.Get().([]byte), p)
|
||||
}
|
||||
|
||||
// Free implements Pool.Free().
|
||||
func (p *SyncPool) Free(buffer *Buffer) {
|
||||
rawBuffer := buffer.v
|
||||
if rawBuffer == nil {
|
||||
|
||||
Reference in New Issue
Block a user