2016-01-14 18:45:04 +00:00
|
|
|
/*
|
2016-02-15 20:18:30 +00:00
|
|
|
* Copyright (C) 2006-2009,2012,2013,2015,2016 by Jonathan Naylor G4KLX
|
2016-01-14 18:45:04 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef RingBuffer_H
|
|
|
|
#define RingBuffer_H
|
|
|
|
|
2016-02-15 20:18:30 +00:00
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
2016-01-14 18:45:04 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
template<class T> class CRingBuffer {
|
|
|
|
public:
|
2016-02-15 20:18:30 +00:00
|
|
|
CRingBuffer(unsigned int length, const char* name) :
|
2016-01-14 18:45:04 +00:00
|
|
|
m_length(length),
|
2016-02-15 20:18:30 +00:00
|
|
|
m_name(name),
|
2016-01-14 18:45:04 +00:00
|
|
|
m_buffer(NULL),
|
|
|
|
m_iPtr(0U),
|
2016-02-15 20:59:24 +00:00
|
|
|
m_oPtr(0U)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
|
|
|
assert(length > 0U);
|
2016-02-15 20:18:30 +00:00
|
|
|
assert(name != NULL);
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
m_buffer = new T[length];
|
|
|
|
|
|
|
|
::memset(m_buffer, 0x00, m_length * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
~CRingBuffer()
|
|
|
|
{
|
|
|
|
delete[] m_buffer;
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:18:30 +00:00
|
|
|
bool addData(const T* buffer, unsigned int nSamples)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
if (nSamples >= freeSpace()) {
|
|
|
|
LogError("**** Overflow in %s ring buffer, %u >= %u", m_name, nSamples, freeSpace());
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-15 20:18:30 +00:00
|
|
|
|
2016-02-15 20:59:24 +00:00
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
2016-01-14 18:45:04 +00:00
|
|
|
m_buffer[m_iPtr++] = buffer[i];
|
|
|
|
|
|
|
|
if (m_iPtr == m_length)
|
|
|
|
m_iPtr = 0U;
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:18:30 +00:00
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 20:59:24 +00:00
|
|
|
bool getData(T* buffer, unsigned int nSamples)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
if (dataSize() < nSamples) {
|
|
|
|
LogError("**** Underflow in %s ring buffer, %u < %u", m_name, dataSize(), nSamples);
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
buffer[i] = m_buffer[m_oPtr++];
|
|
|
|
|
|
|
|
if (m_oPtr == m_length)
|
|
|
|
m_oPtr = 0U;
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:59:24 +00:00
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-15 20:59:24 +00:00
|
|
|
bool peek(T* buffer, unsigned int nSamples)
|
2016-01-14 18:45:04 +00:00
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
if (dataSize() < nSamples) {
|
|
|
|
LogError("**** Underflow peek in %s ring buffer, %u < %u", m_name, dataSize(), nSamples);
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
unsigned int ptr = m_oPtr;
|
|
|
|
for (unsigned int i = 0U; i < nSamples; i++) {
|
|
|
|
buffer[i] = m_buffer[ptr++];
|
|
|
|
|
|
|
|
if (ptr == m_length)
|
|
|
|
ptr = 0U;
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:59:24 +00:00
|
|
|
return true;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
2016-02-15 20:18:30 +00:00
|
|
|
m_iPtr = 0U;
|
|
|
|
m_oPtr = 0U;
|
2016-01-14 18:45:04 +00:00
|
|
|
|
|
|
|
::memset(m_buffer, 0x00, m_length * sizeof(T));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int freeSpace() const
|
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
if (m_oPtr == m_iPtr)
|
2016-02-15 20:18:30 +00:00
|
|
|
return m_length;
|
|
|
|
|
2016-02-15 22:21:50 +00:00
|
|
|
if (m_oPtr > m_iPtr)
|
|
|
|
return m_oPtr - m_iPtr;
|
2016-01-14 18:45:04 +00:00
|
|
|
|
2016-02-15 22:21:50 +00:00
|
|
|
return (m_length + m_oPtr) - m_iPtr;
|
2016-02-15 20:18:30 +00:00
|
|
|
}
|
2016-01-14 18:45:04 +00:00
|
|
|
|
2016-02-15 20:18:30 +00:00
|
|
|
unsigned int dataSize() const
|
|
|
|
{
|
|
|
|
return m_length - freeSpace();
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasSpace(unsigned int length) const
|
|
|
|
{
|
|
|
|
return freeSpace() > length;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasData() const
|
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
return m_oPtr != m_iPtr;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isEmpty() const
|
|
|
|
{
|
2016-02-15 20:59:24 +00:00
|
|
|
return m_oPtr == m_iPtr;
|
2016-01-14 18:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-02-15 20:59:24 +00:00
|
|
|
unsigned int m_length;
|
|
|
|
const char* m_name;
|
|
|
|
T* m_buffer;
|
|
|
|
unsigned int m_iPtr;
|
|
|
|
unsigned int m_oPtr;
|
2016-01-14 18:45:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|