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

Comparing src/common/modobject.c (file contents):
Revision 2.20 by greg, Mon Feb 1 17:36:45 2021 UTC vs.
Revision 2.26 by greg, Sun Jun 22 17:21:37 2025 UTC

# Line 10 | Line 10 | static const char RCSid[] = "$Id$";
10   #include "copyright.h"
11  
12   #include  "standard.h"
13
13   #include  "object.h"
15
14   #include  "otypes.h"
15  
16  
17   static struct ohtab {
18 <        int  hsiz;                      /* current table size */
19 <        OBJECT  *htab;                  /* table, if allocated */
18 >        int     hsiz;                   /* current table size */
19 >        OBJECT  *htab;                  /* table, if allocated */
20   }  modtab = {100, NULL}, objtab = {1000, NULL}; /* modifiers and objects */
21  
22   static int  otndx(char *, struct ohtab *);
# Line 29 | Line 27 | objndx(                                /* get object number from pointer */
27          OBJREC  *op
28   )
29   {
30 <        int     i;
31 <        long    j;
30 > #ifndef ONCACHESIZ
31 > #define ONCACHESIZ      907     /* keep a cache of previous searches */
32 > #endif
33 >        static OBJECT   oncache[ONCACHESIZ];
34 >        const int       ent = (size_t)op % ONCACHESIZ;  /* hash on pointer */
35 >        int             i, lastblock;
36  
37 <        for (i = (nobjects-1)>>OBJBLKSHFT; i >= 0; i--) {
38 <                j = op - objblock[i];
39 <                if ((j >= 0) & (j < OBJBLKSIZ))
40 <                        return(((OBJECT)i<<OBJBLKSHFT) + (OBJECT)j);
41 <        }
42 <        return(OVOID);
37 > #if OVOID != 0                  /* clear cache on first call */
38 >        for (i = ONCACHESIZ*(!oncache[0] & !oncache[1]); i--; )
39 >                oncache[i] = OVOID;
40 > #endif
41 >                                /* is this pointer in cache? */
42 >        if ((oncache[ent] != OVOID) & (oncache[ent] < nobjects) &&
43 >                                objptr(oncache[ent]) == op)
44 >                return(oncache[ent]);           /* matches previous search */
45 >                                /* else search for our block from first */
46 >        lastblock = (nobjects-1)>>OBJBLKSHFT;
47 >        for (i = 0; i <= lastblock; i++)
48 >                if ((objblock[i] <= op) & (op < objblock[i]+OBJBLKSIZ)) {
49 >                                /* found it -- cache corresponding index */
50 >                        oncache[ent] = ((OBJECT)i << OBJBLKSHFT) +
51 >                                                (op - objblock[i]);
52 >                        return(oncache[ent]);
53 >                }
54 >        return(OVOID);          /* not allocated -- may still be valid */
55   }
56  
57  
# Line 48 | Line 62 | lastmod(                       /* find modifier definition before obj */
62   )
63   {
64          OBJREC  *op;
65 <        int  i;
65 >        OBJECT  i;
66  
67          i = modifier(mname);            /* try hash table first */
68          if ((obj == OVOID) | (i < obj))
69                  return(i);
70          for (i = obj; i-- > 0; ) {      /* need to search */
71                  op = objptr(i);
72 <                if (ismodifier(op->otype) && op->oname[0] == mname[0] &&
73 <                                        !strcmp(op->oname, mname))
72 >                if ((ismodifier(op->otype) != 0) & (op->oname[0] == mname[0])
73 >                                && !strcmp(op->oname, mname))
74                          return(i);
75          }
76          return(OVOID);
# Line 196 | Line 210 | truncobjndx(void)              /* remove bogus table entries past
210  
211          if (nobjects <= 0) {
212                  if (modtab.htab != NULL) {
213 <                        free((void *)modtab.htab);
213 >                        free(modtab.htab);
214                          modtab.htab = NULL;
215                          modtab.hsiz = 100;
216                  }
217                  if (objtab.htab != NULL) {
218 <                        free((void *)objtab.htab);
218 >                        free(objtab.htab);
219                          objtab.htab = NULL;
220                          objtab.hsiz = 100;
221                  }
# Line 278 | Line 292 | tryagain:
292                          i = otndx(onm, tab);
293                          tab->htab[i] = oldhtab[ndx];
294                  }
295 <        free((void *)oldhtab);
295 >        free(oldhtab);
296          goto tryagain;                  /* should happen only once! */
297   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)