Repository navigation
Expand file tree
/
Copy pathstack_go_memory_arena.go
More file actions
218 lines (195 loc) · 4.58 KB
/
Copy pathstack_go_memory_arena.go
File metadata and controls
218 lines (195 loc) · 4.58 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:build darwin || linux
package tun
import (
"cmp"
"slices"
"sync"
"unsafe"
E "github.com/sagernet/sing/common/exceptions"
"golang.org/x/sys/unix"
)
const (
goSlabChunkSlabs = 32
goSlabChunkSize = goSlabChunkSlabs * goSlabSize
)
const (
goSlabFresh uint8 = iota
goSlabLive
goSlabCached
goSlabReusable
)
func newGoSlabStore(lowWater int) goSlabStore {
if !goArenaEnabled() {
return goHeapSlabStore{}
}
return &goArenaSlabStore{cached: make([]*goSlab, 0, lowWater), lowWater: lowWater}
}
func goRegionMap(size int) []byte {
size = (size + goPageSize - 1) &^ (goPageSize - 1)
memory, err := unix.Mmap(-1, 0, size, unix.PROT_READ|unix.PROT_WRITE, goRegionMapFlags)
if err != nil {
panic(E.Cause(err, "go: map memory region"))
}
return memory
}
func goRegionUnmap(memory []byte) {
unix.Munmap(memory)
}
type goSlabChunk struct {
base uintptr
memory []byte
states [goSlabChunkSlabs]uint8
live int
cached int
reusable int
fresh int
}
func (c *goSlabChunk) slab(index int) *goSlab {
return (*goSlab)(unsafe.Pointer(&c.memory[index*goSlabSize]))
}
func (c *goSlabChunk) span(index int) []byte {
return c.memory[index*goSlabSize : (index+1)*goSlabSize]
}
func (c *goSlabChunk) idle() bool {
return c.live == 0 && c.cached == 0
}
func (c *goSlabChunk) take(state uint8) (*goSlab, bool) {
for index := range c.states {
if c.states[index] != state {
continue
}
switch state {
case goSlabReusable:
c.reusable--
goRegionReuse(c.span(index))
case goSlabFresh:
c.fresh--
}
c.states[index] = goSlabLive
c.live++
return c.slab(index), true
}
return nil, false
}
type goArenaSlabStore struct {
access sync.Mutex
chunks []*goSlabChunk
cached []*goSlab
lowWater int
closed bool
}
func (s *goArenaSlabStore) acquire() *goSlab {
s.access.Lock()
defer s.access.Unlock()
if count := len(s.cached); count > 0 {
slab := s.cached[count-1]
s.cached[count-1] = nil
s.cached = s.cached[:count-1]
chunk, index := s.locate(slab)
chunk.states[index] = goSlabLive
chunk.cached--
chunk.live++
return slab
}
for _, chunk := range s.chunks {
if chunk.reusable == 0 {
continue
}
slab, _ := chunk.take(goSlabReusable)
return slab
}
for _, chunk := range s.chunks {
if chunk.fresh == 0 {
continue
}
slab, _ := chunk.take(goSlabFresh)
return slab
}
memory := goRegionMap(goSlabChunkSize)
chunk := &goSlabChunk{base: uintptr(unsafe.Pointer(&memory[0])), memory: memory, fresh: goSlabChunkSlabs}
position, _ := slices.BinarySearchFunc(s.chunks, chunk.base, goCompareChunk)
s.chunks = slices.Insert(s.chunks, position, chunk)
slab, _ := chunk.take(goSlabFresh)
return slab
}
func goCompareChunk(chunk *goSlabChunk, base uintptr) int {
return cmp.Compare(chunk.base, base)
}
func (s *goArenaSlabStore) locate(slab *goSlab) (*goSlabChunk, int) {
address := uintptr(unsafe.Pointer(slab))
position, found := slices.BinarySearchFunc(s.chunks, address, goCompareChunk)
if !found {
position--
}
chunk := s.chunks[position]
return chunk, int((address - chunk.base) / goSlabSize)
}
func (s *goArenaSlabStore) release(slab *goSlab, discard bool) {
s.access.Lock()
defer s.access.Unlock()
chunk, index := s.locate(slab)
chunk.live--
if discard || s.closed {
s.park(chunk, index)
s.trim(chunk)
return
}
chunk.states[index] = goSlabCached
chunk.cached++
s.cached = append(s.cached, slab)
if len(s.cached) <= s.lowWater {
return
}
oldest := s.cached[0]
remaining := copy(s.cached, s.cached[1:])
s.cached[remaining] = nil
s.cached = s.cached[:remaining]
oldestChunk, oldestIndex := s.locate(oldest)
oldestChunk.cached--
s.park(oldestChunk, oldestIndex)
s.trim(oldestChunk)
}
func (s *goArenaSlabStore) park(chunk *goSlabChunk, index int) {
goRegionPark(chunk.span(index))
chunk.states[index] = goSlabReusable
chunk.reusable++
}
func (s *goArenaSlabStore) trim(chunk *goSlabChunk) {
if !chunk.idle() {
return
}
if !s.closed {
idle := 0
for _, candidate := range s.chunks {
if candidate.idle() {
idle++
}
}
if idle <= 1 {
return
}
}
position, _ := slices.BinarySearchFunc(s.chunks, chunk.base, goCompareChunk)
s.chunks = slices.Delete(s.chunks, position, position+1)
goRegionUnmap(chunk.memory)
}
func (s *goArenaSlabStore) purge() {
s.access.Lock()
defer s.access.Unlock()
for _, slab := range s.cached {
chunk, index := s.locate(slab)
chunk.cached--
s.park(chunk, index)
}
clear(s.cached)
s.cached = s.cached[:0]
for _, chunk := range slices.Backward(s.chunks) {
s.trim(chunk)
}
}
func (s *goArenaSlabStore) close() {
s.access.Lock()
s.closed = true
s.access.Unlock()
s.purge()
}