mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-25 10:01:28 +00:00
Merge pull request #599 from XTLS/fix/buffer
Check buffer before releasing and reusing
This commit is contained in:
commit
73e10f0f6f
|
@ -24,10 +24,17 @@ type Buffer struct {
|
||||||
UDP *net.Destination
|
UDP *net.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
// New creates a Buffer with 0 length and 2K capacity.
|
// New creates a Buffer with 0 length and 8K capacity.
|
||||||
func New() *Buffer {
|
func New() *Buffer {
|
||||||
|
buf := pool.Get().([]byte)
|
||||||
|
if cap(buf) >= Size {
|
||||||
|
buf = buf[:Size]
|
||||||
|
} else {
|
||||||
|
buf = make([]byte, Size)
|
||||||
|
}
|
||||||
|
|
||||||
return &Buffer{
|
return &Buffer{
|
||||||
v: pool.Get().([]byte),
|
v: buf,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,8 +57,15 @@ func NewExisted(b []byte) *Buffer {
|
||||||
// StackNew creates a new Buffer object on stack.
|
// StackNew creates a new Buffer object on stack.
|
||||||
// This method is for buffers that is released in the same function.
|
// This method is for buffers that is released in the same function.
|
||||||
func StackNew() Buffer {
|
func StackNew() Buffer {
|
||||||
|
buf := pool.Get().([]byte)
|
||||||
|
if cap(buf) >= Size {
|
||||||
|
buf = buf[:Size]
|
||||||
|
} else {
|
||||||
|
buf = make([]byte, Size)
|
||||||
|
}
|
||||||
|
|
||||||
return Buffer{
|
return Buffer{
|
||||||
v: pool.Get().([]byte),
|
v: buf,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,7 +78,10 @@ func (b *Buffer) Release() {
|
||||||
p := b.v
|
p := b.v
|
||||||
b.v = nil
|
b.v = nil
|
||||||
b.Clear()
|
b.Clear()
|
||||||
|
|
||||||
|
if cap(p) == Size {
|
||||||
pool.Put(p)
|
pool.Put(p)
|
||||||
|
}
|
||||||
b.UDP = nil
|
b.UDP = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue