--- ray/src/rt/data.c 1991/12/19 14:54:58 2.2 +++ ray/src/rt/data.c 1994/04/14 04:50:27 2.8 @@ -1,4 +1,4 @@ -/* Copyright (c) 1991 Regents of the University of California */ +/* Copyright (c) 1993 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -18,14 +18,29 @@ static char SCCSid[] = "$SunId$ LBL"; #include "data.h" + /* picture memory usage before warning */ +#ifndef PSIZWARN +#ifdef BIGMEM +#define PSIZWARN 3000000 +#else +#define PSIZWARN 1000000 +#endif +#endif +#ifndef TABSIZ +#define TABSIZ 97 /* table size (prime) */ +#endif + +#define hash(s) (shash(s)%TABSIZ) + + extern char *fgetword(); -extern char *libpath; /* library search path */ +extern char *getlibpath(); /* library search path */ -static DATARRAY *dlist = NULL; /* data array list */ +static DATARRAY *dtab[TABSIZ]; /* data array list */ -static DATARRAY *plist = NULL; /* picture list */ +static DATARRAY *ptab[TABSIZ]; /* picture list */ DATARRAY * @@ -39,13 +54,13 @@ char *dname; register int i, j; register DATARRAY *dp; /* look for array in list */ - for (dp = dlist; dp != NULL; dp = dp->next) + for (dp = dtab[hash(dname)]; dp != NULL; dp = dp->next) if (!strcmp(dname, dp->name)) return(dp); /* found! */ /* * If we haven't loaded the data already, we will look - * for it in the directorys specified by the library path. + * for it in the directories specified by the library path. * * The file has the following format: * @@ -63,7 +78,7 @@ char *dname; * 0 0 ni p0i p1i .. pni */ - if ((dfname = getpath(dname, libpath, R_OK)) == NULL) { + if ((dfname = getpath(dname, getlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find data file \"%s\"", dname); error(USER, errmsg); } @@ -127,8 +142,9 @@ char *dname; dp->arr[i] = atof(word); } fclose(fp); - dp->next = dlist; - return(dlist = dp); + i = hash(dname); + dp->next = dtab[i]; + return(dtab[i] = dp); memerr: error(SYSTEM, "out of memory in getdata"); @@ -139,14 +155,13 @@ scanerr: } -static double inpaspect; /* aspect ratio of input picture */ - static -headaspect(s) /* check string for aspect ratio */ +headaspect(s, iap) /* check string for aspect ratio */ char *s; +double *iap; { if (isaspect(s)) - inpaspect *= aspectval(s); + *iap *= aspectval(s); } @@ -154,22 +169,22 @@ DATARRAY * getpict(pname) /* get picture pname */ char *pname; { - extern char *libpath; + double inpaspect; char *pfname; FILE *fp; COLOR *scanin; int sl, ns; - RESOLU inpres; + RESOLU inpres; FLOAT loc[2]; int y; register int x, i; register DATARRAY *pp; /* look for array in list */ - for (pp = plist; pp != NULL; pp = pp->next) + for (pp = ptab[hash(pname)]; pp != NULL; pp = pp->next) if (!strcmp(pname, pp->name)) return(pp); /* found! */ - if ((pfname = getpath(pname, libpath, R_OK)) == NULL) { + if ((pfname = getpath(pname, getlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find picture file \"%s\"", pname); error(USER, errmsg); } @@ -184,11 +199,23 @@ char *pname; sprintf(errmsg, "cannot open picture file \"%s\"", pfname); error(SYSTEM, errmsg); } +#ifdef MSDOS + setmode(fileno(fp), O_BINARY); +#endif /* get dimensions */ inpaspect = 1.0; - getheader(fp, headaspect); + getheader(fp, headaspect, &inpaspect); if (!fgetsresolu(&inpres, fp)) goto readerr; +#if PSIZWARN + /* check memory usage */ + i = 3*sizeof(DATATYPE)*inpres.xr*inpres.yr; + if (i > PSIZWARN) { + sprintf(errmsg, "picture file \"%s\" using %d bytes of memory", + pname, i); + error(WARNING, errmsg); + } +#endif for (i = 0; i < 3; i++) { pp[i].nd = 2; pp[i].dim[0].ne = inpres.yr; @@ -229,10 +256,11 @@ char *pname; } free((char *)scanin); fclose(fp); + i = hash(pname); pp[0].next = pp[1].next = - pp[2].next = plist; - return(plist = pp); + pp[2].next = ptab[i]; + return(ptab[i] = pp); memerr: error(SYSTEM, "out of memory in getpict"); @@ -245,57 +273,75 @@ readerr: freedata(dname) /* free memory associated with dname */ char *dname; { + DATARRAY head; + int hval, nents; register DATARRAY *dp, *dpl; register int i; - for (dpl = NULL, dp = dlist; dp != NULL; dpl = dp, dp = dp->next) - if (!strcmp(dname, dp->name)) { - if (dpl == NULL) - dlist = dp->next; - else + if (dname == NULL) { /* free all if NULL */ + hval = 0; nents = TABSIZ; + } else { + hval = hash(dname); nents = 1; + } + while (nents--) { + head.next = dtab[hval]; + dpl = &head; + while ((dp = dpl->next) != NULL) + if (dname == NULL || !strcmp(dname, dp->name)) { dpl->next = dp->next; - free((char *)dp->arr); - for (i = 0; i < dp->nd; i++) - if (dp->dim[i].p != NULL) - free((char *)dp->dim[i].p); - freestr(dp->name); - free((char *)dp); - return; - } + free((char *)dp->arr); + for (i = 0; i < dp->nd; i++) + if (dp->dim[i].p != NULL) + free((char *)dp->dim[i].p); + freestr(dp->name); + free((char *)dp); + } else + dpl = dp; + dtab[hval++] = head.next; + } } freepict(pname) /* free memory associated with pname */ char *pname; { + DATARRAY head; + int hval, nents; register DATARRAY *pp, *ppl; - for (ppl = NULL, pp = plist; pp != NULL; ppl = pp, pp = pp->next) - if (!strcmp(pname, pp->name)) { - if (ppl == NULL) - plist = pp->next; - else + if (pname == NULL) { /* free all if NULL */ + hval = 0; nents = TABSIZ; + } else { + hval = hash(pname); nents = 1; + } + while (nents--) { + head.next = ptab[hval]; + ppl = &head; + while ((pp = ppl->next) != NULL) + if (pname == NULL || !strcmp(pname, pp->name)) { ppl->next = pp->next; - free((char *)pp[0].arr); - free((char *)pp[1].arr); - free((char *)pp[2].arr); - freestr(pp[0].name); - free((char *)pp); - return; - } + free((char *)pp[0].arr); + free((char *)pp[1].arr); + free((char *)pp[2].arr); + freestr(pp[0].name); + free((char *)pp); + } else + ppl = pp; + ptab[hval++] = head.next; + } } double datavalue(dp, pt) /* interpolate data value at a point */ register DATARRAY *dp; -double *pt; +double *pt; { DATARRAY sd; int asize; int lower, upper; register int i; - double x, y, y0, y1; + double x, y, y0, y1; /* set up dimensions for recursion */ sd.nd = dp->nd - 1; asize = 1;