代码拉取完成,页面将自动刷新
package util
import (
"math"
"sync"
)
type levelPool struct {
size int
pool sync.Pool
}
func newLevelPool(size int) *levelPool {
return &levelPool{
size: size,
pool: sync.Pool{
New: func() interface{} {
data := make([]byte, size)
return &data
},
},
}
}
type LimitedPool struct {
minSize int
maxSize int
pools []*levelPool
}
func NewLimitedPool(minSize, maxSize int) *LimitedPool {
if maxSize < minSize {
panic("maxSize can't be less than minSize")
}
const multiplier = 2
var pools []*levelPool
curSize := minSize
for curSize < maxSize {
pools = append(pools, newLevelPool(curSize))
curSize *= multiplier
}
pools = append(pools, newLevelPool(maxSize))
return &LimitedPool{
minSize: minSize,
maxSize: maxSize,
pools: pools,
}
}
func (p *LimitedPool) findPool(size int) *levelPool {
if size > p.maxSize {
return nil
}
idx := int(math.Ceil(math.Log2(float64(size) / float64(p.minSize))))
if idx < 0 {
idx = 0
}
if idx > len(p.pools)-1 {
return nil
}
return p.pools[idx]
}
func (p *LimitedPool) findPutPool(size int) *levelPool {
if size > p.maxSize {
return nil
}
if size < p.minSize {
return nil
}
idx := int(math.Floor(math.Log2(float64(size) / float64(p.minSize))))
if idx < 0 {
idx = 0
}
if idx > len(p.pools)-1 {
return nil
}
return p.pools[idx]
}
func (p *LimitedPool) Get(size int) *[]byte {
sp := p.findPool(size)
if sp == nil {
data := make([]byte, size)
return &data
}
buf := sp.pool.Get().(*[]byte)
*buf = (*buf)[:size]
return buf
}
func (p *LimitedPool) Put(b *[]byte) {
sp := p.findPutPool(cap(*b))
if sp == nil {
return
}
*b = (*b)[:cap(*b)]
sp.pool.Put(b)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。