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 |
|
* |
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 rtio.h |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#include "copyright.h" |
22 |
+ |
|
23 |
+ |
#include <stdlib.h> |
24 |
+ |
|
25 |
+ |
#include "rterror.h" |
26 |
+ |
#include "rtio.h" |
27 |
+ |
|
28 |
|
#ifndef NHASH |
29 |
< |
#define NHASH 509 /* hash table size (prime!) */ |
29 |
> |
#define NHASH 2039 /* hash table size (prime!) */ |
30 |
|
#endif |
31 |
|
|
32 |
|
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 * |
49 |
< |
savestr(str) /* save a string */ |
51 |
< |
char *str; |
49 |
> |
savestr(const char *str) /* save a string */ |
50 |
|
{ |
51 |
< |
register int hval; |
52 |
< |
register S_HEAD *sp; |
51 |
> |
int hval; |
52 |
> |
S_HEAD *sp; |
53 |
|
|
54 |
|
if (str == NULL) |
55 |
|
return(NULL); |
56 |
< |
hval = shash(str); |
56 |
> |
if (!*str) |
57 |
> |
return ""; |
58 |
> |
hval = hash(str); |
59 |
|
for (sp = stab[hval]; sp != NULL; sp = sp->next) |
60 |
|
if (!strcmp(str, string(sp))) { |
61 |
|
sp->nl++; |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
< |
freestr(s) /* free a string */ |
77 |
< |
char *s; |
76 |
> |
void |
77 |
> |
freestr(char *s) /* free a string */ |
78 |
|
{ |
79 |
|
int hval; |
80 |
< |
register S_HEAD *spl, *sp; |
80 |
> |
S_HEAD *spl, *sp; |
81 |
|
|
82 |
< |
if (s == NULL) |
82 |
> |
if (s == NULL || !*s) |
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) |
97 |
|
|
98 |
|
|
99 |
|
int |
100 |
< |
strcmp(s1, s2) /* check for s1==s2 */ |
101 |
< |
register char *s1, *s2; |
100 |
> |
shash(const char *s) |
101 |
|
{ |
102 |
< |
if (s1 == s2) |
104 |
< |
return(0); |
102 |
> |
int h = 0; |
103 |
|
|
106 |
– |
while (*s1 == *s2++) |
107 |
– |
if (!*s1++) |
108 |
– |
return(0); |
109 |
– |
|
110 |
– |
return(*s1 - *--s2); |
111 |
– |
} |
112 |
– |
|
113 |
– |
|
114 |
– |
static int |
115 |
– |
shash(s) /* hash a string */ |
116 |
– |
register char *s; |
117 |
– |
{ |
118 |
– |
register int h = 0; |
119 |
– |
|
104 |
|
while (*s) |
105 |
< |
h += *s++; |
106 |
< |
|
123 |
< |
return(h % NHASH); |
105 |
> |
h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); |
106 |
> |
return(h); |
107 |
|
} |