| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
schorsch |
2.7 |
static const char RCSid[] = "$Id: savestr.c,v 2.6 2003/02/25 02:47:22 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 <string.h> |
| 24 |
|
|
#include <stdlib.h> |
| 25 |
|
|
|
| 26 |
|
|
#include "standard.h" |
| 27 |
|
|
|
| 28 |
greg |
1.1 |
#ifndef NHASH |
| 29 |
|
|
#define NHASH 509 /* hash table size (prime!) */ |
| 30 |
|
|
#endif |
| 31 |
|
|
|
| 32 |
|
|
typedef struct s_head { |
| 33 |
|
|
struct s_head *next; /* next in hash list */ |
| 34 |
|
|
int nl; /* links count */ |
| 35 |
|
|
} S_HEAD; /* followed by the string itself */ |
| 36 |
|
|
|
| 37 |
|
|
static S_HEAD *stab[NHASH]; |
| 38 |
|
|
|
| 39 |
greg |
2.3 |
#define hash(s) (shash(s)%NHASH) |
| 40 |
greg |
1.1 |
|
| 41 |
|
|
#define string(sp) ((char *)((sp)+1)) |
| 42 |
|
|
|
| 43 |
|
|
#define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str)) |
| 44 |
|
|
|
| 45 |
greg |
2.5 |
#define sfree(sp) free((void *)(sp)) |
| 46 |
greg |
1.1 |
|
| 47 |
|
|
|
| 48 |
|
|
char * |
| 49 |
|
|
savestr(str) /* save a string */ |
| 50 |
|
|
char *str; |
| 51 |
|
|
{ |
| 52 |
|
|
register int hval; |
| 53 |
|
|
register S_HEAD *sp; |
| 54 |
|
|
|
| 55 |
|
|
if (str == NULL) |
| 56 |
|
|
return(NULL); |
| 57 |
greg |
2.3 |
hval = hash(str); |
| 58 |
greg |
1.1 |
for (sp = stab[hval]; sp != NULL; sp = sp->next) |
| 59 |
|
|
if (!strcmp(str, string(sp))) { |
| 60 |
|
|
sp->nl++; |
| 61 |
|
|
return(string(sp)); |
| 62 |
|
|
} |
| 63 |
|
|
if ((sp = salloc(str)) == NULL) { |
| 64 |
|
|
eputs("Out of memory in savestr\n"); |
| 65 |
|
|
quit(1); |
| 66 |
|
|
} |
| 67 |
|
|
strcpy(string(sp), str); |
| 68 |
|
|
sp->nl = 1; |
| 69 |
|
|
sp->next = stab[hval]; |
| 70 |
|
|
stab[hval] = sp; |
| 71 |
|
|
return(string(sp)); |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
|
| 75 |
greg |
2.5 |
void |
| 76 |
greg |
1.1 |
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 |
} |