pull/320/head
Darien Raymond 2016-12-11 09:43:20 +01:00
parent bc80cc72c7
commit ff87377acf
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 23 additions and 14 deletions

View File

@ -19,16 +19,6 @@ type Buffer struct {
end int end int
} }
// CreateBuffer creates a new Buffer object based on given container and parent pool.
func CreateBuffer(container []byte, parent Pool) *Buffer {
b := new(Buffer)
b.v = container
b.pool = parent
b.start = 0
b.end = 0
return b
}
// Release recycles the buffer into an internal buffer pool. // Release recycles the buffer into an internal buffer pool.
func (b *Buffer) Release() { func (b *Buffer) Release() {
if b == nil || b.v == nil { if b == nil || b.v == nil {
@ -81,6 +71,7 @@ func (b *Buffer) Bytes() []byte {
return b.v[b.start:b.end] return b.v[b.start:b.end]
} }
// Reset resets the content of the Buffer with a supplier.
func (b *Buffer) Reset(writer Supplier) error { func (b *Buffer) Reset(writer Supplier) error {
b.start = 0 b.start = 0
nBytes, err := writer(b.v[b.start:]) nBytes, err := writer(b.v[b.start:])
@ -99,6 +90,7 @@ func (b *Buffer) BytesRange(from, to int) []byte {
return b.v[b.start+from : b.start+to] return b.v[b.start+from : b.start+to]
} }
// BytesFrom returns a slice of this Buffer starting from the given position.
func (b *Buffer) BytesFrom(from int) []byte { func (b *Buffer) BytesFrom(from int) []byte {
if from < 0 { if from < 0 {
from += b.Len() from += b.Len()
@ -106,6 +98,7 @@ func (b *Buffer) BytesFrom(from int) []byte {
return b.v[b.start+from : b.end] return b.v[b.start+from : b.end]
} }
// BytesFrom returns a slice of this Buffer from start to the given position.
func (b *Buffer) BytesTo(to int) []byte { func (b *Buffer) BytesTo(to int) []byte {
if to < 0 { if to < 0 {
to += b.Len() to += b.Len()
@ -175,6 +168,7 @@ func (b *Buffer) Read(data []byte) (int, error) {
return nBytes, nil return nBytes, nil
} }
// String returns the string form of this Buffer.
func (b *Buffer) String() string { func (b *Buffer) String() string {
return string(b.Bytes()) return string(b.Bytes())
} }
@ -191,5 +185,8 @@ func NewSmall() *Buffer {
// NewLocal creates and returns a buffer on current thread. // NewLocal creates and returns a buffer on current thread.
func NewLocal(size int) *Buffer { func NewLocal(size int) *Buffer {
return CreateBuffer(make([]byte, size), nil) return &Buffer{
v: make([]byte, size),
pool: nil,
}
} }

View File

@ -31,7 +31,10 @@ func NewSyncPool(bufferSize uint32) *SyncPool {
// Allocate implements Pool.Allocate(). // Allocate implements Pool.Allocate().
func (p *SyncPool) Allocate() *Buffer { func (p *SyncPool) Allocate() *Buffer {
return CreateBuffer(p.allocator.Get().([]byte), p) return &Buffer{
v: p.allocator.Get().([]byte),
pool: p,
}
} }
// Free implements Pool.Free(). // Free implements Pool.Free().
@ -43,11 +46,13 @@ func (p *SyncPool) Free(buffer *Buffer) {
p.allocator.Put(rawBuffer) p.allocator.Put(rawBuffer)
} }
// BufferPool is a Pool that utilizes an internal cache.
type BufferPool struct { type BufferPool struct {
chain chan []byte chain chan []byte
allocator *sync.Pool allocator *sync.Pool
} }
// NewBufferPool creates a new BufferPool with given buffer size, and internal cache size.
func NewBufferPool(bufferSize, poolSize uint32) *BufferPool { func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
pool := &BufferPool{ pool := &BufferPool{
chain: make(chan []byte, poolSize), chain: make(chan []byte, poolSize),
@ -61,6 +66,7 @@ func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
return pool return pool
} }
// Allocate implements Pool.Allocate().
func (p *BufferPool) Allocate() *Buffer { func (p *BufferPool) Allocate() *Buffer {
var b []byte var b []byte
select { select {
@ -68,9 +74,13 @@ func (p *BufferPool) Allocate() *Buffer {
default: default:
b = p.allocator.Get().([]byte) b = p.allocator.Get().([]byte)
} }
return CreateBuffer(b, p) return &Buffer{
v: b,
pool: p,
}
} }
// Free implements Pool.Free().
func (p *BufferPool) Free(buffer *Buffer) { func (p *BufferPool) Free(buffer *Buffer) {
rawBuffer := buffer.v rawBuffer := buffer.v
if rawBuffer == nil { if rawBuffer == nil {
@ -84,7 +94,9 @@ func (p *BufferPool) Free(buffer *Buffer) {
} }
const ( const (
// Size of a regular buffer.
Size = 8 * 1024 Size = 8 * 1024
// Size of a small buffer.
SizeSmall = 2 * 1024 SizeSmall = 2 * 1024
PoolSizeEnvKey = "v2ray.buffer.size" PoolSizeEnvKey = "v2ray.buffer.size"