forked from mosn/mosn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytebufferpool.go
167 lines (143 loc) · 3.31 KB
/
bytebufferpool.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package buffer
import (
"context"
"sync"
)
var ins = ByteBufferCtx{}
func init() {
RegisterBuffer(&ins)
}
const minShift = 6
const maxShift = 18
const errSlot = -1
var bbPool *byteBufferPool
func init() {
bbPool = newByteBufferPool()
}
// byteBufferPool is []byte pools
type byteBufferPool struct {
minShift int
minSize int
maxSize int
pool []*bufferSlot
}
type bufferSlot struct {
defaultSize int
pool sync.Pool
}
// newByteBufferPool returns byteBufferPool
func newByteBufferPool() *byteBufferPool {
p := &byteBufferPool{
minShift: minShift,
minSize: 1 << minShift,
maxSize: 1 << maxShift,
}
for i := 0; i <= maxShift-minShift; i++ {
slab := &bufferSlot{
defaultSize: 1 << (uint)(i+minShift),
}
p.pool = append(p.pool, slab)
}
return p
}
func (p *byteBufferPool) slot(size int) int {
if size > p.maxSize {
return errSlot
}
slot := 0
shift := 0
if size > p.minSize {
size--
for size > 0 {
size = size >> 1
shift++
}
slot = shift - p.minShift
}
return slot
}
func newBytes(size int) []byte {
return make([]byte, size)
}
// take returns *[]byte from byteBufferPool
func (p *byteBufferPool) take(size int) *[]byte {
slot := p.slot(size)
if slot == errSlot {
b := newBytes(size)
return &b
}
v := p.pool[slot].pool.Get()
if v == nil {
b := newBytes(p.pool[slot].defaultSize)
b = b[0:size]
return &b
}
b := v.(*[]byte)
*b = (*b)[0:size]
return b
}
// give returns *[]byte to byteBufferPool
func (p *byteBufferPool) give(buf *[]byte) {
if buf == nil {
return
}
size := cap(*buf)
slot := p.slot(size)
if slot == errSlot {
return
}
if size != int(p.pool[slot].defaultSize) {
return
}
p.pool[slot].pool.Put(buf)
}
type ByteBufferCtx struct {
TempBufferCtx
}
type ByteBufferPoolContainer struct {
bytes []*[]byte
*byteBufferPool
}
func (ctx ByteBufferCtx) New() interface{} {
return &ByteBufferPoolContainer{
byteBufferPool: bbPool,
}
}
func (ctx ByteBufferCtx) Reset(i interface{}) {
p := i.(*ByteBufferPoolContainer)
for _, buf := range p.bytes {
p.give(buf)
}
p.bytes = p.bytes[:0]
}
// GetBytesByContext returns []byte from byteBufferPool by context
func GetBytesByContext(context context.Context, size int) *[]byte {
p := PoolContext(context).Find(&ins, nil).(*ByteBufferPoolContainer)
buf := p.take(size)
p.bytes = append(p.bytes, buf)
return buf
}
// GetBytes returns *[]byte from byteBufferPool
func GetBytes(size int) *[]byte {
return bbPool.take(size)
}
// PutBytes Put *[]byte to byteBufferPool
func PutBytes(buf *[]byte) {
bbPool.give(buf)
}