-
Notifications
You must be signed in to change notification settings - Fork 36
/
IdBank.cpp
349 lines (305 loc) · 10.1 KB
/
IdBank.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
////////////////////////////////////////////////////////////////////////////////
//
// 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
//
// IdBank.cpp
//
// History:
// 01/29/97 JMI Started.
//
// 01/30/97 JMI Removed the bug I put in Add() that access the Head of
// the list to add to, whether or not it was a valid node.
//
// 01/30/97 JMI Fixed bug in Remove(). In rush for demo.
//
// 02/24/97 JMI Changed Add() to Insert() and created new Add() that
// adds at the end.
// Also, removed m_u16HeadUsedId. It wasn't useful.
//
// 03/05/97 JMI GetThingByID() now fails for IdNil without complaint.
//
//////////////////////////////////////////////////////////////////////////////
//
// See .H for details.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// C Headers -- Must be included before RSPiX.h b/c RSPiX utilizes SHMalloc.
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// RSPiX Headers.
// If PATHS_IN_INCLUDES macro is defined, we can utilize relative
// paths to a header file. In this case we generally go off of our
// RSPiX root directory. System.h MUST be included before this macro
// is evaluated. System.h is the header that, based on the current
// platform (or more so in this case on the compiler), defines
// PATHS_IN_INCLUDES. Blue.h includes system.h so you can include that
// instead.
///////////////////////////////////////////////////////////////////////////////
#include "RSPiX.h"
#ifdef PATHS_IN_INCLUDES
#else
#endif
//////////////////////////////////////////////////////////////////////////////
// Postal includes.
//////////////////////////////////////////////////////////////////////////////
#include "IdBank.h"
//////////////////////////////////////////////////////////////////////////////
// Module specific macros.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Module specific typedefs.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Exported (extern) variables.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Module specific (static) variables / Instantiate class statics.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Module specific (static) protos.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// Helper to insert an ID into a particular list.
//
//////////////////////////////////////////////////////////////////////////////
void CIdBank::Insert( // Returns nothing.
U16 u16Id, // ID to insert.
U16* pu16IdHead) // Head of list to add to.
{
// Point this node's next at the current head of the free list.
m_aids[u16Id].u16IdNext = *pu16IdHead;
// If there is a head . . .
if (*pu16IdHead != IdNil)
{
// Point current head node's prev at this node.
m_aids[*pu16IdHead].u16IdPrev = u16Id;
}
// Don't look back.
m_aids[u16Id].u16IdPrev = IdNil;
// Make this node the new head of the list.
*pu16IdHead = u16Id;
}
//////////////////////////////////////////////////////////////////////////////
//
// Helper to add an ID to a particular list.
//
//////////////////////////////////////////////////////////////////////////////
void CIdBank::Add( // Returns nothing.
U16 u16Id, // ID to add.
U16* pu16IdTail) // Tail of list to add to.
{
// Point this node's prev at the current tail of the free list.
m_aids[u16Id].u16IdPrev = *pu16IdTail;
// If there is a head . . .
if (*pu16IdTail != IdNil)
{
// Point current tail node's next at this node.
m_aids[*pu16IdTail].u16IdNext = u16Id;
}
// Don't look forward.
m_aids[u16Id].u16IdNext = IdNil;
// Make this node the new tail of the list.
*pu16IdTail = u16Id;
}
//////////////////////////////////////////////////////////////////////////////
//
// Helper to remove an ID from a particular list.
//
//////////////////////////////////////////////////////////////////////////////
void CIdBank::Remove( // Returns nothing.
U16 u16Id, // ID to remove.
U16* pu16IdHead, // Head of list to remove from.
U16* pu16IdTail) // Tail of list to remove from.
{
// If this was the head . . .
if (*pu16IdHead == u16Id)
{
// Make the head this node's next.
*pu16IdHead = m_aids[u16Id].u16IdNext;
// If not the end . . .
if (*pu16IdHead != IdNil)
{
// Make the new head's prev NIL.
m_aids[*pu16IdHead].u16IdPrev = IdNil;
}
}
else
{
// Set prev's next to this ID's next.
m_aids[m_aids[u16Id].u16IdPrev].u16IdNext = m_aids[u16Id].u16IdNext;
}
// If this was the tail . . .
if (*pu16IdTail == u16Id)
{
// Make the tail this node's prev.
*pu16IdTail = m_aids[u16Id].u16IdPrev;
// If not the beginning . . .
if (*pu16IdTail != IdNil)
{
// Make the new tail's next NIL.
m_aids[*pu16IdTail].u16IdNext = IdNil;
}
}
else
{
// Set next's prev to this ID's prev.
m_aids[m_aids[u16Id].u16IdNext].u16IdPrev = m_aids[u16Id].u16IdPrev;
}
}
//////////////////////////////////////////////////////////////////////////////
//
// Resets all IDs to free. This must be called before the first use of
// any of the rest of these functions.
//
//////////////////////////////////////////////////////////////////////////////
void CIdBank::Reset(void)
{
// Reset all IDs.
// Initialize all IDs regardless of current contents.
U16 u16Cur;
U16 u16Prev;
for (u16Cur = 0, u16Prev = IdNil; u16Cur < NumIds; u16Cur++, u16Prev++)
{
m_aids[u16Cur].u16IdPrev = u16Prev;
m_aids[u16Cur].u16IdNext = u16Cur + 1;
m_aids[u16Cur].pthing = NULL;
}
// Last item's next should indicate end.
m_aids[u16Prev].u16IdNext = IdNil;
// Start at beginning for Free List head.
m_u16HeadFreeId = 0;
// Start at end for Free List tail.
m_u16TailFreeId = NumIds - 1;
}
//////////////////////////////////////////////////////////////////////////////
//
// Get a unique ID and associate it with a thing (CThing, that is).
//
//////////////////////////////////////////////////////////////////////////////
short CIdBank::Get( // Returns 0 on success.
CThing* pthing, // In: Thing that wants to get an ID and be put in
// the ID table.
U16* pu16ID) // Out: ID for this particular CThing.
{
short sRes = 0; // Assume success.
// Make sure there's one left . . .
if (m_u16HeadFreeId != IdNil)
{
// Get ID.
*pu16ID = m_u16HeadFreeId;
// Set IDs value.
m_aids[*pu16ID].pthing = pthing;
// Remove from free list and get next head.
m_u16HeadFreeId = m_aids[*pu16ID].u16IdNext;
}
else
{
TRACE("GetUniqueID(): Out of IDs!\n");
sRes = -1;
}
return sRes;
}
//////////////////////////////////////////////////////////////////////////////
//
// Take a unique ID and associate it with a thing (CThing).
//
//////////////////////////////////////////////////////////////////////////////
short CIdBank::Take( // Returns 0 on success.
CThing* pthing, // In: Thing that wants to take an ID and be put in
// the ID table.
U16 u16ID) // In: ID for this particular CThing.
{
short sRes = 0; // Assume success.
// Range check.
ASSERT(u16ID < NumIds);
// Make sure the ID is available . . .
if (m_aids[u16ID].pthing == NULL)
{
// Set IDs value.
m_aids[u16ID].pthing = pthing;
// Remove from free list.
Remove(u16ID, &m_u16HeadFreeId, &m_u16TailFreeId);
}
else
{
TRACE("TakeUniqueID(): ID not available!\n");
sRes = -1;
}
return sRes;
}
//////////////////////////////////////////////////////////////////////////////
//
// Release ID and unregister thing associated with it.
//
//////////////////////////////////////////////////////////////////////////////
void CIdBank::Release( // Returns nothing.
U16 u16ID) // ID to release.
{
// If a valid ID . . .
if (u16ID != IdNil)
{
// Range check.
ASSERT(u16ID < NumIds);
// The ID should be in use. If not, something has hosened.
ASSERT(m_aids[u16ID].pthing != NULL);
// Clear ID.
m_aids[u16ID].pthing = NULL;
// Add to free list.
Add(u16ID, &m_u16TailFreeId);
}
}
//////////////////////////////////////////////////////////////////////////////
//
// Get a CThing via its ID.
//
//////////////////////////////////////////////////////////////////////////////
short CIdBank::GetThingByID( // Returns 0 on success.
CThing** ppthing, // Out: Ptr to CThing identified by u16ID.
U16 u16ID) // In: ID of thing to get.
{
short sRes = 0; // Assume success.
if (u16ID != IdNil)
{
// Range check.
ASSERT(u16ID < NumIds);
// Get thing.
*ppthing = m_aids[u16ID].pthing;
// This ID should be used.
if (*ppthing != NULL)
{
// Success.
}
else
{
// TRACE("GetThingByID(): No such ID.\n");
sRes = -2;
}
}
else
{
*ppthing = NULL;
sRes = -1;
}
return sRes;
}
///////////////////////////////////////////////////////////////////////////////
// EOF
///////////////////////////////////////////////////////////////////////////////