summaryrefslogblamecommitdiffstats
path: root/Src/nu/RingBuffer.cpp
blob: d59acb5e489109c4ebdea5fb5926b6fd9bd49614 (plain) (tree)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461












































































































































































































































































































































































































































































                                                                                                                                                               
/*
 *  RingBuffer.cpp
 *  simple_mp3_playback
 *
 *  Created by Ben Allison on 11/10/07.
 *  Copyright 2007 Nullsoft, Inc. All rights reserved.
 *
 */

#include "RingBuffer.h"
#include "../replicant/foundation/error.h"

#include "bfc/platform/types.h"
#include "bfc/platform/minmax.h"

#include <stdlib.h>
#include <string.h>
#include <algorithm>

#ifdef MIN
#undef MIN
#endif // MIN

#define MIN(a,b) ((a<b)?(a):(b))


RingBuffer::~RingBuffer()
{
	if ( ringBuffer )
		free( ringBuffer );

	ringBuffer = 0;
}

void RingBuffer::Reset()
{
	if ( ringBuffer )
		free( ringBuffer );

	ringBuffer = 0;
}

bool RingBuffer::reserve( size_t bytes )
{
	Reset();

	ringBufferSize = bytes;

	ringBuffer = (char *)calloc( ringBufferSize, sizeof( char ) );
	if ( !ringBuffer )
		return false;

	clear();

	return true;
}

int RingBuffer::expand( size_t bytes )
{
	if ( bytes > ringBufferSize )
	{
		char *new_buffer = (char *)realloc( ringBuffer, bytes );
		if ( !new_buffer )
			return NErr_OutOfMemory;

		size_t write_offset = ringReadPosition - ringBuffer;
		size_t read_offset  = ringWritePosition - ringBuffer;

		/* update write pointer for the new buffer */
		ringWritePosition = new_buffer + write_offset;

		if ( write_offset > read_offset || !ringBufferUsed ) /* ringBufferUsed will resolve the ambiguity when ringWritePosition == ringReadPosition */
		{
			/* the ring buffer looks like [  RXXXW    ], so we don't need to move anything.
			Just update the read pointer */

			ringReadPosition = new_buffer + write_offset;
		}
		else
		{
			/* [XXW    RXX] needs to become [XXW            RXX] */
			size_t end_bytes       = ringBufferSize - read_offset; // number of bytes that we need to relocate (the RXX portion)
			char *new_read_pointer = &new_buffer[ bytes - end_bytes ];

			memmove( new_read_pointer, ringReadPosition, end_bytes );

			ringReadPosition = new_read_pointer; /* update read pointer */
		}

		ringBufferSize = bytes;
		ringBuffer     = new_buffer;

		return NErr_Success;
	}
	else
		return NErr_NoAction;
}

bool RingBuffer::empty() const
{
	return ( ringBufferUsed == 0 );
}

size_t RingBuffer::read( void *dest, size_t len )
{
	int8_t *out   = (int8_t *)dest; // lets us do pointer math easier
	size_t toCopy = MIN( ringBufferUsed, len );
	size_t copied = 0;

	len -= toCopy;

	// read to the end of the ring buffer
	size_t end   = ringBufferSize - ( ringReadPosition - ringBuffer );
	size_t read1 = MIN( end, toCopy );

	memcpy( out, ringReadPosition, read1 );

	copied           += read1;
	ringReadPosition += read1;

	if ( ringReadPosition == ringBuffer + ringBufferSize )
		ringReadPosition = ringBuffer;

	// update positions
	ringBufferUsed -= read1;
	toCopy         -= read1;
	out             = (int8_t *)out + read1;

	// see if we still have more to read after wrapping around
	if ( toCopy )
	{
		memcpy( out, ringReadPosition, toCopy );

		copied           += toCopy;
		ringReadPosition += toCopy;
		ringBufferUsed   -= toCopy;

		if ( ringReadPosition == ringBuffer + ringBufferSize )
			ringReadPosition = ringBuffer;
	}

	return copied;
}

