forked from creachadair/jrpc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
64 lines (54 loc) · 1.14 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2021 Michael J. Fromberger. All Rights Reserved.
package jrpc2
type queue struct {
front, back *entry
free *entry
nelts int
}
func newQueue() *queue {
sentinel := new(entry)
return &queue{front: sentinel, back: sentinel}
}
func (q *queue) isEmpty() bool { return q.front.link == nil }
func (q *queue) size() int { return q.nelts }
func (q *queue) reset() { q.front.link = nil; q.back = q.front; q.nelts = 0 }
func (q *queue) alloc(data jmessages) *entry {
if q.free == nil {
return &entry{data: data}
}
out := q.free
q.free = out.link
out.data = data
out.link = nil
return out
}
func (q *queue) release(e *entry) {
e.link, q.free = q.free, e
e.data = nil
}
func (q *queue) each(f func(jmessages)) {
for cur := q.front.link; cur != nil; cur = cur.link {
f(cur.data)
}
}
func (q *queue) push(m jmessages) {
e := q.alloc(m)
q.back.link = e
q.back = e
q.nelts++
}
func (q *queue) pop() jmessages {
out := q.front.link
q.front.link = out.link
if out == q.back {
q.back = q.front
}
q.nelts--
data := out.data
q.release(out)
return data
}
type entry struct {
data jmessages
link *entry
}