-
Notifications
You must be signed in to change notification settings - Fork 0
/
BufQ.h
580 lines (524 loc) · 18.2 KB
/
BufQ.h
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 RWS Inc, All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of version 2 of the GNU General Public License as published by
// the Free Software Foundation
//
// 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.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
// BufQ.H
//
// History:
// 05/24/97 JMI Started.
//
// 05/24/97 JMI Added UnGet().
//
// 08/14/97 MJR Moved everything into this header so that it would
// inline, which is highly likely considering the small
// size of these functions. Also reworked the endian
// stuff so that the basic nature of the bufQ is to store
// data in standard "network order", which is big endian.
//
// 08/15/97 MJR Changed GetFree() and GetUsed() to return non-linear
// values instead of linear values.
//
// MJR Lots more cleaning up and fixed several serious bugs
// that resulted from incorrect calculations of "linear"
// bytes.
//
//////////////////////////////////////////////////////////////////////////////
//
// This class is designed as a simple means of accessing a circular buffer
// in via two types of interfaces: { Get (deQ), Put (enQ) } and/or { LockBuf,
// ReleaseBuf } styles.
// Potentially bad schtuff:
// - Safety on bad behavior for LockBuf/ReleaseBuf pairs is low.
// - Since different types of data can be written w/o this class
// storing what type it was, reads must be done with the same care
// used when accessing files (i.e., either know the format of the data
// explicitly or 'learn' it by data context).
//
//////////////////////////////////////////////////////////////////////////////
#ifndef BUFQ_H
#define BUFQ_H
#include "System.h"
//////////////////////////////////////////////////////////////////////////////
//
// This impliments a fast buffer queue.
//
// The queue is basically a circular buffer with a get position and a put
// position.
//
// The get and put positions both start out at the same position, which
// happens to be 0. Whenever the get and put positions are the same,
// the queue is empty.
//
// Each time we put a byte to the queue, we put it at the current put
// position and then increment the put position.
//
// Each time we get a byte from the queue, we get it from the current get
// position and then increment the get position.
//
// When either position reaches the end of the queue, it wraps back around to
// the beginning.
//
// When the put position is moved forward so many times that it "wraps
// around" and ends up one position before the get position, then the queue
// is considered full.
//
// Note that because of the way we detect whether the queue is full, we
// need the queue's memory to be one byte larger than the desired "volume"
// of the queue. In other words, if you want the queue to hold 100 bytes,
// the memory buffer must be 101 bytes. Let's say both the get and put
// positions start at 0 and we put 100 bytes into the queue. The put
// position will now be at 100 (0 through 99 have been written to). The
// put position is now 1 position before the get position, and the queue
// is full. If we incremented the put position again, it would wrap around
// and become equal to the get position. We can't let that happen, because
// then we would consider the queue to be empty! Hence, we need that extra
// byte to differentiate between empty and full.
//
//////////////////////////////////////////////////////////////////////////////
class CBufQ
{
//----------------------------------------------------------------------------
// Macros
//----------------------------------------------------------------------------
// Calculate the next position in the queue, relative to the specified position 'i'
#define BUFQ_NEXT(i) (((i)+(short)1) >= QueueSize ? (short)0 : ((i)+(short)1))
// Calculate the previous position in the queue, relative to the specified position 'i'
#define BUFQ_PREV(i) (((i)-(short)1) >= 0 ? ((i)-(short)1) : (QueueSize-(short)1))
//----------------------------------------------------------------------------
// Types, enums, etc.
//----------------------------------------------------------------------------
public:
enum
{
// This is the volume of the queue. In other words, how many bytes
// it can actually hold before it is considered "full".
QueueVolume = 4096,
// This is the actual size (in bytes) of the queue's memory. It is
// one larger than the volume so that we can tell the difference
// between it being empty and full. See above for details.
QueueSize
};
//----------------------------------------------------------------------------
// Variables
//----------------------------------------------------------------------------
public:
U8 m_au8Buf[QueueSize]; // Buffer.
short m_sPutPos; // Current put position in buffer.
short m_sGetPos; // Current get position in buffer.
//----------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------
public:
///////////////////////////////////////////////////////////////////////////////
// Default (and only) constructor
///////////////////////////////////////////////////////////////////////////////
CBufQ()
{
Reset();
}
///////////////////////////////////////////////////////////////////////////////
// Destructor
///////////////////////////////////////////////////////////////////////////////
~CBufQ()
{
}
///////////////////////////////////////////////////////////////////////////////
// Reset the queue
///////////////////////////////////////////////////////////////////////////////
void Reset(void) // Returns nothing.
{
m_sPutPos = 0;
m_sGetPos = 0;
}
///////////////////////////////////////////////////////////////////////////////
// Simple queries
///////////////////////////////////////////////////////////////////////////////
// Determine whether queue is full
bool IsFull(void)
{
return BUFQ_NEXT(m_sPutPos) == m_sGetPos;
}
// Determine whether queue is empty
bool IsEmpty(void)
{
return m_sGetPos == m_sPutPos;
}
// Determine whether we can Put() another byte
bool CanPutByte(void)
{
return BUFQ_NEXT(m_sPutPos) != m_sGetPos;
}
// Determine whether we can Get() another byte
bool CanGetByte(void)
{
return m_sGetPos != m_sPutPos;
}
// Check how many bytes can be gotten via Get()
long CheckGetable(void)
{
// Calculate total amount of used space in queue
return (m_sGetPos <= m_sPutPos) ? m_sPutPos - m_sGetPos : QueueSize - m_sGetPos + m_sPutPos;
}
// Check how many bytes can be put via Put()
long CheckPutable(void)
{
// Calculate total amount of free space in queue
return (m_sPutPos < m_sGetPos) ? m_sGetPos - (m_sPutPos + 1) : QueueSize - (m_sPutPos + 1) + m_sGetPos;
}
// Check how many bytes can be gotten via LockGetPtr()
long CheckLockGetPtr(void)
{
// We need to figure out how many bytes can be gotten from the queue
// without wrapping around or hitting the put position.
return (m_sGetPos <= m_sPutPos) ? m_sPutPos - m_sGetPos : QueueSize - m_sGetPos;
}
// Check how many bytes can be put via LockPutPtr()
long CheckLockPutPtr(void)
{
// We need to figure out how many bytes can be put to the queue without
// wrapping around or hitting the get position. A special case exists
// when the get position is at 0, because then we can't fill out to the
// end of the queue, but must instead stop one byte before the end.
long lPutable;
if (m_sPutPos < m_sGetPos)
lPutable = m_sGetPos - (m_sPutPos + 1);
else
{
if (m_sGetPos)
lPutable = QueueSize - m_sPutPos;
else
lPutable = QueueSize - (m_sPutPos + 1);
}
return lPutable;
}
///////////////////////////////////////////////////////////////////////////////
// Various flavors of Put()
///////////////////////////////////////////////////////////////////////////////
long Put( // Returns 1 if it fit, 0 if not enough room in queue
U8 u8Val) // In: Data to enqueue in buffer.
{
long lResult = 0;
if (CanPutByte())
{
*(m_au8Buf + m_sPutPos) = u8Val;
m_sPutPos = BUFQ_NEXT(m_sPutPos);
lResult++;
}
return lResult;
}
long Put( // Returns number of items that were put into queue
U8* pu8Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of bytes to put.
{
long lNumPut = -1;
while (++lNumPut < lNum)
{
if (!Put(*pu8Buf++))
break;
}
return lNumPut;
}
long Put( // Returns 1 if it fit, 0 if not enough room in queue
S8 s8Val) // In: Data to enqueue in buffer.
{
return Put((U8)s8Val);
}
long Put( // Returns number of items that were put into queue
S8* ps8Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of bytes to put.
{
return Put((U8*)ps8Buf, lNum);
}
long Put( // Returns number of items that were put into queue
void* pvBuf, // In: Data to enqueue in buffer.
long lNum) // In: Number of bytes to put.
{
return Put((U8*)pvBuf, lNum);
}
long Put( // Returns number of items that were put into queue
U16* pu16Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of U16s to put.
{
long lNumPut = -1;
U8* pu8Buf = (U8*)pu16Buf;
#ifdef SYS_ENDIAN_BIG
while (++lNumPut < lNum)
{
Put(*pu8Buf++);
if (!Put(*pu8Buf++))
break;
}
#else
while (++lNumPut < lNum)
{
Put(*(pu8Buf + 1));
if (!Put(*(pu8Buf + 0)))
break;
pu8Buf += 2;
}
#endif
return lNumPut;
}
long Put( // Returns number of items that were put into queue
S16* ps16Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of S16s to put.
{
return Put((U16*)ps16Buf, lNum);
}
long Put( // Returns 1 if it fit, 0 if not enough room in queue
U16 u16Val) // In: Data to enqueue in buffer.
{
return Put(&u16Val);
}
long Put( // Returns 1 if it fit, 0 if not enough room in queue
S16 s16Val) // In: Data to enqueue in buffer.
{
return Put(&s16Val);
}
long Put( // Returns number of items that were put into queue
U32* pu32Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of U32s to put.
{
long lNumPut = -1;
U8* pu8Buf = (U8*)pu32Buf;
#ifdef SYS_ENDIAN_BIG
while (++lNumPut < lNum)
{
Put(*pu8Buf++);
Put(*pu8Buf++);
Put(*pu8Buf++);
if (!Put(*pu8Buf++))
break;
}
#else
while (++lNumPut < lNum)
{
Put(*(pu8Buf + 3));
Put(*(pu8Buf + 2));
Put(*(pu8Buf + 1));
if (!Put(*(pu8Buf + 0)))
break;
pu8Buf += 4;
}
#endif
return lNumPut;
}
long Put( // Returns number of items that were put into queue
S32* ps32Buf, // In: Data to enqueue in buffer.
long lNum = 1) // In: Number of S32s to put.
{
return Put((U32*)ps32Buf, lNum);
}
long Put( // Returns 1 if it fit, 0 if not enough room in queue
U32 u32Val) // In: Data to enqueue in buffer.
{
return Put(&u32Val);
}
long Put( // Returns 1 if it fit, 0 if not enough room in queue
S32 s32Val) // In: Data to enqueue in buffer.
{
return Put(&s32Val);
}
///////////////////////////////////////////////////////////////////////////////
// Various flavors of Get()
///////////////////////////////////////////////////////////////////////////////
long Get( // Returns 1 if item was dequeued, 0 otherwise
U8* pu8Val) // Out: Where to dequeue from buffer.
{
long lNumGot = 0;
if (CanGetByte())
{
*pu8Val = *(m_au8Buf + m_sGetPos);
m_sGetPos = BUFQ_NEXT(m_sGetPos);
lNumGot++;
}
return lNumGot;
}
long Get( // Returns number of items dequeued.
U8* pu8Buf, // Out: Where to dequeue from buffer.
long lNum) // In: Number of bytes to get.
{
long lNumGot = -1;
while (++lNumGot < lNum)
{
if (!Get(pu8Buf++))
break;
}
return lNumGot;
}
long Get( // Returns number of items dequeued.
S8* ps8Buf, // Out: Where to dequeue from buffer.
long lNum = 1) // In: Number of bytes to get.
{
return Get((U8*)ps8Buf, lNum);
}
long Get( // Returns number of items dequeued
void* pvBuf, // Out: Where to dequeue from buffer.
long lNum) // In: Number of bytes to get.
{
return Get((U8*)pvBuf, lNum);
}
long Get( // Returns number of items dequeued.
U16* pu16Buf, // Out: Where to dequeue from buffer.
long lNum = 1) // In: Number of U16s to get.
{
long lNumGot = -1;
U8* pu8Buf = (U8*)pu16Buf;
#ifdef SYS_ENDIAN_BIG
while (++lNumGot < lNum)
{
Get(pu8Buf++);
if (!Get(pu8Buf++))
break;
}
#else
while (++lNumGot < lNum)
{
Get(pu8Buf + 1);
if (!Get(pu8Buf + 0))
break;
pu8Buf += 2;
}
#endif
return lNumGot;
}
long Get( // Returns number of items dequeued.
S16* ps16Buf, // Out: Where to dequeue from buffer.
long lNum = 1) // In: Number of S16s to get.
{
return Get((U16*)ps16Buf, lNum);
}
long Get( // Returns number of items dequeued.
U32* pu32Buf, // Out: Where to dequeue from buffer.
long lNum = 1) // In: Number of U32s to get.
{
long lNumGot = -1;
U8* pu8Buf = (U8*)pu32Buf;
#ifdef SYS_ENDIAN_BIG
while (++lNumGot < lNum)
{
Get(pu8Buf++);
Get(pu8Buf++);
Get(pu8Buf++);
if (!Get(pu8Buf++))
break;
}
#else
while (++lNumGot < lNum)
{
Get(pu8Buf + 3);
Get(pu8Buf + 2);
Get(pu8Buf + 1);
if (!Get(pu8Buf + 0))
break;
pu8Buf += 4;
}
#endif
return lNumGot;
}
long Get( // Returns number of items dequeued.
S32* ps32Buf, // Out: Where to dequeue from buffer.
long lNum = 1) // In: Number of S32s to get.
{
return Get((U32*)ps32Buf, lNum);
}
///////////////////////////////////////////////////////////////////////////////
// Un-put a byte. Can be called repeatedly until all data has been "removed".
// Use caution when mixing with other functions, especially UnGet().
///////////////////////////////////////////////////////////////////////////////
short UnPut(void) // Returns 1 if able to unput, 0 if nothing to unput
{
short sResult = 0;
// Being able to get a byte also happens to indicate that we can unput a byte!
// In other words, if we can move the get pointer forward 1 byte, it means we
// can instead move the put pointer back 1 byte. Get it?
if (CanGetByte())
{
// Move the put position back by 1 byte
m_sPutPos = BUFQ_PREV(m_sPutPos);
sResult++;
}
return sResult;
}
///////////////////////////////////////////////////////////////////////////////
// Un-Get a byte. Can be called repeatedly until all data has been "restored".
// Use caution when mixing with other functions, especially UnPut().
///////////////////////////////////////////////////////////////////////////////
short UnGet(void) // Returns 1 if able to unget, 0 if nothing to unget
{
short sResult = 0;
// Being able to put a byte also happens to indicate that we can unput a byte!
// In other words, if we can move the put pointer forward 1 byte, it means we
// can instead move the get pointer back 1 byte. Get it?
if (CanPutByte())
{
// Move the get position back by 1 byte
m_sGetPos = BUFQ_PREV(m_sGetPos);
sResult++;
}
return sResult;
}
///////////////////////////////////////////////////////////////////////////////
// Lock down the put ptr so you can write directly into the buffer.
// Don't forget to release this ptr with ReleasePutPtr()!!!!
///////////////////////////////////////////////////////////////////////////////
void LockPutPtr(
U8** ppu8Put, // Out: Pointer to which up to *plAmountAvail bytes can be put
long* plAvail) // Out: Number of bytes that can be put to above pointer
{
*plAvail = CheckLockPutPtr();
*ppu8Put = m_au8Buf + m_sPutPos;
}
///////////////////////////////////////////////////////////////////////////////
// Release a put ptr previously locked with LockPutPtr().
// This function is indiscriminant and will screw you if you lie!
///////////////////////////////////////////////////////////////////////////////
void ReleasePutPtr(
long lBytes) // In: Number of bytes written to locked pointer
{
ASSERT(lBytes <= CheckLockPutPtr());
m_sPutPos += lBytes;
if (m_sPutPos == QueueSize)
m_sPutPos = 0;
}
///////////////////////////////////////////////////////////////////////////////
// Lock down the get ptr so you can read directly from the buffer.
// Don't forget to release this ptr with ReleaseGetPtr()!!!!
///////////////////////////////////////////////////////////////////////////////
void LockGetPtr(
U8** ppu8Get, // Out: Pointer from which up to *plAmountAvail bytes can be gotten
long* plAvail) // Out: Number of bytes that can be gotten from above pointer
{
*plAvail = CheckLockGetPtr();
*ppu8Get = m_au8Buf + m_sGetPos;
}
///////////////////////////////////////////////////////////////////////////////
// Release a get ptr previously locked with LockGetPtr().
// This function is indiscriminant and will screw you if you lie!
///////////////////////////////////////////////////////////////////////////////
void ReleaseGetPtr(
long lBytes) // In: Number of bytes that were gotten from locked pointer
{
ASSERT(lBytes <= CheckLockGetPtr());
m_sGetPos += lBytes;
if (m_sGetPos == QueueSize)
m_sGetPos = 0;
}
};
#endif // BUFQ_H
//////////////////////////////////////////////////////////////////////////////
// EOF
//////////////////////////////////////////////////////////////////////////////