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).
|
|
|
|
*
|
2003-05-12 17:46:34 +02:00
|
|
|
* $Id: ringbuffer.c 1.17 2003/05/12 17:38:11 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
|
|
|
|
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;
|
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;
|
2001-08-05 12:23:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cRingBuffer::~cRingBuffer()
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-05-12 17:46:34 +02:00
|
|
|
void cRingBuffer::WaitForPut(void)
|
|
|
|
{
|
|
|
|
if (putTimeout) {
|
|
|
|
putMutex.Lock();
|
|
|
|
readyForPut.TimedWait(putMutex, putTimeout);
|
|
|
|
putMutex.Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::WaitForGet(void)
|
|
|
|
{
|
|
|
|
if (getTimeout) {
|
|
|
|
getMutex.Lock();
|
|
|
|
readyForGet.TimedWait(getMutex, getTimeout);
|
|
|
|
getMutex.Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::EnablePut(void)
|
|
|
|
{
|
|
|
|
if (putTimeout)
|
|
|
|
readyForPut.Broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::EnableGet(void)
|
|
|
|
{
|
|
|
|
if (getTimeout)
|
|
|
|
readyForGet.Broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBuffer::SetTimeouts(int PutTimeout, int GetTimeout)
|
|
|
|
{
|
|
|
|
putTimeout = PutTimeout;
|
|
|
|
getTimeout = GetTimeout;
|
|
|
|
}
|
|
|
|
|
2002-06-16 12:57:31 +02:00
|
|
|
// --- cRingBufferLinear -----------------------------------------------------
|
2001-08-05 12:23:24 +02:00
|
|
|
|
2003-01-26 09:59:35 +01:00
|
|
|
cRingBufferLinear::cRingBufferLinear(int Size, int Margin, bool Statistics)
|
2001-08-05 12:23:24 +02:00
|
|
|
:cRingBuffer(Size, Statistics)
|
|
|
|
{
|
2003-01-26 09:59:35 +01:00
|
|
|
margin = Margin;
|
2001-08-05 12:23:24 +02:00
|
|
|
buffer = NULL;
|
2002-06-16 12:57:31 +02:00
|
|
|
getThreadPid = -1;
|
2001-08-05 12:23:24 +02:00
|
|
|
if (Size > 1) { // 'Size - 1' must not be 0!
|
2003-01-26 09:59:35 +01:00
|
|
|
buffer = MALLOC(uchar, Size);
|
2001-03-31 08:42:27 +02:00
|
|
|
if (!buffer)
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: can't allocate ring buffer (size=%d)", Size);
|
2001-03-31 08:42:27 +02:00
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
else
|
2002-05-13 16:35:49 +02:00
|
|
|
esyslog("ERROR: illegal size for ring buffer (%d)", Size);
|
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
|
|
|
{
|
2003-01-26 09:59:35 +01:00
|
|
|
free(buffer);
|
2001-06-02 10:47:40 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
int cRingBufferLinear::Available(void)
|
2001-06-02 10:47:40 +02:00
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
Lock();
|
2001-06-02 10:47:40 +02:00
|
|
|
int diff = head - tail;
|
2001-08-05 12:23:24 +02:00
|
|
|
Unlock();
|
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
|
|
|
{
|
2001-08-05 12:23:24 +02:00
|
|
|
Lock();
|
2003-01-26 09:59:35 +01:00
|
|
|
head = tail = margin;
|
|
|
|
lastGet = -1;
|
2001-08-05 12:23:24 +02:00
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
|
|
|
EnableGet();
|
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) {
|
2001-08-05 12:23:24 +02:00
|
|
|
Lock();
|
|
|
|
int rest = Size() - head;
|
2001-03-31 08:42:27 +02:00
|
|
|
int diff = tail - head;
|
2003-02-15 13:21:50 +01:00
|
|
|
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;
|
2002-06-16 12:57:31 +02:00
|
|
|
if (fill > maxFill)
|
2001-06-02 10:47:40 +02:00
|
|
|
maxFill = fill;
|
2002-06-16 12:57:31 +02:00
|
|
|
int percent = maxFill * 100 / (Size() - 1) / 5 * 5;
|
|
|
|
if (abs(lastPercent - percent) >= 5) {
|
2001-06-02 10:47:40 +02:00
|
|
|
if (percent > 75)
|
2002-06-16 12:57:31 +02:00
|
|
|
dsyslog("buffer usage: %d%% (pid=%d)", percent, getThreadPid);
|
|
|
|
lastPercent = percent;
|
2001-06-02 10:47:40 +02:00
|
|
|
}
|
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 > maxFill)
|
|
|
|
maxFill = Count;
|
|
|
|
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;
|
|
|
|
Unlock();
|
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
|
|
|
{
|
2003-04-27 09:55:53 +02:00
|
|
|
uchar *p = NULL;
|
2003-01-26 09:59:35 +01:00
|
|
|
Lock();
|
|
|
|
if (getThreadPid < 0)
|
|
|
|
getThreadPid = getpid();
|
|
|
|
int rest = Size() - tail;
|
2003-02-15 13:21:50 +01: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;
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
2003-01-26 09:59:35 +01:00
|
|
|
int diff = head - tail;
|
|
|
|
int cont = (diff >= 0) ? diff : Size() + diff - margin;
|
|
|
|
if (cont > rest)
|
|
|
|
cont = rest;
|
|
|
|
if (cont >= margin) {
|
|
|
|
p = buffer + tail;
|
|
|
|
Count = lastGet = cont;
|
|
|
|
}
|
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
if (!p)
|
|
|
|
WaitForGet();
|
2003-01-26 09:59:35 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cRingBufferLinear::Del(int Count)
|
|
|
|
{
|
|
|
|
if (Count > 0 && Count <= lastGet) {
|
2003-02-15 13:21:50 +01:00
|
|
|
Lock();
|
2003-01-26 09:59:35 +01:00
|
|
|
tail += Count;
|
|
|
|
lastGet -= Count;
|
|
|
|
if (tail >= Size())
|
|
|
|
tail = margin;
|
2003-02-15 13:21:50 +01:00
|
|
|
Unlock();
|
2003-05-12 17:46:34 +02:00
|
|
|
EnablePut();
|
2003-01-26 09:59:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: invalid Count in cRingBufferLinear::Del: %d", Count);
|
2001-03-31 08:42:27 +02:00
|
|
|
}
|
|
|
|
|
2001-08-05 12:23:24 +02:00
|
|
|
// --- cFrame ----------------------------------------------------------------
|
|
|
|
|
2001-11-03 11:05:15 +01:00
|
|
|
cFrame::cFrame(const uchar *Data, int Count, eFrameType Type, int Index)
|
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;
|
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;
|
|
|
|
}
|