--- ray/src/common/savestr.c 1989/02/02 10:34:40 1.1 +++ ray/src/common/savestr.c 2004/03/04 16:34:34 2.10 @@ -1,8 +1,7 @@ -/* - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: savestr.c,v 2.10 2004/03/04 16:34:34 greg Exp $"; #endif +/* * savestr.c - routines for efficient string storage. * * Savestr(s) stores a shared read-only string. @@ -10,18 +9,26 @@ static char SCCSid[] = "$SunId$ LBL"; * All strings must be null-terminated. There is * no imposed length limit. * Strings stored with savestr(s) can be equated - * reliably using their pointer values. A tailored version - * of strcmp(s1,s2) is included. + * reliably using their pointer values. * Calls to savestr(s) and freestr(s) should be * balanced (obviously). The last call to freestr(s) * frees memory associated with the string; it should * never be referenced again. * - * 5/14/87 + * External symbols declared in standard.h */ +#include "copyright.h" + +#include +#include + +#include "rtmisc.h" +#include "rterror.h" +#include "rtio.h" + #ifndef NHASH -#define NHASH 509 /* hash table size (prime!) */ +#define NHASH 2039 /* hash table size (prime!) */ #endif typedef struct s_head { @@ -31,27 +38,24 @@ typedef struct s_head { static S_HEAD *stab[NHASH]; -extern char *savestr(), *strcpy(), *malloc(); +#define hash(s) (shash(s)%NHASH) -#define NULL 0 - #define string(sp) ((char *)((sp)+1)) #define salloc(str) (S_HEAD *)malloc(sizeof(S_HEAD)+1+strlen(str)) -#define sfree(sp) free((char *)(sp)) +#define sfree(sp) free((void *)(sp)) char * -savestr(str) /* save a string */ -char *str; +savestr(char *str) /* save a string */ { register int hval; register S_HEAD *sp; if (str == NULL) return(NULL); - hval = shash(str); + hval = hash(str); for (sp = stab[hval]; sp != NULL; sp = sp->next) if (!strcmp(str, string(sp))) { sp->nl++; @@ -69,15 +73,15 @@ char *str; } -freestr(s) /* free a string */ -char *s; +void +freestr(char *s) /* free a string */ { int hval; register S_HEAD *spl, *sp; if (s == NULL) return; - hval = shash(s); + hval = hash(s); for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next) if (s == string(sp)) { if (--sp->nl > 0) @@ -93,28 +97,11 @@ char *s; int -strcmp(s1, s2) /* check for s1==s2 */ -register char *s1, *s2; +shash(register char *s) { - if (s1 == s2) - return(0); - - while (*s1 == *s2++) - if (!*s1++) - return(0); - - return(*s1 - *--s2); -} - - -static int -shash(s) /* hash a string */ -register char *s; -{ register int h = 0; while (*s) - h += *s++; - - return(h % NHASH); + h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); + return(h); }