1 |
– |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Table lookup routines |
6 |
|
*/ |
7 |
|
|
8 |
|
#include <stdio.h> |
9 |
+ |
#include <stdlib.h> |
10 |
+ |
|
11 |
|
#include "lookup.h" |
12 |
|
|
14 |
– |
#ifndef MEM_PTR |
15 |
– |
#define MEM_PTR void * |
16 |
– |
#endif |
13 |
|
|
18 |
– |
extern MEM_PTR calloc(); |
19 |
– |
|
20 |
– |
|
14 |
|
int |
15 |
|
lu_init(tbl, nel) /* initialize tbl for at least nel elements */ |
16 |
|
register LUTAB *tbl; |
37 |
|
} |
38 |
|
|
39 |
|
|
40 |
< |
long |
40 |
> |
unsigned long |
41 |
|
lu_shash(s) /* hash a nul-terminated string */ |
42 |
|
char *s; |
43 |
|
{ |
68 |
|
41, 198, 99 |
69 |
|
}; |
70 |
|
register int i = 0; |
71 |
< |
register long h = 0; |
71 |
> |
register unsigned long h = 0; |
72 |
|
register unsigned char *t = (unsigned char *)s; |
73 |
|
|
74 |
|
while (*t) |
75 |
< |
h ^= (long)shuffle[*t++] << ((i+=11) & 0xf); |
75 |
> |
h ^= (unsigned long)shuffle[*t++] << ((i+=11) & 0xf); |
76 |
|
|
77 |
|
return(h); |
78 |
|
} |
83 |
|
register LUTAB *tbl; |
84 |
|
char *key; |
85 |
|
{ |
86 |
< |
long hval; |
86 |
> |
unsigned long hval; |
87 |
|
int i, n; |
88 |
|
register int ndx; |
89 |
|
register LUENT *le; |
90 |
|
/* look up object */ |
91 |
< |
if (tbl->tsiz == 0) |
92 |
< |
lu_init(tbl, 1); |
91 |
> |
if (tbl->tsiz == 0 && !lu_init(tbl, 1)) |
92 |
> |
return(NULL); |
93 |
|
hval = (*tbl->hashf)(key); |
94 |
|
tryagain: |
95 |
|
ndx = hval % tbl->tsiz; |
103 |
– |
le = &tbl->tabl[ndx]; |
96 |
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) { |
97 |
+ |
le = &tbl->tabl[ndx]; |
98 |
|
if (le->key == NULL) { |
99 |
|
le->hval = hval; |
100 |
|
return(le); |
103 |
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) { |
104 |
|
return(le); |
105 |
|
} |
106 |
< |
le += n; |
114 |
< |
if ((ndx += n) >= tbl->tsiz) { /* this happens rarely */ |
106 |
> |
if ((ndx += n) >= tbl->tsiz) /* this happens rarely */ |
107 |
|
ndx = ndx % tbl->tsiz; |
116 |
– |
le = &tbl->tabl[ndx]; |
117 |
– |
} |
108 |
|
} |
109 |
|
/* table is full, reallocate */ |
110 |
|
le = tbl->tabl; |
116 |
|
tbl->ndel = i; |
117 |
|
return(NULL); |
118 |
|
} |
129 |
– |
if (!ndx) |
130 |
– |
goto tryagain; |
119 |
|
/* |
120 |
|
* The following code may fail if the user has reclaimed many |
121 |
|
* deleted entries and the system runs out of memory in a |
122 |
|
* recursive call to lu_find(). |
123 |
|
*/ |
124 |
|
while (ndx--) |
125 |
< |
if (le[ndx].key != NULL) |
125 |
> |
if (le[ndx].key != NULL) { |
126 |
|
if (le[ndx].data != NULL) |
127 |
< |
*lu_find(tbl, le[ndx].key) = le[ndx]; |
127 |
> |
*lu_find(tbl,le[ndx].key) = le[ndx]; |
128 |
|
else if (tbl->freek != NULL) |
129 |
|
(*tbl->freek)(le[ndx].key); |
130 |
< |
free((MEM_PTR)le); |
130 |
> |
} |
131 |
> |
free((void *)le); |
132 |
|
goto tryagain; /* should happen only once! */ |
133 |
|
} |
134 |
|
|
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
+ |
int |
155 |
+ |
lu_doall(tbl, f) /* loop through all valid table entries */ |
156 |
+ |
register LUTAB *tbl; |
157 |
+ |
int (*f)(LUENT *); |
158 |
+ |
{ |
159 |
+ |
int rval = 0; |
160 |
+ |
register LUENT *tp; |
161 |
+ |
|
162 |
+ |
for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; ) |
163 |
+ |
if (tp->data != NULL) { |
164 |
+ |
if (f != NULL) |
165 |
+ |
rval += (*f)(tp); |
166 |
+ |
else |
167 |
+ |
rval++; |
168 |
+ |
} |
169 |
+ |
return(rval); |
170 |
+ |
} |
171 |
+ |
|
172 |
+ |
|
173 |
|
void |
174 |
|
lu_done(tbl) /* free table and contents */ |
175 |
|
register LUTAB *tbl; |
185 |
|
if (tp->data != NULL && tbl->freed != NULL) |
186 |
|
(*tbl->freed)(tp->data); |
187 |
|
} |
188 |
< |
free((MEM_PTR)tbl->tabl); |
188 |
> |
free((void *)tbl->tabl); |
189 |
|
tbl->tabl = NULL; |
190 |
|
tbl->tsiz = 0; |
191 |
|
tbl->ndel = 0; |