Nicolas Borla
/
BBR_1Ebene
BBR 1 Ebene
mbed-os/platform/CircularBuffer.h@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2015 ARM Limited |
borlanic | 0:fbdae7e6d805 | 3 | * |
borlanic | 0:fbdae7e6d805 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:fbdae7e6d805 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:fbdae7e6d805 | 6 | * You may obtain a copy of the License at |
borlanic | 0:fbdae7e6d805 | 7 | * |
borlanic | 0:fbdae7e6d805 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:fbdae7e6d805 | 9 | * |
borlanic | 0:fbdae7e6d805 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:fbdae7e6d805 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:fbdae7e6d805 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:fbdae7e6d805 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:fbdae7e6d805 | 14 | * limitations under the License. |
borlanic | 0:fbdae7e6d805 | 15 | */ |
borlanic | 0:fbdae7e6d805 | 16 | #ifndef MBED_CIRCULARBUFFER_H |
borlanic | 0:fbdae7e6d805 | 17 | #define MBED_CIRCULARBUFFER_H |
borlanic | 0:fbdae7e6d805 | 18 | |
borlanic | 0:fbdae7e6d805 | 19 | #include "platform/mbed_critical.h" |
borlanic | 0:fbdae7e6d805 | 20 | #include "platform/mbed_assert.h" |
borlanic | 0:fbdae7e6d805 | 21 | |
borlanic | 0:fbdae7e6d805 | 22 | namespace mbed { |
borlanic | 0:fbdae7e6d805 | 23 | |
borlanic | 0:fbdae7e6d805 | 24 | namespace internal { |
borlanic | 0:fbdae7e6d805 | 25 | /* Detect if CounterType of the Circular buffer is of unsigned type. */ |
borlanic | 0:fbdae7e6d805 | 26 | template<typename T> |
borlanic | 0:fbdae7e6d805 | 27 | struct is_unsigned { static const bool value = false; }; |
borlanic | 0:fbdae7e6d805 | 28 | template<> |
borlanic | 0:fbdae7e6d805 | 29 | struct is_unsigned<unsigned char> { static const bool value = true; }; |
borlanic | 0:fbdae7e6d805 | 30 | template<> |
borlanic | 0:fbdae7e6d805 | 31 | struct is_unsigned<unsigned short> { static const bool value = true; }; |
borlanic | 0:fbdae7e6d805 | 32 | template<> |
borlanic | 0:fbdae7e6d805 | 33 | struct is_unsigned<unsigned int> { static const bool value = true; }; |
borlanic | 0:fbdae7e6d805 | 34 | template<> |
borlanic | 0:fbdae7e6d805 | 35 | struct is_unsigned<unsigned long> { static const bool value = true; }; |
borlanic | 0:fbdae7e6d805 | 36 | template<> |
borlanic | 0:fbdae7e6d805 | 37 | struct is_unsigned<unsigned long long> { static const bool value = true; }; |
borlanic | 0:fbdae7e6d805 | 38 | }; |
borlanic | 0:fbdae7e6d805 | 39 | |
borlanic | 0:fbdae7e6d805 | 40 | /** \addtogroup platform */ |
borlanic | 0:fbdae7e6d805 | 41 | /** @{*/ |
borlanic | 0:fbdae7e6d805 | 42 | /** |
borlanic | 0:fbdae7e6d805 | 43 | * \defgroup platform_CircularBuffer CircularBuffer functions |
borlanic | 0:fbdae7e6d805 | 44 | * @{ |
borlanic | 0:fbdae7e6d805 | 45 | */ |
borlanic | 0:fbdae7e6d805 | 46 | |
borlanic | 0:fbdae7e6d805 | 47 | /** Templated Circular buffer class |
borlanic | 0:fbdae7e6d805 | 48 | * |
borlanic | 0:fbdae7e6d805 | 49 | * @note Synchronization level: Interrupt safe |
borlanic | 0:fbdae7e6d805 | 50 | * @note CounterType must be unsigned and consistent with BufferSize |
borlanic | 0:fbdae7e6d805 | 51 | */ |
borlanic | 0:fbdae7e6d805 | 52 | template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
borlanic | 0:fbdae7e6d805 | 53 | class CircularBuffer { |
borlanic | 0:fbdae7e6d805 | 54 | public: |
borlanic | 0:fbdae7e6d805 | 55 | CircularBuffer() : _head(0), _tail(0), _full(false) { |
borlanic | 0:fbdae7e6d805 | 56 | MBED_STATIC_ASSERT( |
borlanic | 0:fbdae7e6d805 | 57 | internal::is_unsigned<CounterType>::value, |
borlanic | 0:fbdae7e6d805 | 58 | "CounterType must be unsigned" |
borlanic | 0:fbdae7e6d805 | 59 | ); |
borlanic | 0:fbdae7e6d805 | 60 | |
borlanic | 0:fbdae7e6d805 | 61 | MBED_STATIC_ASSERT( |
borlanic | 0:fbdae7e6d805 | 62 | (sizeof(CounterType) >= sizeof(uint32_t)) || |
borlanic | 0:fbdae7e6d805 | 63 | (BufferSize < (((uint64_t) 1) << (sizeof(CounterType) * 8))), |
borlanic | 0:fbdae7e6d805 | 64 | "Invalid BufferSize for the CounterType" |
borlanic | 0:fbdae7e6d805 | 65 | ); |
borlanic | 0:fbdae7e6d805 | 66 | } |
borlanic | 0:fbdae7e6d805 | 67 | |
borlanic | 0:fbdae7e6d805 | 68 | ~CircularBuffer() { |
borlanic | 0:fbdae7e6d805 | 69 | } |
borlanic | 0:fbdae7e6d805 | 70 | |
borlanic | 0:fbdae7e6d805 | 71 | /** Push the transaction to the buffer. This overwrites the buffer if it's |
borlanic | 0:fbdae7e6d805 | 72 | * full |
borlanic | 0:fbdae7e6d805 | 73 | * |
borlanic | 0:fbdae7e6d805 | 74 | * @param data Data to be pushed to the buffer |
borlanic | 0:fbdae7e6d805 | 75 | */ |
borlanic | 0:fbdae7e6d805 | 76 | void push(const T& data) { |
borlanic | 0:fbdae7e6d805 | 77 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 78 | if (full()) { |
borlanic | 0:fbdae7e6d805 | 79 | _tail++; |
borlanic | 0:fbdae7e6d805 | 80 | _tail %= BufferSize; |
borlanic | 0:fbdae7e6d805 | 81 | } |
borlanic | 0:fbdae7e6d805 | 82 | _pool[_head++] = data; |
borlanic | 0:fbdae7e6d805 | 83 | _head %= BufferSize; |
borlanic | 0:fbdae7e6d805 | 84 | if (_head == _tail) { |
borlanic | 0:fbdae7e6d805 | 85 | _full = true; |
borlanic | 0:fbdae7e6d805 | 86 | } |
borlanic | 0:fbdae7e6d805 | 87 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 88 | } |
borlanic | 0:fbdae7e6d805 | 89 | |
borlanic | 0:fbdae7e6d805 | 90 | /** Pop the transaction from the buffer |
borlanic | 0:fbdae7e6d805 | 91 | * |
borlanic | 0:fbdae7e6d805 | 92 | * @param data Data to be popped from the buffer |
borlanic | 0:fbdae7e6d805 | 93 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
borlanic | 0:fbdae7e6d805 | 94 | */ |
borlanic | 0:fbdae7e6d805 | 95 | bool pop(T& data) { |
borlanic | 0:fbdae7e6d805 | 96 | bool data_popped = false; |
borlanic | 0:fbdae7e6d805 | 97 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 98 | if (!empty()) { |
borlanic | 0:fbdae7e6d805 | 99 | data = _pool[_tail++]; |
borlanic | 0:fbdae7e6d805 | 100 | _tail %= BufferSize; |
borlanic | 0:fbdae7e6d805 | 101 | _full = false; |
borlanic | 0:fbdae7e6d805 | 102 | data_popped = true; |
borlanic | 0:fbdae7e6d805 | 103 | } |
borlanic | 0:fbdae7e6d805 | 104 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 105 | return data_popped; |
borlanic | 0:fbdae7e6d805 | 106 | } |
borlanic | 0:fbdae7e6d805 | 107 | |
borlanic | 0:fbdae7e6d805 | 108 | /** Check if the buffer is empty |
borlanic | 0:fbdae7e6d805 | 109 | * |
borlanic | 0:fbdae7e6d805 | 110 | * @return True if the buffer is empty, false if not |
borlanic | 0:fbdae7e6d805 | 111 | */ |
borlanic | 0:fbdae7e6d805 | 112 | bool empty() const { |
borlanic | 0:fbdae7e6d805 | 113 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 114 | bool is_empty = (_head == _tail) && !_full; |
borlanic | 0:fbdae7e6d805 | 115 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 116 | return is_empty; |
borlanic | 0:fbdae7e6d805 | 117 | } |
borlanic | 0:fbdae7e6d805 | 118 | |
borlanic | 0:fbdae7e6d805 | 119 | /** Check if the buffer is full |
borlanic | 0:fbdae7e6d805 | 120 | * |
borlanic | 0:fbdae7e6d805 | 121 | * @return True if the buffer is full, false if not |
borlanic | 0:fbdae7e6d805 | 122 | */ |
borlanic | 0:fbdae7e6d805 | 123 | bool full() const { |
borlanic | 0:fbdae7e6d805 | 124 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 125 | bool full = _full; |
borlanic | 0:fbdae7e6d805 | 126 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 127 | return full; |
borlanic | 0:fbdae7e6d805 | 128 | } |
borlanic | 0:fbdae7e6d805 | 129 | |
borlanic | 0:fbdae7e6d805 | 130 | /** Reset the buffer |
borlanic | 0:fbdae7e6d805 | 131 | * |
borlanic | 0:fbdae7e6d805 | 132 | */ |
borlanic | 0:fbdae7e6d805 | 133 | void reset() { |
borlanic | 0:fbdae7e6d805 | 134 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 135 | _head = 0; |
borlanic | 0:fbdae7e6d805 | 136 | _tail = 0; |
borlanic | 0:fbdae7e6d805 | 137 | _full = false; |
borlanic | 0:fbdae7e6d805 | 138 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 139 | } |
borlanic | 0:fbdae7e6d805 | 140 | |
borlanic | 0:fbdae7e6d805 | 141 | /** Get the number of elements currently stored in the circular_buffer */ |
borlanic | 0:fbdae7e6d805 | 142 | CounterType size() const { |
borlanic | 0:fbdae7e6d805 | 143 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 144 | CounterType elements; |
borlanic | 0:fbdae7e6d805 | 145 | if (!_full) { |
borlanic | 0:fbdae7e6d805 | 146 | if (_head < _tail) { |
borlanic | 0:fbdae7e6d805 | 147 | elements = BufferSize + _head - _tail; |
borlanic | 0:fbdae7e6d805 | 148 | } else { |
borlanic | 0:fbdae7e6d805 | 149 | elements = _head - _tail; |
borlanic | 0:fbdae7e6d805 | 150 | } |
borlanic | 0:fbdae7e6d805 | 151 | } else { |
borlanic | 0:fbdae7e6d805 | 152 | elements = BufferSize; |
borlanic | 0:fbdae7e6d805 | 153 | } |
borlanic | 0:fbdae7e6d805 | 154 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 155 | return elements; |
borlanic | 0:fbdae7e6d805 | 156 | } |
borlanic | 0:fbdae7e6d805 | 157 | |
borlanic | 0:fbdae7e6d805 | 158 | /** Peek into circular buffer without popping |
borlanic | 0:fbdae7e6d805 | 159 | * |
borlanic | 0:fbdae7e6d805 | 160 | * @param data Data to be peeked from the buffer |
borlanic | 0:fbdae7e6d805 | 161 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
borlanic | 0:fbdae7e6d805 | 162 | */ |
borlanic | 0:fbdae7e6d805 | 163 | bool peek(T& data) const { |
borlanic | 0:fbdae7e6d805 | 164 | bool data_updated = false; |
borlanic | 0:fbdae7e6d805 | 165 | core_util_critical_section_enter(); |
borlanic | 0:fbdae7e6d805 | 166 | if (!empty()) { |
borlanic | 0:fbdae7e6d805 | 167 | data = _pool[_tail]; |
borlanic | 0:fbdae7e6d805 | 168 | data_updated = true; |
borlanic | 0:fbdae7e6d805 | 169 | } |
borlanic | 0:fbdae7e6d805 | 170 | core_util_critical_section_exit(); |
borlanic | 0:fbdae7e6d805 | 171 | return data_updated; |
borlanic | 0:fbdae7e6d805 | 172 | } |
borlanic | 0:fbdae7e6d805 | 173 | |
borlanic | 0:fbdae7e6d805 | 174 | private: |
borlanic | 0:fbdae7e6d805 | 175 | T _pool[BufferSize]; |
borlanic | 0:fbdae7e6d805 | 176 | volatile CounterType _head; |
borlanic | 0:fbdae7e6d805 | 177 | volatile CounterType _tail; |
borlanic | 0:fbdae7e6d805 | 178 | volatile bool _full; |
borlanic | 0:fbdae7e6d805 | 179 | }; |
borlanic | 0:fbdae7e6d805 | 180 | |
borlanic | 0:fbdae7e6d805 | 181 | /**@}*/ |
borlanic | 0:fbdae7e6d805 | 182 | |
borlanic | 0:fbdae7e6d805 | 183 | /**@}*/ |
borlanic | 0:fbdae7e6d805 | 184 | |
borlanic | 0:fbdae7e6d805 | 185 | } |
borlanic | 0:fbdae7e6d805 | 186 | |
borlanic | 0:fbdae7e6d805 | 187 | #endif |