--- ray/src/common/instance.c 1993/02/02 13:24:50 2.3 +++ ray/src/common/instance.c 2003/02/25 02:47:21 2.6 @@ -1,17 +1,16 @@ -/* Copyright (c) 1990 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: instance.c,v 2.6 2003/02/25 02:47:21 greg Exp $"; #endif - /* * instance.c - routines for octree objects. - * - * 11/10/88 */ +#include "copyright.h" + #include "standard.h" +#include "octree.h" + #include "object.h" #include "instance.h" @@ -26,22 +25,24 @@ getscene(sname, flags) /* load octree sname */ char *sname; int flags; { - extern char *libpath; char *pathname; register SCENE *sc; flags &= ~IO_ILLEGAL; /* not allowed */ for (sc = slist; sc != NULL; sc = sc->next) if (!strcmp(sname, sc->name)) { - if ((sc->ldflags & flags) == flags) + if ((sc->ldflags & flags) == flags) { + sc->nref++; return(sc); /* loaded */ + } break; /* load the rest */ } if (sc == NULL) { sc = (SCENE *)malloc(sizeof(SCENE)); if (sc == NULL) - error(SYSTEM, "out of memory in getscene"); + error(SYSTEM, "out of memory ins getscene"); sc->name = savestr(sname); + sc->nref = 1; sc->ldflags = 0; sc->scube.cutree = EMPTY; sc->scube.cuorg[0] = sc->scube.cuorg[1] = @@ -51,7 +52,7 @@ int flags; sc->next = slist; slist = sc; } - if ((pathname = getpath(sname, libpath, R_OK)) == NULL) { + if ((pathname = getpath(sname, getlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find octree file \"%s\"", sname); error(USER, errmsg); } @@ -71,35 +72,68 @@ getinstance(o, flags) /* get instance structure */ register OBJREC *o; int flags; { - register INSTANCE *in; + register INSTANCE *ins; flags &= ~IO_ILLEGAL; /* not allowed */ - if ((in = (INSTANCE *)o->os) == NULL) { - if ((in = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) - error(SYSTEM, "out of memory in getinstance"); + if ((ins = (INSTANCE *)o->os) == NULL) { + if ((ins = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) + error(SYSTEM, "out of memory ins getinstance"); if (o->oargs.nsargs < 1) objerror(o, USER, "bad # of arguments"); - if (fullxf(&in->x, o->oargs.nsargs-1, + if (fullxf(&ins->x, o->oargs.nsargs-1, o->oargs.sarg+1) != o->oargs.nsargs-1) objerror(o, USER, "bad transform"); - if (in->x.f.sca < 0.0) - in->x.f.sca = -in->x.f.sca; - if (in->x.b.sca < 0.0) - in->x.b.sca = -in->x.b.sca; - in->obj = NULL; - o->os = (char *)in; + if (ins->x.f.sca < 0.0) { + ins->x.f.sca = -ins->x.f.sca; + ins->x.b.sca = -ins->x.b.sca; + } + ins->obj = NULL; + o->os = (char *)ins; } - if (in->obj == NULL || (in->obj->ldflags & flags) != flags) - in->obj = getscene(o->oargs.sarg[0], flags); - return(in); + if (ins->obj == NULL || (ins->obj->ldflags & flags) != flags) + ins->obj = getscene(o->oargs.sarg[0], flags); + return(ins); } +void +freescene(sc) /* release a scene reference */ +SCENE *sc; +{ + SCENE shead; + register SCENE *scp; + + if (sc == NULL) + return; + if (sc->nref <= 0) + error(CONSISTENCY, "unreferenced scene in freescene"); + sc->nref--; + if (sc->nref) /* still in use? */ + return; + shead.next = slist; /* else remove from our list */ + for (scp = &shead; scp->next != NULL; scp = scp->next) + if (scp->next == sc) { + scp->next = sc->next; + sc->next = NULL; + break; + } + if (sc->next != NULL) /* can't be in list anymore */ + error(CONSISTENCY, "unlisted scene in freescene"); + slist = shead.next; + freestr(sc->name); /* free memory */ + octfree(sc->scube.cutree); + freeobjects(sc->firstobj, sc->nobjs); + free((void *)sc); +} + + +void freeinstance(o) /* free memory associated with instance */ OBJREC *o; { if (o->os == NULL) return; + freescene((*(INSTANCE *)o->os).obj); free(o->os); o->os = NULL; }