mirror of https://github.com/v2ray/v2ray-core
Merge branch 'master' of https://github.com/v2ray/v2ray-core
commit
d6e6b464c1
|
@ -9,6 +9,7 @@ const (
|
||||||
defaultOffset = 16
|
defaultOffset = 16
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// BytesWriter is a writer that writes contents into the given buffer.
|
||||||
type BytesWriter func([]byte) int
|
type BytesWriter func([]byte) int
|
||||||
|
|
||||||
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
|
// Buffer is a recyclable allocation of a byte array. Buffer.Release() recycles
|
||||||
|
@ -22,6 +23,7 @@ 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 {
|
func CreateBuffer(container []byte, parent Pool) *Buffer {
|
||||||
b := new(Buffer)
|
b := new(Buffer)
|
||||||
b.v = container
|
b.v = container
|
||||||
|
@ -67,6 +69,7 @@ func (b *Buffer) Append(data []byte) {
|
||||||
b.end += nBytes
|
b.end += nBytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppendFunc appends the content of a BytesWriter to the buffer.
|
||||||
func (b *Buffer) AppendFunc(writer BytesWriter) {
|
func (b *Buffer) AppendFunc(writer BytesWriter) {
|
||||||
nBytes := writer(b.v[b.end:])
|
nBytes := writer(b.v[b.end:])
|
||||||
b.end += nBytes
|
b.end += nBytes
|
||||||
|
@ -79,6 +82,7 @@ func (b *Buffer) Prepend(data []byte) {
|
||||||
copy(b.v[b.start:], data)
|
copy(b.v[b.start:], data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrependBytes prepends all data in front of the buffer.
|
||||||
func (b *Buffer) PrependBytes(data ...byte) {
|
func (b *Buffer) PrependBytes(data ...byte) {
|
||||||
b.Prepend(data)
|
b.Prepend(data)
|
||||||
}
|
}
|
||||||
|
@ -88,10 +92,12 @@ func (b *Buffer) PrependFunc(offset int, writer BytesWriter) {
|
||||||
writer(b.v[b.start:])
|
writer(b.v[b.start:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Byte returns the bytes at index.
|
||||||
func (b *Buffer) Byte(index int) byte {
|
func (b *Buffer) Byte(index int) byte {
|
||||||
return b.v[b.start+index]
|
return b.v[b.start+index]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetByte sets the byte value at index.
|
||||||
func (b *Buffer) SetByte(index int, value byte) {
|
func (b *Buffer) SetByte(index int, value byte) {
|
||||||
b.v[b.start+index] = value
|
b.v[b.start+index] = value
|
||||||
}
|
}
|
||||||
|
@ -105,6 +111,7 @@ func (b *Buffer) SetBytesFunc(writer BytesWriter) {
|
||||||
b.end = b.start + writer(b.v[b.start:])
|
b.end = b.start + writer(b.v[b.start:])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BytesRange returns a slice of this buffer with given from and to bounary.
|
||||||
func (b *Buffer) BytesRange(from, to int) []byte {
|
func (b *Buffer) BytesRange(from, to int) []byte {
|
||||||
if from < 0 {
|
if from < 0 {
|
||||||
from += b.Len()
|
from += b.Len()
|
||||||
|
@ -169,6 +176,7 @@ func (b *Buffer) Len() int {
|
||||||
return b.end - b.start
|
return b.end - b.start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsEmpty returns true if the buffer is empty.
|
||||||
func (b *Buffer) IsEmpty() bool {
|
func (b *Buffer) IsEmpty() bool {
|
||||||
return b.Len() == 0
|
return b.Len() == 0
|
||||||
}
|
}
|
||||||
|
@ -220,10 +228,12 @@ func NewBuffer() *Buffer {
|
||||||
return mediumPool.Allocate()
|
return mediumPool.Allocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSmallBuffer returns a buffer with 2K bytes capacity.
|
||||||
func NewSmallBuffer() *Buffer {
|
func NewSmallBuffer() *Buffer {
|
||||||
return smallPool.Allocate()
|
return smallPool.Allocate()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewLocalBuffer creates and returns a buffer on current thread.
|
||||||
func NewLocalBuffer(size int) *Buffer {
|
func NewLocalBuffer(size int) *Buffer {
|
||||||
return CreateBuffer(make([]byte, size), nil)
|
return CreateBuffer(make([]byte, size), nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,15 +6,20 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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{
|
||||||
|
@ -24,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.v
|
rawBuffer := buffer.v
|
||||||
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…
Reference in New Issue