-
Notifications
You must be signed in to change notification settings - Fork 1
/
fifo.h
143 lines (115 loc) · 3.44 KB
/
fifo.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
/*************************************************
* Publicly released by Rhoban System, August 2012
* www.rhoban-system.fr
*
* Freely usable for non-commercial purposes
*
* Licence Creative Commons *CC BY-NC-SA
* http://creativecommons.org/licenses/by-nc-sa/3.0
*************************************************/
/*****************************************************************************/
/* fifo */
/* Author: Olivier Ly */
/* Date : 2008/07 */
/*****************************************************************************/
#ifndef FIFO_H
#define FIFO_H
#include <stdlib.h>
#include <stdio.h>
#include "util.h"
/* General use fifo.
* The fifo size must be less than 255 byte, because of the
* size of the indexes. */
/*****************************************************************************/
/* Data Structure */
/*****************************************************************************/
template <typename FIFO_TYPE>
class Fifo {
FIFO_TYPE * q_buffer;
int q_size;
int q_start;
int q_end;
/* fifo index manipulation */
inline
int incr_index(int i) {
int idx = i;
idx++;
if (idx == q_size)
idx=0;
return idx;
}
public:
Fifo(int size);
~Fifo();
void reset_fifo();
int size();
bool full();
/* get a new byte from the fifo.
* Caution: if the fifo is empty, return 0. */
FIFO_TYPE get_item();
/* get a new byte from the fifo.
* Caution: if the fifo is empty, return 0. */
FIFO_TYPE get_nth_item(int idx);
/* Push a new byte into the fifo. */
void push_item(FIFO_TYPE b);
};
/*****************************************************************************/
template <typename FIFO_TYPE>
Fifo<FIFO_TYPE>::Fifo(int siz) {
q_buffer = (FIFO_TYPE*) malloc((siz+1)*sizeof(FIFO_TYPE));
if (q_buffer == NULL) {
printf("FIFO buffer allocation error!\n");
return;
}
q_size = siz+1;
reset_fifo();
}
template <typename FIFO_TYPE>
Fifo<FIFO_TYPE>::~Fifo() {
free(q_buffer);
}
template <typename FIFO_TYPE>
void Fifo<FIFO_TYPE>::reset_fifo() {
/* set the starting of the fifo to 0 */
q_start=0;
/* set current position to 0 */
q_end=0;
}
template <typename FIFO_TYPE>
int Fifo<FIFO_TYPE>::size() {
if (q_end >= q_start)
return q_end - q_start;
else
return q_end + (q_size - q_start);
}
template <typename FIFO_TYPE>
FIFO_TYPE Fifo<FIFO_TYPE>::get_item() {
FIFO_TYPE c;
if (q_start == q_end)
return c;
c = q_buffer[q_start];
q_start = incr_index(q_start);
return c;
}
template <typename FIFO_TYPE>
FIFO_TYPE Fifo<FIFO_TYPE>::get_nth_item(int idx) {
return q_buffer[(q_start + idx) % q_size];
}
template <typename FIFO_TYPE>
bool Fifo<FIFO_TYPE>::full() {
int idx = incr_index(q_end);
return (idx == q_start);
}
template <typename FIFO_TYPE>
void Fifo<FIFO_TYPE>::push_item(FIFO_TYPE b) {
int idx = incr_index(q_end);
if (idx == q_start) {
printf("FIFO buffer full, push operation aborted [q_start=%d; q_end=%d; q_size=%d].\n", q_start, q_end, q_size);
return;
}
q_buffer[q_end] = b;
q_end = idx;
}
#endif /* FIFO_H */
/*****************************************************************************/
/*****************************************************************************/