circ_buf.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * See Documentation/core-api/circular-buffers.rst for more information.
  4. */
  5. #ifndef _LINUX_CIRC_BUF_H
  6. #define _LINUX_CIRC_BUF_H 1
  7. struct circ_buf {
  8. char *buf;
  9. int head;
  10. int tail;
  11. };
  12. /* Return count in buffer. */
  13. #define CIRC_CNT(head,tail,size) (((head) - (tail)) & ((size)-1))
  14. /* Return space available, 0..size-1. We always leave one free char
  15. as a completely full buffer has head == tail, which is the same as
  16. empty. */
  17. #define CIRC_SPACE(head,tail,size) CIRC_CNT((tail),((head)+1),(size))
  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. /* Return space available up to the end of the buffer. */
  26. #define CIRC_SPACE_TO_END(head,tail,size) \
  27. ({int end = (size) - 1 - (head); \
  28. int n = (end + (tail)) & ((size)-1); \
  29. n <= end ? n : end+1;})
  30. #endif /* _LINUX_CIRC_BUF_H */