mirror of https://github.com/siddontang/go.git
update area
This commit is contained in:
parent
5f5f3de07d
commit
ecf49fc073
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue