ringbuffer.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #ifndef BX_RINGBUFFER_H_HEADER_GUARD
  6. #define BX_RINGBUFFER_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "cpu.h"
  9. #include "uint32_t.h"
  10. namespace bx
  11. {
  12. ///
  13. class RingBufferControl
  14. {
  15. BX_CLASS(RingBufferControl
  16. , NO_COPY
  17. , NO_ASSIGNMENT
  18. );
  19. public:
  20. ///
  21. RingBufferControl(uint32_t _size);
  22. ///
  23. ~RingBufferControl();
  24. ///
  25. uint32_t available() const;
  26. ///
  27. uint32_t consume(uint32_t _size); // consumer only
  28. ///
  29. uint32_t reserve(uint32_t _size, bool _mustSucceed = false); // producer only
  30. ///
  31. uint32_t commit(uint32_t _size); // producer only
  32. ///
  33. uint32_t distance(uint32_t _from, uint32_t _to) const; // both
  34. ///
  35. void reset();
  36. const uint32_t m_size;
  37. uint32_t m_current;
  38. uint32_t m_write;
  39. uint32_t m_read;
  40. };
  41. ///
  42. class SpScRingBufferControl
  43. {
  44. BX_CLASS(SpScRingBufferControl
  45. , NO_COPY
  46. , NO_ASSIGNMENT
  47. );
  48. public:
  49. ///
  50. SpScRingBufferControl(uint32_t _size);
  51. ///
  52. ~SpScRingBufferControl();
  53. ///
  54. uint32_t available() const;
  55. ///
  56. uint32_t consume(uint32_t _size); // consumer only
  57. ///
  58. uint32_t reserve(uint32_t _size); // producer only
  59. ///
  60. uint32_t commit(uint32_t _size); // producer only
  61. ///
  62. uint32_t distance(uint32_t _from, uint32_t _to) const; // both
  63. ///
  64. void reset();
  65. const uint32_t m_size;
  66. uint32_t m_current;
  67. uint32_t m_write;
  68. uint32_t m_read;
  69. };
  70. ///
  71. template <typename ControlT>
  72. class ReadRingBufferT
  73. {
  74. BX_CLASS(ReadRingBufferT
  75. , NO_DEFAULT_CTOR
  76. , NO_COPY
  77. , NO_ASSIGNMENT
  78. );
  79. public:
  80. ///
  81. ReadRingBufferT(ControlT& _control, const char* _buffer, uint32_t _size);
  82. ///
  83. ~ReadRingBufferT();
  84. ///
  85. void end();
  86. ///
  87. void read(char* _data, uint32_t _len);
  88. ///
  89. void skip(uint32_t _len);
  90. private:
  91. template <typename Ty>
  92. friend class WriteRingBufferT;
  93. ControlT& m_control;
  94. uint32_t m_read;
  95. uint32_t m_end;
  96. const uint32_t m_size;
  97. const char* m_buffer;
  98. };
  99. ///
  100. typedef ReadRingBufferT<RingBufferControl> ReadRingBuffer;
  101. ///
  102. typedef ReadRingBufferT<SpScRingBufferControl> SpScReadRingBuffer;
  103. ///
  104. template <typename ControlT>
  105. class WriteRingBufferT
  106. {
  107. BX_CLASS(WriteRingBufferT
  108. , NO_DEFAULT_CTOR
  109. , NO_COPY
  110. , NO_ASSIGNMENT
  111. );
  112. public:
  113. ///
  114. WriteRingBufferT(ControlT& _control, char* _buffer, uint32_t _size);
  115. ///
  116. ~WriteRingBufferT();
  117. ///
  118. void end();
  119. ///
  120. void write(const char* _data, uint32_t _len);
  121. ///
  122. void write(ReadRingBufferT<ControlT>& _read, uint32_t _len);
  123. ///
  124. void skip(uint32_t _len);
  125. private:
  126. ControlT& m_control;
  127. uint32_t m_write;
  128. uint32_t m_end;
  129. const uint32_t m_size;
  130. char* m_buffer;
  131. };
  132. ///
  133. typedef WriteRingBufferT<RingBufferControl> WriteRingBuffer;
  134. ///
  135. typedef WriteRingBufferT<SpScRingBufferControl> SpScWriteRingBuffer;
  136. } // namespace bx
  137. #include "inline/ringbuffer.inl"
  138. #endif // BX_RINGBUFFER_H_HEADER_GUARD