size_t RingBuffer::at(size_t offset, void *dest, size_t len) const
{
	size_t toCopy = ringBufferUsed;

	// make a local copy of this so we don't blow the original
	char *ringReadPosition = this->ringReadPosition;

	/* --- do a "dummy read" to deal with the offset request --- */
	size_t dummy_end = ringBufferSize-(ringReadPosition-ringBuffer);

	offset = MIN(toCopy, offset);
	size_t read0 = MIN(dummy_end, offset);
	ringReadPosition+=read0;

	if (ringReadPosition == ringBuffer + ringBufferSize)
		ringReadPosition = ringBuffer;

	// update positions
	toCopy -= read0;
	offset -= read0;

	// do second-half read (wraparound)
	if ( offset )
	{
		ringReadPosition += offset;
		toCopy           -= offset;
	}

  // dummy read done

	/* --- set up destination buffer and copy size --- */
	int8_t *out = (int8_t *)dest; // lets us do pointer math easier

	if ( toCopy > len )
		toCopy = len;

	size_t copied=0;

	/* --- read to the end of the ring buffer --- */
	size_t end   = ringBufferSize - ( ringReadPosition - ringBuffer );
	size_t read1 = MIN( end, toCopy );

	memcpy( out, ringReadPosition, read1 );

	copied           += read1;
	ringReadPosition += read1;

	if (ringReadPosition == ringBuffer + ringBufferSize)
		ringReadPosition = ringBuffer;

	// update positions
	toCopy -= read1;
	out     = (int8_t *)out + read1;

	/* --- see if we still have more to read after wrapping around --- */
	if (toCopy)
	{
		memcpy(out, ringReadPosition, toCopy);

		copied           += toCopy;
		ringReadPosition += toCopy;
	}

	return copied;
}

size_t RingBuffer::peek( void *dest, size_t len ) const
{
	int8_t *out = (int8_t *)dest; // lets us do pointer math easier

	size_t toCopy = MIN( ringBufferUsed, len );
	size_t copied = 0;

	// make a local copy of this so we don't blow the original
	char *ringReadPosition = this->ringReadPosition;

	// read to the end of the ring buffer
	size_t end   = ringBufferSize - ( ringReadPosition - ringBuffer );
	size_t read1 = MIN( end, toCopy );

	memcpy( out, ringReadPosition, read1 );

	copied           += read1;
	ringReadPosition += read1;

	if ( ringReadPosition == ringBuffer + ringBufferSize )
		ringReadPosition = ringBuffer;

	// update positions
	toCopy -= read1;
	out     = (int8_t *)out + read1;

	// see if we still have more to read after wrapping around
	if ( toCopy )
	{
		memcpy( out, ringReadPosition, toCopy );

		copied += toCopy;
		ringReadPosition += toCopy;
	}

	return copied;
}

size_t RingBuffer::advance( size_t len )
{
	size_t toCopy = MIN( ringBufferUsed, len );
	size_t copied = 0;

	len -= toCopy;

	// read to the end of the ring buffer
	size_t end   = ringBufferSize - ( ringReadPosition - ringBuffer );
	size_t read1 = MIN( end, toCopy );
	
	copied           += read1;
	ringReadPosition += read1;

	if ( ringReadPosition == ringBuffer + ringBufferSize )
		ringReadPosition = ringBuffer;

	// update positions
	toCopy         -= read1;
	ringBufferUsed -= read1;

	// see if we still have more to read after wrapping around
	if ( toCopy )
	{
		copied           += toCopy;
		ringReadPosition += toCopy;
		ringBufferUsed   -= toCopy;

		if ( ringReadPosition == ringBuffer + ringBufferSize )
			ringReadPosition = ringBuffer;
	}

	return copied;
}

size_t RingBuffer::avail() const
{
	return ringBufferSize - ringBufferUsed;
}

