-
Notifications
You must be signed in to change notification settings - Fork 0
/
rawstream_queue.go
46 lines (43 loc) · 972 Bytes
/
rawstream_queue.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
package copper
// rawStreamQueue is a ring-buffer queue of rawStream pointers
type rawStreamQueue struct {
buf []*rawStream
off int
size int
}
// push adds a pointer to the end of the queue, expanding it when necessary
func (q *rawStreamQueue) push(s *rawStream) {
pos := q.off + q.size
if q.size == len(q.buf) {
dst := make([]*rawStream, minpow2(len(q.buf)+1))
if pos == len(q.buf) {
// queue doesn't wrap, simple copy
copy(dst, q.buf)
} else {
// queue wraps, copy with two steps
z := copy(dst, q.buf[q.off:])
copy(dst[z:], q.buf[:q.size-z])
pos = q.size
}
q.buf = dst
q.off = 0
} else if pos >= len(q.buf) {
pos -= len(q.buf)
}
q.buf[pos] = s
q.size++
}
// take removes a pointer from the front of the queue
func (q *rawStreamQueue) take() *rawStream {
if q.size == 0 {
return nil
}
stream := q.buf[q.off]
q.buf[q.off] = nil
q.off++
q.size--
if q.off == len(q.buf) || q.size == 0 {
q.off = 0
}
return stream
}