circ_buf.h 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /*
  2. * See Documentation/circular-buffers.txt for more information.
  3. */
  4. #ifndef _LINUX_CIRC_BUF_H
  5. #define _LINUX_CIRC_BUF_H 1
  6. struct circ_buf {
  7. char *buf;
  8. int head;
  9. int tail;
  10. };
  11. /* Return count in buffer. */
  12. #define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1))
  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. /* Return count up to the end of the buffer. Carefully avoid
  18. accessing head and tail more than once, so they can change
  19. underneath us without returning inconsistent results. */
  20. #define CIRC_CNT_TO_END(head,tail,size) \
  21. ({int end = (size) - (tail); \
  22. int n = ((head) + end) & ((size)-1); \
  23. n < end ? n : end;})
  24. /* Return space available up to the end of the buffer. */
  25. #define CIRC_SPACE_TO_END(head,tail,size) \
  26. ({int end = (size) - 1 - (head); \
  27. int n = (end + (tail)) & ((size)-1); \
  28. n <= end ? n : end+1;})
  29. #endif /* _LINUX_CIRC_BUF_H */