This commit was manufactured by cvs2svn to create tag 'buildscript'.
[IRC.git] /
1 #ifdef TASK
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include "mem.h"
5 #include "checkpoint.h"
6 #include "Queue.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
11 #include <sys/mman.h>
12 #include <string.h>
13 #include <signal.h>
14
15 extern int injectfailures;
16 extern float failurechance;
17 extern int debugtask;
18 extern int instaccum;
19
20 #ifdef CONSCHECK
21 #include "instrument.h"
22 #endif
23
24 struct genhashtable * activetasks;
25 struct parameterwrapper * objectqueues[NUMCLASSES];
26 struct genhashtable * failedtasks;
27 struct taskparamdescriptor * currtpd;
28 struct RuntimeHash * forward;
29 struct RuntimeHash * reverse;
30
31 int main(int argc, char **argv) {
32 #ifdef BOEHM_GC
33   GC_init(); // Initialize the garbage collector
34 #endif
35 #ifdef CONSCHECK
36   initializemmap();
37 #endif
38   processOptions();
39   initializeexithandler();
40   /* Create table for failed tasks */
41   failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
42                                    (int (*)(void *,void *)) &comparetpd);
43   /* Create queue of active tasks */
44   activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
45                                    (int (*)(void *,void *)) &comparetpd);
46   
47   /* Process task information */
48   processtasks();
49
50   /* Create startup object */
51   createstartupobject(argc, argv);
52
53   /* Start executing the tasks */
54   executetasks();
55 }
56
57 void createstartupobject(int argc, char ** argv) {
58   int i;
59   
60   /* Allocate startup object     */
61 #ifdef PRECISE_GC
62   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
63   struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1); 
64 #else
65   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
66   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1); 
67 #endif
68   /* Build array of strings */
69   startupobject->___parameters___=stringarray;
70   for(i=1;i<argc;i++) {
71     int length=strlen(argv[i]);
72 #ifdef PRECISE_GC
73     struct ___String___ *newstring=NewString(NULL, argv[i],length);
74 #else
75     struct ___String___ *newstring=NewString(argv[i],length);
76 #endif
77     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
78   }
79   
80   /* Set initialized flag for startup object */
81   flagorand(startupobject,1,0xFFFFFFFF);
82 }
83
84 int hashCodetpd(struct taskparamdescriptor *ftd) {
85   int hash=(int)ftd->task;
86   int i;
87   for(i=0;i<ftd->numParameters;i++){ 
88     hash^=(int)ftd->parameterArray[i];
89   }
90   return hash;
91 }
92
93 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
94   int i;
95   if (ftd1->task!=ftd2->task)
96     return 0;
97   for(i=0;i<ftd1->numParameters;i++)
98     if(ftd1->parameterArray[i]!=ftd2->parameterArray[i])
99       return 0;
100 #ifdef OPTIONAL
101   for(i=0;i<ftd1->numParameters;i++) {
102     if(ftd1->failed[i]!=ftd2->failed[i])
103       return 0;
104   }
105 #endif
106   return 1;
107 }
108
109 /* This function sets a tag. */
110 #ifdef PRECISE_GC
111 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
112 #else
113 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
114 #endif
115   struct ___Object___ * tagptr=obj->___tags___;
116   if (tagptr==NULL) {
117     obj->___tags___=(struct ___Object___ *)tagd;
118   } else {
119     /* Have to check if it is already set */
120     if (tagptr->type==TAGTYPE) {
121       struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
122       if (td==tagd)
123         return;
124 #ifdef PRECISE_GC
125       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
126       struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
127       obj=(struct ___Object___ *)ptrarray[2];
128       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
129       td=(struct ___TagDescriptor___ *) obj->___tags___;
130 #else
131       struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
132 #endif
133       ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
134       ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
135       obj->___tags___=(struct ___Object___ *) ao;
136       ao->___cachedCode___=2;
137     } else {
138       /* Array Case */
139       int i;
140       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
141       for(i=0;i<ao->___cachedCode___;i++) {
142         struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
143         if (td==tagd)
144           return;
145       }
146       if (ao->___cachedCode___<ao->___length___) {
147         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
148         ao->___cachedCode___++;
149       } else {
150 #ifdef PRECISE_GC
151         int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
152         struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
153         obj=(struct ___Object___ *)ptrarray[2];
154         tagd=(struct ___TagDescriptor___ *) ptrarray[3];
155         ao=(struct ArrayObject *)obj->___tags___;
156 #else
157         struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
158 #endif
159         aonew->___cachedCode___=ao->___length___+1;
160         for(i=0;i<ao->___length___;i++) {
161           ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
162         }
163         ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
164       }
165     }
166   }
167
168   {
169     struct ___Object___ * tagset=tagd->flagptr;
170     if(tagset==NULL) {
171       tagd->flagptr=obj;
172     } else if (tagset->type!=OBJECTARRAYTYPE) {
173 #ifdef PRECISE_GC
174       int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
175       struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
176       obj=(struct ___Object___ *)ptrarray[2];
177       tagd=(struct ___TagDescriptor___ *)ptrarray[3];
178 #else
179       struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
180 #endif
181       ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
182       ARRAYSET(ao, struct ___Object___ *, 1, obj);
183       ao->___cachedCode___=2;
184       tagd->flagptr=(struct ___Object___ *)ao;
185     } else {
186       struct ArrayObject *ao=(struct ArrayObject *) tagset;
187       if (ao->___cachedCode___<ao->___length___) {
188         ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
189       } else {
190         int i;
191 #ifdef PRECISE_GC
192         int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
193         struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
194         obj=(struct ___Object___ *)ptrarray[2];
195         tagd=(struct ___TagDescriptor___ *)ptrarray[3];
196         ao=(struct ArrayObject *)tagd->flagptr;
197 #else
198         struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
199 #endif
200         aonew->___cachedCode___=ao->___cachedCode___+1;
201         for(i=0;i<ao->___length___;i++) {
202           ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
203         }
204         ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
205         tagd->flagptr=(struct ___Object___ *) aonew;
206       }
207     }
208   }
209 }
210
211 /* This function clears a tag. */
212 #ifdef PRECISE_GC
213 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
214 #else
215 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
216 #endif
217   /* We'll assume that tag is alway there.
218      Need to statically check for this of course. */
219   struct ___Object___ * tagptr=obj->___tags___;
220
221   if (tagptr->type==TAGTYPE) {
222     if ((struct ___TagDescriptor___ *)tagptr==tagd)
223       obj->___tags___=NULL;
224     else
225       printf("ERROR 1 in tagclear\n");
226   } else {
227     struct ArrayObject *ao=(struct ArrayObject *) tagptr;
228     int i;
229     for(i=0;i<ao->___cachedCode___;i++) {
230       struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
231       if (td==tagd) {
232         ao->___cachedCode___--;
233         if (i<ao->___cachedCode___)
234           ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
235         ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
236         if (ao->___cachedCode___==0)
237           obj->___tags___=NULL;
238         goto PROCESSCLEAR;
239       }
240     }
241     printf("ERROR 2 in tagclear\n");
242   }
243  PROCESSCLEAR:
244   {
245     struct ___Object___ *tagset=tagd->flagptr;
246     if (tagset->type!=OBJECTARRAYTYPE) {
247       if (tagset==obj)
248         tagd->flagptr=NULL;
249       else
250         printf("ERROR 3 in tagclear\n");
251     } else {
252       struct ArrayObject *ao=(struct ArrayObject *) tagset;
253       int i;
254       for(i=0;i<ao->___cachedCode___;i++) {
255         struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
256         if (tobj==obj) {
257           ao->___cachedCode___--;
258           if (i<ao->___cachedCode___)
259             ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
260           ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
261           if (ao->___cachedCode___==0)
262             tagd->flagptr=NULL;
263           goto ENDCLEAR;
264         }
265       }
266       printf("ERROR 4 in tagclear\n");
267     }
268   }
269  ENDCLEAR:
270   return;
271 }
272  
273 /* This function allocates a new tag. */
274 #ifdef PRECISE_GC
275 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
276   struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
277 #else
278 struct ___TagDescriptor___ * allocate_tag(int index) {
279   struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
280 #endif
281   v->type=TAGTYPE;
282   v->flag=index;
283   return v;
284
285
286
287
288 /* This function updates the flag for object ptr.  It or's the flag
289    with the or mask and and's it with the andmask. */
290
291 void flagbody(struct ___Object___ *ptr, int flag);
292 #ifdef OPTIONAL
293 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index);
294 #endif
295  
296  int flagcomp(const int *val1, const int *val2) {
297    return (*val1)-(*val2);
298  } 
299
300 void flagorand(void * ptr, int ormask, int andmask) {
301 #ifdef OPTIONAL
302   struct ___Object___ * obj = (struct ___Object___ *)ptr;
303   if(obj->numfses){/*store the information about fses*/
304     int flag, i, j,counter, offset=0;
305     for(i=0;i<obj->numfses;i++) {
306       int oldoffset;
307       counter=obj->fses[offset++];
308       oldoffset=offset;
309       for(j=0;j<counter;j++) {
310         flag=obj->fses[offset];
311         obj->fses[offset++]=(flag|ormask)&andmask;
312       }
313       qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
314     }
315     enqueueoptional(obj, 0, NULL, NULL, 0);
316   }
317   else
318 #endif
319     {
320       int oldflag=((int *)ptr)[1];
321       int flag=ormask|oldflag;
322       flag&=andmask;
323       flagbody(ptr, flag);
324     }
325 }
326  
327 void intflagorand(void * ptr, int ormask, int andmask) {
328 #ifdef OPTIONAL
329   struct ___Object___ * obj = (struct ___Object___ *)ptr;
330   if(obj->numfses) {/*store the information about fses*/
331     int flag, i, j,counter, offset=0;
332     for(i=0;i<obj->numfses;i++) {
333       int oldoffset;
334       counter=obj->fses[offset++];
335       oldoffset=offset;
336       for(j=0;j<counter;j++) {
337         flag=obj->fses[offset];
338         obj->fses[offset++]=(flag|ormask)&andmask;
339       }
340       qsort(&obj->fses[oldoffset], sizeof(int), counter, (int (*)(const void *, const void *)) &flagcomp);
341     }
342     enqueueoptional(obj, 0, NULL, NULL, 0);
343   }
344   else
345 #endif
346     {
347       int oldflag=((int *)ptr)[1];
348       int flag=ormask|oldflag;
349       flag&=andmask;
350       if (flag==oldflag) /* Don't do anything */
351         return;
352       else flagbody(ptr, flag);
353     }
354 }
355
356 void flagorandinit(void * ptr, int ormask, int andmask) {
357   int oldflag=((int *)ptr)[1];
358   int flag=ormask|oldflag;
359   flag&=andmask;
360   flagbody(ptr,flag);
361 }
362
363 void flagbody(struct ___Object___ *ptr, int flag) {
364   struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
365   ptr->flag=flag;
366   
367   /*Remove object from all queues */
368   while(flagptr!=NULL) {
369     struct parameterwrapper *next;
370     int UNUSED, UNUSED2;
371     int * enterflags;
372     ObjectHashget(flagptr->objectset, (int) ptr, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
373     ObjectHashremove(flagptr->objectset, (int)ptr);
374     if (enterflags!=NULL)
375       free(enterflags);
376     flagptr=next;
377   }
378   
379   {
380     struct QueueItem *tmpptr;
381     struct parameterwrapper * parameter=objectqueues[ptr->type];
382     int i;
383     struct parameterwrapper * prevptr=NULL;
384     struct ___Object___ *tagptr=ptr->___tags___;
385     
386     /* Outer loop iterates through all parameter queues an object of
387        this type could be in.  */
388     
389     while(parameter!=NULL) {
390       /* Check tags */
391       if (parameter->numbertags>0) {
392         if (tagptr==NULL)
393           goto nextloop;//that means the object has no tag but that param needs tag
394         else if(tagptr->type==TAGTYPE) {//one tag
395           struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
396           for(i=0;i<parameter->numbertags;i++) {
397             //slotid is parameter->tagarray[2*i];
398             int tagid=parameter->tagarray[2*i+1];
399             if (tagid!=tagptr->flag)
400               goto nextloop; /*We don't have this tag */          
401            }
402         } else {//multiple tags
403           struct ArrayObject * ao=(struct ArrayObject *) tagptr;
404           for(i=0;i<parameter->numbertags;i++) {
405             //slotid is parameter->tagarray[2*i];
406             int tagid=parameter->tagarray[2*i+1];
407             int j;
408             for(j=0;j<ao->___cachedCode___;j++) {
409               if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, j)->flag)
410                 goto foundtag;
411             }
412             goto nextloop;
413           foundtag:
414             ;
415           }
416         }
417       }
418       
419       /* Check flags */
420       for(i=0;i<parameter->numberofterms;i++) {
421         int andmask=parameter->intarray[i*2];
422         int checkmask=parameter->intarray[i*2+1];
423         if ((flag&andmask)==checkmask) {
424           enqueuetasks(parameter, prevptr, ptr, NULL, 0);
425           prevptr=parameter;
426           break;
427         }
428       }
429     nextloop:
430       parameter=parameter->next;
431     }
432     ptr->flagptr=prevptr;
433   }
434 }
435  
436 #ifdef OPTIONAL
437
438 int checktags(struct ___Object___ * currobj, struct fsanalysiswrapper * fswrapper) {
439   /* Check Tags */
440   struct ___Object___ * tagptr = currobj->___tags___;
441   if(fswrapper->numtags>0){
442     if (tagptr==NULL)
443       return 0; //that means the object has no tag but that param
444     //needs tag
445     else if(tagptr->type==TAGTYPE) {//one tag
446       if(fswrapper->numtags!=1) 
447         return 0; //we don't have the right number of tags
448       struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
449       if (fswrapper->tags[0]!=tagptr->flag)
450         return 0;
451     } else {  //multiple tags
452       struct ArrayObject * ao=(struct ArrayObject *) tagptr;
453       int tag_counter=0;
454       int foundtag=0;
455       
456       if(ao->___length___!=fswrapper->numtags) 
457         return 0;//we don't have the right number of tags
458       for(tag_counter=0;tag_counter<fswrapper->numtags;tag_counter++) {
459         int tagid=fswrapper->tags[tag_counter];
460         int j;
461         for(j=0;j<ao->___cachedCode___;j++) {
462           if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, tag_counter)->flag)
463             return 1;
464         }
465         return 0;
466       }
467     }
468   }
469   return 1;
470 }
471
472 int getlength(int *flist, int len) {
473   int count=0;
474   int i;
475   for(i=0;i<len;i++) {
476     int size=flist[count];
477     count+=1+size;
478   }
479   return count;
480 }
481
482 int * domergeor(int *flist1, int len1, int *flist2, int len2) {
483   int size1=getlength(flist1, len1);
484   int size2=getlength(flist2, len2);
485   int *merge=RUNMALLOC((size1+size2)*sizeof(int));
486   memcpy(merge, flist1, size1*sizeof(int));
487   memcpy(&merge[size1], flist2, size2*sizeof(int));
488   return merge;
489 }
490
491 int domerge(int * flist1, int len1, int *flist2, int len2, int *merge) {
492   int count=0;
493   int i=0;
494   int j=0;
495   while(i<len1||j<len2) {
496     if (i<len1&&(j==len2||flist1[i]<flist2[j])) {
497       if(merge!=NULL) {
498         merge[count]=flist1[i];
499       }
500       i++;
501       count++;
502     } else if (j<len2&&(i==len1||flist2[j]<flist1[i])) {
503       if(merge!=NULL) {
504         merge[count]=flist2[j];
505       }
506       j++;
507       count++;
508     } else if (i<len1&&j<len2&&flist1[i]==flist2[j]) {
509       if(merge!=NULL) {
510         merge[count]=flist1[i];
511       }
512       i++;
513       j++;
514       count++;
515     }
516   }
517   return count;
518 }
519
520 /* Merge flags from ftlmerge into ftl. */
521 void mergeitems(struct failedtasklist *ftl, struct failedtasklist *ftlmerge) {
522   int length=0;
523   int i,j;
524   int *mergedlist;
525   int offset=0;
526   for(i=0;i<ftl->numflags;i++) {
527     int len=ftl->flags[offset++];
528     int offsetmerge=0;
529     for(j=0;j<ftlmerge->numflags;j++) {
530       int lenmerge=ftlmerge->flags[offsetmerge++];
531       length+=1+domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge, NULL);
532       offsetmerge+=lenmerge;
533     }
534     offset+=len;
535   }
536   mergedlist=RUNMALLOC(sizeof(int)*length);
537   
538   offset=0;
539   length=0;
540   for(i=0;i<ftl->numflags;i++) {
541     int len=ftl->flags[offset++];
542     int offsetmerge=0;
543     for(j=0;j<ftlmerge->numflags;j++) {
544       int lenmerge=ftlmerge->flags[offsetmerge++];
545       int size=domerge(&ftl->flags[offset],len,&ftlmerge->flags[offsetmerge],lenmerge,&mergedlist[length+1]);
546       mergedlist[length]=size;
547       length+=size+1;
548     }
549   }
550   RUNFREE(ftl->flags);
551   ftl->flags=mergedlist;
552   ftl->numflags*=ftlmerge->numflags;
553 }
554
555 void mergefailedlists(struct failedtasklist **andlist, struct failedtasklist *list) {
556   struct failedtasklist *tmpptr;
557   while((*andlist)!=NULL) {
558     struct failedtasklist *searchftl=list;
559     while(searchftl!=NULL) {
560       if ((*andlist)->task==searchftl->task&&
561           (*andlist)->index==searchftl->index) {
562         mergeitems(*andlist, searchftl);
563         break;
564       }
565       searchftl=searchftl->next;
566     }
567     if (searchftl==NULL) {
568       //didn't find andlist
569       tmpptr=*andlist;
570       *andlist=(*andlist)->next;//splice item out of list
571       RUNFREE(tmpptr->flags); //free the item
572       RUNFREE(tmpptr);
573     } else {
574       andlist=&((*andlist)->next); //iterate to next item
575     }
576   }
577   //free the list we're searching
578   while(list!=NULL) {
579     tmpptr=list->next;
580     RUNFREE(list->flags);
581     RUNFREE(list);
582     list=tmpptr;
583   }
584 }
585
586 struct failedtasklist * processfailstate(struct classanalysiswrapper * classwrapper, struct taskdescriptor *task, int index, struct ___Object___ * currobj, int flagstate) {
587   struct failedtasklist *list=NULL;
588   int i,h;
589   struct fsanalysiswrapper *fswrapper=NULL;
590   for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
591     struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
592     if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
593       //we only match exactly here
594       fswrapper=tmp;
595       break;
596     }
597   }
598   if (fswrapper==NULL)
599     return list;
600   for(i=0;i<fswrapper->numtaskfailures;i++) {
601     int j;
602     struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
603     if (taskfail->task==task&&taskfail->index==index) {
604       int start=0;
605       while(start<taskfail->numoptionaltaskdescriptors) {
606         struct taskdescriptor *currtask=NULL;
607         struct failedtasklist *tmpftl;
608         int currindex;
609         int totallength=0;
610         int *enterflags;
611         int numenterflags, offset;
612         struct parameterwrapper *pw;
613         for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
614           struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
615           if(currtask==NULL) {
616             currtask=otd->task;
617             currindex=otd->index;
618           } else if (currtask!=otd->task||currindex!=otd->index)
619             break;
620           totallength+=otd->numenterflags;
621         }
622         pw=currtask->descriptorarray[currindex]->queue;
623         enterflags=RUNMALLOC(totallength*sizeof(int));
624         numenterflags=j-start;
625         offset=0;
626         for(start;start<j;start++) {
627           struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
628           enterflags[offset++]=otd->numenterflags;
629           memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
630           offset+=otd->numenterflags;
631         }
632         tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
633         tmpftl->next=list;
634         tmpftl->task=currtask;
635         tmpftl->numflags=numenterflags;
636         tmpftl->flags=enterflags;
637         list=tmpftl;
638       }
639     }
640   }
641   return list;
642 }
643
644 struct failedtasklist * processnormfailstate(struct classanalysiswrapper * classwrapper, struct ___Object___ * currobj, int flagstate) {
645   struct failedtasklist *list=NULL;
646   int i,h;
647   int start=0;
648   struct fsanalysiswrapper *fswrapper=NULL;
649   for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
650     struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
651     if (tmp->flags==flagstate&&checktags(currobj, tmp)) {
652       //we only match exactly here
653       fswrapper=tmp;
654       break;
655     }
656   }
657   if(fswrapper==NULL)
658     return NULL;
659
660   while(start<fswrapper->numoptionaltaskdescriptors) {
661     struct taskdescriptor *currtask=NULL;
662     struct failedtasklist *tmpftl;
663     int j;
664     int currindex;
665     int totallength=0;
666     int *enterflags;
667     int numenterflags, offset;
668     struct parameterwrapper *pw;
669     for(j=start;j<fswrapper->numoptionaltaskdescriptors;j++) {
670       struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[j];
671       if(currtask==NULL) {
672         currtask=otd->task;
673         currindex=otd->index;
674       } else if (currtask!=otd->task||currindex!=otd->index)
675         break;
676       totallength+=otd->numenterflags;
677     }
678     pw=currtask->descriptorarray[currindex]->queue;
679     enterflags=RUNMALLOC(totallength*sizeof(int));
680     numenterflags=j-start;
681     offset=0;
682     for(start;start<j;start++) {
683       struct optionaltaskdescriptor *otd=fswrapper->optionaltaskdescriptorarray[start];
684       enterflags[offset++]=otd->numenterflags;
685       memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
686       offset+=otd->numenterflags;
687     }
688     tmpftl=RUNMALLOC(sizeof(struct failedtasklist));
689     tmpftl->next=list;
690     tmpftl->task=currtask;
691     tmpftl->numflags=numenterflags;
692     tmpftl->flags=enterflags;
693     list=tmpftl;
694   }
695   return list;
696 }
697
698
699
700 void enqueuelist(struct ___Object___ * currobj, struct failedtasklist * andlist) {
701   while(andlist!=NULL) {
702     struct failedtasklist *tmp=andlist;
703     struct parameterwrapper *pw=andlist->task->descriptorarray[andlist->index]->queue;
704     struct parmaeterwrapper *next;
705     int * flags;
706     int numflags;
707     int isnonfailed;
708     
709     if (enqueuetasks(pw, currobj->flagptr, currobj, tmp->flags, tmp->numflags))
710       currobj->flagptr=pw;
711     
712     andlist=andlist->next;
713     RUNFREE(tmp);
714   }
715 }
716
717 void enqueueoptional(struct ___Object___ * currobj, int numfailedfses, int * failedfses, struct taskdescriptor * task, int index) {
718   struct classanalysiswrapper * classwrapper=NULL; 
719   
720   /*test what optionaltaskdescriptors are available, find the class
721     corresponding*/
722   if (classanalysiswrapperarray[currobj->type]!=NULL) {
723     classwrapper = classanalysiswrapperarray[currobj->type];
724   } else
725     return;
726   
727   if(task!=NULL) { 
728     /* We have a failure */
729     if (failedfses==NULL) {
730       /* Failed in normal state */
731       /*first time the method is invoked*/
732       int i,h;
733       struct fsanalysiswrapper *fswrapper=NULL;
734
735       for(h=0;h<classwrapper->numfsanalysiswrappers;h++) {
736         struct fsanalysiswrapper * tmp=classwrapper->fsanalysiswrapperarray[h];
737         if (tmp->flags==currobj->flag&&checktags(currobj, tmp)) {
738           //we only match exactly here
739           fswrapper=tmp;
740           break;
741         }
742       }
743       if(fswrapper==NULL) //nothing to do in this state
744         return;
745       for(i=0;i<fswrapper->numtaskfailures;i++) {
746         int j;
747         struct taskfailure * taskfail=fswrapper->taskfailurearray[i];
748         if (taskfail->task==task&&taskfail->index==index) {
749           int start=0;
750           while(start<taskfail->numoptionaltaskdescriptors) {
751             struct taskdescriptor *currtask=NULL;
752             int currindex;
753             int totallength=0;
754             int *enterflags;
755             int numenterflags, offset;
756             struct parameterwrapper *pw;
757             for(j=start;j<taskfail->numoptionaltaskdescriptors;j++) {
758               struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[j];
759               if(currtask==NULL) {
760                 currtask=otd->task;
761                 currindex=otd->index;
762               } else if (currtask!=otd->task||currindex!=otd->index)
763                 break;
764               totallength+=otd->numenterflags;//1 is to store the lengths
765             }
766             pw=currtask->descriptorarray[currindex]->queue;
767             enterflags=RUNMALLOC((totallength+numenterflags)*sizeof(int));
768             numenterflags=j-start;
769
770             offset=0;
771             for(start;start<j;start++) {
772               struct optionaltaskdescriptor *otd=taskfail->optionaltaskdescriptorarray[start];
773               enterflags[offset++]=otd->numenterflags;
774               memcpy(&enterflags[offset], otd->enterflags, otd->numenterflags*sizeof(int));
775               offset+=otd->numenterflags;
776             }
777             //Enqueue this one
778             if (enqueuetasks(pw, currobj->flagptr, currobj, enterflags, numenterflags))
779               currobj->flagptr=pw;
780           }
781         }
782       }
783     } else {
784       /* Failed in failed state */
785       int i;
786       int offset=0;
787       for(i=0;i<numfailedfses;i++) {
788         int numfses=failedfses[offset++];
789         int j;
790         struct failedtasklist *andlist=NULL;
791         for(j=0;j<numfses;j++) {
792           int flagstate=failedfses[offset++];
793           struct failedtasklist *currlist=processfailstate(classwrapper, task, index, currobj, flagstate);
794           if (andlist==NULL)
795             andlist=currlist;
796           else
797             mergefailedlists(&andlist, currlist);
798         }
799         enqueuelist(currobj, andlist);
800       }
801     }
802   } else {
803     /* No failure, but we are in a failed state */
804     struct parameterwrapper *flagptr=(struct parameterwrapper *)currobj->flagptr;
805
806     /*Remove object from all queues */
807     while(flagptr!=NULL) {
808       struct parameterwrapper *next;
809       int UNUSED, UNUSED2;
810       int * enterflags;
811       ObjectHashget(flagptr->objectset, (int) currobj, (int *) &next, (int *) &enterflags, &UNUSED, &UNUSED2);
812       ObjectHashremove(flagptr->objectset, (int)currobj);
813       if (enterflags!=NULL)
814         free(enterflags);
815       flagptr=next;
816     }
817
818     /* Failed in failed state */
819     int i;
820     int offset=0;
821     for(i=0;i<currobj->numfses;i++) {
822       int numfses=currobj->fses[offset++];
823       int j;
824       struct failedtasklist *andlist=NULL;
825       for(j=0;j<numfses;j++) {
826         int flagstate=currobj->fses[offset++];
827         struct failedtasklist *currlist=processnormfailstate(classwrapper, currobj, flagstate);
828         if (andlist==NULL)
829           andlist=currlist;
830         else
831           mergefailedlists(&andlist, currlist);
832       }
833       enqueuelist(currobj, andlist);
834     }
835   }
836
837  
838  
839 #endif
840  
841 int enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr, int * enterflags, int numenterflags) {
842   void * taskpointerarray[MAXTASKPARAMS];
843 #ifdef OPTIONAL
844   int failed[MAXTASKPARAMS];
845 #endif
846   int j;
847   int numparams=parameter->task->numParameters;
848   int numiterators=parameter->task->numTotal-1;
849   int retval=1;
850   int addnormal=1;
851   int adderror=1;
852
853   struct taskdescriptor * task=parameter->task;
854
855 #ifdef OPTIONAL  
856   if (ObjectHashcontainskey(parameter->objectset, (int) ptr)) {
857     /* The object is already here...or it with the existing item */
858     int * oldflags;
859     int oldnumflags;
860     int oldptr;
861     int oldstatus;
862     int *mergedflags;
863     ObjectHashget(parameter->objectset, (int) ptr, & oldptr, (int *) &oldflags, &oldnumflags, &oldstatus);
864     mergedflags=domergeor(oldflags, oldnumflags, enterflags, numenterflags);
865     ObjectHashupdate(parameter->objectset, (int) ptr, oldptr, mergedflags, oldnumflags+numenterflags, oldstatus||(enterflags==NULL));
866
867     RUNFREE(oldflags);
868     RUNFREE(enterflags);
869
870     //only add if truly needed
871     if (oldstatus)
872       addnormal=0;
873     if (oldnumflags>0)
874       adderror=0;
875
876     retval=0;
877   } else {
878 #endif
879     ObjectHashadd(parameter->objectset, (int) ptr, (int) prevptr, (int) enterflags, numenterflags, enterflags==NULL);//this add the object to parameterwrapper
880 #ifdef OPTIONAL
881   }
882 #endif
883  
884   /* Add enqueued object to parameter vector */
885   taskpointerarray[parameter->slot]=ptr;
886 #ifdef OPTIONAL
887   failed[parameter->slot]=(enterflags!=NULL);
888 #endif
889
890   /* Reset iterators */
891   for(j=0;j<numiterators;j++) {
892     toiReset(&parameter->iterators[j]);
893   }
894
895   /* Find initial state */
896   for(j=0;j<numiterators;j++) {
897   backtrackinit:
898     if(toiHasNext(&parameter->iterators[j], taskpointerarray OPTARG(failed)))
899       toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
900     else if (j>0) {
901       /* Need to backtrack */
902       toiReset(&parameter->iterators[j]);
903       j--;
904       goto backtrackinit;
905     } else {
906       /* Nothing to enqueue */
907       return retval;
908     }
909   }
910
911   
912   while(1) {
913     /* Enqueue current state */
914     int launch = 0;
915     struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
916     tpd->task=task;
917     tpd->numParameters=numiterators+1;
918     tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
919 #ifdef OPTIONAL
920     tpd->failed=RUNMALLOC(sizeof(int)*(numiterators+1));
921 #endif
922     for(j=0;j<=numiterators;j++){
923       tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
924 #ifdef OPTIONAL
925       tpd->failed[j]=failed[j];
926       if (failed[j]!=0&&failed[j]!=1) {
927         printf("BAD\n");
928       }
929 #endif
930     }
931     /* Enqueue task */
932     if ((!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd))) {
933       genputtable(activetasks, tpd, tpd);
934     } else {
935       RUNFREE(tpd->parameterArray);
936 #ifdef OPTIONAL
937       RUNFREE(tpd->failed);
938 #endif
939       RUNFREE(tpd);
940     }
941     
942     /* This loop iterates to the next parameter combination */
943     if (numiterators==0)
944       return retval;
945
946     for(j=numiterators-1; j<numiterators;j++) {
947     backtrackinc:
948       if(toiHasNext(&parameter->iterators[j], taskpointerarray OPTARG(failed)))
949         toiNext(&parameter->iterators[j], taskpointerarray OPTARG(failed));
950       else if (j>0) {
951         /* Need to backtrack */
952         toiReset(&parameter->iterators[j]);
953         j--;
954         goto backtrackinc;
955       } else {
956         /* Nothing more to enqueue */
957         return retval;
958       }
959     }
960   }
961   return retval;
962 }
963  
964 /* Handler for signals. The signals catch null pointer errors and
965    arithmatic errors. */
966
967 void myhandler(int sig, siginfo_t *info, void *uap) {
968   sigset_t toclear;
969 #ifdef DEBUG
970   printf("sig=%d\n",sig);
971   printf("signal\n");
972 #endif
973   sigemptyset(&toclear);
974   sigaddset(&toclear, sig);
975   sigprocmask(SIG_UNBLOCK, &toclear,NULL); 
976   longjmp(error_handler,1);
977 }
978
979 fd_set readfds;
980 int maxreadfd;
981 struct RuntimeHash *fdtoobject;
982
983 void addreadfd(int fd) {
984   if (fd>=maxreadfd)
985     maxreadfd=fd+1;
986   FD_SET(fd, &readfds);
987 }
988
989 void removereadfd(int fd) {
990   FD_CLR(fd, &readfds);
991   if (maxreadfd==(fd+1)) {
992     maxreadfd--;
993     while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
994       maxreadfd--;
995   }
996 }
997
998 #ifdef PRECISE_GC
999 #define OFFSET 2
1000 #else
1001 #define OFFSET 0
1002 #endif
1003
1004 #ifdef OPTIONAL
1005  int * fsescopy(int *src, int len) {
1006    int *dst;
1007    if (src==NULL)
1008      return NULL;
1009    dst=RUNMALLOC(len*sizeof(int));
1010    memcpy(dst, src, len*sizeof(int));
1011    return dst;
1012  }
1013 #endif
1014
1015 void executetasks() {
1016   void * taskpointerarray[MAXTASKPARAMS+OFFSET];
1017 #ifdef OPTIONAL
1018   int * fsesarray[MAXTASKPARAMS];
1019   int * oldfsesarray[MAXTASKPARAMS];
1020   int numfsesarray[MAXTASKPARAMS];
1021 #endif  
1022
1023   /* Set up signal handlers */
1024   struct sigaction sig;
1025   sig.sa_sigaction=&myhandler;
1026   sig.sa_flags=SA_SIGINFO;
1027   sigemptyset(&sig.sa_mask);
1028
1029   /* Catch bus errors, segmentation faults, and floating point exceptions*/
1030   sigaction(SIGBUS,&sig,0);
1031   sigaction(SIGSEGV,&sig,0);
1032   sigaction(SIGFPE,&sig,0);
1033   sigaction(SIGPIPE,&sig,0);
1034
1035   /* Zero fd set */
1036   FD_ZERO(&readfds);
1037   maxreadfd=0;
1038   fdtoobject=allocateRuntimeHash(100);
1039
1040   /* Map first block of memory to protected, anonymous page */
1041   mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
1042
1043   newtask:
1044   while((hashsize(activetasks)>0)||(maxreadfd>0)) {
1045
1046     /* Check if any filedescriptors have IO pending */
1047     if (maxreadfd>0) {
1048       int i;
1049       struct timeval timeout={0,0};
1050       fd_set tmpreadfds;
1051       int numselect;
1052       tmpreadfds=readfds;
1053       numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
1054       if (numselect>0) {
1055         /* Process ready fd's */
1056         int fd;
1057         for(fd=0;fd<maxreadfd;fd++) {
1058           if (FD_ISSET(fd, &tmpreadfds)) {
1059             /* Set ready flag on object */
1060             void * objptr;
1061             //      printf("Setting fd %d\n",fd);
1062             if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
1063               intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
1064             }
1065           }
1066         }
1067       }
1068     }
1069
1070     /* See if there are any active tasks */
1071     if (hashsize(activetasks)>0) {
1072       int i;
1073       currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
1074       genfreekey(activetasks, currtpd);
1075       
1076       /* Check if this task has failed, allow a task that contains optional objects to fire */
1077       if (gencontains(failedtasks, currtpd)) {
1078         // Free up task parameter descriptor
1079         RUNFREE(currtpd->parameterArray);
1080 #ifdef OPTIONAL
1081         RUNFREE(currtpd->failed);
1082 #endif
1083         RUNFREE(currtpd);
1084         goto newtask;
1085       }
1086       int numparams=currtpd->task->numParameters;
1087       int numtotal=currtpd->task->numTotal;
1088       
1089       /* Make sure that the parameters are still in the queues */
1090       for(i=0;i<numparams;i++) {
1091         void * parameter=currtpd->parameterArray[i];
1092         struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
1093         struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
1094         int j;
1095         /* Check that object is still in queue */
1096 #ifdef OPTIONAL
1097         {
1098           int UNUSED, UNUSED2;
1099           int *flags;
1100           int numflags, isnonfailed;
1101           int failed=currtpd->failed[i];
1102           if (!ObjectHashget(pw->objectset, (int) parameter, &UNUSED, (int *) &flags, &numflags, &isnonfailed)) {
1103             RUNFREE(currtpd->parameterArray);
1104             RUNFREE(currtpd->failed);
1105             RUNFREE(currtpd);
1106             goto newtask;
1107           } else {
1108             if (failed&&(flags!=NULL)) {
1109               //Failed parameter
1110               fsesarray[i]=flags;
1111               numfsesarray[i]=numflags;
1112             } else if (!failed && isnonfailed) {
1113               //Non-failed parameter
1114               fsesarray[i]=NULL;
1115               numfsesarray[i]=0;
1116             } else {
1117               RUNFREE(currtpd->parameterArray);
1118               RUNFREE(currtpd->failed);
1119               RUNFREE(currtpd);
1120               goto newtask;
1121             }
1122           }
1123         }
1124 #else
1125         {
1126           if (!ObjectHashcontainskey(pw->objectset, (int) parameter)) {
1127             RUNFREE(currtpd->parameterArray);
1128             RUNFREE(currtpd);
1129             goto newtask;
1130           }
1131         }
1132 #endif
1133       parameterpresent:
1134         ;
1135         /* Check that object still has necessary tags */
1136         for(j=0;j<pd->numbertags;j++) {
1137           int slotid=pd->tagarray[2*j]+numparams;
1138           struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
1139           if (!containstag(parameter, tagd)) {
1140             RUNFREE(currtpd->parameterArray);
1141 #ifdef OPTIONAL
1142             RUNFREE(currtpd->failed);
1143 #endif
1144             RUNFREE(currtpd);
1145             goto newtask;
1146           }
1147         }
1148         
1149         taskpointerarray[i+OFFSET]=parameter;
1150       }
1151       /* Copy the tags */
1152       for(;i<numtotal;i++) {
1153         taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
1154       }
1155
1156       {
1157         /* Checkpoint the state */
1158         forward=allocateRuntimeHash(100);
1159         reverse=allocateRuntimeHash(100);
1160         void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
1161         int x;
1162         if (x=setjmp(error_handler)) {
1163           int counter;
1164           /* Recover */
1165 #ifdef DEBUG
1166           printf("Fatal Error=%d, Recovering!\n",x);
1167 #endif
1168           genputtable(failedtasks,currtpd,currtpd);
1169           restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
1170
1171 #ifdef OPTIONAL
1172           for(counter=0; counter<currtpd->task->numParameters; counter++){
1173             //enqueue as failed
1174             enqueueoptional(currtpd->parameterArray[counter], numfsesarray[counter], fsesarray[counter], currtpd->task, counter);
1175
1176             //free fses copies
1177             if (fsesarray[counter]!=NULL)
1178               RUNFREE(fsesarray[counter]);
1179           }
1180 #endif
1181           freeRuntimeHash(forward);
1182           freeRuntimeHash(reverse);
1183           freemalloc();
1184           forward=NULL;
1185           reverse=NULL;
1186         } else {
1187           if (injectfailures) {
1188             if ((((double)random())/RAND_MAX)<failurechance) {
1189               printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
1190               longjmp(error_handler,10);
1191             }
1192           }
1193           /* Actually call task */
1194 #ifdef PRECISE_GC
1195           ((int *)taskpointerarray)[0]=currtpd->numParameters;
1196           taskpointerarray[1]=NULL;
1197 #endif
1198 #ifdef OPTIONAL
1199           //get the task flags set
1200           for(i=0;i<numparams;i++) {
1201             oldfsesarray[i]=((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses;
1202             fsesarray[i]=fsescopy(fsesarray[i], numfsesarray[i]);
1203             ((struct ___Object___ *)taskpointerarray[i+OFFSET])->fses=fsesarray[i];         
1204           }
1205 #endif
1206           if(debugtask){
1207             printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1208             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1209             printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
1210           } else
1211             ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
1212
1213 #ifdef OPTIONAL
1214           for(i=0;i<numparams;i++) {
1215             //free old fses
1216             if(oldfsesarray[i]!=NULL)
1217               RUNFREE(oldfsesarray[i]);
1218           }
1219 #endif
1220           
1221           freeRuntimeHash(forward);
1222           freeRuntimeHash(reverse);
1223           freemalloc();
1224           // Free up task parameter descriptor
1225           RUNFREE(currtpd->parameterArray);
1226 #ifdef OPTIONAL
1227           RUNFREE(currtpd->failed);
1228 #endif
1229           RUNFREE(currtpd);
1230           forward=NULL;
1231           reverse=NULL;
1232         }
1233       }
1234     }
1235   }
1236 }
1237  
1238 /* This function processes an objects tags */
1239 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
1240   int i;
1241   
1242   for(i=0;i<pd->numbertags;i++) {
1243     int slotid=pd->tagarray[2*i];
1244     int tagid=pd->tagarray[2*i+1];
1245     
1246     if (statusarray[slotid+numparams]==0) {
1247       parameter->iterators[*iteratorcount].istag=1;
1248       parameter->iterators[*iteratorcount].tagid=tagid;
1249       parameter->iterators[*iteratorcount].slot=slotid+numparams;
1250       parameter->iterators[*iteratorcount].tagobjectslot=index;
1251       statusarray[slotid+numparams]=1;
1252       (*iteratorcount)++;
1253     }
1254   }
1255 }
1256
1257
1258 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
1259   int i;
1260   int tagcount=0;
1261   struct ObjectHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
1262
1263   parameter->iterators[*iteratorcount].istag=0;
1264   parameter->iterators[*iteratorcount].slot=index;
1265   parameter->iterators[*iteratorcount].objectset=objectset;
1266   statusarray[index]=1;
1267
1268   for(i=0;i<pd->numbertags;i++) {
1269     int slotid=pd->tagarray[2*i];
1270     int tagid=pd->tagarray[2*i+1];
1271     if (statusarray[slotid+numparams]!=0) {
1272       /* This tag has already been enqueued, use it to narrow search */
1273       parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
1274       tagcount++;
1275     }
1276   }
1277   parameter->iterators[*iteratorcount].numtags=tagcount;
1278
1279   (*iteratorcount)++;
1280 }
1281
1282 /* This function builds the iterators for a task & parameter */
1283
1284 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
1285   int statusarray[MAXTASKPARAMS];
1286   int i;
1287   int numparams=task->numParameters;
1288   int iteratorcount=0;
1289   for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
1290
1291   statusarray[index]=1; /* Initial parameter */
1292   /* Process tags for initial iterator */
1293   
1294   processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
1295   
1296   while(1) {
1297   loopstart:
1298     /* Check for objects with existing tags */
1299     for(i=0;i<numparams;i++) {
1300       if (statusarray[i]==0) {
1301         struct parameterdescriptor *pd=task->descriptorarray[i];
1302         int j;
1303         for(j=0;j<pd->numbertags;j++) {
1304           int slotid=pd->tagarray[2*j];
1305           if(statusarray[slotid+numparams]!=0) {
1306             processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1307             processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1308             goto loopstart;
1309           }
1310         }
1311       }
1312     }
1313
1314     /* Next do objects w/ unbound tags*/
1315
1316     for(i=0;i<numparams;i++) {
1317       if (statusarray[i]==0) {
1318         struct parameterdescriptor *pd=task->descriptorarray[i];
1319         if (pd->numbertags>0) {
1320           processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1321           processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1322           goto loopstart;
1323         }
1324       }
1325     }
1326
1327     /* Nothing with a tag enqueued */
1328
1329     for(i=0;i<numparams;i++) {
1330       if (statusarray[i]==0) {
1331         struct parameterdescriptor *pd=task->descriptorarray[i];
1332         processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
1333         processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
1334         goto loopstart;
1335       }
1336     }
1337
1338     /* Nothing left */
1339     return;
1340   }
1341 }
1342
1343  void printdebug() {
1344    int i;
1345    int j;
1346    for(i=0;i<numtasks;i++) {
1347      struct taskdescriptor * task=taskarray[i];
1348      printf("%s\n", task->name);
1349      for(j=0;j<task->numParameters;j++) {
1350        struct parameterdescriptor *param=task->descriptorarray[j];
1351        struct parameterwrapper *parameter=param->queue;
1352        struct ObjectHash * set=parameter->objectset;
1353        struct ObjectIterator objit;
1354        printf("  Parameter %d\n", j);
1355        ObjectHashiterator(set, &objit);
1356        while(ObjhasNext(&objit)) {
1357          struct ___Object___ * obj=(struct ___Object___ *)Objkey(&objit);
1358          struct ___Object___ * tagptr=obj->___tags___;
1359          int nonfailed=Objdata4(&objit);
1360          int numflags=Objdata3(&objit);
1361          int flags=Objdata2(&objit);
1362          Objnext(&objit);
1363          printf("    Contains %lx\n", obj);
1364          printf("      flag=%d\n", obj->flag); 
1365 #ifdef OPTIONAL
1366          printf("      flagsstored=%x\n",flags);
1367          printf("      numflags=%d\n", numflags);
1368          printf("      nonfailed=%d\n",nonfailed);
1369 #endif
1370          if (tagptr==NULL) {
1371          } else if (tagptr->type==TAGTYPE) {
1372            printf("      tag=%lx\n",tagptr);
1373          } else {
1374            int tagindex=0;
1375            struct ArrayObject *ao=(struct ArrayObject *)tagptr;
1376            for(;tagindex<ao->___cachedCode___;tagindex++) {
1377              printf("      tag=%lx\n",ARRAYGET(ao, struct ___TagDescriptor___*, tagindex));
1378            }
1379          }
1380        }
1381      }
1382    }
1383  }
1384  
1385
1386 /* This function processes the task information to create queues for
1387    each parameter type. */
1388
1389 void processtasks() {
1390   int i;
1391   for(i=0;i<numtasks;i++) {
1392     struct taskdescriptor * task=taskarray[i];
1393     int j;
1394
1395     for(j=0;j<task->numParameters;j++) {
1396       struct parameterdescriptor *param=task->descriptorarray[j];
1397       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
1398       struct parameterwrapper ** ptr=&objectqueues[param->type];
1399
1400       param->queue=parameter;
1401       parameter->objectset=allocateObjectHash(10);
1402       parameter->numberofterms=param->numberterms;
1403       parameter->intarray=param->intarray;
1404       parameter->numbertags=param->numbertags;
1405       parameter->tagarray=param->tagarray;
1406       parameter->task=task;
1407       /* Link new queue in */
1408       while((*ptr)!=NULL)
1409         ptr=&((*ptr)->next);
1410       (*ptr)=parameter;
1411     }
1412
1413     /* Build iterators for parameters */
1414     for(j=0;j<task->numParameters;j++) {
1415       struct parameterdescriptor *param=task->descriptorarray[j];
1416       struct parameterwrapper *parameter=param->queue;      
1417       parameter->slot=j;
1418       builditerators(task, j, parameter);
1419     }
1420   }
1421 }
1422
1423 void toiReset(struct tagobjectiterator * it) {
1424   if (it->istag) {
1425     it->tagobjindex=0;
1426   } else if (it->numtags>0) {
1427     it->tagobjindex=0;
1428 #ifdef OPTIONAL
1429     it->failedstate=0;
1430 #endif
1431   } else {
1432     ObjectHashiterator(it->objectset, &it->it);
1433 #ifdef OPTIONAL
1434     it->failedstate=0;
1435 #endif
1436   }
1437 }
1438
1439 int toiHasNext(struct tagobjectiterator *it, void ** objectarray OPTARG(int * failed)) {
1440   if (it->istag) {
1441     /* Iterate tag */
1442     /* Get object with tags */
1443     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1444     struct ___Object___ *tagptr=obj->___tags___;
1445     if (tagptr->type==TAGTYPE) {
1446       if ((it->tagobjindex==0)&& /* First object */
1447           (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
1448         return 1;
1449       else
1450         return 0;
1451     } else {
1452       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1453       int tagindex=it->tagobjindex;
1454       for(;tagindex<ao->___cachedCode___;tagindex++) {
1455         struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
1456         if (td->flag==it->tagid) {
1457           it->tagobjindex=tagindex; /* Found right type of tag */
1458           return 1;
1459         }
1460       }
1461       return 0;
1462     }
1463   } else if (it->numtags>0) {
1464     /* Use tags to locate appropriate objects */
1465     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1466     struct ___Object___ *objptr=tag->flagptr;
1467     int i;
1468     if (objptr->type!=OBJECTARRAYTYPE) {
1469       if (it->tagobjindex>0)
1470         return 0;
1471       if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1472         return 0;
1473       for(i=1;i<it->numtags;i++) {
1474         struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1475         if (!containstag(objptr,tag2))
1476           return 0;
1477       }
1478 #ifdef OPTIONAL
1479       if (it->failedstate==1) {
1480         int UNUSED, UNUSED2;
1481         int * flags;
1482         int isnonfailed;
1483         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1484         if (flags!=NULL) {
1485           return 1;
1486         } else {
1487           it->tagobjindex++;
1488           it->failedstate=0;
1489           return 0;
1490         }
1491       } else {
1492         int UNUSED, UNUSED2;
1493         int * flags;
1494         int isnonfailed;
1495         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1496         if (!isnonfailed) {
1497           it->failedstate=1;
1498         }
1499         return 1;
1500       }
1501 #endif      
1502       return 1;
1503     } else {
1504       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1505       int tagindex;
1506       int i;
1507 #ifdef OPTIONAL
1508       if (it->failedstate==1) {
1509         int UNUSED, UNUSED2;
1510         int * flags;
1511         int isnonfailed;
1512         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, it->tagobjindex);
1513         ObjectHashget(it->objectset, (int) objptr, &UNUSED, (int *) &flags, &UNUSED2, &isnonfailed);
1514         if (flags!=NULL) {
1515           return 1;
1516         } else {
1517           it->failedstate=0;
1518           it->tagobjindex++;
1519         }
1520       }
1521 #endif
1522       for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
1523         struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1524         if (!ObjectHashcontainskey(it->objectset, (int) objptr))
1525           continue;
1526         for(i=1;i<it->numtags;i++) {
1527           struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
1528           if (!containstag(objptr,tag2))
1529             goto nexttag;
1530         }
1531 #ifdef OPTIONAL
1532         {
1533           int UNUSED, UNUSED2;
1534           int flags, isnonfailed;
1535           struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
1536           ObjectHashget(it->objectset, (int) objptr, &UNUSED, &flags, &UNUSED2, &isnonfailed);
1537           if (!isnonfailed) {
1538             it->failedstate=1;
1539           }
1540         }
1541 #endif
1542         it->tagobjindex=tagindex;
1543         return 1;
1544       nexttag:
1545         ;
1546       }
1547       it->tagobjindex=tagindex;
1548       return 0;
1549     }
1550   } else {
1551 #ifdef OPTIONAL
1552     if (it->failedstate==1) {
1553       if (Objdata2(&it->it))
1554         return 1;
1555       else {
1556         it->failedstate=0;
1557         Objnext(&it->it);
1558       }
1559     }
1560     if (ObjhasNext(&it->it)) {
1561       if (!Objdata4(&it->it)) {
1562         //failed state only
1563         it->failedstate=1;
1564       }
1565       return 1;
1566     } else
1567       return 0;
1568 #else
1569     return ObjhasNext(&it->it);
1570 #endif
1571   }
1572 }
1573
1574 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
1575   int j;
1576   struct ___Object___ * objptr=tag->flagptr;
1577   if (objptr->type==OBJECTARRAYTYPE) {
1578     struct ArrayObject *ao=(struct ArrayObject *)objptr;
1579     for(j=0;j<ao->___cachedCode___;j++) {
1580       if (ptr==ARRAYGET(ao, struct ___Object___*, j))
1581         return 1;
1582     }
1583     return 0;
1584   } else
1585     return objptr==ptr;
1586 }
1587
1588 void toiNext(struct tagobjectiterator *it , void ** objectarray OPTARG(int * failed)) {
1589   /* hasNext has all of the intelligence */
1590   if(it->istag) {
1591     /* Iterate tag */
1592     /* Get object with tags */
1593     struct ___Object___ *obj=objectarray[it->tagobjectslot];
1594     struct ___Object___ *tagptr=obj->___tags___;
1595 #ifdef OPTIONAL
1596     failed[it->slot]=0; //have to set it to something
1597 #endif
1598     if (tagptr->type==TAGTYPE) {
1599       it->tagobjindex++;
1600       objectarray[it->slot]=tagptr;
1601     } else {
1602       struct ArrayObject *ao=(struct ArrayObject *) tagptr;
1603       objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
1604     }
1605   } else if (it->numtags>0) {
1606     /* Use tags to locate appropriate objects */
1607     struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
1608     struct ___Object___ *objptr=tag->flagptr;
1609     if (objptr->type!=OBJECTARRAYTYPE) {
1610 #ifdef OPTIONAL
1611     failed[it->slot]=it->failedstate;
1612     objectarray[it->slot]=objptr;
1613     if (it->failedstate==0) {
1614       it->failedstate=1;
1615     } else {
1616       it->failedstate=0;
1617       it->tagobjindex++;
1618     }
1619 #else
1620       it->tagobjindex++;
1621       objectarray[it->slot]=objptr;
1622 #endif
1623     } else {
1624       struct ArrayObject *ao=(struct ArrayObject *) objptr;
1625 #ifdef OPTIONAL
1626     failed[it->slot]=it->failedstate;
1627     objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex);
1628     if (it->failedstate==0) {
1629       it->failedstate=1;
1630     } else {
1631       it->failedstate=0;
1632       it->tagobjindex++;
1633     }
1634 #else
1635       objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
1636 #endif
1637     }
1638   } else {
1639     /* Iterate object */
1640     objectarray[it->slot]=(void *)Objkey(&it->it);
1641 #ifdef OPTIONAL
1642     failed[it->slot]=it->failedstate;
1643     if (it->failedstate==0) {
1644       it->failedstate=1;
1645     } else {
1646       it->failedstate=0;
1647       Objnext(&it->it);
1648     }
1649 #else
1650     Objnext(&it->it);
1651 #endif
1652   }
1653 }
1654 #endif