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

Comparing ray/src/cv/mgflib/lookup.c (file contents):
Revision 1.1 by greg, Tue Jun 21 14:45:45 1994 UTC vs.
Revision 1.3 by greg, Thu Jun 23 08:48:28 1994 UTC

# Line 28 | Line 28 | int    nel;
28          };
29          register int  *hsp;
30  
31 <        nel += nel>>2;                  /* 75% occupancy */
31 >        nel += nel>>1;                  /* 66% occupancy */
32          for (hsp = hsiztab; *hsp; hsp++)
33                  if (*hsp > nel)
34                          break;
# Line 37 | Line 37 | int    nel;
37          tbl->tabl = (LUENT *)calloc(tbl->tsiz, sizeof(LUENT));
38          if (tbl->tabl == NULL)
39                  tbl->tsiz = 0;
40 +        tbl->ndel = 0;
41          return(tbl->tsiz);
42   }
43  
# Line 60 | Line 61 | char   *key;
61   {
62          int  hval, i;
63          register int  ndx;
64 <        register LUENT  *le;
64 <        LUENT  *oldtabl;
64 >        register LUENT  *oldtabl;
65                                          /* look up object */
66          hval = lu_hash(key);
67   tryagain:
# Line 74 | Line 74 | tryagain:
74                                          /* table is full, reallocate */
75          oldtabl = tbl->tabl;
76          ndx = tbl->tsiz;
77 <        if (!lu_init(tbl, ndx)) {       /* no more memory! */
77 >        i = tbl->ndel;
78 >        if (!lu_init(tbl, ndx-i)) {     /* no more memory! */
79                  tbl->tabl = oldtabl;
80                  tbl->tsiz = ndx;
81 +                tbl->ndel = i;
82                  return(NULL);
83          }
84          if (!ndx)
85                  goto tryagain;
86 +        /*
87 +         * The following code may fail if the user has reclaimed many
88 +         * deleted entries and the system runs out of memory in a
89 +         * recursive call to lu_find().
90 +         */
91          while (ndx--)
92 <                if (oldtabl[ndx].key != NULL) {
93 <                        le = lu_find(tbl, oldtabl[ndx].key);
94 <                        le->key = oldtabl[ndx].key;
95 <                        le->data = oldtabl[ndx].data;
96 <                }
92 >                if (oldtabl[ndx].key != NULL)
93 >                        if (oldtabl[ndx].data != NULL)
94 >                                *lu_find(tbl, oldtabl[ndx].key) = oldtabl[ndx];
95 >                        else if (tbl->freek != NULL)
96 >                                (*tbl->freek)(oldtabl[ndx].key);
97          free((MEM_PTR)oldtabl);
98          goto tryagain;                  /* should happen only once! */
99   }
100  
101  
102   void
103 < lu_done(tbl, f)                 /* free table and contents */
104 < LUTAB   *tbl;
105 < int     (*f)();
103 > lu_delete(tbl, key)             /* delete a table entry */
104 > register LUTAB  *tbl;
105 > char    *key;
106   {
107 +        register LUENT  *le;
108 +
109 +        if ((le = lu_find(tbl, key)) == NULL)
110 +                return;
111 +        if (le->key == NULL || le->data == NULL)
112 +                return;
113 +        if (tbl->freed != NULL)
114 +                (*tbl->freed)(le->data);
115 +        le->data = NULL;
116 +        tbl->ndel++;
117 + }
118 +
119 +
120 + void
121 + lu_done(tbl)                    /* free table and contents */
122 + register LUTAB  *tbl;
123 + {
124          register LUENT  *tp;
125  
126          if (!tbl->tsiz)
127                  return;
128 <        if (f != NULL)
129 <                for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
130 <                        if (tp->key != NULL)
131 <                                (*f)(tp);
128 >        for (tp = tbl->tabl + tbl->tsiz; tp-- > tbl->tabl; )
129 >                if (tp->key != NULL) {
130 >                        if (tbl->freek != NULL)
131 >                                (*tbl->freek)(tp->key);
132 >                        if (tp->data != NULL && tbl->freed != NULL)
133 >                                (*tbl->freed)(tp->data);
134 >                }
135          free((MEM_PTR)tbl->tabl);
136          tbl->tabl = NULL;
137          tbl->tsiz = 0;
138 +        tbl->ndel = 0;
139   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines