mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
comments
This commit is contained in:
parent
bc80cc72c7
commit
ff87377acf
@ -19,16 +19,6 @@ type Buffer struct {
|
||||
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.
|
||||
func (b *Buffer) Release() {
|
||||
if b == nil || b.v == nil {
|
||||
@ -81,6 +71,7 @@ func (b *Buffer) Bytes() []byte {
|
||||
return b.v[b.start:b.end]
|
||||
}
|
||||
|
||||
// Reset resets the content of the Buffer with a supplier.
|
||||
func (b *Buffer) Reset(writer Supplier) error {
|
||||
b.start = 0
|
||||
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]
|
||||
}
|
||||
|
||||
// BytesFrom returns a slice of this Buffer starting from the given position.
|
||||
func (b *Buffer) BytesFrom(from int) []byte {
|
||||
if from < 0 {
|
||||
from += b.Len()
|
||||
@ -106,6 +98,7 @@ func (b *Buffer) BytesFrom(from int) []byte {
|
||||
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 {
|
||||
if to < 0 {
|
||||
to += b.Len()
|
||||
@ -175,6 +168,7 @@ func (b *Buffer) Read(data []byte) (int, error) {
|
||||
return nBytes, nil
|
||||
}
|
||||
|
||||
// String returns the string form of this Buffer.
|
||||
func (b *Buffer) String() string {
|
||||
return string(b.Bytes())
|
||||
}
|
||||
@ -191,5 +185,8 @@ func NewSmall() *Buffer {
|
||||
|
||||
// NewLocal creates and returns a buffer on current thread.
|
||||
func NewLocal(size int) *Buffer {
|
||||
return CreateBuffer(make([]byte, size), nil)
|
||||
return &Buffer{
|
||||
v: make([]byte, size),
|
||||
pool: nil,
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,10 @@ func NewSyncPool(bufferSize uint32) *SyncPool {
|
||||
|
||||
// Allocate implements Pool.Allocate().
|
||||
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().
|
||||
@ -43,11 +46,13 @@ func (p *SyncPool) Free(buffer *Buffer) {
|
||||
p.allocator.Put(rawBuffer)
|
||||
}
|
||||
|
||||
// BufferPool is a Pool that utilizes an internal cache.
|
||||
type BufferPool struct {
|
||||
chain chan []byte
|
||||
allocator *sync.Pool
|
||||
}
|
||||
|
||||
// NewBufferPool creates a new BufferPool with given buffer size, and internal cache size.
|
||||
func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
|
||||
pool := &BufferPool{
|
||||
chain: make(chan []byte, poolSize),
|
||||
@ -61,6 +66,7 @@ func NewBufferPool(bufferSize, poolSize uint32) *BufferPool {
|
||||
return pool
|
||||
}
|
||||
|
||||
// Allocate implements Pool.Allocate().
|
||||
func (p *BufferPool) Allocate() *Buffer {
|
||||
var b []byte
|
||||
select {
|
||||
@ -68,9 +74,13 @@ func (p *BufferPool) Allocate() *Buffer {
|
||||
default:
|
||||
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) {
|
||||
rawBuffer := buffer.v
|
||||
if rawBuffer == nil {
|
||||
@ -84,7 +94,9 @@ func (p *BufferPool) Free(buffer *Buffer) {
|
||||
}
|
||||
|
||||
const (
|
||||
// Size of a regular buffer.
|
||||
Size = 8 * 1024
|
||||
// Size of a small buffer.
|
||||
SizeSmall = 2 * 1024
|
||||
|
||||
PoolSizeEnvKey = "v2ray.buffer.size"
|
||||
|
Loading…
Reference in New Issue
Block a user