2021-11-27 20:13:00 +00:00
|
|
|
package mem
|
|
|
|
|
2022-01-22 09:35:12 +00:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
2021-11-27 20:13:00 +00:00
|
|
|
|
|
|
|
var pools []*sync.Pool
|
|
|
|
|
|
|
|
const (
|
|
|
|
min_size = 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
func bitCount(size int) (count int) {
|
|
|
|
for ; size > min_size; count++ {
|
2022-01-22 16:05:04 +00:00
|
|
|
size = (size + 1) >> 1
|
2021-11-27 20:13:00 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 21:50:28 +00:00
|
|
|
func getSlotPool(size int) (*sync.Pool, bool) {
|
2021-11-27 20:13:00 +00:00
|
|
|
index := bitCount(size)
|
2022-03-02 21:50:28 +00:00
|
|
|
if index >= len(pools) {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return pools[index], true
|
2021-11-27 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Allocate(size int) []byte {
|
2022-03-02 21:50:28 +00:00
|
|
|
if pool, found := getSlotPool(size); found {
|
2022-02-26 11:22:41 +00:00
|
|
|
slab := *pool.Get().(*[]byte)
|
|
|
|
return slab[:size]
|
|
|
|
}
|
|
|
|
return make([]byte, size)
|
2021-11-27 20:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Free(buf []byte) {
|
2022-03-02 21:50:28 +00:00
|
|
|
if pool, found := getSlotPool(cap(buf)); found {
|
2022-02-26 11:22:41 +00:00
|
|
|
pool.Put(&buf)
|
|
|
|
}
|
2021-11-27 20:13:00 +00:00
|
|
|
}
|