ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/savestr.c
Revision: 2.4
Committed: Thu Sep 23 12:15:21 1993 UTC (30 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -1 lines
Log Message:
added safety code for signed characters in shash()

File Contents

# User Rev Content
1 greg 1.2 /* Copyright 1988 Regents of the University of California */
2 greg 1.1
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6 greg 1.2
7     /*
8 greg 1.1 * savestr.c - routines for efficient string storage.
9     *
10     * Savestr(s) stores a shared read-only string.
11     * Freestr(s) indicates a client is finished with a string.
12     * All strings must be null-terminated. There is
13     * no imposed length limit.
14     * Strings stored with savestr(s) can be equated
15     * reliably using their pointer values. A tailored version
16     * of strcmp(s1,s2) is included.
17     * Calls to savestr(s) and freestr(s) should be
18     * balanced (obviously). The last call to freestr(s)
19     * frees memory associated with the string; it should
20     * never be referenced again.
21     *
22     * 5/14/87
23     */
24    
25     #ifndef NHASH
26     #define NHASH 509 /* hash table size (prime!) */
27     #endif
28    
29     typedef struct s_head {
30     struct s_head *next; /* next in hash list */
31     int nl; /* links count */
32     } S_HEAD; /* followed by the string itself */
33    
34     static S_HEAD *stab[NHASH];
35    
36 greg 2.3 #define hash(s) (shash(s)%NHASH)
37 greg 1.3
38 greg 1.1 extern char *savestr(), *strcpy(), *malloc();
39    
40     #define NULL 0
41    
42     #define string(sp) ((char *)((sp)+1))
43    
44     #define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str))
45    
46     #define sfree(sp) free((char *)(sp))
47    
48    
49     char *
50     savestr(str) /* save a string */
51     char *str;
52     {
53     register int hval;
54     register S_HEAD *sp;
55    
56     if (str == NULL)
57     return(NULL);
58 greg 2.3 hval = hash(str);
59 greg 1.1 for (sp = stab[hval]; sp != NULL; sp = sp->next)
60     if (!strcmp(str, string(sp))) {
61     sp->nl++;
62     return(string(sp));
63     }
64     if ((sp = salloc(str)) == NULL) {
65     eputs("Out of memory in savestr\n");
66     quit(1);
67     }
68     strcpy(string(sp), str);
69     sp->nl = 1;
70     sp->next = stab[hval];
71     stab[hval] = sp;
72     return(string(sp));
73     }
74    
75    
76     freestr(s) /* free a string */
77     char *s;
78     {
79     int hval;
80     register S_HEAD *spl, *sp;
81    
82     if (s == NULL)
83     return;
84 greg 2.3 hval = hash(s);
85 greg 1.1 for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next)
86     if (s == string(sp)) {
87     if (--sp->nl > 0)
88     return;
89     if (spl != NULL)
90     spl->next = sp->next;
91     else
92     stab[hval] = sp->next;
93     sfree(sp);
94     return;
95     }
96     }
97    
98    
99 greg 2.3 int
100     shash(s)
101 greg 1.1 register char *s;
102     {
103     register int h = 0;
104    
105     while (*s)
106 greg 2.4 h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);
107 greg 2.3 return(h);
108 greg 1.1 }