|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2015 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef MBED_CIRCULARBUFFER_H |
| 17 | +#define MBED_CIRCULARBUFFER_H |
| 18 | + |
| 19 | +#include "critical.h" |
| 20 | + |
| 21 | +namespace mbed { |
| 22 | + |
| 23 | +/** Templated Circular buffer class |
| 24 | + * |
| 25 | + * @Note Synchronization level: Interrupt safe |
| 26 | + */ |
| 27 | +template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
| 28 | +class CircularBuffer { |
| 29 | +public: |
| 30 | + CircularBuffer() : _head(0), _tail(0), _full(false) { |
| 31 | + } |
| 32 | + |
| 33 | + ~CircularBuffer() { |
| 34 | + } |
| 35 | + |
| 36 | + /** Push the transaction to the buffer. This overwrites the buffer if it's |
| 37 | + * full |
| 38 | + * |
| 39 | + * @param data Data to be pushed to the buffer |
| 40 | + */ |
| 41 | + void push(const T& data) { |
| 42 | + core_util_critical_section_enter(); |
| 43 | + if (full()) { |
| 44 | + _tail++; |
| 45 | + _tail %= BufferSize; |
| 46 | + } |
| 47 | + _pool[_head++] = data; |
| 48 | + _head %= BufferSize; |
| 49 | + if (_head == _tail) { |
| 50 | + _full = true; |
| 51 | + } |
| 52 | + core_util_critical_section_exit(); |
| 53 | + } |
| 54 | + |
| 55 | + /** Pop the transaction from the buffer |
| 56 | + * |
| 57 | + * @param data Data to be pushed to the buffer |
| 58 | + * @return True if the buffer is not empty and data contains a transaction, false otherwise |
| 59 | + */ |
| 60 | + bool pop(T& data) { |
| 61 | + bool data_popped = false; |
| 62 | + core_util_critical_section_enter(); |
| 63 | + if (!empty()) { |
| 64 | + data = _pool[_tail++]; |
| 65 | + _tail %= BufferSize; |
| 66 | + _full = false; |
| 67 | + data_popped = true; |
| 68 | + } |
| 69 | + core_util_critical_section_exit(); |
| 70 | + return data_popped; |
| 71 | + } |
| 72 | + |
| 73 | + /** Check if the buffer is empty |
| 74 | + * |
| 75 | + * @return True if the buffer is empty, false if not |
| 76 | + */ |
| 77 | + bool empty() { |
| 78 | + core_util_critical_section_enter(); |
| 79 | + bool is_empty = (_head == _tail) && !_full; |
| 80 | + core_util_critical_section_exit(); |
| 81 | + return is_empty; |
| 82 | + } |
| 83 | + |
| 84 | + /** Check if the buffer is full |
| 85 | + * |
| 86 | + * @return True if the buffer is full, false if not |
| 87 | + */ |
| 88 | + bool full() { |
| 89 | + core_util_critical_section_enter(); |
| 90 | + bool full = _full; |
| 91 | + core_util_critical_section_exit(); |
| 92 | + return full; |
| 93 | + } |
| 94 | + |
| 95 | + /** Reset the buffer |
| 96 | + * |
| 97 | + */ |
| 98 | + void reset() { |
| 99 | + core_util_critical_section_enter(); |
| 100 | + _head = 0; |
| 101 | + _tail = 0; |
| 102 | + _full = false; |
| 103 | + core_util_critical_section_exit(); |
| 104 | + } |
| 105 | + |
| 106 | +private: |
| 107 | + T _pool[BufferSize]; |
| 108 | + volatile CounterType _head; |
| 109 | + volatile CounterType _tail; |
| 110 | + volatile bool _full; |
| 111 | +}; |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +#endif |
0 commit comments