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