forked from xtaci/smux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
120 lines (110 loc) · 2.21 KB
/
Copy pathbuffer.go
File metadata and controls
120 lines (110 loc) · 2.21 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
package smux
import "sync"
type ringbuffer struct {
lock sync.Mutex
index int // to read the index
offset int // offset of the indexed data
next int // next empty place, -1 means to grow
buffers [][]byte
alloc Allocator // put buffer back to Allocator
}
func newRingbuffer(alloc Allocator) *ringbuffer {
return &ringbuffer{next: -1, alloc: alloc}
}
func (r *ringbuffer) Enqueue(p []byte) {
r.lock.Lock()
if r.next >= 0 {
r.buffers[r.next] = p
r.next = (r.next + 1) % len(r.buffers)
if r.buffers[r.next] != nil {
r.next = -1
}
r.lock.Unlock()
return
}
buffers := append(r.buffers, p) // runtime.growslice
n := copy(buffers, r.buffers[r.index:])
copy(buffers[n:], r.buffers[0:r.index])
r.buffers = buffers
r.index = 0
if len(r.buffers) < cap(r.buffers) {
r.next = len(r.buffers)
r.buffers = r.buffers[:cap(r.buffers)]
}
r.lock.Unlock()
}
func (r *ringbuffer) Read(p []byte) (n int) {
r.lock.Lock()
if len(r.buffers) == 0 {
r.lock.Unlock()
return
}
putback := false
buff := r.buffers[r.index]
if buff != nil {
n = copy(p, buff[r.offset:])
r.offset += n
if r.offset == len(buff) {
r.buffers[r.index] = nil
if r.next == -1 {
r.next = r.index
}
r.index = (r.index + 1) % len(r.buffers)
r.offset = 0
putback = true
}
}
r.lock.Unlock()
if putback {
r.alloc.Put(buff)
}
return
}
func (r *ringbuffer) Dequeue() (p []byte, reuse func()) {
r.lock.Lock()
if len(r.buffers) == 0 {
r.lock.Unlock()
return
}
if buff := r.buffers[r.index]; buff != nil {
reuse = func() { r.alloc.Put(buff) }
p = buff[r.offset:]
r.buffers[r.index] = nil
if r.next == -1 {
r.next = r.index
}
r.index = (r.index + 1) % len(r.buffers)
r.offset = 0
}
r.lock.Unlock()
return
}
func (r *ringbuffer) Recycle() (n int) {
r.lock.Lock()
for idx := range r.buffers {
if buff := r.buffers[idx]; buff != nil {
n += len(buff)
if idx == r.index {
n -= r.offset
r.index = 0
r.offset = 0
}
r.buffers[idx] = nil
r.alloc.Put(buff)
}
r.next = 0
}
r.lock.Unlock()
return
}
func (r *ringbuffer) HasData() (has bool) {
r.lock.Lock()
for idx := range r.buffers {
if r.buffers[idx] != nil {
has = true
break
}
}
r.lock.Unlock()
return
}