go/arena/arena.go

31 lines
413 B
Go
Raw Normal View History

2014-07-08 10:48:55 +04:00
package arena
type Arena struct {
buf []byte
2014-10-29 18:12:19 +03:00
offset int
2014-07-08 10:48:55 +04:00
}
func NewArena(size int) *Arena {
a := new(Arena)
a.buf = make([]byte, size, size)
2014-10-29 18:12:19 +03:00
a.offset = 0
2014-07-08 10:48:55 +04:00
return a
}
func (a *Arena) Make(size int) []byte {
2014-10-29 18:12:19 +03:00
if len(a.buf) < size || len(a.buf)-a.offset < size {
2014-07-08 10:48:55 +04:00
return make([]byte, size)
}
2014-10-29 18:12:19 +03:00
b := a.buf[a.offset : size+a.offset]
a.offset += size
2014-07-08 10:48:55 +04:00
return b
}
2014-10-29 18:12:19 +03:00
func (a *Arena) Reset() {
a.offset = 0
}