route.hpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #ifndef __ROUTE_HPP__
  2. #define __ROUTE_HPP__
  3. #include <pthread.h>
  4. struct MsgBuffer {
  5. pthread_mutex_t mutex;
  6. uint8_t *buf;
  7. // The number of messages (not bytes) in, or on their way into, the
  8. // buffer
  9. uint32_t reserved;
  10. // The number of messages definitely in the buffer
  11. uint32_t inserted;
  12. MsgBuffer() : buf(NULL), reserved(0), inserted(0) {
  13. pthread_mutex_init(&mutex, NULL);
  14. }
  15. ~MsgBuffer() {
  16. delete[] buf;
  17. }
  18. // The number passed is messages, not bytes
  19. void alloc(uint32_t msgs) {
  20. delete[] buf;
  21. buf = NULL;
  22. reserved = 0;
  23. inserted = 0;
  24. // This may throw bad_alloc, but we'll catch it higher up
  25. buf = new uint8_t[size_t(msgs) * g_teems_config.msg_size];
  26. }
  27. // Reset the contents of the buffer
  28. void reset() {
  29. memset(buf, 0, inserted * g_teems_config.msg_size);
  30. reserved = 0;
  31. inserted = 0;
  32. }
  33. // You can't copy a MsgBuffer
  34. MsgBuffer(const MsgBuffer&) = delete;
  35. MsgBuffer &operator=(const MsgBuffer&) = delete;
  36. };
  37. // Call this near the end of ecall_config_load, but before
  38. // comms_init_nodestate. Returns true on success, false on failure.
  39. bool route_init();
  40. #endif