2001-03-31 08:42:27 +02:00
|
|
|
/*
|
2002-06-16 12:57:31 +02:00
|
|
|
* ringbuffer.c: A ring buffer
|
2001-03-31 08:42:27 +02:00
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
|
|
|
* Parts of this file were inspired by the 'ringbuffy.c' from the
|
|
|
|
* LinuxDVB driver (see linuxtv.org).
|
|
|
|
*
|
2016-12-22 11:33:12 +01:00
|
|
|
* $Id: ringbuffer.c 4.1 2016/12/22 10:26:13 kls Exp $
|
2001-03-31 08:42:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ringbuffer.h"
|
2002-07-28 12:48:44 +02:00
|
|
|
#include <stdlib.h>
|
2002-06-16 12:57:31 +02:00
|
|
|
#include <unistd.h>
|
2001-03-31 08:42:27 +02:00
|
|
|
#include "tools.h"
|
|
|
|
|
2002-06-16 12:57:31 +02:00
|
|
|
// --- cRingBuffer -----------------------------------------------------------
|
2001-03-31 08:42:27 +02:00
|
|
|
|
2004-03-07 14:41:45 +01:00
|
|
|
#define OVERFLOWREPORTDELTA 5 // seconds between reports
|
2004-10-16 09:36:28 +02:00
|
|
|
#define PERCENTAGEDELTA 10
|
|
|
|
#define PERCENTAGETHRESHOLD 70
|
2012-09-22 11:52:33 +02:00
|
|
|
#define IOTHROTTLELOW 20
|
|
|
|
#define IOTHROTTLEHIGH 50
|
2004-03-07 14:41:45 +01:00
|
|
|
|
2001-06-02 10:47:40 +02:00
|
|
|
cRingBuffer::cRingBuffer(int Size, bool Statistics)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
|
|
|
size = Size;
|
2001-06-02 10:47:40 +02:00
|
|
|
statistics = Statistics;
|
2004-10-16 09:36:28 +02:00
|
|
|
getThreadTid = 0;
|
2001-03-31 08:42:27 +02:00
|
|
|
maxFill = 0;
|
2002-06-16 12:57:31 +02:00
|
|
|
lastPercent = 0;
|
2003-05-12 17:46:34 +02:00
|
|
|
putTimeout = getTimeout = 0;
|
2004-03-07 14:41:45 +01:00
|
|
|
lastOverflowReport = 0;
|
|
|
|
overflowCount = overflowBytes = 0;
|
2012-09-22 11:52:33 +02:00
|
|
|
ioThrottle = NULL;
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cRingBuffer::~cRingBuffer()
|
|
|
|
{
|
2012-09-22 11:52:33 +02:00
|
|
|
delete ioThrottle;
|
2001-08-05 12:23:24 +02:00
|
|
|
if (statistics)
|
2002-05-13 16:35:49 +02:00
|
|
|
dsyslog("buffer stats: %d (%d%%) used", maxFill, maxFill * 100 / (size - 1));
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
void cRingBuffer::UpdatePercentage(int Fill)
|
2003-05-12 17:46:34 +02:00
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
if (Fill > maxFill)
|
|
|
|
maxFill = Fill;
|
2009-05-17 10:07:17 +02:00
|
|
|
int percent = Fill * 100 / (Size() - 1) / PERCENTAGEDELTA * PERCENTAGEDELTA; // clamp down to nearest quantum
|
2004-10-16 09:36:28 +02:00
|
|
|
if (percent != lastPercent) {
|
|
|
|
if (percent >= PERCENTAGETHRESHOLD && percent > lastPercent || percent < PERCENTAGETHRESHOLD && lastPercent >= PERCENTAGETHRESHOLD) {
|
2005-12-11 12:10:28 +01:00
|
|
|
dsyslog("buffer usage: %d%% (tid=%d)", percent, getThreadTid);
|
2004-10-16 09:36:28 +02:00
|
|
|
lastPercent = percent;
|
|
|
|
}
|
2003-05-12 17:46:34 +02:00
|
|
|
}
|
2012-09-22 11:52:33 +02:00
|
|
|
if (ioThrottle) {
|
|
|
|
if (percent >= IOTHROTTLEHIGH)
|
|
|
|
ioThrottle->Activate();
|
|
|
|
else if (percent < IOTHROTTLELOW)
|
|
|
|
ioThrottle->Release();
|
|
|
|
}
|
2003-05-12 17:46:34 +02:00
|
|
|
}
|
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
void cRingBuffer::WaitForPut(void)
|
|
|
|
{
|
|
|
|
if (putTimeout)
|
|
|
|
readyForPut.Wait(putTimeout);
|
|
|
|
}
|
|
|
|
|
2003-05-12 17:46:34 +02:00
|
|
|
void cRingBuffer::WaitForGet(void)
|
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
if (getTimeout)
|
|
|
|
readyForGet.Wait(getTimeout);
|
2003-05-12 17:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::EnablePut(void)
|
|
|
|
{
|
2012-09-17 08:48:20 +02:00
|
|
|
if (putTimeout && Free() > Size() / 10)
|
2004-10-16 09:36:28 +02:00
|
|
|
readyForPut.Signal();
|
2003-05-12 17:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::EnableGet(void)
|
|
|
|
{
|
2012-09-17 08:48:20 +02:00
|
|
|
if (getTimeout && Available() > Size() / 10)
|
2004-10-16 09:36:28 +02:00
|
|
|
readyForGet.Signal();
|
2003-05-12 17:46:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::SetTimeouts(int PutTimeout, int GetTimeout)
|
|
|
|
{
|
|
|
|
putTimeout = PutTimeout;
|
|
|
|
getTimeout = GetTimeout;
|
|
|
|
}
|
|
|
|
|
2012-09-22 11:52:33 +02:00
|
|
|
void cRingBuffer::SetIoThrottle(void)
|
|
|
|
{
|
|
|
|
if (!ioThrottle)
|
|
|
|
ioThrottle = new cIoThrottle;
|
|
|
|
}
|
|
|
|
|
2004-03-07 14:41:45 +01:00
|
|
|
void cRingBuffer::ReportOverflow(int Bytes)
|
|
|
|
{
|
|
|
|
overflowCount++;
|
|
|
|
overflowBytes += Bytes;
|
|
|
|
if (time(NULL) - lastOverflowReport > OVERFLOWREPORTDELTA) {
|
|
|
|
esyslog("ERROR: %d ring buffer overflow%s (%d bytes dropped)", overflowCount, overflowCount > 1 ? "s" : "", overflowBytes);
|
|
|
|
overflowCount = overflowBytes = 0;
|
|
|
|
lastOverflowReport = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-16 12:57:31 +02:00
|
|
|
// --- cRingBufferLinear -----------------------------------------------------
|
2001-08-05 12:23:24 +02:00
|
|
|
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
#define MAXRBLS 30
|
|
|
|
#define DEBUGRBLWIDTH 45
|
|
|
|
|
|
|
|
cRingBufferLinear *cRingBufferLinear::RBLS[MAXRBLS] = { NULL };
|
|
|
|
|
|
|
|
void cRingBufferLinear::AddDebugRBL(cRingBufferLinear *RBL)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAXRBLS; i++) {
|
|
|
|
if (!RBLS[i]) {
|
|
|
|
RBLS[i] = RBL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBufferLinear::DelDebugRBL(cRingBufferLinear *RBL)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < MAXRBLS; i++) {
|
|
|
|
if (RBLS[i] == RBL) {
|
|
|
|
RBLS[i] = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBufferLinear::PrintDebugRBL(void)
|
|
|
|
{
|
|
|
|
bool printed = false;
|
|
|
|
for (int i = 0; i < MAXRBLS; i++) {
|
|
|
|
cRingBufferLinear *p = RBLS[i];
|
|
|
|
if (p) {
|
|
|
|
printed = true;
|
|
|
|
int lh = p->lastHead;
|
|
|
|
int lt = p->lastTail;
|
|
|
|
int h = lh * DEBUGRBLWIDTH / p->Size();
|
|
|
|
int t = lt * DEBUGRBLWIDTH / p->Size();
|
|
|
|
char buf[DEBUGRBLWIDTH + 10];
|
|
|
|
memset(buf, '-', DEBUGRBLWIDTH);
|
|
|
|
if (lt <= lh)
|
|
|
|
memset(buf + t, '*', max(h - t, 1));
|
|
|
|
else {
|
|
|
|
memset(buf, '*', h);
|
|
|
|
memset(buf + t, '*', DEBUGRBLWIDTH - t);
|
|
|
|
}
|
|
|
|
buf[t] = '<';
|
|
|
|
buf[h] = '>';
|
|
|
|
buf[DEBUGRBLWIDTH] = 0;
|
|
|
|
printf("%2d %s %8d %8d %s\n", i, buf, p->lastPut, p->lastGet, p->description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (printed)
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cRingBufferLinear::cRingBufferLinear(int Size, int Margin, bool Statistics, const char *Description)
|
2001-08-05 12:23:24 +02:00
|
|
|
:cRingBuffer(Size, Statistics)
|
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
description = Description ? strdup(Description) : NULL;
|
|
|
|
tail = head = margin = Margin;
|
2006-06-16 09:33:30 +02:00
|
|
|
gotten = 0;
|
2001-08-05 12:23:24 +02:00
|
|
|
buffer = NULL;
|
|
|
|
if (Size > 1) { // 'Size - 1' must not be 0!
|
2004-10-16 09:36:28 +02:00
|
|
|
if (Margin <= Size / 2) {
|
|
|
|
buffer = MALLOC(uchar, Size);
|
|
|
|
if (!buffer)
|
|
|
|
esyslog("ERROR: can't allocate ring buffer (size=%d)", Size);
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
2005-12-30 15:43:21 +01:00
|
|
|
esyslog("ERROR: invalid margin for ring buffer (%d > %d)", Margin, Size / 2);
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
else
|
2005-12-30 15:43:21 +01:00
|
|
|
esyslog("ERROR: invalid size for ring buffer (%d)", Size);
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastHead = head;
|
|
|
|
lastTail = tail;
|
|
|
|
lastPut = lastGet = -1;
|
|
|
|
AddDebugRBL(this);
|
|
|
|
#endif
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
cRingBufferLinear::~cRingBufferLinear()
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
DelDebugRBL(this);
|
|
|
|
#endif
|
2003-01-26 09:59:35 +01:00
|
|
|
free(buffer);
|
2004-10-16 09:36:28 +02:00
|
|
|
free(description);
|
2001-06-02 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
2007-11-17 13:59:08 +01:00
|
|
|
int cRingBufferLinear::DataReady(const uchar *Data, int Count)
|
|
|
|
{
|
|
|
|
return Count >= margin ? Count : 0;
|
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
int cRingBufferLinear::Available(void)
|
2001-06-02 10:47:40 +02:00
|
|
|
{
|
|
|
|
int diff = head - tail;
|
2003-01-26 09:59:35 +01:00
|
|
|
return (diff >= 0) ? diff : Size() + diff - margin;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
void cRingBufferLinear::Clear(void)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2009-11-22 11:30:27 +01:00
|
|
|
tail = head = margin;
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastHead = head;
|
|
|
|
lastTail = tail;
|
|
|
|
lastPut = lastGet = -1;
|
|
|
|
#endif
|
|
|
|
maxFill = 0;
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
2004-10-16 09:36:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int cRingBufferLinear::Read(int FileHandle, int Max)
|
|
|
|
{
|
|
|
|
int Tail = tail;
|
|
|
|
int diff = Tail - head;
|
|
|
|
int free = (diff > 0) ? diff - 1 : Size() - head;
|
|
|
|
if (Tail <= margin)
|
|
|
|
free--;
|
2009-11-22 11:30:27 +01:00
|
|
|
int Count = -1;
|
|
|
|
errno = EAGAIN;
|
2004-10-16 09:36:28 +02:00
|
|
|
if (free > 0) {
|
|
|
|
if (0 < Max && Max < free)
|
|
|
|
free = Max;
|
|
|
|
Count = safe_read(FileHandle, buffer + head, free);
|
|
|
|
if (Count > 0) {
|
|
|
|
int Head = head + Count;
|
|
|
|
if (Head >= Size())
|
|
|
|
Head = margin;
|
|
|
|
head = Head;
|
|
|
|
if (statistics) {
|
|
|
|
int fill = head - Tail;
|
|
|
|
if (fill < 0)
|
|
|
|
fill = Size() + fill;
|
|
|
|
else if (fill >= Size())
|
|
|
|
fill = Size() - 1;
|
|
|
|
UpdatePercentage(fill);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastHead = head;
|
|
|
|
lastPut = Count;
|
|
|
|
#endif
|
2003-05-12 17:46:34 +02:00
|
|
|
EnableGet();
|
2004-10-16 09:36:28 +02:00
|
|
|
if (free == 0)
|
|
|
|
WaitForPut();
|
|
|
|
return Count;
|
2009-11-22 11:30:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int cRingBufferLinear::Read(cUnbufferedFile *File, int Max)
|
|
|
|
{
|
|
|
|
int Tail = tail;
|
|
|
|
int diff = Tail - head;
|
|
|
|
int free = (diff > 0) ? diff - 1 : Size() - head;
|
|
|
|
if (Tail <= margin)
|
|
|
|
free--;
|
|
|
|
int Count = -1;
|
|
|
|
errno = EAGAIN;
|
|
|
|
if (free > 0) {
|
|
|
|
if (0 < Max && Max < free)
|
|
|
|
free = Max;
|
|
|
|
Count = File->Read(buffer + head, free);
|
|
|
|
if (Count > 0) {
|
|
|
|
int Head = head + Count;
|
|
|
|
if (Head >= Size())
|
|
|
|
Head = margin;
|
|
|
|
head = Head;
|
|
|
|
if (statistics) {
|
|
|
|
int fill = head - Tail;
|
|
|
|
if (fill < 0)
|
|
|
|
fill = Size() + fill;
|
|
|
|
else if (fill >= Size())
|
|
|
|
fill = Size() - 1;
|
|
|
|
UpdatePercentage(fill);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastHead = head;
|
|
|
|
lastPut = Count;
|
|
|
|
#endif
|
|
|
|
EnableGet();
|
|
|
|
if (free == 0)
|
|
|
|
WaitForPut();
|
|
|
|
return Count;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
int cRingBufferLinear::Put(const uchar *Data, int Count)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
|
|
|
if (Count > 0) {
|
2004-10-16 09:36:28 +02:00
|
|
|
int Tail = tail;
|
2001-08-05 12:23:24 +02:00
|
|
|
int rest = Size() - head;
|
2004-10-16 09:36:28 +02:00
|
|
|
int diff = Tail - head;
|
|
|
|
int free = ((Tail < margin) ? rest : (diff > 0) ? diff : Size() + diff - margin) - 1;
|
2001-06-02 10:47:40 +02:00
|
|
|
if (statistics) {
|
2001-08-05 12:23:24 +02:00
|
|
|
int fill = Size() - free - 1 + Count;
|
|
|
|
if (fill >= Size())
|
|
|
|
fill = Size() - 1;
|
2004-10-16 09:36:28 +02:00
|
|
|
UpdatePercentage(fill);
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2002-04-19 12:40:04 +02:00
|
|
|
if (free > 0) {
|
|
|
|
if (free < Count)
|
|
|
|
Count = free;
|
|
|
|
if (Count >= rest) {
|
|
|
|
memcpy(buffer + head, Data, rest);
|
|
|
|
if (Count - rest)
|
2003-01-26 09:59:35 +01:00
|
|
|
memcpy(buffer + margin, Data + rest, Count - rest);
|
|
|
|
head = margin + Count - rest;
|
2002-04-19 12:40:04 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
memcpy(buffer + head, Data, Count);
|
|
|
|
head += Count;
|
|
|
|
}
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2002-04-19 12:40:04 +02:00
|
|
|
else
|
|
|
|
Count = 0;
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastHead = head;
|
|
|
|
lastPut = Count;
|
|
|
|
#endif
|
2003-05-12 17:46:34 +02:00
|
|
|
EnableGet();
|
|
|
|
if (Count == 0)
|
|
|
|
WaitForPut();
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2003-04-27 09:55:53 +02:00
|
|
|
uchar *cRingBufferLinear::Get(int &Count)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
int Head = head;
|
2003-10-18 11:14:33 +02:00
|
|
|
if (getThreadTid <= 0)
|
2005-12-11 12:10:28 +01:00
|
|
|
getThreadTid = cThread::ThreadId();
|
2003-01-26 09:59:35 +01:00
|
|
|
int rest = Size() - tail;
|
2004-10-16 09:36:28 +02:00
|
|
|
if (rest < margin && Head < tail) {
|
2003-01-26 09:59:35 +01:00
|
|
|
int t = margin - rest;
|
|
|
|
memcpy(buffer + t, buffer + tail, rest);
|
|
|
|
tail = t;
|
2004-10-16 09:36:28 +02:00
|
|
|
rest = Head - tail;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2004-10-16 09:36:28 +02:00
|
|
|
int diff = Head - tail;
|
2003-01-26 09:59:35 +01:00
|
|
|
int cont = (diff >= 0) ? diff : Size() + diff - margin;
|
|
|
|
if (cont > rest)
|
|
|
|
cont = rest;
|
2007-11-17 13:59:08 +01:00
|
|
|
uchar *p = buffer + tail;
|
|
|
|
if ((cont = DataReady(p, cont)) > 0) {
|
2004-10-16 09:36:28 +02:00
|
|
|
Count = gotten = cont;
|
2007-11-17 13:59:08 +01:00
|
|
|
return p;
|
2003-01-26 09:59:35 +01:00
|
|
|
}
|
2007-11-17 13:59:08 +01:00
|
|
|
WaitForGet();
|
|
|
|
return NULL;
|
2003-01-26 09:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBufferLinear::Del(int Count)
|
|
|
|
{
|
2004-10-16 09:36:28 +02:00
|
|
|
if (Count > gotten) {
|
|
|
|
esyslog("ERROR: invalid Count in cRingBufferLinear::Del: %d (limited to %d)", Count, gotten);
|
|
|
|
Count = gotten;
|
|
|
|
}
|
|
|
|
if (Count > 0) {
|
|
|
|
int Tail = tail;
|
|
|
|
Tail += Count;
|
|
|
|
gotten -= Count;
|
|
|
|
if (Tail >= Size())
|
|
|
|
Tail = margin;
|
|
|
|
tail = Tail;
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
2003-01-26 09:59:35 +01:00
|
|
|
}
|
2004-10-16 09:36:28 +02:00
|
|
|
#ifdef DEBUGRINGBUFFERS
|
|
|
|
lastTail = tail;
|
|
|
|
lastGet = Count;
|
|
|
|
#endif
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
// --- cFrame ----------------------------------------------------------------
|
|
|
|
|
2016-12-22 11:33:12 +01:00
|
|
|
cFrame::cFrame(const uchar *Data, int Count, eFrameType Type, int Index, uint32_t Pts, bool Independent)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2003-01-19 15:43:58 +01:00
|
|
|
count = abs(Count);
|
2001-11-03 11:05:15 +01:00
|
|
|
type = Type;
|
2001-08-05 12:23:24 +02:00
|
|
|
index = Index;
|
2009-03-13 14:45:12 +01:00
|
|
|
pts = Pts;
|
2016-12-22 11:33:12 +01:00
|
|
|
independent = Type == ftAudio ? true : Independent;
|
2003-01-19 15:43:58 +01:00
|
|
|
if (Count < 0)
|
|
|
|
data = (uchar *)Data;
|
|
|
|
else {
|
2003-01-26 19:50:19 +01:00
|
|
|
data = MALLOC(uchar, count);
|
2003-01-19 15:43:58 +01:00
|
|
|
if (data)
|
|
|
|
memcpy(data, Data, count);
|
|
|
|
else
|
|
|
|
esyslog("ERROR: can't allocate frame buffer (count=%d)", count);
|
|
|
|
}
|
2001-08-05 12:23:24 +02:00
|
|
|
next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cFrame::~cFrame()
|
|
|
|
{
|
2003-01-26 19:50:19 +01:00
|
|
|
free(data);
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// --- cRingBufferFrame ------------------------------------------------------
|
|
|
|
|
2002-05-18 08:57:42 +02:00
|
|
|
cRingBufferFrame::cRingBufferFrame(int Size, bool Statistics)
|
2001-08-05 12:23:24 +02:00
|
|
|
:cRingBuffer(Size, Statistics)
|
|
|
|
{
|
|
|
|
head = NULL;
|
|
|
|
currentFill = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cRingBufferFrame::~cRingBufferFrame()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBufferFrame::Clear(void)
|
|
|
|
{
|
|
|
|
Lock();
|
2003-04-27 09:55:53 +02:00
|
|
|
cFrame *p;
|
2002-06-16 12:57:31 +02:00
|
|
|
while ((p = Get()) != NULL)
|
2001-08-05 12:23:24 +02:00
|
|
|
Drop(p);
|
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
|
|
|
EnableGet();
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cRingBufferFrame::Put(cFrame *Frame)
|
|
|
|
{
|
|
|
|
if (Frame->Count() <= Free()) {
|
|
|
|
Lock();
|
|
|
|
if (head) {
|
|
|
|
Frame->next = head->next;
|
|
|
|
head->next = Frame;
|
|
|
|
head = Frame;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2001-08-05 12:23:24 +02:00
|
|
|
else {
|
|
|
|
head = Frame->next = Frame;
|
|
|
|
}
|
|
|
|
currentFill += Frame->Count();
|
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
EnableGet();
|
2001-08-05 12:23:24 +02:00
|
|
|
return true;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2001-08-05 12:23:24 +02:00
|
|
|
return false;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2003-04-27 09:55:53 +02:00
|
|
|
cFrame *cRingBufferFrame::Get(void)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
Lock();
|
|
|
|
cFrame *p = head ? head->next : NULL;
|
|
|
|
Unlock();
|
|
|
|
return p;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2003-04-27 09:55:53 +02:00
|
|
|
void cRingBufferFrame::Delete(cFrame *Frame)
|
2001-03-31 08:42:27 +02:00
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
currentFill -= Frame->Count();
|
|
|
|
delete Frame;
|
|
|
|
}
|
|
|
|
|
2003-04-27 09:55:53 +02:00
|
|
|
void cRingBufferFrame::Drop(cFrame *Frame)
|
2001-08-05 12:23:24 +02:00
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
if (head) {
|
|
|
|
if (Frame == head->next) {
|
|
|
|
if (head->next != head) {
|
|
|
|
head->next = Frame->next;
|
|
|
|
Delete(Frame);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Delete(head);
|
|
|
|
head = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: attempt to drop wrong frame from ring buffer!");
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
int cRingBufferFrame::Available(void)
|
|
|
|
{
|
|
|
|
Lock();
|
|
|
|
int av = currentFill;
|
|
|
|
Unlock();
|
|
|
|
return av;
|
|
|
|
}
|