-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbytepool_mmap.go
118 lines (100 loc) · 2.72 KB
/
mbytepool_mmap.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
package bp
import (
"sort"
)
type MultiMmapBytePool struct {
pools []*MmapBytePool
}
func (b *MultiMmapBytePool) find(size int) (*MmapBytePool, bool) {
for i, p := range b.pools {
if size <= p.alignSize {
return b.pools[i], true
}
}
return nil, false
}
func (b *MultiMmapBytePool) GetRef(size int) *ByteRef {
if pool, ok := b.find(size); ok {
data := pool.Get()
ref := newByteRef(data[:size], pool)
ref.setFinalizer()
return ref
}
// fallback
data := make([]byte, size)
ref := newByteRef(data, b.pools[len(b.pools)-1])
ref.setFinalizer()
return ref
}
func (b *MultiMmapBytePool) Get(size int) []byte {
if pool, ok := b.find(size); ok {
data := pool.Get()
return data[:size]
}
return make([]byte, size) // fallback
}
func (b *MultiMmapBytePool) Put(data []byte) bool {
if pool, ok := b.find(cap(data)); ok {
return pool.Put(data)
}
// discard
return false
}
type multiMmapBytePoolOptionFunc func(*multiMmapBytePoolOption)
type multiMmapBytePoolOption struct {
tuples []mmapBytepoolTuple
poolFuncs []optionFunc
}
type mmapBytepoolTuple struct {
poolSize, bufSize int
}
func newMultiMmapBytePoolOption() *multiMmapBytePoolOption {
return &multiMmapBytePoolOption{
tuples: make([]mmapBytepoolTuple, 0),
poolFuncs: make([]optionFunc, 0),
}
}
func MultiMmapBytePoolSize(poolSize int, bufSize int) multiMmapBytePoolOptionFunc {
return func(opt *multiMmapBytePoolOption) {
opt.tuples = append(opt.tuples, mmapBytepoolTuple{poolSize, bufSize})
}
}
func MultiMmapBytePoolOption(funcs ...optionFunc) multiMmapBytePoolOptionFunc {
return func(opt *multiMmapBytePoolOption) {
opt.poolFuncs = append(opt.poolFuncs, funcs...)
}
}
func uniqMmapBytepoolTuple(tuples []mmapBytepoolTuple) []mmapBytepoolTuple {
uniq := make(map[int]mmapBytepoolTuple)
for _, t := range tuples {
if _, ok := uniq[t.bufSize]; ok {
continue
}
uniq[t.bufSize] = t
}
uniqTuples := make([]mmapBytepoolTuple, 0, len(uniq))
for _, t := range uniq {
uniqTuples = append(uniqTuples, mmapBytepoolTuple{t.poolSize, t.bufSize})
}
return uniqTuples
}
func NewMultiMmapBytePool(funcs ...multiMmapBytePoolOptionFunc) *MultiMmapBytePool {
mOpt := newMultiMmapBytePoolOption()
for _, fn := range funcs {
fn(mOpt)
}
tuples := uniqMmapBytepoolTuple(mOpt.tuples)
poolFuncs := mOpt.poolFuncs
pools := make([]*MmapBytePool, len(tuples))
for i, t := range tuples {
pools[i] = NewMmapBytePool(t.poolSize, t.bufSize, poolFuncs...)
}
sort.Slice(pools, func(a, b int) bool {
return pools[a].alignSize < pools[b].alignSize
})
return &MultiMmapBytePool{
pools: pools,
}
}