Files
origin/network/slicepool.go
2020-12-14 14:28:22 +08:00

88 lines
1.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package network
import (
"sync"
)
type memAreaPool struct {
minAreaValue int //最小范围值
maxAreaValue int //最大范围值
growthValue int //内存增长值
pool []sync.Pool
}
//小于2048时按64步长增长.>2048时则按2048长度增长
var memAreaPoolList = [2]*memAreaPool{&memAreaPool{minAreaValue:1,maxAreaValue: 2048,growthValue:64}, &memAreaPool{minAreaValue: 2049,maxAreaValue:65536,growthValue:2048}}
func init(){
for i:=0;i<len(memAreaPoolList);i++{
memAreaPoolList[i].makePool()
}
}
func (areaPool *memAreaPool) makePool(){
poolLen := (areaPool.maxAreaValue - areaPool.minAreaValue+1)/areaPool.growthValue
areaPool.pool = make([]sync.Pool,poolLen)
for i:=0;i<poolLen;i++{
memSize := (areaPool.minAreaValue-1)+(i+1)*areaPool.growthValue
areaPool.pool[i] = sync.Pool{New: func() interface{}{
//fmt.Println("make memsize:",memSize)
return make([]byte,memSize)
}}
}
}
func (areaPool *memAreaPool) makeByteSlice(size int) []byte{
pos := areaPool.getPosByteSize(size)
if pos > len(areaPool.pool) || pos == -1 {
return nil
}
return areaPool.pool[pos].Get().([]byte)[:size]
}
func (areaPool *memAreaPool) getPosByteSize(size int) int{
pos := (size - areaPool.minAreaValue)/areaPool.growthValue
if pos >= len(areaPool.pool) {
return -1
}
return pos
}
func (areaPool *memAreaPool) releaseByteSlice(byteBuff []byte) bool{
pos := areaPool.getPosByteSize(cap(byteBuff))
if pos > len(areaPool.pool) || pos == -1{
panic("assert!")
return false
}
areaPool.pool[pos].Put(byteBuff)
return true
}
func MakeByteSlice(size int) []byte{
for i:=0;i<len(memAreaPoolList);i++{
if size <= memAreaPoolList[i].maxAreaValue {
return memAreaPoolList[i].makeByteSlice(size)
}
}
return nil
}
func ReleaseByteSlice(byteBuff []byte) bool {
for i:=0;i<len(memAreaPoolList);i++{
if cap(byteBuff) <= memAreaPoolList[i].maxAreaValue {
return memAreaPoolList[i].releaseByteSlice(byteBuff)
}
}
return false
}