-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.h
117 lines (81 loc) · 2.07 KB
/
ringbuffer.h
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
#ifndef __RINGBUFFER_H__
#define __RINGBUFFER_H__
#ifndef RB_counter
#define RB_counter unsigned int
#endif
#define NEEDSPACE
#ifndef NEEDSPACE
#define RB_create(NAME, T, SIZE) \
struct {\
RB_counter head;\
RB_counter tail;\
T elems[SIZE];\
} NAME;\
static const RB_counter NAME##_rbsize = SIZE
#define RB_init(o) {\
o.head = 0;\
o.tail = 0;\
}
#define RB_clear(o) {\
o.head = 0;\
o.tail = 0;\
}
#define RB_capacity(o) o##_rbsize
#define RB_size(o) (o.head <= o.tail ? o.tail-o.head : o##_rbsize-(o.head-o.tail))
#define RB_empty(o) (o.head == o.tail)
#define RB_full(o) ((o.head+1)%o##_rbsize == o.tail)
#define RB_front(o) o.elems[o.head]
#define RB_back(o) o.elems[o.tail]
#define RB_popfront(o) o.head = (o.head+1)%o##_rbsize;
#define RB_pushback(o) {\
o.tail = (o.tail+1)%o##_rbsize;\
if (RB_empty(o))\
o.head = (o.head+1)%o##_rbsize;\
}
#else
#define RB_create(NAME, T, SIZE)\
struct {\
RB_counter head;\
RB_counter count;\
T elems[SIZE];\
} NAME;\
static const RB_counter NAME##_rbsize = SIZE
#define RB_init(o) {\
o.head = 0;\
o.count = 0;\
}
#define RB_clear(o) {\
o.head = 0;\
o.count = 0;\
}
#define RB_capacity(o) o##_rbsize
#define RB_size(o) o.count
#define RB_empty(o) (o.count == 0)
#define RB_full(o) (o.count == o##_rbsize)
#define RB_front(o) o.elems[o.head]
#define RB_back(o) o.elems[(o.head+o.count)%o##_rbsize]
#define RB_popfront(o) {\
o.head = (o.head+1)%o##_rbsize;\
o.count--;\
}
#define RB_pushback(o) {\
if (RB_full(o))\
o.head = (o.head+1)%o##_rbsize;\
else\
o.count = o.count + 1;\
}
#define RB_idx(o,i) o.elems[(o.head+i)%o##_rbsize]
#define RB_begin(o) o.head
#define RB_next(o,i) i = ((i+1)%o##_rbsize)
#define RB_end(o) ((o.head+o.count)%o##_rbsize)
#if 0
#define RB_begin(o) &o.elems[o.head]
#define RB_next(o,i) {\
i++;\
if (i == &o.elems[o##_rbsize])\
i = &o.elems[0];\
}
#define RB_end(o) &o.elems[(o.head+o.count)%o##_rbsize]
#endif
#endif
#endif//__RINGBUFFER_H__