2016-04-12 10:52:57 -04:00
|
|
|
package alloc
|
|
|
|
|
|
|
|
import (
|
2016-08-25 05:21:32 -04:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2016-04-12 10:52:57 -04:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2016-12-06 11:26:51 -05:00
|
|
|
// Pool provides functionality to generate and recycle buffers on demand.
|
2016-07-28 10:24:15 -04:00
|
|
|
type Pool interface {
|
2016-12-06 11:36:28 -05:00
|
|
|
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
|
2016-07-28 10:24:15 -04:00
|
|
|
Allocate() *Buffer
|
2016-12-06 11:36:28 -05:00
|
|
|
// Free recycles the given buffer.
|
2016-07-28 10:24:15 -04:00
|
|
|
Free(*Buffer)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:36:28 -05:00
|
|
|
// SyncPool is a buffer pool based on sync.Pool
|
2016-11-21 16:08:34 -05:00
|
|
|
type SyncPool struct {
|
|
|
|
allocator *sync.Pool
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:36:28 -05:00
|
|
|
// NewSyncPool creates a SyncPool with given buffer size.
|
2016-11-21 16:08:34 -05:00
|
|
|
func NewSyncPool(bufferSize uint32) *SyncPool {
|
|
|
|
pool := &SyncPool{
|
|
|
|
allocator: &sync.Pool{
|
|
|
|
New: func() interface{} { return make([]byte, bufferSize) },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:36:28 -05:00
|
|
|
// Allocate implements Pool.Allocate().
|
2016-11-21 16:08:34 -05:00
|
|
|
func (p *SyncPool) Allocate() *Buffer {
|
|
|
|
return CreateBuffer(p.allocator.Get().([]byte), p)
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:36:28 -05:00
|
|
|
// Free implements Pool.Free().
|
2016-11-21 16:08:34 -05:00
|
|
|
func (p *SyncPool) Free(buffer *Buffer) {
|
2016-12-06 07:42:12 -05:00
|
|
|
rawBuffer := buffer.v
|
2016-11-21 16:08:34 -05:00
|
|
|
if rawBuffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.allocator.Put(rawBuffer)
|
|
|
|
}
|
|
|
|
|
2016-04-12 10:52:57 -04:00
|
|
|
type BufferPool struct {
|
|
|
|
chain chan []byte
|
|
|
|
allocator *sync.Pool
|
|
|
|
}
|
|
|
|
|
2016-08-25 05:21:32 -04:00
|
|
|
func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
|
2016-04-12 10:52:57 -04:00
|
|
|
pool := &BufferPool{
|
|
|
|
chain: make(chan []byte, poolSize),
|
|
|
|
allocator: &sync.Pool{
|
|
|
|
New: func() interface{} { return make([]byte, bufferSize) },
|
|
|
|
},
|
|
|
|
}
|
2016-08-25 05:21:32 -04:00
|
|
|
for i := uint32(0); i < poolSize; i++ {
|
2016-04-12 10:52:57 -04:00
|
|
|
pool.chain <- make([]byte, bufferSize)
|
|
|
|
}
|
|
|
|
return pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BufferPool) Allocate() *Buffer {
|
|
|
|
var b []byte
|
|
|
|
select {
|
|
|
|
case b = <-p.chain:
|
|
|
|
default:
|
|
|
|
b = p.allocator.Get().([]byte)
|
|
|
|
}
|
2016-11-19 15:13:00 -05:00
|
|
|
return CreateBuffer(b, p)
|
2016-04-12 10:52:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *BufferPool) Free(buffer *Buffer) {
|
2016-12-06 07:42:12 -05:00
|
|
|
rawBuffer := buffer.v
|
2016-04-12 10:52:57 -04:00
|
|
|
if rawBuffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case p.chain <- rawBuffer:
|
|
|
|
default:
|
|
|
|
p.allocator.Put(rawBuffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 13:54:20 -04:00
|
|
|
const (
|
2016-08-25 05:21:32 -04:00
|
|
|
mediumBufferByteSize = 8 * 1024
|
|
|
|
BufferSize = mediumBufferByteSize - defaultOffset
|
|
|
|
|
2016-11-21 16:08:34 -05:00
|
|
|
smallBufferByteSize = 2 * 1024
|
|
|
|
SmallBufferSize = smallBufferByteSize - defaultOffset
|
2016-08-25 05:21:32 -04:00
|
|
|
|
|
|
|
PoolSizeEnvKey = "v2ray.buffer.size"
|
2016-05-11 13:54:20 -04:00
|
|
|
)
|
|
|
|
|
2016-08-25 05:21:32 -04:00
|
|
|
var (
|
2016-12-04 18:48:41 -05:00
|
|
|
mediumPool Pool
|
2016-11-21 16:08:34 -05:00
|
|
|
smallPool = NewSyncPool(2048)
|
2016-08-25 05:21:32 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
var size uint32 = 20
|
|
|
|
sizeStr := os.Getenv(PoolSizeEnvKey)
|
|
|
|
if len(sizeStr) > 0 {
|
|
|
|
customSize, err := strconv.ParseUint(sizeStr, 10, 32)
|
|
|
|
if err == nil {
|
|
|
|
size = uint32(customSize)
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 18:48:41 -05:00
|
|
|
if size > 0 {
|
|
|
|
totalByteSize := size * 1024 * 1024
|
|
|
|
mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/mediumBufferByteSize)
|
|
|
|
} else {
|
|
|
|
mediumPool = NewSyncPool(mediumBufferByteSize)
|
|
|
|
}
|
2016-08-25 05:21:32 -04:00
|
|
|
}
|