size_t RingBuffer::write( const void *buffer, size_t bytes )
{
	size_t used  = ringBufferUsed;
	size_t avail = ringBufferSize - used;

	bytes = MIN( avail, bytes );

	// write to the end of the ring buffer
	size_t end    = ringBufferSize - ( ringWritePosition - ringBuffer );
	size_t copied = 0;
	size_t write1 = MIN( end, bytes );

	memcpy( ringWritePosition, buffer, write1 );

	copied            += write1;
	ringWritePosition += write1;

	if ( ringWritePosition == ringBuffer + ringBufferSize )
		ringWritePosition = ringBuffer;

	// update positions
	ringBufferUsed += write1;
	bytes          -= write1;
	buffer          = (const int8_t *)buffer + write1;

	// see if we still have more to write after wrapping around
	if ( bytes )
	{
		memcpy( ringWritePosition, buffer, bytes );

		copied            += bytes;
		ringWritePosition += bytes;
		ringBufferUsed    += bytes;

		if ( ringWritePosition == ringBuffer + ringBufferSize )
			ringWritePosition = ringBuffer;
	}

	return copied;
}

size_t RingBuffer::drain( Drainer *drainer, size_t max_bytes )
{
	// read to the end of the ring buffer
	size_t used  = ringBufferUsed;
	size_t bytes = used;

	bytes = MIN(bytes, max_bytes);

	size_t copied = 0;
	size_t end    = ringBufferSize-(ringReadPosition-ringBuffer);
	size_t drain1 = MIN(end, bytes);

	if (!drain1)
		return 0;

	size_t read1 = drainer->Write(ringReadPosition, drain1);
	if (read1 == 0)
		return 0;

	copied+=read1;
	ringReadPosition+=read1;
	if (ringReadPosition == ringBuffer + ringBufferSize)
		ringReadPosition=ringBuffer;

		// update positions
	ringBufferUsed -= read1;
	bytes-=read1;

	// see if we still have more to read after wrapping around
	if (drain1 == read1 && bytes)
	{
		size_t read2 =  drainer->Write(ringReadPosition, bytes);

		copied           += read2;
		ringReadPosition += read2;
		ringBufferUsed   -= read2;

		if (ringReadPosition == ringBuffer + ringBufferSize)
			ringReadPosition=ringBuffer;
	}

	return copied;
}

size_t RingBuffer::fill(Filler *filler, size_t max_bytes)
{
	// write to the end of the ring buffer
	size_t used  = ringBufferUsed;
	size_t bytes = ringBufferSize - used;

	bytes = MIN(bytes, max_bytes);

	size_t copied = 0;
	size_t end    = ringBufferSize-(ringWritePosition-ringBuffer);
	size_t fill1  = MIN(end, bytes);

	if (!fill1)
		return 0;

	size_t write1 = filler->Read(ringWritePosition, fill1);
	if (write1 == 0)
		return 0;

	copied+=write1;
	ringWritePosition+=write1;

	if (ringWritePosition == ringBuffer + ringBufferSize)
		ringWritePosition=ringBuffer;

	// update positions
	ringBufferUsed += write1;
	bytes-=write1;

	// see if we still have more to write after wrapping around
	if (fill1 == write1 && bytes)
	{
		size_t write2 =  filler->Read(ringWritePosition, bytes);

		copied            += write2;
		ringWritePosition += write2;
		ringBufferUsed    += write2;

		if (ringWritePosition == ringBuffer + ringBufferSize)
			ringWritePosition=ringBuffer;
	}

	return copied;
}

size_t RingBuffer::size() const
{
	return ringBufferUsed;
}

void RingBuffer::clear()
{
	ringBufferUsed    = 0;
	ringWritePosition = ringBuffer;
	ringReadPosition  = ringBuffer;
}

void *RingBuffer::LockBuffer()
{
	return ringBuffer;
}

void RingBuffer::UnlockBuffer( size_t written )
{
	ringWritePosition = ringBuffer+written;
	ringBufferUsed    = written;
}

size_t RingBuffer::write_position() const
{
	return (size_t)ringWritePosition;
}

size_t RingBuffer::read_position() const
{
	return (size_t)ringReadPosition;
}

void RingBuffer::get_read_buffer(size_t bytes, const void **buffer, size_t *bytes_available) const
{
	size_t toCopy = MIN( ringBufferUsed, bytes );

	// read to the end of the ring buffer
	size_t end = ringBufferSize-(ringReadPosition-ringBuffer);

	*bytes_available = MIN(end, toCopy);
	*buffer          = ringReadPosition;
}