mirror of
https://github.com/libp2p/go-libp2p-resource-manager.git
synced 2025-02-05 09:10:27 +08:00
add nil receiver safety to basic resource scopes
This commit is contained in:
parent
61d2f1c8ce
commit
004a6948b2
22
scope.go
22
scope.go
@ -41,6 +41,10 @@ func (rc *ResourceScope) releaseBuffers() {
|
||||
}
|
||||
|
||||
func (rc *ResourceScope) ReserveMemory(size int) error {
|
||||
if rc == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
@ -53,6 +57,10 @@ func (rc *ResourceScope) ReserveMemory(size int) error {
|
||||
}
|
||||
|
||||
func (rc *ResourceScope) ReleaseMemory(size int) {
|
||||
if rc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
@ -65,6 +73,10 @@ func (rc *ResourceScope) ReleaseMemory(size int) {
|
||||
}
|
||||
|
||||
func (rc *ResourceScope) GetBuffer(size int) ([]byte, error) {
|
||||
if rc == nil {
|
||||
return make([]byte, size), nil
|
||||
}
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
@ -81,6 +93,12 @@ func (rc *ResourceScope) GetBuffer(size int) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (rc *ResourceScope) GrowBuffer(oldbuf []byte, newsize int) ([]byte, error) {
|
||||
if rc == nil {
|
||||
newbuf := make([]byte, newsize)
|
||||
copy(newbuf, oldbuf)
|
||||
return newbuf, nil
|
||||
}
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
@ -115,6 +133,10 @@ func (rc *ResourceScope) ReleaseBuffer(buf []byte) {
|
||||
}
|
||||
|
||||
func (rc *ResourceScope) Stat() network.ScopeStat {
|
||||
if rc == nil {
|
||||
return network.ScopeStat{}
|
||||
}
|
||||
|
||||
rc.Lock()
|
||||
defer rc.Unlock()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user