From c884d04c7b16b02d4145ef302f06e961575ff882 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Sat, 19 Nov 2016 14:38:35 +0100 Subject: [PATCH] buffer api --- common/alloc/buffer.go | 24 +++++++++++++----------- common/alloc/buffer_pool.go | 4 +--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 8014d55e..d20dd01f 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -16,18 +16,20 @@ const ( // the buffer into an internal buffer pool, in order to recreate a buffer more // quickly. type Buffer struct { - head []byte - pool Pool - Value []byte - offset int + head []byte + pool Pool + Value []byte + offset int + startingOffset int } -func CreateBuffer(container []byte, parent Pool) *Buffer { +func CreateBuffer(container []byte, offset int, parent Pool) *Buffer { b := new(Buffer) b.head = container b.pool = parent - b.Value = b.head[defaultOffset:] - b.offset = defaultOffset + b.Value = b.head[offset:] + b.offset = offset + b.startingOffset = offset return b } @@ -47,15 +49,15 @@ func (b *Buffer) Release() { // Clear clears the content of the buffer, results an empty buffer with // Len() = 0. func (b *Buffer) Clear() *Buffer { - b.offset = defaultOffset + b.offset = b.startingOffset b.Value = b.head[b.offset:b.offset] return b } // Reset resets this Buffer into its original state. func (b *Buffer) Reset() *Buffer { - b.offset = defaultOffset - b.Value = b.head + b.offset = b.startingOffset + b.Value = b.head[b.offset:] return b } @@ -219,5 +221,5 @@ func NewBuffer() *Buffer { //} func NewLocalBuffer(size int) *Buffer { - return CreateBuffer(make([]byte, size), nil) + return CreateBuffer(make([]byte, size), defaultOffset, nil) } diff --git a/common/alloc/buffer_pool.go b/common/alloc/buffer_pool.go index 74d24dcd..11aed888 100644 --- a/common/alloc/buffer_pool.go +++ b/common/alloc/buffer_pool.go @@ -36,7 +36,7 @@ func (p *BufferPool) Allocate() *Buffer { default: b = p.allocator.Get().([]byte) } - return CreateBuffer(b, p) + return CreateBuffer(b, defaultOffset, p) } func (p *BufferPool) Free(buffer *Buffer) { @@ -63,7 +63,6 @@ const ( var ( mediumPool *BufferPool - //largePool *BufferPool ) func init() { @@ -77,5 +76,4 @@ func init() { } totalByteSize := size * 1024 * 1024 mediumPool = NewBufferPool(mediumBufferByteSize, totalByteSize/mediumBufferByteSize) - //largePool = NewBufferPool(largeBufferByteSize, totalByteSize/4/largeBufferByteSize) }