aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrislu <chris.lu@gmail.com>2022-02-26 03:22:41 -0800
committerchrislu <chris.lu@gmail.com>2022-02-26 03:22:41 -0800
commit708e14fcfa7182a08a8ed02c250d49de8eb3b5a6 (patch)
tree02a37137b2034e46bf55d835540ee0cd0ed00dfc
parent3345a50d9bd06be741a26887b815bb16143c223a (diff)
downloadseaweedfs-708e14fcfa7182a08a8ed02c250d49de8eb3b5a6.tar.xz
seaweedfs-708e14fcfa7182a08a8ed02c250d49de8eb3b5a6.zip
avoid possible too big memory allocation
-rw-r--r--weed/util/mem/slot_pool.go23
1 files changed, 16 insertions, 7 deletions
diff --git a/weed/util/mem/slot_pool.go b/weed/util/mem/slot_pool.go
index 5bd759ab7..f7aa86e05 100644
--- a/weed/util/mem/slot_pool.go
+++ b/weed/util/mem/slot_pool.go
@@ -41,14 +41,23 @@ func getSlotPool(size int) *sync.Pool {
var total int64
func Allocate(size int) []byte {
- newVal := atomic.AddInt64(&total, 1)
- glog.V(4).Infof("++> %d", newVal)
- slab := *getSlotPool(size).Get().(*[]byte)
- return slab[:size]
+ pool := getSlotPool(size)
+ if pool != nil {
+
+ newVal := atomic.AddInt64(&total, 1)
+ glog.V(4).Infof("++> %d", newVal)
+
+ slab := *pool.Get().(*[]byte)
+ return slab[:size]
+ }
+ return make([]byte, size)
}
func Free(buf []byte) {
- newVal := atomic.AddInt64(&total, -1)
- glog.V(4).Infof("--> %d", newVal)
- getSlotPool(cap(buf)).Put(&buf)
+ pool := getSlotPool(cap(buf))
+ if pool != nil {
+ newVal := atomic.AddInt64(&total, -1)
+ glog.V(4).Infof("--> %d", newVal)
+ pool.Put(&buf)
+ }
}