-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiosc.c
301 lines (276 loc) · 6.84 KB
/
iosc.c
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
/*
* Searchable Collections:
*
* Expanding hash tables with a key and data.
*/
#include "system.h"
#include "iosc.h"
#include "assert.h"
#define USEPRIMESIZES
#if !defined(USEPRIMESIZES)
static int sizes[] = {
4, 8, 16, 32,
64, 128, 256, 512,
1024, 2048, 4096, 8192,
16*1024, 32*1024, 64*1024, 128*1024,
256*1024, 512*1024, 1024*1024, 2*1024*1024,
4*1024*1024, 8*1024*1024, 16*1024*1024 };
#define Hash(a,sc) (IOScHASH(a) & (sc->size - 1))
#else
static int sizes[] = {
5, 7, 17, 31,
67, 131, 257, 521,
1031, 2053, 4099, 8093,
16193, 32377, 65557, 131071,
262187, 524869, 1048829, 2097223,
4194371, 8388697, 16777291 };
#define Hash(key, sc) (IOScHASH(key) % sc->size)
#endif
#define MAXFILL(x) (((x) * 17) / 20)
/*
* Turning on DEBUGSC will cause the package to self-check on every (modifying)
* operation. The package runs very slowly when this is enabled.
*/
#ifdef DEBUGSC
static void CheckOutHashTable(IOSc sc);
#define CHECKOUTHASHTABLE(sc) CheckOutHashTable(sc)
#else
#define CHECKOUTHASHTABLE(sc)
#endif
static int chooseSize(int size)
{
int i;
for (i = 0; sizes[i] <= size; i++) ;
return sizes[i];
}
/* Return a new, empty IOSc */
IOSc IOScCreateN(int size)
{
register int i;
register IOSc sc;
sc = (IOSc) vmMalloc(sizeof(IOScRecord));
sc->size = chooseSize(size);
sc->maxCount = MAXFILL(sc->size);
sc->count = 0;
sc->table = (IOScTEPtr) vmMalloc((unsigned) sc->size * sizeof(IOScTE));
for (i = 0; i < sc->size; i++) {
sc->table[i].key = IOScKNIL;
}
CHECKOUTHASHTABLE(sc);
return sc;
}
IOSc IOScCreate(void)
{
return IOScCreateN(2);
}
void IOScDestroy(sc)
register IOSc sc;
{
vmFree((char *)sc->table);
vmFree((char *)sc);
}
/* Expand the hash table. Each element in the table is re-hashed and entered
* in the new table. */
static void ExpandHashTable(IOSc sc)
{
register IOScTE *nh, *oe, *ne;
register int oldHashTableSize = sc->size, i;
register IOScDomainType key;
int index;
for (i = 0; sizes[i] <= oldHashTableSize; i++) ;
sc->size = sizes[i];
sc->maxCount = MAXFILL(sc->size);
nh = (IOScTEPtr) vmMalloc((unsigned)(sc->size * sizeof(IOScTE)));
for (i = 0; i < sc->size; i++) nh[i].key = IOScKNIL;
for (i = 0; i < oldHashTableSize; i++) {
oe = &sc->table[i];
key = oe->key;
if (IOScKIsNIL(key)) continue;
index = Hash(key, sc);
while (1) {
ne = &nh[index];
if (IOScKIsNIL(ne->key)) {
ne->key = oe->key;
ne->value = oe->value;
break;
} else {
assert(ne->key !=key);
index++;
if (index >= sc->size) index = 0;
}
}
}
vmFree((char *)sc->table);
sc->table = nh;
CHECKOUTHASHTABLE(sc);
}
/* Return the value associated with key in collection sc, or IOScNIL */
IOScRangeType IOScLookup(sc, key)
register IOSc sc;
register IOScDomainType key;
{
register int index = Hash(key, sc);
register IOScTEPtr e;
CHECKOUTHASHTABLE(sc);
while (1) {
e = &sc->table[index];
if (IOScKIsNIL(e->key)) { /* we did not find it */
return IOScNIL;
} else if (IOScCOMPARE(e->key, key)) {
return e->value;
}
if (++index >= sc->size) index = 0;
}
}
/* Insert the key, value pair in sc. If the key already exists, change its
* value. */
void IOScInsert(sc, key, value)
register IOSc sc;
register IOScDomainType key;
IOScRangeType value;
{
register int index;
register IOScTEPtr e;
assert(!IOScKIsNIL(key));
if (sc->count >= sc->maxCount) ExpandHashTable(sc);
index = Hash(key, sc);
while (1) {
e = &sc->table[index];
if (IOScKIsNIL(e->key)) { /* put it here */
e->key = key;
e->value = value;
sc->count++;
CHECKOUTHASHTABLE(sc);
return;
} else if (IOScCOMPARE(e->key, key)) {
e->value = value;
CHECKOUTHASHTABLE(sc);
return;
}
if (++index >= sc->size) index = 0;
}
}
/* Select a random (the first) key from the set sc */
IOScDomainType IOScSelect(sc,rangeptr)
register IOSc sc;
IOScRangeType *rangeptr;
{
register int index = 0;
register IOScTEPtr e;
CHECKOUTHASHTABLE(sc);
while (1) {
e = &sc->table[index];
if (!IOScKIsNIL(e->key)) { /* we found it */
*rangeptr = e->value;
return e->key;
}
if (++index >= sc->size) return (int)NULL;
}
}
/* Remove the entry, if it is there */
void IOScDelete(sc, key)
register IOSc sc;
register IOScDomainType key;
{
register int index = Hash(key, sc);
register IOScRangeType value;
register IOScTEPtr e;
while (1) {
e = &sc->table[index];
if (IOScKIsNIL(e->key)) { /* we did not find it */
CHECKOUTHASHTABLE(sc);
return;
}
if (IOScCOMPARE(e->key, key)) {
/* Found it, now remove it */
sc->count--;
e->key = IOScKNIL;
while (1) {
/* rehash until we reach nil again */
if (++index >= sc->size) index = 0;
e = & sc->table[index];
key = e->key;
if (IOScKIsNIL(key)) {
CHECKOUTHASHTABLE(sc);
return;
}
/* rehashing is done by removing then reinserting */
value = e->value;
e->key = IOScKNIL;
sc->count--;
IOScInsert(sc, key, value);
}
}
if (++index >= sc->size) index = 0;
}
}
/* DEBUGGING: Print the sc */
#define PrintOID(o) printf("%#x.%04x.%04x.%#x", \
o.ipaddress, o.port, o.epoch, o.Seq)
void IOScPrint(sc)
register IOSc sc;
{
IOScDomainType key;
IOScRangeType value;
int index;
printf(
"\nDump of sc @ 0x%05x, %d entr%s, current max %d\nIndex\tKey\t\tValue\n",
(int)sc, sc->count, sc->count == 1 ? "y" : "ies", sc->maxCount);
for (index = 0; index < sc->size; index++) {
key = sc->table[index].key;
value = sc->table[index].value;
printf("%3d\t0x%08x\t", index, key);
PrintOID(value);
printf("\n");
}
}
#ifdef DEBUGSC
/* Make sure that the hash table is internally consistent:
* every key is findable,
* count reflects the number of elements
*/
static void CheckOutHashTable(sc)
register IOSc sc;
{
register int i;
register IOScTEPtr realElement, e;
register int index, firstIndex, count;
count = 0;
for (i = 0; i < sc->size; i++) {
realElement = &sc->table[i];
if (!IOScKIsNIL(realElement->key)) {
count++;
index = Hash(realElement->key, sc);
firstIndex = index;
while (1) {
e = &sc->table[index];
if (IOScKIsNIL(e->key)) { /* we did not find it */
break;
} else if (IOScCOMPARE(e->key, realElement->key)) {
break;
} else {
index++;
if (index >= sc->size) index = 0;
if (index == firstIndex) {
index = -1;
break;
}
}
}
if (index == -1 || !IOScCOMPARE(e->key, realElement->key)) {
FIX THIS
fprintf(stderr,
"Sc problem: Key 0x%x, rightIndex %d, realIndex %d value 0x%x\n",
realElement->key, firstIndex, index, realElement->value);
IOScPrint(sc);
}
}
}
if (count != sc->count) {
fprintf(stderr,
"Sc problem: Should have %d entries, but found %d.\n", sc->count,
count);
IOScPrint(sc);
}
}
#endif