pull/314/head
Darien Raymond 8 years ago
parent 5f6366d2fa
commit 36e4330e58
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -8,14 +8,18 @@ import (
// Pool provides functionality to generate and recycle buffers on demand. // Pool provides functionality to generate and recycle buffers on demand.
type Pool interface { type Pool interface {
// Allocate either returns a unused buffer from the pool, or generates a new one from system.
Allocate() *Buffer Allocate() *Buffer
// Free recycles the given buffer.
Free(*Buffer) Free(*Buffer)
} }
// SyncPool is a buffer pool based on sync.Pool
type SyncPool struct { type SyncPool struct {
allocator *sync.Pool allocator *sync.Pool
} }
// NewSyncPool creates a SyncPool with given buffer size.
func NewSyncPool(bufferSize uint32) *SyncPool { func NewSyncPool(bufferSize uint32) *SyncPool {
pool := &SyncPool{ pool := &SyncPool{
allocator: &sync.Pool{ allocator: &sync.Pool{
@ -25,10 +29,12 @@ func NewSyncPool(bufferSize uint32) *SyncPool {
return pool return pool
} }
// Allocate implements Pool.Allocate().
func (p *SyncPool) Allocate() *Buffer { func (p *SyncPool) Allocate() *Buffer {
return CreateBuffer(p.allocator.Get().([]byte), p) return CreateBuffer(p.allocator.Get().([]byte), p)
} }
// Free implements Pool.Free().
func (p *SyncPool) Free(buffer *Buffer) { func (p *SyncPool) Free(buffer *Buffer) {
rawBuffer := buffer.head rawBuffer := buffer.head
if rawBuffer == nil { if rawBuffer == nil {

@ -63,6 +63,7 @@ func (v *AdaptiveReader) Read() (*alloc.Buffer, error) {
return buffer, nil return buffer, nil
} }
// Release implements Releasable.Release().
func (v *AdaptiveReader) Release() { func (v *AdaptiveReader) Release() {
v.reader = nil v.reader = nil
} }

@ -2,10 +2,12 @@ package io
import ( import (
"io" "io"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
) )
// Pipe dumps all content from reader to writer, until an error happens.
func Pipe(reader Reader, writer Writer) error { func Pipe(reader Reader, writer Writer) error {
for { for {
buffer, err := reader.Read() buffer, err := reader.Read()
@ -28,6 +30,7 @@ func Pipe(reader Reader, writer Writer) error {
} }
} }
// PipeUntilEOF behaves the same as Pipe(). The only difference is PipeUntilEOF returns nil on EOF.
func PipeUntilEOF(reader Reader, writer Writer) error { func PipeUntilEOF(reader Reader, writer Writer) error {
err := Pipe(reader, writer) err := Pipe(reader, writer)
if err != nil && errors.Cause(err) != io.EOF { if err != nil && errors.Cause(err) != io.EOF {

@ -39,6 +39,7 @@ func (v *AdaptiveWriter) Write(buffer *alloc.Buffer) error {
return nil return nil
} }
// Release implements Releasable.Release().
func (v *AdaptiveWriter) Release() { func (v *AdaptiveWriter) Release() {
v.writer = nil v.writer = nil
} }

Loading…
Cancel
Save