kernel 提供下面四个宏用于circ_buf的编程
返回buffer中没有读的count
#define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1))
返回还有几个空间可以写
#define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size))
我们假定size =8,head=6,tail=4.
head-tail = -3 ,计算机中是用补码来表示的。-3的补码是1111 1101 & 0000 0111 = 0000 0101 =5表示还有5个空间可以写.8-4+1刚好等于5.
25 #define CIRC_CNT_TO_END(head,tail,size) \
26 ({int end = (size) - (tail); \
27 int n = ((head) + end) & ((size)-1); \
28 n < end ? n : end;})
从header到size还有几个位置,head =6,tail=4 size=8.则返回2
31 #define CIRC_SPACE_TO_END(head,tail,size) \
32 ({int end = (size) - 1 - (head); \
33 int n = (end + (tail)) & ((size)-1); \
34 n <= end ? n : end+1;})
从tail到size还有几个位置,head =6,tail=4 size=8.则返回5