blob: a2ed0591fb19c57f5544f74f6233afe7cda60d35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_CIRC_BUF_H
2#define _LINUX_CIRC_BUF_H 1
3
4struct circ_buf {
5 char *buf;
6 int head;
7 int tail;
8};
9
10/* Return count in buffer. */
11#define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1))
12
13/* Return space available, 0..size-1. We always leave one free char
14 as a completely full buffer has head == tail, which is the same as
15 empty. */
16#define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size))
17
18/* Return count up to the end of the buffer. Carefully avoid
19 accessing head and tail more than once, so they can change
20 underneath us without returning inconsistent results. */
21#define CIRC_CNT_TO_END(head,tail,size) \
22 ({int end = (size) - (tail); \
23 int n = ((head) + end) & ((size)-1); \
24 n < end ? n : end;})
25
26/* Return space available up to the end of the buffer. */
27#define CIRC_SPACE_TO_END(head,tail,size) \
28 ({int end = (size) - 1 - (head); \
29 int n = (end + (tail)) & ((size)-1); \
30 n <= end ? n : end+1;})
31
32#endif /* _LINUX_CIRC_BUF_H */