-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIOFifo.cpp
466 lines (375 loc) · 10.7 KB
/
IOFifo.cpp
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
/******************************************************************************/
/* */
/* Copyright (c) 2010, 2014 Sylwester Wysocki <[email protected]> */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining a */
/* copy of this software and associated documentation files (the "Software"), */
/* to deal in the Software without restriction, including without limitation */
/* the rights to use, copy, modify, merge, publish, distribute, sublicense, */
/* and/or sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be included in */
/* all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL */
/* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING */
/* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER */
/* DEALINGS IN THE SOFTWARE. */
/* */
/******************************************************************************/
//
// Purpose: Cyclic buffer to store I/O buffer.
//
// Incoming data is appended to the end.
// Outcoming data is popped from the begin.
//
// xx xx xx xx xx xx xx ... yy yy yy yy yy
//
// ^^^ ^^^
// Read position. Write position.
// Pop data from here. Push data here.
//
#include <cstring>
#include <cstdlib>
#include "IOFifo.h"
#include <Tegenaria/Debug.h>
namespace Tegenaria
{
// ----------------------------------------------------------------------------
//
// Constructors and destructors
//
// ----------------------------------------------------------------------------
//
// Create new IOFifo with given capacity.
//
// capacity - size of fifo in bytes (IN).
//
IOFifo::IOFifo(unsigned int capacity)
{
DBG_SET_ADD("IOFifo", this);
buffer_ = (char *) malloc(capacity);
capacity_ = capacity;
bytesLeft_ = capacity;
writePos_ = 0;
readPos_ = 0;
mutex_ = new Mutex("IOFifo");
}
//
// Free buffers allocated in constructor.
//
IOFifo::~IOFifo()
{
DBG_SET_DEL("IOFifo", this);
if (buffer_)
{
free(buffer_);
buffer_ = NULL;
}
if (mutex_)
{
delete mutex_;
mutex_ = NULL;
}
}
// ----------------------------------------------------------------------------
//
// Destructive functions (push/pop)
//
// ----------------------------------------------------------------------------
//
// Add data to the end of FIFO.
//
// Buffer before: xx xx xx xx xx
// Buffer after : xx xx xx xx xx yy yy yy yy ...
//
// source - source buffer with data to append (IN).
// len - number of bytes to append (IN).
//
// RETURNS: 0 if all bytes appended,
// -1 otherwise.
//
int IOFifo::push(void *source, int len)
{
DBG_ENTER3("IOFifo::push");
int exitCode = -1;
char *src = (char *) source;
//
// Check is it sufficient space left.
//
FAILEX(len > bytesLeft_,
"ERROR: Going to append [%d] bytes to IOFifo PTR#%p,"
" but only [%d] bytes left.\n", len, this, bytesLeft_);
//
// Write data to buffer.
//
if ((writePos_ + len) < capacity_)
{
memcpy(buffer_ + writePos_, src, len);
}
else
{
unsigned int bytesToEnd = capacity_ - writePos_;
unsigned int overflow = (len - bytesToEnd) % capacity_;
memcpy(buffer_ + writePos_, src, bytesToEnd);
memcpy(buffer_, src + bytesToEnd, overflow);
}
writePos_ = (writePos_ + len) % capacity_;
bytesLeft_ -= len;
DEBUG2("Appended [%d] bytes to IOFifo PTR#%p,"
" [%d] bytes left.\n", len, this, bytesLeft_);
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("IOFifo::push");
return exitCode;
}
//
// Pop data from the begin of FIFO.
//
// Buffer before: xx xx xx xx yy yy yy yy ...
// Buffer after : yy yy yy yy ...
//
//
// TIP#1: If dest buffer is NULL, data are popped from FIFO,
// but don't written anywhere.
//
// TIP#2: Skip len parameter or set to -1 if you want to pop up
// all data stored in queue.
//
// TIP#3: Use peekOnly flag to get data WITHOUT removing it from FIFO.
//
//
// dest - buffer where to write popped data (OUT/OPT).
//
// len - number of bytes to pop, if set to -1 all available data
// will be popped (IN/OPT).
//
// peekOnly - set to 1 if you want copy data to dest buffer WITHOUT
// remove it from buffer (IN/OPT).
//
// RETURNS: 0 if all bytes popped,
// -1 otherwise..
//
int IOFifo::pop(void *dest, int len, int peekOnly)
{
DBG_ENTER3("IOFifo::pop");
int exitCode = -1;
char *dst = (char *) dest;
//
// Check is it sufficient space left.
//
FAILEX(capacity_ - bytesLeft_ < len,
"ERROR: Going to pop [%d] bytes from IOFifo PTR#%p,"
" but only [%d] bytes available.\n",
len, this, capacity_ - bytesLeft_);
//
// Read data from buffer if needed.
//
if (dst)
{
if ((readPos_ + len) < capacity_)
{
memcpy(dst, buffer_ + readPos_, len);
}
else
{
unsigned int bytesToEnd = capacity_ - readPos_;
unsigned int overflow = (len - bytesToEnd) % capacity_;
memcpy(dst, buffer_ + readPos_, bytesToEnd);
memcpy(dst + bytesToEnd, buffer_, overflow);
}
}
//
// Move read position.
//
if (peekOnly == 0)
{
readPos_ = (readPos_ + len) % capacity_;
bytesLeft_ += len;
DEBUG2("Popped [%d] bytes from IOFifo PTR#%p,"
" [%d] bytes left.\n", len, this, bytesLeft_);
}
//
// Error handler.
//
exitCode = 0;
fail:
DBG_LEAVE3("IOFifo::pop");
return exitCode;
}
//
// Eat data from the begin of FIFO.
//
// Buffer before: xx xx xx xx yy yy yy yy ...
// Buffer after : yy yy yy yy ...
//
//
// Works as pop() method with destination set to NULL.
//
// len - number of bytes to eat (IN).
//
// RETURNS: 0 if all bytes eated,
// -1 otherwise..
//
int IOFifo::eat(int len)
{
return pop(NULL, len);
}
// ----------------------------------------------------------------------------
//
// Non destructive read (peek)
//
// ----------------------------------------------------------------------------
//
// Copy <len> bytes from begin of FIFO to <dest>, but
// do NOT remove them from FIFO.
//
// Works as pop() with peekOnly flag set to 1.
//
//
// dest - buffer where to store readed data (OUT).
//
// len - number of bytes to read. If set to -1 all
// available data will be copied (IN/OPT).
//
//
// RETURNS: 0 if all bytes copied,
// -1 otherwise.
//
//
int IOFifo::peek(void *dest, int len)
{
return pop(dest, len, 1);
}
//
// Peek QWORD from fifo begin, but do NOT remove it from buffer.
//
// WARNING: If there is less than 8 bytes in buffer, return value
// is always zero.
//
// endian - set to IO_BIG_ENDIAN or IO_LITTLE_ENDIAN (IN).
//
// RETURNS: First QWORD in queue.
//
uint64_t IOFifo::peekQword(int endian)
{
uint64_t ret = 0;
peek(&ret, 8);
//
// Inverse bytes if needed.
//
if (endian == IO_BIG_ENDIAN)
{
uint64_t tmp = ret;
uint8_t *src = (uint8_t *) &tmp;
uint8_t *dst = (uint8_t *) &ret;
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
dst[4] = src[3];
dst[5] = src[2];
dst[6] = src[1];
dst[7] = src[0];
}
return ret;
}
//
// Peek DWORD from fifo begin, but do NOT remove it from buffer.
//
// WARNING: If there is less than 4 bytes in buffer, return value
// is always zero.
//
// endian - set to IO_BIG_ENDIAN or IO_LITTLE_ENDIAN (IN).
//
// RETURNS: First DWORD in queue.
//
uint32_t IOFifo::peekDword(int endian)
{
uint32_t ret = 0;
peek(&ret, 4);
//
// Inverse bytes if needed.
//
if (endian == IO_BIG_ENDIAN)
{
uint32_t tmp = ret;
uint8_t *src = (uint8_t *) &tmp;
uint8_t *dst = (uint8_t *) &ret;
dst[0] = src[7];
dst[1] = src[6];
dst[2] = src[5];
dst[3] = src[4];
}
return ret;
}
//
// Peek byte from fifo begin, but do NOT remove it from buffer.
//
// WARNING: If there is less than 1 bytes in buffer, return value
// is always zero.
//
// RETURNS: First byte in queue.
//
uint8_t IOFifo::peekByte()
{
uint8_t ret = 0;
if (capacity_ - bytesLeft_ >= 1)
{
ret = buffer_[writePos_];
}
return ret;
}
// ----------------------------------------------------------------------------
//
// Multithread synchronization
//
// ----------------------------------------------------------------------------
//
// Lock access to fifo.
//
void IOFifo::lock()
{
mutex_ -> lock();
}
//
// Unlock access to fifo blocked by lock() before.
//
void IOFifo::unlock()
{
mutex_ -> unlock();
}
// ----------------------------------------------------------------------------
//
// Getters and setters
//
// ----------------------------------------------------------------------------
//
// Return number of free bytes, which can be append to fifo.
//
unsigned int IOFifo::bytesLeft()
{
return bytesLeft_;
}
//
// Return number of bytes already stored inside fifo.
//
unsigned int IOFifo::size()
{
return capacity_ - bytesLeft_;
}
//
// Return total fifo capacity in bytes.
//
unsigned int IOFifo::capacity()
{
return capacity_;
}
} /* namespace Tegenaria */