update area

This commit is contained in:
siddontang 2014-10-29 23:12:19 +08:00
parent 5f5f3de07d
commit ecf49fc073
1 changed files with 9 additions and 7 deletions

View File

@ -3,26 +3,28 @@ package arena
type Arena struct { type Arena struct {
buf []byte buf []byte
size int offset int
} }
func NewArena(size int) *Arena { func NewArena(size int) *Arena {
a := new(Arena) a := new(Arena)
a.size = size
a.buf = make([]byte, size, size) a.buf = make([]byte, size, size)
a.offset = 0
return a return a
} }
func (a *Arena) Make(size int) []byte { 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) return make([]byte, size)
} else if len(a.buf) < size {
a.buf = make([]byte, a.size)
} }
b := a.buf[0:size] b := a.buf[a.offset : size+a.offset]
a.buf = a.buf[size:] a.offset += size
return b return b
} }
func (a *Arena) Reset() {
a.offset = 0
}