/
bufferpool.go
67 lines (60 loc) · 1.16 KB
/
bufferpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package netlogger
import (
"math"
"sync"
)
const (
minByte = uint32(1 << 7) //128 b
capGrow = uint32(1)
maxByte = 1 << 24 //16M
)
var BUFFERPOOL *BufferPool
func init() {
rawDataCap := minByte
bp := &BufferPool{
capSlice: make([]uint32, 0),
pool: map[uint32]*sync.Pool{},
}
for rawDataCap <= maxByte {
bp.capSlice = append(bp.capSlice, rawDataCap)
tmpCap := rawDataCap
bp.pool[tmpCap] = &sync.Pool{
New: func() interface{} {
slice := make([]byte, tmpCap)
return &slice
},
}
//fmt.Println(rawDataCap)
rawDataCap <<= capGrow
}
BUFFERPOOL = bp
}
type BufferPool struct {
capSlice []uint32
pool map[uint32]*sync.Pool
}
func (b *BufferPool) Put(bs []byte) {
p := b.pool[uint32(cap(bs))]
if p != nil {
p.Put(&bs)
}
}
func (b *BufferPool) Get(l uint32) *[]byte {
offset := int(math.Log2(float64(b.capSlice[0])))
index := int(math.Ceil(math.Log2(float64(l))))
//fmt.Println(index)
if index <= offset {
index = 0
} else {
index = index - offset
}
if index > len(b.capSlice)-1 {
index = len(b.capSlice) - 1
}
if index < 0 {
index = 0
}
capSize := b.capSlice[index]
p := b.pool[capSize]
return p.Get().(*[]byte)
}