cringbuffer_internal.h 828 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright: SPDX-License-Identifier: GPL-3.0-only
  2. #ifndef CRINGBUFFER_INTERNAL_H
  3. #define CRINGBUFFER_INTERNAL_H
  4. struct rbuf_t {
  5. char *data;
  6. // points to next byte where we can write
  7. char *head;
  8. // points to oldest (next to be poped) readable byte
  9. char *tail;
  10. // to avoid calculating data + size
  11. // all the time
  12. char *end;
  13. size_t size;
  14. size_t size_data;
  15. };
  16. /* this exists so that it can be tested by unit tests
  17. * without optimization that resets head and tail to
  18. * beginning if buffer empty
  19. */
  20. inline static int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes)
  21. {
  22. if (bytes > buffer->size_data)
  23. return 0;
  24. int i = buffer->tail - buffer->data;
  25. buffer->tail = &buffer->data[(i + bytes) % buffer->size];
  26. buffer->size_data -= bytes;
  27. return 1;
  28. }
  29. #endif