-
Notifications
You must be signed in to change notification settings - Fork 36
/
CtrlBuf.h
287 lines (251 loc) · 8.47 KB
/
CtrlBuf.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
////////////////////////////////////////////////////////////////////////////////
//
// 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
//
// CtrlBuf.h
// Project: Nostril (aka Postal)
//
// History:
// 05/26/97 MJR Started.
//
// 06/15/97 MJR Added Reset().
//
////////////////////////////////////////////////////////////////////////////////
#ifndef CTRLBUF_H
#define CTRLBUF_H
#include "RSPiX.h"
////////////////////////////////////////////////////////////////////////////////
//
// CCtrlBuf impliments a specialized buffer designed for random access.
// Also, when getting a series of values from the buffer, they must ALWAYS
// be in a single, contiguous chunk of memory, which is not the case with a
// traditional circular buffer.
//
// This is pretty crude right now, as it may result in a memmove() each time
// a value is discarded from the buffer. One optimization might be to move
// the front of the buffer forward until the back hits the end of the
// available space, and only then do a memmove() of the remaining data back
// to the beginning of the buffer. Alternately, maybe a traditional queue
// should be used as the core, with an intermediate buffer to take care of the
// special case where a chunk of data wraps around the end of the buffer.
//
////////////////////////////////////////////////////////////////////////////////
class CCtrlBuf
{
//------------------------------------------------------------------------------
// Types, enums, etc.
//------------------------------------------------------------------------------
public:
enum
{
MaxBufEntries = 256,
InvalidEntry = 0xffffffff
};
//------------------------------------------------------------------------------
// Variables
//------------------------------------------------------------------------------
private:
long m_lNumCtrls;
long m_lOldestSeq;
long m_alBuf[MaxBufEntries];
//------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------
public:
////////////////////////////////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////////////////////////////////
CCtrlBuf()
{
Reset();
}
////////////////////////////////////////////////////////////////////////////////
// Destructor
////////////////////////////////////////////////////////////////////////////////
~CCtrlBuf()
{
}
////////////////////////////////////////////////////////////////////////////////
// Reset
////////////////////////////////////////////////////////////////////////////////
void Reset(void)
{
m_lNumCtrls = 0;
m_lOldestSeq = 0;
}
////////////////////////////////////////////////////////////////////////////////
// Add new ctrl to buffer
////////////////////////////////////////////////////////////////////////////////
short Add(
long lSeq,
long lNum,
long* plCtrls,
long* plNumAdded)
{
short sResult = 0;
// This can and should be optimized!
*plNumAdded = 0;
for (long l = 0; l < lNum; l++)
{
sResult = Add(lSeq++, *plCtrls++);
if (sResult == 0)
(*plNumAdded)++;
else
break;
}
return sResult;
}
short Add(
long lSeq,
long lCtrl)
{
short sResult = 0;
// This needs to deal with the fact that the ctrls don't always come
// sequentially, and there will often be gaps between ctrl values that
// get filled in later on.
if (m_lNumCtrls == 0)
{
// If array is empty, set oldest seq to specified seq, add ctrl to
// array, and adjust the number of ctrls.
m_lOldestSeq = lSeq;
m_alBuf[0] = lCtrl;
m_lNumCtrls++;
}
else
{
// Calculate index in array based on sequence number
long lIndex = lSeq - m_lOldestSeq;
// Make sure it isn't older than our oldest value. This can happen if
// we receive an older data packet. If it does happen, we quietly
// ignore the value since it is apparently no longer needed.
if (lIndex >= 0)
{
// Make sure it will fit in the buffer
if (lIndex < MaxBufEntries)
{
// Add new ctrl to array
m_alBuf[lIndex] = lCtrl;
// Check if new ctrl went beyond the "current" number of entries
if (lIndex >= m_lNumCtrls)
{
// Invalidate any unused ctrls (there may not be any)
for (long l = m_lNumCtrls; l < lIndex; l++)
m_alBuf[l] = InvalidEntry;
// Set new number of entries (last index + 1)
m_lNumCtrls = lIndex + 1;
}
}
else
{
sResult = -1;
TRACE("No room in buf!\n");
}
}
}
return sResult;
}
////////////////////////////////////////////////////////////////////////////////
// Get specified entry. If the returned value is 'InvalidEntry', then that
// entry was not available.
////////////////////////////////////////////////////////////////////////////////
long GetAt(
long lSeq)
{
if (m_lNumCtrls > 0)
{
long lIndex = lSeq - m_lOldestSeq;
if ((lIndex >= 0) && (lIndex < m_lNumCtrls))
return m_alBuf[lIndex];
}
return InvalidEntry;
}
////////////////////////////////////////////////////////////////////////////////
// Get pointer to specified entry. If the returned value is 0 (NULL), then
// that entry was not available.
////////////////////////////////////////////////////////////////////////////////
long* GetPtrTo(
long lSeq)
{
if (m_lNumCtrls > 0)
{
long lIndex = lSeq - m_lOldestSeq;
if ((lIndex >= 0) && (lIndex < m_lNumCtrls))
return &(m_alBuf[lIndex]);
}
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Discard all ctrls up to the specified sequence number
////////////////////////////////////////////////////////////////////////////////
void DiscardThrough(
long lSeq)
{
if (m_lNumCtrls > 0)
{
if (lSeq >= m_lOldestSeq)
{
// Calculate index of first ctrl that will be KEPT
long lKeepIndex = (lSeq - m_lOldestSeq) + 1;
// If index is less than number of ctrls, then there's something to be moved.
// Otherwise, the entire array has been discarded.
if (lKeepIndex < m_lNumCtrls)
{
// Move remaining ctrls down to start of array
memmove(&(m_alBuf[0]), &(m_alBuf[lKeepIndex]), (m_lNumCtrls - lKeepIndex) * sizeof(m_alBuf[0]));
m_lNumCtrls -= lKeepIndex;
m_lOldestSeq = lSeq + 1;
}
else
{
// All entries were discarded
m_lNumCtrls = 0;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
// Determine whether buffer is empty
////////////////////////////////////////////////////////////////////////////////
bool IsEmpty(void)
{
return m_lNumCtrls == 0;
}
////////////////////////////////////////////////////////////////////////////////
// Determine whether buffer is full
////////////////////////////////////////////////////////////////////////////////
bool IsFull(void)
{
return m_lNumCtrls == MaxBufEntries;
}
////////////////////////////////////////////////////////////////////////////////
// Get oldest sequence (not valid if buffer is empty!)
////////////////////////////////////////////////////////////////////////////////
long GetOldestSeq(void)
{
return m_lOldestSeq;
}
////////////////////////////////////////////////////////////////////////////////
// Get newest sequence (not valid if buffer is empty!)
////////////////////////////////////////////////////////////////////////////////
long GetNewestSeq(void)
{
return m_lOldestSeq + (m_lNumCtrls - 1);
}
};
#endif //CTRLBUF_H
////////////////////////////////////////////////////////////////////////////////
// EOF
////////////////////////////////////////////////////////////////////////////////