From ecf49fc0738105e87d20e29aa82c403b666ff0b4 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 29 Oct 2014 23:12:19 +0800 Subject: [PATCH] update area --- arena/arena.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/arena/arena.go b/arena/arena.go index ceec084..2ca98aa 100644 --- a/arena/arena.go +++ b/arena/arena.go @@ -3,26 +3,28 @@ package arena type Arena struct { buf []byte - size int + offset int } func NewArena(size int) *Arena { a := new(Arena) - a.size = size a.buf = make([]byte, size, size) + a.offset = 0 return a } func (a *Arena) Make(size int) []byte { - if a.size < size { + if len(a.buf) < size || len(a.buf)-a.offset < size { return make([]byte, size) - } else if len(a.buf) < size { - a.buf = make([]byte, a.size) } - b := a.buf[0:size] - a.buf = a.buf[size:] + b := a.buf[a.offset : size+a.offset] + a.offset += size return b } + +func (a *Arena) Reset() { + a.offset = 0 +}