123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #ifndef MBED_CIRCULARBUFFER_H
- #define MBED_CIRCULARBUFFER_H
- #include "platform/mbed_critical.h"
- #include "platform/mbed_assert.h"
- namespace mbed {
- namespace internal {
- template<typename T>
- struct is_unsigned {
- static const bool value = false;
- };
- template<>
- struct is_unsigned<unsigned char> {
- static const bool value = true;
- };
- template<>
- struct is_unsigned<unsigned short> {
- static const bool value = true;
- };
- template<>
- struct is_unsigned<unsigned int> {
- static const bool value = true;
- };
- template<>
- struct is_unsigned<unsigned long> {
- static const bool value = true;
- };
- template<>
- struct is_unsigned<unsigned long long> {
- static const bool value = true;
- };
- };
- template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
- class CircularBuffer {
- public:
- CircularBuffer() : _head(0), _tail(0), _full(false)
- {
- MBED_STATIC_ASSERT(
- internal::is_unsigned<CounterType>::value,
- "CounterType must be unsigned"
- );
- MBED_STATIC_ASSERT(
- (sizeof(CounterType) >= sizeof(uint32_t)) ||
- (BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))),
- "Invalid BufferSize for the CounterType"
- );
- }
- ~CircularBuffer()
- {
- }
-
- void push(const T &data)
- {
- core_util_critical_section_enter();
- if (full()) {
- _tail++;
- _tail %= BufferSize;
- }
- _pool[_head++] = data;
- _head %= BufferSize;
- if (_head == _tail) {
- _full = true;
- }
- core_util_critical_section_exit();
- }
-
- bool pop(T &data)
- {
- bool data_popped = false;
- core_util_critical_section_enter();
- if (!empty()) {
- data = _pool[_tail++];
- _tail %= BufferSize;
- _full = false;
- data_popped = true;
- }
- core_util_critical_section_exit();
- return data_popped;
- }
-
- bool empty() const
- {
- core_util_critical_section_enter();
- bool is_empty = (_head == _tail) && !_full;
- core_util_critical_section_exit();
- return is_empty;
- }
-
- bool full() const
- {
- core_util_critical_section_enter();
- bool full = _full;
- core_util_critical_section_exit();
- return full;
- }
-
- void reset()
- {
- core_util_critical_section_enter();
- _head = 0;
- _tail = 0;
- _full = false;
- core_util_critical_section_exit();
- }
-
- CounterType size() const
- {
- core_util_critical_section_enter();
- CounterType elements;
- if (!_full) {
- if (_head < _tail) {
- elements = BufferSize + _head - _tail;
- } else {
- elements = _head - _tail;
- }
- } else {
- elements = BufferSize;
- }
- core_util_critical_section_exit();
- return elements;
- }
-
- bool peek(T &data) const
- {
- bool data_updated = false;
- core_util_critical_section_enter();
- if (!empty()) {
- data = _pool[_tail];
- data_updated = true;
- }
- core_util_critical_section_exit();
- return data_updated;
- }
- private:
- T _pool[BufferSize];
- volatile CounterType _head;
- volatile CounterType _tail;
- volatile bool _full;
- };
- }
- #endif
|