--- ray/src/hd/rholo3.c 1997/11/04 09:58:24 3.3 +++ ray/src/hd/rholo3.c 1997/11/21 17:47:56 3.9 @@ -15,10 +15,10 @@ static char SCCSid[] = "$SunId$ SGI"; #define sgn(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0) -static PACKHEAD *complist; /* list of beams to compute */ -static int complen; /* length of complist */ -static int listpos; /* current list position for next_packet */ -static int lastin = -1; /* last ordered position in list */ +static PACKHEAD *complist=NULL; /* list of beams to compute */ +static int complen=0; /* length of complist */ +static int listpos=0; /* current list position for next_packet */ +static int lastin= -1; /* last ordered position in list */ int @@ -116,19 +116,26 @@ int nents; default: error(CONSISTENCY, "bundle_set called with unknown operation"); } - n = 0; /* allocate packet holder */ - for (i = 0; i < nents; i++) - if (clist[i].nr > n) - n = clist[i].nr; - p = (PACKHEAD *)malloc(sizeof(PACKHEAD) + n*sizeof(RAYVAL)); + if (outdev == NULL) + return; + n = 8*RPACKSIZ; /* allocate packet holder */ + p = (PACKHEAD *)malloc(packsiz(n)); if (p == NULL) goto memerr; /* display what we have */ for (i = 0; i < nents; i++) if ((b = hdgetbeam(hdlist[clist[i].hd], clist[i].bi)) != NULL) { - bcopy((char *)hdbray(b), (char *)(p+1), + if (b->nrm > n) { + n = b->nrm; + p = (PACKHEAD *)realloc((char *)p, packsiz(n)); + if (p == NULL) + goto memerr; + } + bcopy((char *)hdbray(b), (char *)packra(p), (p->nr=b->nrm)*sizeof(RAYVAL)); - disp_packet((PACKET *)p); + p->hd = clist[i].hd; + p->bi = clist[i].bi; + disp_packet(p); } free((char *)p); /* clean up */ return; @@ -236,7 +243,7 @@ init_global() /* initialize global ray computation * while (k--) complist[k].nr = frac * complist[k].nr; } - listpos = 0; lastin = -1; + listpos = 0; lastin = -1; /* flag initial sort */ } @@ -272,7 +279,7 @@ sortcomplist() /* fix our list order */ done_packets(flush_queue()); if (complen <= 0) /* check to see if there is even a list */ return; - if (lastin < 0) /* flag to sort entire list */ + if (lastin < 0 || listpos*4 >= complen*3) qsort((char *)complist, complen, sizeof(PACKHEAD), beamcmp); else if (listpos) { /* else sort and merge sublist */ list2 = (PACKHEAD *)malloc(listpos*sizeof(PACKHEAD)); @@ -294,7 +301,11 @@ sortcomplist() /* fix our list order */ for (i = complen; i-- && complist[i].nr <= bnrays(hdlist[complist[i].hd],complist[i].bi); ) ; - if (i < complen-1) { + if (i < 0) { + free((char *)complist); + complist = NULL; + complen = 0; + } else if (i < complen-1) { list2 = (PACKHEAD *)realloc((char *)complist, (i+1)*sizeof(PACKHEAD)); if (list2 != NULL) {