mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
avoid possible too big memory allocation
This commit is contained in:
parent
3345a50d9b
commit
708e14fcfa
|
@ -41,14 +41,23 @@ func getSlotPool(size int) *sync.Pool {
|
|||
var total int64
|
||||
|
||||
func Allocate(size int) []byte {
|
||||
pool := getSlotPool(size)
|
||||
if pool != nil {
|
||||
|
||||
newVal := atomic.AddInt64(&total, 1)
|
||||
glog.V(4).Infof("++> %d", newVal)
|
||||
slab := *getSlotPool(size).Get().(*[]byte)
|
||||
|
||||
slab := *pool.Get().(*[]byte)
|
||||
return slab[:size]
|
||||
}
|
||||
return make([]byte, size)
|
||||
}
|
||||
|
||||
func Free(buf []byte) {
|
||||
pool := getSlotPool(cap(buf))
|
||||
if pool != nil {
|
||||
newVal := atomic.AddInt64(&total, -1)
|
||||
glog.V(4).Infof("--> %d", newVal)
|
||||
getSlotPool(cap(buf)).Put(&buf)
|
||||
pool.Put(&buf)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue