--- ray/src/common/savestr.c 1991/11/12 16:54:33 2.1 +++ ray/src/common/savestr.c 2003/10/27 10:19:31 2.8 @@ -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.8 2003/10/27 10:19:31 schorsch Exp $"; #endif - /* * savestr.c - routines for efficient string storage. * @@ -12,16 +9,22 @@ 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" + #ifndef NHASH #define NHASH 509 /* hash table size (prime!) */ #endif @@ -33,17 +36,13 @@ typedef struct s_head { static S_HEAD *stab[NHASH]; -static int shash(); +#define hash(s) (shash(s)%NHASH) -extern char *savestr(), *strcpy(), *malloc(); - -#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 * @@ -55,7 +54,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 +72,7 @@ char *str; } +void freestr(s) /* free a string */ char *s; { @@ -81,7 +81,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) @@ -97,28 +97,12 @@ char *s; int -strcmp(s1, s2) /* check for s1==s2 */ -register char *s1, *s2; -{ - if (s1 == s2) - return(0); - - while (*s1 == *s2++) - if (!*s1++) - return(0); - - return(*s1 - *--s2); -} - - -static int -shash(s) /* hash a string */ +shash(s) register char *s; { register int h = 0; while (*s) - h += *s++; - - return(h % NHASH); + h = (h<<1 & 0x7fff) ^ (*s++ & 0xff); + return(h); }