/
allocator.go
154 lines (130 loc) · 3.33 KB
/
allocator.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
/*
* Copyright (C) 2020-2022, IrineSistiana
*
* This file is part of mosdns.
*
* mosdns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mosdns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// This file is a modified version from https://github.com/xtaci/smux/blob/master/alloc.go f386d90
// license of smux: MIT https://github.com/xtaci/smux/blob/master/LICENSE
package pool
import (
"fmt"
"math"
"math/bits"
"sync"
)
const intSize = 32 << (^uint(0) >> 63)
// defaultBufPool is an Allocator that has a maximum capacity.
var defaultBufPool = NewAllocator(intSize - 1)
// GetBuf returns a *Buffer from pool with most appropriate cap.
// It panics if size < 0.
func GetBuf(size int) *Buffer {
return defaultBufPool.Get(size)
}
type Allocator struct {
maxPoolLen int
buffers []sync.Pool
}
// NewAllocator initiates a []byte allocatorL.
// []byte that has less than 1 << maxPoolBitsLen bytes is managed by sync.Pool.
// The waste(memory fragmentation) of space allocation is guaranteed to be
// no more than 50%.
func NewAllocator(maxPoolBitsLen int) *Allocator {
if maxPoolBitsLen > intSize-1 || maxPoolBitsLen <= 0 {
panic("invalid pool length")
}
ml := 1 << maxPoolBitsLen
if maxPoolBitsLen == intSize-1 {
ml = math.MaxInt
}
alloc := &Allocator{
maxPoolLen: ml,
buffers: make([]sync.Pool, maxPoolBitsLen+1),
}
for i := range alloc.buffers {
var bufSize int
if i == intSize-1 {
bufSize = math.MaxInt
} else {
bufSize = 1 << i
}
alloc.buffers[i].New = func() interface{} {
return newBuffer(alloc, make([]byte, bufSize))
}
}
return alloc
}
// Get returns a []byte from pool with most appropriate cap
func (alloc *Allocator) Get(size int) *Buffer {
if size < 0 {
panic(fmt.Sprintf("invalid slice size %d", size))
}
if size > alloc.maxPoolLen {
panic(fmt.Sprintf("slice size %d is too large", size))
}
i := shard(size)
buf := alloc.buffers[i].Get().(*Buffer)
buf.SetLen(size)
return buf
}
// Release releases the buf to the allocatorL.
func (alloc *Allocator) Release(buf *Buffer) {
c := buf.Cap()
i := shard(c)
if c == 0 || c > alloc.maxPoolLen || c != 1<<i {
panic("unexpected cap size")
}
alloc.buffers[i].Put(buf)
}
// shard returns the shard index that is suitable for the size.
func shard(size int) int {
if size <= 1 {
return 0
}
return bits.Len64(uint64(size - 1))
}
type Buffer struct {
a *Allocator
l int
b []byte
}
func newBuffer(a *Allocator, b []byte) *Buffer {
return &Buffer{
a: a,
l: len(b),
b: b,
}
}
func (b *Buffer) SetLen(l int) {
if l > len(b.b) {
panic("buffer length overflowed")
}
b.l = l
}
func (b *Buffer) AllBytes() []byte {
return b.b
}
func (b *Buffer) Bytes() []byte {
return b.b[:b.l]
}
func (b *Buffer) Len() int {
return b.l
}
func (b *Buffer) Cap() int {
return cap(b.b)
}
func (b *Buffer) Release() {
b.a.Release(b)
}