-
Notifications
You must be signed in to change notification settings - Fork 8
/
circ_buf.h
45 lines (34 loc) · 1.33 KB
/
circ_buf.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
#ifndef CIRC_BUF_H_
#define CIRC_BUF_H_ 1
/* number of items in circ_buf */
#define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)- 1))
/* space remaining in circ_buf */
#define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size))
/* is circ_buf full */
#define CIRC_FULL(head,tail,size) (CIRC_NEXT(tail,size) == (head))
/* next index (head/tail) location */
#define CIRC_NEXT(index,size) CIRC_NEXT_I(index,1,size)
#define CIRC_NEXT_I(index,isz,size) (((index) + (isz)) & ((size) - 1))
/* assign next index (head/tail) location to index */
#define CIRC_NEXT_EQ(index,size) CIRC_NEXT_I_EQ(index,1,size)
#define CIRC_NEXT_I_EQ(index,isz,size) ((index) = (((index) + (isz)) & ((size - 1))))
#define CIRC_CNT_TO_END(head,tail,size) \
({typeof(head) end = (size) - (tail); \
typeof(head) n = ((head) + end) & ((size) - 1); \
n < end ? n : end;})
#define CIRC_SPACE_TO_END(head,tail,size) \
({typeof(head) end = (size) - 1 - (head); \
typeof(head) n = (end + (tail)) & ((size)-1); \
n <= end ? n : end+1;})
/* the following expect q to be a structure of the form */
#if 0
struct q {
uint8_t head;
uint8_t tail;
uint8_t buf[16];
};
#endif
#define circ_next(q, ix_var) ( q.ix_var = CIRC_NEXT(q.ix_var, sizeof(q.buf)) )
#define circ_next_head(q) ( circ_next(q, head) )
#define circ_next_tail(q) ( circ_next(q, tail) )
#endif