--- ray/src/common/savestr.c 1992/07/03 10:51:50 2.2 +++ ray/src/common/savestr.c 2003/02/25 02:47:22 2.6 @@ -1,9 +1,6 @@ -/* Copyright 1988 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: savestr.c,v 2.6 2003/02/25 02:47:22 greg Exp $"; #endif - /* * savestr.c - routines for efficient string storage. * @@ -12,16 +9,17 @@ 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" + #ifndef NHASH #define NHASH 509 /* hash table size (prime!) */ #endif @@ -33,7 +31,7 @@ typedef struct s_head { static S_HEAD *stab[NHASH]; -static int shash(); +#define hash(s) (shash(s)%NHASH) extern char *savestr(), *strcpy(), *malloc(); @@ -43,7 +41,7 @@ extern char *savestr(), *strcpy(), *malloc(); #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 * @@ -55,7 +53,7 @@ char *str; 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++; @@ -73,6 +71,7 @@ char *str; } +void freestr(s) /* free a string */ char *s; { @@ -81,7 +80,7 @@ char *s; 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) @@ -96,14 +95,13 @@ char *s; } -static int -shash(s) /* hash a string */ +int +shash(s) register char *s; { register int h = 0; while (*s) - h += *s++; - - return(h % NHASH); + h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); + return(h); }