| 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 |
> |
#ifdef SMLMEM |
| 30 |
> |
#define NHASH 2039 /* hash table size (prime!) */ |
| 31 |
> |
#else |
| 32 |
> |
#define NHASH 17117 /* hash table size (prime!) */ |
| 33 |
|
#endif |
| 34 |
+ |
#endif |
| 35 |
|
|
| 36 |
|
typedef struct s_head { |
| 37 |
|
struct s_head *next; /* next in hash list */ |
| 40 |
|
|
| 41 |
|
static S_HEAD *stab[NHASH]; |
| 42 |
|
|
| 43 |
< |
extern char *savestr(), *strcpy(), *malloc(); |
| 43 |
> |
#define hash(s) (shash(s)%NHASH) |
| 44 |
|
|
| 38 |
– |
#define NULL 0 |
| 39 |
– |
|
| 45 |
|
#define string(sp) ((char *)((sp)+1)) |
| 46 |
|
|
| 47 |
|
#define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str)) |
| 48 |
|
|
| 49 |
< |
#define sfree(sp) free((char *)(sp)) |
| 49 |
> |
#define sfree(sp) free((void *)(sp)) |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
char * |
| 53 |
< |
savestr(str) /* save a string */ |
| 49 |
< |
char *str; |
| 53 |
> |
savestr(const char *str) /* save a string */ |
| 54 |
|
{ |
| 55 |
< |
register int hval; |
| 56 |
< |
register S_HEAD *sp; |
| 55 |
> |
int hval; |
| 56 |
> |
S_HEAD *sp; |
| 57 |
|
|
| 58 |
|
if (str == NULL) |
| 59 |
|
return(NULL); |
| 60 |
< |
hval = shash(str); |
| 60 |
> |
if (!*str) |
| 61 |
> |
return ""; |
| 62 |
> |
hval = hash(str); |
| 63 |
|
for (sp = stab[hval]; sp != NULL; sp = sp->next) |
| 64 |
|
if (!strcmp(str, string(sp))) { |
| 65 |
|
sp->nl++; |
| 77 |
|
} |
| 78 |
|
|
| 79 |
|
|
| 80 |
< |
freestr(s) /* free a string */ |
| 81 |
< |
char *s; |
| 80 |
> |
void |
| 81 |
> |
freestr(char *s) /* free a string */ |
| 82 |
|
{ |
| 83 |
|
int hval; |
| 84 |
< |
register S_HEAD *spl, *sp; |
| 84 |
> |
S_HEAD *spl, *sp; |
| 85 |
|
|
| 86 |
< |
if (s == NULL) |
| 86 |
> |
if (s == NULL || !*s) |
| 87 |
|
return; |
| 88 |
< |
hval = shash(s); |
| 88 |
> |
hval = hash(s); |
| 89 |
|
for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next) |
| 90 |
|
if (s == string(sp)) { |
| 91 |
|
if (--sp->nl > 0) |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
int |
| 104 |
< |
strcmp(s1, s2) /* check for s1==s2 */ |
| 99 |
< |
register char *s1, *s2; |
| 104 |
> |
shash(const char *s) |
| 105 |
|
{ |
| 106 |
< |
if (s1 == s2) |
| 102 |
< |
return(0); |
| 106 |
> |
int h = 0; |
| 107 |
|
|
| 104 |
– |
while (*s1 == *s2++) |
| 105 |
– |
if (!*s1++) |
| 106 |
– |
return(0); |
| 107 |
– |
|
| 108 |
– |
return(*s1 - *--s2); |
| 109 |
– |
} |
| 110 |
– |
|
| 111 |
– |
|
| 112 |
– |
static int |
| 113 |
– |
shash(s) /* hash a string */ |
| 114 |
– |
register char *s; |
| 115 |
– |
{ |
| 116 |
– |
register int h = 0; |
| 117 |
– |
|
| 108 |
|
while (*s) |
| 109 |
< |
h += *s++; |
| 110 |
< |
|
| 121 |
< |
return(h % NHASH); |
| 109 |
> |
h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); |
| 110 |
> |
return(h); |
| 111 |
|
} |