cyclic_buffer & fifo_buffer
This commit is contained in:
parent
d85c4cdefd
commit
c4d499fdf2
|
@ -0,0 +1,179 @@
|
||||||
|
/**
|
||||||
|
* @file cyclic_buffer.h
|
||||||
|
* @brief A cyclic buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.
|
||||||
|
*
|
||||||
|
* http://en.wikipedia.org/wiki/Circular_buffer
|
||||||
|
*
|
||||||
|
* @date Dec 8, 2013
|
||||||
|
* @author Andrey Belomutskiy, Daniel Hill
|
||||||
|
*
|
||||||
|
* Daniel Hill - Modified to use C++ - Mar 2, 2014
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <limits>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
static const short CB_MAX_SIZE = 128;
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize = CB_MAX_SIZE>
|
||||||
|
class cyclic_buffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
cyclic_buffer();
|
||||||
|
explicit cyclic_buffer(int size);
|
||||||
|
|
||||||
|
public:
|
||||||
|
void add(T value);
|
||||||
|
T get(int index) const;
|
||||||
|
T sum(size_t length) const;
|
||||||
|
T maxValue(size_t length) const;
|
||||||
|
T minValue(size_t length) const;
|
||||||
|
void setSize(size_t size);
|
||||||
|
bool contains(T value) const;
|
||||||
|
int getSize() const;
|
||||||
|
int getCount() const;
|
||||||
|
void clear();
|
||||||
|
T elements[maxSize];
|
||||||
|
uint16_t currentIndex;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
uint16_t size;
|
||||||
|
/**
|
||||||
|
* number of elements added into this buffer, would be eventually bigger then size
|
||||||
|
*/
|
||||||
|
size_t count;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
cyclic_buffer<T, maxSize>::cyclic_buffer() : cyclic_buffer(maxSize) {
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
cyclic_buffer<T, maxSize>::cyclic_buffer(int size) {
|
||||||
|
setSize(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void cyclic_buffer<T, maxSize>::add(T value) {
|
||||||
|
// Too lazy to make this thread safe, but at the very least let's never let currentIndex
|
||||||
|
// become invalid. And yes I did see a crash due to an overrun here.
|
||||||
|
uint16_t idx = currentIndex;
|
||||||
|
|
||||||
|
((T &)elements[idx]) = value;
|
||||||
|
|
||||||
|
if (++idx == size) {
|
||||||
|
idx = 0;
|
||||||
|
}
|
||||||
|
currentIndex = idx;
|
||||||
|
|
||||||
|
count = count + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
bool cyclic_buffer<T, maxSize>::contains(T value) const {
|
||||||
|
for (int i = 0; i < currentIndex ; i++) {
|
||||||
|
if (elements[i] == value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void cyclic_buffer<T, maxSize>::setSize(size_t size) {
|
||||||
|
clear();
|
||||||
|
this->size = size < maxSize ? size : maxSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
int cyclic_buffer<T, maxSize>::getSize() const {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
int cyclic_buffer<T, maxSize>::getCount() const {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T cyclic_buffer<T, maxSize>::get(int index) const {
|
||||||
|
while (index < 0) {
|
||||||
|
index += size;
|
||||||
|
}
|
||||||
|
while (index >= size) {
|
||||||
|
index -= size;
|
||||||
|
}
|
||||||
|
return elements[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T cyclic_buffer<T, maxSize>::maxValue(size_t length) const {
|
||||||
|
if (length > count) {
|
||||||
|
// not enough data in buffer
|
||||||
|
length = count;
|
||||||
|
}
|
||||||
|
int ci = currentIndex; // local copy to increase thread-safety
|
||||||
|
T result = std::numeric_limits<T>::min();
|
||||||
|
for (size_t i = 0; i < length; ++i) {
|
||||||
|
int index = ci - i - 1;
|
||||||
|
while (index < 0) {
|
||||||
|
index += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elements[index] > result) {
|
||||||
|
result = elements[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T cyclic_buffer<T, maxSize>::minValue(size_t length) const {
|
||||||
|
if (length > count) {
|
||||||
|
length = count;
|
||||||
|
}
|
||||||
|
int ci = currentIndex; // local copy to increase thread-safety
|
||||||
|
T result = std::numeric_limits<T>::max();
|
||||||
|
for (size_t i = 0; i < length; ++i) {
|
||||||
|
int index = ci - i - 1;
|
||||||
|
while (index < 0) {
|
||||||
|
index += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elements[index] < result) {
|
||||||
|
result = elements[index];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T cyclic_buffer<T, maxSize>::sum(size_t length) const {
|
||||||
|
if (length > count) {
|
||||||
|
length = count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ci = currentIndex; // local copy to increase thread-safety
|
||||||
|
T result = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < length; ++i) {
|
||||||
|
int index = ci - i - 1;
|
||||||
|
while (index < 0) {
|
||||||
|
index += size;
|
||||||
|
}
|
||||||
|
|
||||||
|
result += elements[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void cyclic_buffer<T, maxSize>::clear() {
|
||||||
|
memset((void*) elements, 0, sizeof(elements)); // I would usually use static_cast, but due to the elements being volatile we cannot.
|
||||||
|
count = 0;
|
||||||
|
currentIndex = 0;
|
||||||
|
}
|
|
@ -0,0 +1,159 @@
|
||||||
|
/**
|
||||||
|
* @file fifo_buffer.h
|
||||||
|
* @brief A FIFO buffer (base on cyclic_buffer)
|
||||||
|
*
|
||||||
|
* https://en.wikipedia.org/wiki/FIFO_(computing_and_electronics)
|
||||||
|
*
|
||||||
|
* @date Aug 6, 2020
|
||||||
|
* @author andreika <prometheus.pcb@gmail.com>
|
||||||
|
* @author Andrey Belomutskiy
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef FIFO_BUFFER_H
|
||||||
|
#define FIFO_BUFFER_H
|
||||||
|
|
||||||
|
#include "cyclic_buffer.h"
|
||||||
|
|
||||||
|
#if EFI_UNIT_TEST
|
||||||
|
#include "global.h"
|
||||||
|
#endif // EFI_UNIT_TEST
|
||||||
|
|
||||||
|
// todo: this is not a thread-safe version!
|
||||||
|
template<typename T, size_t maxSize = CB_MAX_SIZE>
|
||||||
|
class fifo_buffer : public cyclic_buffer<T, maxSize> {
|
||||||
|
using cyclic_buffer<T, maxSize>::add;
|
||||||
|
using cyclic_buffer<T, maxSize>::getSize;
|
||||||
|
using cyclic_buffer<T, maxSize>::elements;
|
||||||
|
using cyclic_buffer<T, maxSize>::size, cyclic_buffer<T, maxSize>::count;
|
||||||
|
|
||||||
|
public:
|
||||||
|
fifo_buffer() : currentIndexRead(0) {
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual bool put(T item);
|
||||||
|
T get();
|
||||||
|
void clear() /*override*/;
|
||||||
|
|
||||||
|
virtual bool put(const T *items, int numItems);
|
||||||
|
|
||||||
|
bool isEmpty() const {
|
||||||
|
return getCount() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isFull() const {
|
||||||
|
return getCount() >= getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
int getCount() const {
|
||||||
|
return cyclic_buffer<T, maxSize>::getCount();
|
||||||
|
}
|
||||||
|
|
||||||
|
int getSize() const {
|
||||||
|
return cyclic_buffer<T, maxSize>::getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
const volatile T* getElements() const {
|
||||||
|
return elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
int currentIndexRead; // FIFO "tail"
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
bool fifo_buffer<T, maxSize>::put(T item) {
|
||||||
|
// check if full
|
||||||
|
if (!isFull()) {
|
||||||
|
add(item);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
bool fifo_buffer<T, maxSize>::put(const T *items, int numItems) {
|
||||||
|
for (int i = 0; i < numItems; i++) {
|
||||||
|
if (!put(items[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
T fifo_buffer<T, maxSize>::get() {
|
||||||
|
T &ret = (T &)elements[currentIndexRead];
|
||||||
|
if (!isEmpty()) {
|
||||||
|
currentIndexRead = (currentIndexRead + 1) % size;
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize>
|
||||||
|
void fifo_buffer<T, maxSize>::clear() {
|
||||||
|
cyclic_buffer<T, maxSize>::clear();
|
||||||
|
currentIndexRead = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, size_t maxSize = CB_MAX_SIZE>
|
||||||
|
class fifo_buffer_sync : public fifo_buffer<T, maxSize> {
|
||||||
|
public:
|
||||||
|
fifo_buffer_sync() {
|
||||||
|
#if !EFI_UNIT_TEST
|
||||||
|
osalThreadQueueObjectInit(&q_waiting);
|
||||||
|
#endif // EFI_UNIT_TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
bool put(T item) override {
|
||||||
|
chSysLock();
|
||||||
|
if (fifo_buffer<T, maxSize>::isFull()) {
|
||||||
|
chSysUnlock();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
fifo_buffer<T, maxSize>::put(item);
|
||||||
|
osalThreadDequeueNextI(&q_waiting, MSG_OK);
|
||||||
|
chSysUnlock();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool put(const T *items, int numItems) override {
|
||||||
|
for (int i = 0; i < numItems; i++) {
|
||||||
|
if (!put(items[i]))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get(T &item, int timeout) {
|
||||||
|
chSysLock();
|
||||||
|
#if !EFI_UNIT_TEST
|
||||||
|
while (fifo_buffer<T, maxSize>::isEmpty()) {
|
||||||
|
msg_t msg = osalThreadEnqueueTimeoutS(&q_waiting, timeout);
|
||||||
|
if (msg != MSG_OK) {
|
||||||
|
chSysUnlock();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif // EFI_UNIT_TEST
|
||||||
|
item = fifo_buffer<T, maxSize>::get();
|
||||||
|
chSysUnlock();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
chSysLock();
|
||||||
|
fifo_buffer<T, maxSize>::clear();
|
||||||
|
#if !EFI_UNIT_TEST
|
||||||
|
osalThreadDequeueAllI(&q_waiting, MSG_RESET);
|
||||||
|
#endif // EFI_UNIT_TEST
|
||||||
|
chSysUnlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
#if !EFI_UNIT_TEST
|
||||||
|
threads_queue_t q_waiting;
|
||||||
|
#endif // EFI_UNIT_TEST
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* FIFO_BUFFER_H */
|
Loading…
Reference in New Issue