--- ray/src/common/savestr.c 1993/09/23 12:15:21 2.4 +++ ray/src/common/savestr.c 2023/06/08 17:48:01 2.14 @@ -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.14 2023/06/08 17:48:01 greg Exp $"; #endif - /* * savestr.c - routines for efficient string storage. * @@ -12,18 +9,24 @@ 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 rtio.h */ +#include "copyright.h" + +#include + +#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 { @@ -35,26 +38,23 @@ static S_HEAD *stab[NHASH]; #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 * -savestr(str) /* save a string */ -char *str; +savestr(const char *str) /* save a string */ { - register int hval; - register S_HEAD *sp; + int hval; + S_HEAD *sp; if (str == NULL) return(NULL); + if (!*str) + return ""; hval = hash(str); for (sp = stab[hval]; sp != NULL; sp = sp->next) if (!strcmp(str, string(sp))) { @@ -73,13 +73,13 @@ char *str; } -freestr(s) /* free a string */ -char *s; +void +freestr(char *s) /* free a string */ { int hval; - register S_HEAD *spl, *sp; + S_HEAD *spl, *sp; - if (s == NULL) + if (s == NULL || !*s) return; hval = hash(s); for (spl = NULL, sp = stab[hval]; sp != NULL; spl = sp, sp = sp->next) @@ -97,10 +97,9 @@ char *s; int -shash(s) -register char *s; +shash(const char *s) { - register int h = 0; + int h = 0; while (*s) h = (h<<1 & 0x7fff) ^ (*s++ & 0xff);