circ_buf.h 1000 B

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