mirror of https://github.com/v2ray/v2ray-core
refine buffer struct
parent
631db6e69a
commit
9523cb3ec3
|
@ -21,9 +21,18 @@ type Buffer struct {
|
||||||
offset int
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateBuffer(container []byte, parent *BufferPool) *Buffer {
|
||||||
|
b := new(Buffer)
|
||||||
|
b.head = container
|
||||||
|
b.pool = parent
|
||||||
|
b.Value = b.head[defaultOffset:]
|
||||||
|
b.offset = defaultOffset
|
||||||
|
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 {
|
if b == nil || b.head == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b.pool.Free(b)
|
b.pool.Free(b)
|
||||||
|
@ -122,7 +131,7 @@ func (b *Buffer) SliceFrom(from int) *Buffer {
|
||||||
func (b *Buffer) SliceBack(offset int) *Buffer {
|
func (b *Buffer) SliceBack(offset int) *Buffer {
|
||||||
newoffset := b.offset - offset
|
newoffset := b.offset - offset
|
||||||
if newoffset < 0 {
|
if newoffset < 0 {
|
||||||
newoffset = 0
|
panic("Negative buffer offset.")
|
||||||
}
|
}
|
||||||
b.Value = b.head[newoffset : b.offset+len(b.Value)]
|
b.Value = b.head[newoffset : b.offset+len(b.Value)]
|
||||||
b.offset = newoffset
|
b.offset = newoffset
|
||||||
|
|
|
@ -29,12 +29,7 @@ func (p *BufferPool) Allocate() *Buffer {
|
||||||
default:
|
default:
|
||||||
b = p.allocator.Get().([]byte)
|
b = p.allocator.Get().([]byte)
|
||||||
}
|
}
|
||||||
return &Buffer{
|
return CreateBuffer(b, p)
|
||||||
head: b,
|
|
||||||
pool: p,
|
|
||||||
Value: b[defaultOffset:],
|
|
||||||
offset: defaultOffset,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *BufferPool) Free(buffer *Buffer) {
|
func (p *BufferPool) Free(buffer *Buffer) {
|
||||||
|
|
Loading…
Reference in New Issue