blob: 4c8f3904006cf722b9782067326c13fefd054da1 (
plain) (
blame)
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
|
/**
* VOTHANDLE.C -- (Private) Methods to manage interface handles.
*
* @file votHandle.c
* @author Mike Fitzpatrick and Eric Timmermann
* @date 8/03/09
*
* @brief (Private) Methods to manage interface handles.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "votParseP.h"
static Element **handles; /** A pointer to the handles */
static handle_t handleMax = 0; /** max available handles */
static handle_t handleCount = 0; /** count of current used handles */
/**
* vot_handleCount -- Get the number of handle_t used (private method)
*
* @brief Get the number of handle_t used (private method)
* @fn int vot_handleCount (void)
*
* @return The number of handle_t types currently stored
*/
int vot_handleCount () { return (handleCount); }
/**
* vot_lookupHandle -- Lookup the handle_t to an Element (private method)
*
* @brief Lookup the handle_t to an Element (private method)
* @fn handle_t vot_lookupHandle (Element *elem)
*
* @param *elem A pointer to an Element
* @return A handle_t to the Element
*/
handle_t
vot_lookupHandle (Element *elem)
{
#ifdef HANDLE_SEARCH
unsigned int i = 0, j = 0;
unsigned int big = 1024, small = 20;
#endif
if (elem == (Element *) NULL)
return (0);
/* The handle structure is basically an array of pointers. In general the
* address will be an increasing value, but is not guaranteed to always be
* offset by an integer number of Elem structs. The strategy is to search
* in large chunks before resorting to sequential access to the array to
* find the exact match.
*
*/
#ifdef HANDLE_SEARCH
big = max (1024, (handleCount / 10));
small = max (20, (handleCount / 200));
for (i = 0; i < handleMax; i+=big)
if (elem > handles[i] || i >= handleMax)
break;
for (j = (i - big); j < handleMax; j+=small)
if (elem > handles[j] || j >= handleMax)
break;
for (j = (j - small); j < handleMax; j++)
if (handles[j] == elem)
return ((j + 1));
#else
if (elem->handle >= 0)
return (elem->handle);
#endif
return (vot_setHandle (elem));
}
/**
* vot_setHandle -- Assign the Element a handle_t (private method)
*
* @brief Assign the Element a handle_t (private method)
* @fn handle_t vot_setHandle (Element *elem)
*
* @param elem A pointer to an Element to be assigned a handle_t.
* @return A handle_t refering to elem
*/
handle_t
vot_setHandle (Element *elem)
{
unsigned int i = 0;
Element **old_handles;
if (elem == NULL)
return (0);
if (handleCount == handleMax) {
old_handles = handles;
handles = (Element **) calloc ((handleMax + HANDLE_INCREMENT + 1),
sizeof(Element *));
for (i = 0; i < handleMax; i++)
handles[i] = old_handles[i];
handleMax = handleMax + HANDLE_INCREMENT;
free (old_handles);
}
for (i = handleCount+1; i >= 0; i--) {
if (handles[i] == NULL) {
elem->handle = i + 1;
handles[i] = elem;
handleCount++;
return ((i + 1));
}
}
/* If we get this far, it's an error condition.
*/
vot_handleError ("ERROR: Handle overflow.");
return (0);
}
/**
* vot_freeHandle -- Free a handle for use (private method)
*
* @brief Free a handle for use (private method)
* @fn vot_freeHandle (handle_t handle)
*
* @param handle A handle_t to the Element you wish to free
* @return nothing
*/
void
vot_freeHandle (handle_t handle)
{
if (handle < handleMax) {
handles[(handle - 1)] = NULL;
handleCount--;
} else
vot_handleError ("ERROR: Handle overflow.");
}
/**
* vot_getElement -- Get the Element refered to by handle_t (private method)
*
* @brief Get the Element refered to by handle_t (private method)
* @fn Element *vot_getElement (handle_t handle)
*
* @param handle A handle_t to the Element.
* @return A pointer to the requested Element.
*/
Element *
vot_getElement (handle_t handle)
{
if (handle == 0)
vot_handleError ("ERROR: Handle NULL.");
/*return (NULL);*/
else if (handle > handleMax)
vot_handleError ("ERROR: Handle overflow.");
else if (handle < handleMax)
return (Element *) handles[(handle - 1)];
return (NULL);
}
/**
* vot_handleCleanup -- Free all the handle nodes (private method)
*
* @brief Free all the handle nodes (private method)
* @fn vot_handleCleanup (void)
*
* @return nothing
*/
void
vot_handleCleanup (void)
{
unsigned int i = 0;
for (i = 0; i < handleMax; i++) {
if (handles[i] != NULL)
free (handles[i]);
}
handleMax = 0;
handleCount = 0;
free (handles);
handles = NULL;
vot_newHandleTable (); /* FIXME ???? */
}
/**
* vot_newHandleTable -- Initialize a handle table (private method)
*
* @brief Initialize a handle table (private method)
* @fn vot_newHandleTable (void)
*
* @return nothing
*/
void
vot_newHandleTable (void)
{
if (handles == NULL)
handles = (Element **) calloc (HANDLE_INCREMENT, sizeof(Element *));
}
/**
* vot_handleError -- Print an error message.
*
* @brief Print an error message.
* @fn vot_handleError (char *msg)
*
* @return nothing
*/
void
vot_handleError (char *msg)
{
fprintf (stderr, "%s\n", msg);
}
|