ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/lookup.c
(Generate patch)

Comparing ray/src/common/lookup.c (file contents):
Revision 2.4 by gwlarson, Tue Jun 2 15:01:43 1998 UTC vs.
Revision 2.11 by schorsch, Wed Jul 30 10:11:06 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines