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 2.2 by greg, Fri Jul 3 10:51:50 1992 UTC vs.
Revision 2.8 by schorsch, Mon Oct 27 10:19:31 2003 UTC

# Line 1 | Line 1
1 /* Copyright 1988 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  savestr.c - routines for efficient string storage.
6   *
# Line 12 | 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
16 < *  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 +
28   #ifndef  NHASH
29   #define  NHASH          509             /* hash table size (prime!) */
30   #endif
# Line 33 | Line 36 | typedef struct s_head {
36  
37   static S_HEAD  *stab[NHASH];
38  
39 < static int  shash();
39 > #define  hash(s)        (shash(s)%NHASH)
40  
38 extern char  *savestr(), *strcpy(), *malloc();
39
40 #define  NULL           0
41
41   #define  string(sp)     ((char *)((sp)+1))
42  
43   #define  salloc(str)    (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str))
44  
45 < #define  sfree(sp)      free((char *)(sp))
45 > #define  sfree(sp)      free((void *)(sp))
46  
47  
48   char *
# Line 55 | Line 54 | char  *str;
54  
55          if (str == NULL)
56                  return(NULL);
57 <        hval = shash(str);
57 >        hval = hash(str);
58          for (sp = stab[hval]; sp != NULL; sp = sp->next)
59                  if (!strcmp(str, string(sp))) {
60                          sp->nl++;
# Line 73 | Line 72 | char  *str;
72   }
73  
74  
75 + void
76   freestr(s)                              /* free a string */
77   char  *s;
78   {
# Line 81 | Line 81 | char  *s;
81  
82          if (s == NULL)
83                  return;
84 <        hval = shash(s);
84 >        hval = hash(s);
85          for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next)
86                  if (s == string(sp)) {
87                          if (--sp->nl > 0)
# Line 96 | Line 96 | char  *s;
96   }
97  
98  
99 < static int
100 < shash(s)                                /* hash a string */
99 > int
100 > shash(s)
101   register char  *s;
102   {
103          register int  h = 0;
104  
105          while (*s)
106 <                h += *s++;
107 <
108 <        return(h % NHASH);
106 >                h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
107 >        return(h);
108   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines