circularbuffer.cpp 6.22 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//circularbuffer.cpp 
#include "circularbuffer.h"
#include <iostream>
#include <time.h>
using namespace std;

/**
 * The CircularBuffer provides numChunks of buffering.
 * When created, it has (numChunks / 2) chunks of 0s.
 * This provides tolerance for an offset of numChunks/2
 * between the number of reads and writes.
 *
 * FIXME - It might be an idea to let the open threshold vary
 * (asymmetric tolerance.)  I don't really know...
 *
 * If read is called on an empty CircularBuffer, it is reset
 * to its original state.
 *
 * If write is called on a full CircularBuffer, the readPosition is
 * moved forward (numChunks / 2) and the write proceeds (the
 * oldest numChunks/2 chunks are skipped).
 */

CircularBuffer::CircularBuffer (int chunkSize, int numChunks,
				bool blockOnEmpty, int maxSeq):
writePosition (0),
readPosition (0),
blockOnEmpty (blockOnEmpty),
numBufChunks (numChunks),
numChunksFree (numChunks - 1),
chunkSize (chunkSize),
maxSeq (maxSeq)
{
	cbBuffer = (void *) new char[chunkSize * numChunks];
	seqBuffer = new int[numChunks];
	for (int i = 0; i < numChunks; i++) seqBuffer[i] = -1;
	lastSeq = -1;

	if (cbBuffer == NULL)
		cerr << "CircularBuffer Out Of Memory!" << endl;

	prepareReaderWriter ();
	dataLock = new QSemaphore (1);
	secondTry = false;
}

CircularBuffer::CircularBuffer (CircularBuffer * copyMe):
writePosition (0), readPosition (0), isOpen (false)
{
	chunkSize = copyMe->chunkSize;
	numBufChunks = copyMe->numBufChunks;
	numChunksFree = numBufChunks - 1;
	blockOnEmpty = copyMe->blockOnEmpty;

	cbBuffer = (void *) new char[chunkSize * numBufChunks];

	if (cbBuffer == NULL)
		cerr << "CircularBuffer Out Of Memory!" << endl;

	prepareReaderWriter ();
	dataLock = new QSemaphore (1);
}

CircularBuffer::~CircularBuffer ()
{
	delete readSemaphore;
	delete dataLock;
}

void
CircularBuffer::debugDump ()
{
	cerr << "========================================" << endl;
	cerr << "chunkSize=" << chunkSize << endl;
	cerr << "numChunks=" << numBufChunks << endl;
	cerr << "cbBuffer=" << cbBuffer << endl;
	cerr << "readPosition=" << readPosition << endl;
	cerr << "writePosition=" << writePosition << endl << endl;
}

int
CircularBuffer::write (const void *writeChunk)
{
	if (isFull () == false)
	{
		memcpy ((void *) ((char *) cbBuffer +
				  writePosition * chunkSize), writeChunk,
			chunkSize);
		writePosition = ++writePosition % numBufChunks;
	}
	else
	{
		this->overflowReset ();
	}
	if (blockOnEmpty)
jcaceres's avatar
jcaceres committed
96
		(*readSemaphore)--;
jcaceres's avatar
jcaceres committed
97
98
99
100
101
102
103
104
	return chunkSize;
}

int
CircularBuffer::writeRedundant (const void *writeChunk, int z, int seq)
{
//      if (isFull () == false)
	{
jcaceres's avatar
jcaceres committed
105
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
		int tmpPosition = writePosition - z;
		if (tmpPosition < 0)
		{
			tmpPosition += numBufChunks; // numChunks;
		}
		int tmpSeq = seqBuffer[tmpPosition];
		int dif = seq - tmpSeq;
		if (dif < -numBufChunks); // -numChunks)
		{
			dif += maxSeq;
		}
		if ((dif > 0) || (tmpSeq == -1))
		{
			seqBuffer[tmpPosition] = seq;
			memcpy ((void *) ((char *) cbBuffer +
					  tmpPosition * chunkSize),
				writeChunk, chunkSize);
		}
//              cout << "seq " << seq  <<"\t";
		if (z == 0)
			writePosition = ++writePosition % numBufChunks;
jcaceres's avatar
jcaceres committed
127
	(*dataLock)--;
jcaceres's avatar
jcaceres committed
128
	}
jcaceres's avatar
jcaceres committed
129
130
	if (blockOnEmpty)
		(*readSemaphore)--;
jcaceres's avatar
jcaceres committed
131
132
133
134
135
136
137
138
139
//      return chunkSize;
	return writePosition;
}

