/src/rt/circular_buffer.h

http://github.com/jruderman/rust · C Header · 57 lines · 28 code · 11 blank · 18 comment · 0 complexity · d9edaf46db027d603e863185de8dcc33 MD5 · raw file

  1. /*
  2. *
  3. */
  4. #ifndef CIRCULAR_BUFFER_H
  5. #define CIRCULAR_BUFFER_H
  6. #include "rust_globals.h"
  7. #include "rust_kernel.h"
  8. class
  9. circular_buffer : public kernel_owned<circular_buffer> {
  10. static const size_t INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS = 8;
  11. public:
  12. rust_kernel *kernel;
  13. // Size of the data unit in bytes.
  14. const size_t unit_sz;
  15. circular_buffer(rust_kernel *kernel, size_t unit_sz);
  16. ~circular_buffer();
  17. void transfer(void *dst);
  18. void enqueue(void *src);
  19. void dequeue(void *dst);
  20. uint8_t *peek();
  21. bool is_empty();
  22. size_t size();
  23. private:
  24. size_t initial_size();
  25. void grow();
  26. void shrink();
  27. // Size of the buffer in bytes.
  28. size_t _buffer_sz;
  29. // Byte offset within the buffer where to read the next unit of data.
  30. size_t _next;
  31. // Number of bytes that have not been read from the buffer.
  32. size_t _unread;
  33. // The buffer itself.
  34. uint8_t *_buffer;
  35. };
  36. //
  37. // Local Variables:
  38. // mode: C++
  39. // fill-column: 78;
  40. // indent-tabs-mode: nil
  41. // c-basic-offset: 4
  42. // buffer-file-coding-system: utf-8-unix
  43. // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
  44. // End:
  45. //
  46. #endif /* CIRCULAR_BUFFER_H */