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

Comparing ray/src/common/savestr.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:34:40 1989 UTC vs.
Revision 2.6 by greg, Tue Feb 25 02:47:22 2003 UTC

# Line 1 | Line 1
1 /*
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
4 + /*
5   *  savestr.c - routines for efficient string storage.
6   *
7   *      Savestr(s) stores a shared read-only string.
# Line 10 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   *      All strings must be null-terminated.  There is
10   *  no imposed length limit.
11   *      Strings stored with savestr(s) can be equated
12 < *  reliably using their pointer values.  A tailored version
14 < *  of strcmp(s1,s2) is included.
12 > *  reliably using their pointer values.
13   *      Calls to savestr(s) and freestr(s) should be
14   *  balanced (obviously).  The last call to freestr(s)
15   *  frees memory associated with the string; it should
16   *  never be referenced again.
17   *
18 < *     5/14/87
18 > *  External symbols declared in standard.h
19   */
20  
21 + #include "copyright.h"
22 +
23   #ifndef  NHASH
24   #define  NHASH          509             /* hash table size (prime!) */
25   #endif
# Line 31 | Line 31 | typedef struct s_head {
31  
32   static S_HEAD  *stab[NHASH];
33  
34 + #define  hash(s)        (shash(s)%NHASH)
35 +
36   extern char  *savestr(), *strcpy(), *malloc();
37  
38   #define  NULL           0
# Line 39 | Line 41 | extern char  *savestr(), *strcpy(), *malloc();
41  
42   #define  salloc(str)    (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str))
43  
44 < #define  sfree(sp)      free((char *)(sp))
44 > #define  sfree(sp)      free((void *)(sp))
45  
46  
47   char *
# Line 51 | Line 53 | char  *str;
53  
54          if (str == NULL)
55                  return(NULL);
56 <        hval = shash(str);
56 >        hval = hash(str);
57          for (sp = stab[hval]; sp != NULL; sp = sp->next)
58                  if (!strcmp(str, string(sp))) {
59                          sp->nl++;
# Line 69 | Line 71 | char  *str;
71   }
72  
73  
74 + void
75   freestr(s)                              /* free a string */
76   char  *s;
77   {
# Line 77 | Line 80 | char  *s;
80  
81          if (s == NULL)
82                  return;
83 <        hval = shash(s);
83 >        hval = hash(s);
84          for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next)
85                  if (s == string(sp)) {
86                          if (--sp->nl > 0)
# Line 93 | Line 96 | char  *s;
96  
97  
98   int
99 < strcmp(s1, s2)                          /* check for s1==s2 */
97 < register char  *s1, *s2;
98 < {
99 <        if (s1 == s2)
100 <                return(0);
101 <
102 <        while (*s1 == *s2++)
103 <                if (!*s1++)
104 <                        return(0);
105 <
106 <        return(*s1 - *--s2);
107 < }
108 <
109 <
110 < static int
111 < shash(s)                                /* hash a string */
99 > shash(s)
100   register char  *s;
101   {
102          register int  h = 0;
103  
104          while (*s)
105 <                h += *s++;
106 <
119 <        return(h % NHASH);
105 >                h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
106 >        return(h);
107   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines