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 {
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
}