int
CircularBuffer::read (void *readChunk)
{
	if (blockOnEmpty)
	{
jcaceres's avatar
jcaceres committed
140
		(*readSemaphore)++;
jcaceres's avatar
jcaceres committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160


		if (isEmpty () == false)
		{
			memcpy (readChunk,
				(void *) ((char *) cbBuffer +
					  readPosition * chunkSize),
				chunkSize);
			readPosition = ++readPosition % numBufChunks;	// circular increment
		}
		else
		{
			this->underrunReset ();
			memset (readChunk, 0, chunkSize);
		}
	}
	else
	{			// not blockOnEmpty
		if (lastSeq == -1)
			lastSeq = 0;
jcaceres's avatar
jcaceres committed
161
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		int nextSeq = (lastSeq + 1) % maxSeq;
		int i = 0;
		int lag = -maxSeq;
		int bestReadPosition = readPosition;
		int curReadPosition = readPosition;
		while ((lag != 0) && (i < numBufChunks))
		{
			readPosition = ++readPosition % numBufChunks;
			lastSeq = seqBuffer[readPosition];
			int dif = lastSeq - nextSeq;
			if (dif < -numBufChunks) dif += maxSeq;
			if ((dif == 0) || (dif > lag)) 
			{
				lag = dif; // either it's 0 or find latest entry
				bestReadPosition = readPosition;
			}
			i++;
		}
		if (lag < 0) // don't go backwards in time
		{
				bestReadPosition = curReadPosition;
		}
		if (lag != 0) // skipping or stalling, record position
		{
			lastSeq = seqBuffer[bestReadPosition];			
		}
		memcpy (readChunk,
			(void *) ((char *) cbBuffer +
				  bestReadPosition * chunkSize), chunkSize);
jcaceres's avatar
jcaceres committed
191
	(*dataLock)--;
jcaceres's avatar
jcaceres committed
192
193
194
195
196
197
198
199
200
201
	}
	if (blockOnEmpty)
		return chunkSize;
	else
		return readPosition;
}

/** Full if writer is one circular position behind the reader. */
bool CircularBuffer::isFull ()
{
jcaceres's avatar
jcaceres committed
202
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
203
204
205
206
	// cerr << "(" << writePosition << "," << readPosition << ") ";
	// cerr << ((readPosition + numChunks - writePosition) % numChunks) << endl;
	if (writePosition == (readPosition + numBufChunks - 1) % numBufChunks)
	{
jcaceres's avatar
jcaceres committed
207
		(*dataLock)--;
jcaceres's avatar
jcaceres committed
208
209
210
211
		return true;
	}
	else
	{
jcaceres's avatar
jcaceres committed
212
		(*dataLock)--;
jcaceres's avatar
jcaceres committed
213
214
215
216
217
218
219
220
		return false;
	}
}


/** Empty if writer is equal to reader */
bool CircularBuffer::isEmpty ()
{
jcaceres's avatar
jcaceres committed
221
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
222
223
	if (writePosition == readPosition)
	{
jcaceres's avatar
jcaceres committed
224
		(*dataLock)--;
jcaceres's avatar
jcaceres committed
225
226
227
228
		return true;
	}
	else
	{
jcaceres's avatar
jcaceres committed
229
		(*dataLock)--;
jcaceres's avatar
jcaceres committed
230
231
232
233
234
235
236
237
		return false;
	}
}

void
CircularBuffer::prepareReaderWriter ()
{
	readSemaphore = new QSemaphore (1);
jcaceres's avatar
jcaceres committed
238
	(*readSemaphore)++;
jcaceres's avatar
jcaceres committed
239
240
241
242
243
244
245
246
247
248
249
250
251
	fillChunksWithZeros (0, numBufChunks / 2 - 1);
}

void
CircularBuffer::fillChunksWithZeros (int from, int to)
{
	if (from < 0 || to >= numBufChunks - 1)
		cerr << "Invalid parameters to CircularBuffer::fillChunksWithZeros!" << endl;

	for (writePosition = from; writePosition < to; writePosition++)
	{
		memset (((char *) cbBuffer + writePosition * chunkSize), 0,
			chunkSize);
jcaceres's avatar
jcaceres committed
252
253
		if (blockOnEmpty)
			(*readSemaphore)--;
jcaceres's avatar
jcaceres committed
254
255
256
257
258
259
260
261
262
263
264
265
	}
	writePosition = writePosition % numBufChunks;
	readPosition = from;
}

void
CircularBuffer::underrunReset ()
{
	if (blockOnEmpty)
		cerr << "----output- underflow!!" << endl;
	else
		cerr << "-input--- underflow!!" << endl;
jcaceres's avatar
jcaceres committed
266
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
267
268
	delete readSemaphore;
	prepareReaderWriter ();
jcaceres's avatar
jcaceres committed
269
	(*dataLock)--;
jcaceres's avatar
jcaceres committed
270
271
272
273
274
275
276
277
278
279
}

void
CircularBuffer::overflowReset ()
{
	if (blockOnEmpty)
		cerr << "----output- Overflow" << endl;
	else
		cerr << "-input--- Overflow!" << endl;

jcaceres's avatar
jcaceres committed
280
	(*dataLock)++;
jcaceres's avatar
jcaceres committed
281
	readPosition = (writePosition + (numBufChunks / 2)) % numBufChunks;
jcaceres's avatar
jcaceres committed
282
	(*dataLock)--;
jcaceres's avatar
jcaceres committed
283
}