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.11 by greg, Sun Apr 9 21:33:24 2017 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 + #include <string.h>
24 + #include <stdlib.h>
25 +
26 + #include "rtmisc.h"
27 + #include "rterror.h"
28 + #include "rtio.h"
29 +
30   #ifndef  NHASH
31 < #define  NHASH          509             /* hash table size (prime!) */
31 > #define  NHASH          2039            /* hash table size (prime!) */
32   #endif
33  
34   typedef struct s_head {
# Line 31 | Line 38 | typedef struct s_head {
38  
39   static S_HEAD  *stab[NHASH];
40  
41 < extern char  *savestr(), *strcpy(), *malloc();
41 > #define  hash(s)        (shash(s)%NHASH)
42  
36 #define  NULL           0
37
43   #define  string(sp)     ((char *)((sp)+1))
44  
45   #define  salloc(str)    (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str))
46  
47 < #define  sfree(sp)      free((char *)(sp))
47 > #define  sfree(sp)      free((void *)(sp))
48  
49  
50   char *
51 < savestr(str)                            /* save a string */
47 < char  *str;
51 > savestr(char *str)                              /* save a string */
52   {
53 <        register int  hval;
54 <        register S_HEAD  *sp;
53 >        int  hval;
54 >        S_HEAD  *sp;
55  
56          if (str == NULL)
57                  return(NULL);
58 <        hval = shash(str);
58 >        if (!*str)
59 >                return "";
60 >        hval = hash(str);
61          for (sp = stab[hval]; sp != NULL; sp = sp->next)
62                  if (!strcmp(str, string(sp))) {
63                          sp->nl++;
# Line 69 | Line 75 | char  *str;
75   }
76  
77  
78 < freestr(s)                              /* free a string */
79 < char  *s;
78 > void
79 > freestr(char *s)                                /* free a string */
80   {
81          int  hval;
82 <        register S_HEAD  *spl, *sp;
82 >        S_HEAD  *spl, *sp;
83  
84 <        if (s == NULL)
84 >        if (s == NULL || !*s)
85                  return;
86 <        hval = shash(s);
86 >        hval = hash(s);
87          for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next)
88                  if (s == string(sp)) {
89                          if (--sp->nl > 0)
# Line 93 | Line 99 | char  *s;
99  
100  
101   int
102 < strcmp(s1, s2)                          /* check for s1==s2 */
97 < register char  *s1, *s2;
102 > shash(char *s)
103   {
104 <        if (s1 == s2)
100 <                return(0);
104 >        int  h = 0;
105  
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 */
112 register char  *s;
113 {
114        register int  h = 0;
115
106          while (*s)
107 <                h += *s++;
108 <
119 <        return(h % NHASH);
107 >                h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
108 >        return(h);
109   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines