-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_buff.go
106 lines (87 loc) · 1.56 KB
/
ring_buff.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
package main
import (
"fmt"
//"io/ioutil"
//"container/list"
//"runtime"
//"time"
)
type Object []interface{}
type Ringbuffer struct {
write uint32
read uint32
array Object
mask uint32
size uint32
flag uint8
}
func Ring_buffer_create(n uint32) *Ringbuffer {
var sz uint32
var rb *Ringbuffer
if n < 1 || n > 30 {
return nil
}
rb = new(Ringbuffer)
if rb == nil {
return nil
}
sz = (1 << n) + 1
rb.mask = (1 << n) - 1
rb.size = sz
rb.flag = 1
rb.array = make(Object, sz)
if rb.array == nil {
return nil
}
return rb
}
func Ring_buffer_size(rb *Ringbuffer) uint32 {
size := (rb.write - rb.read) & rb.mask
return (size)
}
func Ring_buffer_is_empty(rb *Ringbuffer) int {
if rb.write == rb.read {
return 1
}
return 0
}
func Ring_buffer_is_full(rb *Ringbuffer) int {
if ((rb.write + 1) & rb.mask) == rb.read {
return 1
}
return 0
}
func Ring_buffer_get(rb *Ringbuffer) (ptr interface{}) {
if rb.write == rb.read {
return nil
}
ptr = rb.array[rb.read]
rb.read = (rb.read + 1) & rb.mask
return ptr
}
func Ring_buffer_put(rb *Ringbuffer, ptr interface{}) int {
if ((rb.write + 1) & rb.mask) == rb.read {
return -1
}
rb.array[rb.write] = ptr
rb.write = (rb.write + 1) & rb.mask
return 0
}
func Ring_buffer_puts(start int, pos int, end int, ptr interface{}, slave_work_que Slave_work_que) int {
var i int
for i = start; i < end; i++ {
if Ring_buffer_put(slave_work_que[pos].slave_ring, ptr) == 0 {
break
}
if pos == end-1 {
pos = start
} else {
pos++
}
}
if i != end {
return 0
} else {
return -1
}
}