circ_buf.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef _CIRC_BUF_H
  2. #define _CIRC_BUF_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  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. static __INLINE int CIRC_CNT_TO_END(uint32_t head, uint32_t tail, uint32_t size)
  21. {
  22. int end = (size) - (tail);
  23. int n = ((head) + end) & ((size)-1);
  24. return n < end ? n : end;
  25. }
  26. /* Return space available up to the end of the buffer. */
  27. static __INLINE int CIRC_SPACE_TO_END(uint32_t head, uint32_t tail, uint32_t size)
  28. {
  29. int end = (size) - 1 - (head);
  30. int n = (end + (tail)) & ((size) - 1);
  31. return n <= end ? n : end + 1;
  32. }
  33. #ifdef __cplusplus
  34. }
  35. #endif
  36. #endif /* _LINUX_CIRC_BUF_H */