2021-11-28 04:13:00 +08:00
|
|
|
package mem
|
|
|
|
|
2022-01-22 17:35:12 +08:00
|
|
|
import (
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
2021-11-28 04:13:00 +08:00
|
|
|
|
|
|
|
var pools []*sync.Pool
|
|
|
|
|
|
|
|
const (
|
|
|
|
min_size = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
func bitCount(size int) (count int) {
|
|
|
|
for ; size > min_size; count++ {
|
|
|
|
size = size >> 1
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// 1KB ~ 256MB
|
|
|
|
pools = make([]*sync.Pool, bitCount(1024*1024*256))
|
|
|
|
for i := 0; i < len(pools); i++ {
|
|
|
|
slotSize := 1024 << i
|
|
|
|
pools[i] = &sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
buffer := make([]byte, slotSize)
|
|
|
|
return &buffer
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSlotPool(size int) *sync.Pool {
|
|
|
|
index := bitCount(size)
|
|
|
|
return pools[index]
|
|
|
|
}
|
|
|
|
|
2022-01-22 17:35:12 +08:00
|
|
|
var total int64
|
|
|
|
|
2021-11-28 04:13:00 +08:00
|
|
|
func Allocate(size int) []byte {
|
2022-01-22 17:35:12 +08:00
|
|
|
newVal := atomic.AddInt64(&total, 1)
|
|
|
|
glog.V(4).Infof("++> %d", newVal)
|
2021-11-28 04:13:00 +08:00
|
|
|
slab := *getSlotPool(size).Get().(*[]byte)
|
|
|
|
return slab[:size]
|
|
|
|
}
|
|
|
|
|
|
|
|
func Free(buf []byte) {
|
2022-01-22 17:35:12 +08:00
|
|
|
newVal := atomic.AddInt64(&total, -1)
|
|
|
|
glog.V(4).Infof("--> %d", newVal)
|
2021-11-28 04:13:00 +08:00
|
|
|
getSlotPool(cap(buf)).Put(&buf)
|
|
|
|
}
|