siddontang.golib/arena/arena.go

31 lines
413 B
Go
Raw Permalink Normal View History

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