1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.12 |
static const char RCSid[] = "$Id: savestr.c,v 2.11 2017/04/09 21:33:24 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
greg |
1.2 |
/* |
5 |
greg |
1.1 |
* savestr.c - routines for efficient string storage. |
6 |
|
|
* |
7 |
|
|
* Savestr(s) stores a shared read-only string. |
8 |
|
|
* Freestr(s) indicates a client is finished with a string. |
9 |
|
|
* All strings must be null-terminated. There is |
10 |
|
|
* no imposed length limit. |
11 |
|
|
* Strings stored with savestr(s) can be equated |
12 |
greg |
2.5 |
* reliably using their pointer values. |
13 |
greg |
1.1 |
* 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 |
greg |
2.5 |
* External symbols declared in standard.h |
19 |
|
|
*/ |
20 |
|
|
|
21 |
greg |
2.6 |
#include "copyright.h" |
22 |
greg |
1.1 |
|
23 |
schorsch |
2.7 |
#include <stdlib.h> |
24 |
|
|
|
25 |
schorsch |
2.8 |
#include "rtmisc.h" |
26 |
schorsch |
2.9 |
#include "rterror.h" |
27 |
|
|
#include "rtio.h" |
28 |
schorsch |
2.7 |
|
29 |
greg |
1.1 |
#ifndef NHASH |
30 |
greg |
2.10 |
#define NHASH 2039 /* hash table size (prime!) */ |
31 |
greg |
1.1 |
#endif |
32 |
|
|
|
33 |
|
|
typedef struct s_head { |
34 |
|
|
struct s_head *next; /* next in hash list */ |
35 |
|
|
int nl; /* links count */ |
36 |
|
|
} S_HEAD; /* followed by the string itself */ |
37 |
|
|
|
38 |
|
|
static S_HEAD *stab[NHASH]; |
39 |
|
|
|
40 |
greg |
2.3 |
#define hash(s) (shash(s)%NHASH) |
41 |
greg |
1.1 |
|
42 |
|
|
#define string(sp) ((char *)((sp)+1)) |
43 |
|
|
|
44 |
|
|
#define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str)) |
45 |
|
|
|
46 |
greg |
2.5 |
#define sfree(sp) free((void *)(sp)) |
47 |
greg |
1.1 |
|
48 |
|
|
|
49 |
|
|
char * |
50 |
schorsch |
2.9 |
savestr(char *str) /* save a string */ |
51 |
greg |
1.1 |
{ |
52 |
greg |
2.11 |
int hval; |
53 |
|
|
S_HEAD *sp; |
54 |
greg |
1.1 |
|
55 |
|
|
if (str == NULL) |
56 |
|
|
return(NULL); |
57 |
greg |
2.11 |
if (!*str) |
58 |
|
|
return ""; |
59 |
greg |
2.3 |
hval = hash(str); |
60 |
greg |
1.1 |
for (sp = stab[hval]; sp != NULL; sp = sp->next) |
61 |
|
|
if (!strcmp(str, string(sp))) { |
62 |
|
|
sp->nl++; |
63 |
|
|
return(string(sp)); |
64 |
|
|
} |
65 |
|
|
if ((sp = salloc(str)) == NULL) { |
66 |
|
|
eputs("Out of memory in savestr\n"); |
67 |
|
|
quit(1); |
68 |
|
|
} |
69 |
|
|
strcpy(string(sp), str); |
70 |
|
|
sp->nl = 1; |
71 |
|
|
sp->next = stab[hval]; |
72 |
|
|
stab[hval] = sp; |
73 |
|
|
return(string(sp)); |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
greg |
2.5 |
void |
78 |
schorsch |
2.9 |
freestr(char *s) /* free a string */ |
79 |
greg |
1.1 |
{ |
80 |
|
|
int hval; |
81 |
greg |
2.11 |
S_HEAD *spl, *sp; |
82 |
greg |
1.1 |
|
83 |
greg |
2.11 |
if (s == NULL || !*s) |
84 |
greg |
1.1 |
return; |
85 |
greg |
2.3 |
hval = hash(s); |
86 |
greg |
1.1 |
for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next) |
87 |
|
|
if (s == string(sp)) { |
88 |
|
|
if (--sp->nl > 0) |
89 |
|
|
return; |
90 |
|
|
if (spl != NULL) |
91 |
|
|
spl->next = sp->next; |
92 |
|
|
else |
93 |
|
|
stab[hval] = sp->next; |
94 |
|
|
sfree(sp); |
95 |
|
|
return; |
96 |
|
|
} |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
|
100 |
greg |
2.3 |
int |
101 |
greg |
2.11 |
shash(char *s) |
102 |
greg |
1.1 |
{ |
103 |
greg |
2.11 |
int h = 0; |
104 |
greg |
1.1 |
|
105 |
|
|
while (*s) |
106 |
greg |
2.4 |
h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); |
107 |
greg |
2.3 |
return(h); |
108 |
greg |
1.1 |
} |