TcpService新增设置内存池接口

This commit is contained in:
boyce
2020-12-14 16:05:32 +08:00
parent e25eda14d1
commit 4796ad7404
8 changed files with 41 additions and 16 deletions

View File

@@ -4,6 +4,11 @@ import (
"sync"
)
type INetMempool interface {
MakeByteSlice(size int) []byte
ReleaseByteSlice(byteBuff []byte) bool
}
type memAreaPool struct {
minAreaValue int //最小范围值
maxAreaValue int //最大范围值
@@ -20,6 +25,10 @@ func init(){
}
}
func NewMemAreaPool() *memAreaPool{
return &memAreaPool{}
}
func (areaPool *memAreaPool) makePool(){
poolLen := (areaPool.maxAreaValue - areaPool.minAreaValue+1)/areaPool.growthValue
areaPool.pool = make([]sync.Pool,poolLen)
@@ -50,7 +59,6 @@ func (areaPool *memAreaPool) getPosByteSize(size int) int{
return pos
}
func (areaPool *memAreaPool) releaseByteSlice(byteBuff []byte) bool{
pos := areaPool.getPosByteSize(cap(byteBuff))
if pos > len(areaPool.pool) || pos == -1{
@@ -62,10 +70,9 @@ func (areaPool *memAreaPool) releaseByteSlice(byteBuff []byte) bool{
return true
}
func MakeByteSlice(size int) []byte{
func (areaPool *memAreaPool) MakeByteSlice(size int) []byte{
for i:=0;i<len(memAreaPoolList);i++{
if size <= memAreaPoolList[i].maxAreaValue {
return memAreaPoolList[i].makeByteSlice(size)
}
}
@@ -73,8 +80,7 @@ func MakeByteSlice(size int) []byte{
return nil
}
func ReleaseByteSlice(byteBuff []byte) bool {
func (areaPool *memAreaPool) ReleaseByteSlice(byteBuff []byte) bool {
for i:=0;i<len(memAreaPoolList);i++{
if cap(byteBuff) <= memAreaPoolList[i].maxAreaValue {
return memAreaPoolList[i].releaseByteSlice(byteBuff)
@@ -83,5 +89,3 @@ func ReleaseByteSlice(byteBuff []byte) bool {
return false
}