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 |
|
#include "lookup.h" |
11 |
|
|
12 |
|
#ifndef MEM_PTR |
13 |
|
#define MEM_PTR void * |
14 |
|
#endif |
15 |
|
|
18 |
– |
extern MEM_PTR calloc(); |
16 |
|
|
20 |
– |
|
17 |
|
int |
18 |
|
lu_init(tbl, nel) /* initialize tbl for at least nel elements */ |
19 |
|
register LUTAB *tbl; |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
< |
long |
43 |
> |
unsigned long |
44 |
|
lu_shash(s) /* hash a nul-terminated string */ |
45 |
|
char *s; |
46 |
|
{ |
71 |
|
41, 198, 99 |
72 |
|
}; |
73 |
|
register int i = 0; |
74 |
< |
register long h = 0; |
74 |
> |
register unsigned long h = 0; |
75 |
|
register unsigned char *t = (unsigned char *)s; |
76 |
|
|
77 |
|
while (*t) |
86 |
|
register LUTAB *tbl; |
87 |
|
char *key; |
88 |
|
{ |
89 |
< |
long hval; |
89 |
> |
unsigned long hval; |
90 |
|
int i, n; |
91 |
|
register int ndx; |
92 |
|
register LUENT *le; |
93 |
|
/* look up object */ |
94 |
< |
if (tbl->tsiz == 0) |
95 |
< |
lu_init(tbl, 1); |
94 |
> |
if (tbl->tsiz == 0 && !lu_init(tbl, 1)) |
95 |
> |
return(NULL); |
96 |
|
hval = (*tbl->hashf)(key); |
97 |
|
tryagain: |
98 |
|
ndx = hval % tbl->tsiz; |
103 |
– |
le = &tbl->tabl[ndx]; |
99 |
|
for (i = 0, n = 1; i < tbl->tsiz; i++, n += 2) { |
100 |
+ |
le = &tbl->tabl[ndx]; |
101 |
|
if (le->key == NULL) { |
102 |
|
le->hval = hval; |
103 |
|
return(le); |
106 |
|
(tbl->keycmp == NULL || !(*tbl->keycmp)(le->key, key))) { |
107 |
|
return(le); |
108 |
|
} |
109 |
< |
le += n; |
114 |
< |
if ((ndx += n) >= tbl->tsiz) { /* this happens rarely */ |
109 |
> |
if ((ndx += n) >= tbl->tsiz) /* this happens rarely */ |
110 |
|
ndx = ndx % tbl->tsiz; |
116 |
– |
le = &tbl->tabl[ndx]; |
117 |
– |
} |
111 |
|
} |
112 |
|
/* table is full, reallocate */ |
113 |
|
le = tbl->tabl; |
119 |
|
tbl->ndel = i; |
120 |
|
return(NULL); |
121 |
|
} |
129 |
– |
if (!ndx) |
130 |
– |
goto tryagain; |
122 |
|
/* |
123 |
|
* The following code may fail if the user has reclaimed many |
124 |
|
* deleted entries and the system runs out of memory in a |
125 |
|
* recursive call to lu_find(). |
126 |
|
*/ |
127 |
|
while (ndx--) |
128 |
< |
if (le[ndx].key != NULL) |
128 |
> |
if (le[ndx].key != NULL) { |
129 |
|
if (le[ndx].data != NULL) |
130 |
|
*lu_find(tbl, le[ndx].key) = le[ndx]; |
131 |
|
else if (tbl->freek != NULL) |
132 |
|
(*tbl->freek)(le[ndx].key); |
133 |
+ |
} |
134 |
|
free((MEM_PTR)le); |
135 |
|
goto tryagain; /* should happen only once! */ |
136 |
|
} |