only allocate buffer map if the application requests a buffer, sanity check returns
@@ -37,7 +37,6 @@ var _ network.TransactionalScope = (*ResourceScope)(nil)
func NewResources(limit Limit) *Resources {
return &Resources{
limit: limit,
- buffers: make(map[interface{}][]byte),
}
}
@@ -97,6 +96,9 @@ func (rc *Resources) getBuffer(size int) ([]byte, error) {
buf := pool.Get(size)
rc.memory += int64(size)
+ if rc.buffers == nil {
+ rc.buffers = make(map[interface{}][]byte)
+ }
rc.buffers[buf] = buf
return buf, nil
@@ -108,6 +110,11 @@ func (rc *Resources) growBuffer(oldbuf []byte, newsize int) ([]byte, error) {
return nil, err
}
+ _, ok := rc.buffers[oldbuf]
+ if !ok {
+ return nil, fmt.Errorf("cannot grow unknown buffer")
+ }
+
newbuf := pool.Get(newsize)
copy(newbuf, oldbuf)
@@ -119,6 +126,11 @@ func (rc *Resources) growBuffer(oldbuf []byte, newsize int) ([]byte, error) {
}
func (rc *Resources) releaseBuffer(buf []byte) {
+ _, ok := rc.buffers[buf]
+ if !ok {
+ panic("BUG: release unknown buffer")
+ }
+
rc.memory -= int64(len(buf))
// sanity check for bugs upstream
