From a723810b2e37a054b785489b541f9d556289a4c0 Mon Sep 17 00:00:00 2001 From: boyce Date: Thu, 29 Oct 2020 19:45:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BD=91=E7=BB=9C=E5=B1=82?= =?UTF-8?q?=E7=9A=84GC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/slicepool.go | 91 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 network/slicepool.go diff --git a/network/slicepool.go b/network/slicepool.go new file mode 100644 index 0000000..cf703ea --- /dev/null +++ b/network/slicepool.go @@ -0,0 +1,91 @@ +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(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