3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include <sys/select.h>
10 #include <sys/types.h>
15 extern int injectfailures;
16 extern float failurechance;
21 #include "instrument.h"
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;
32 int main(int argc, char **argv) {
34 GC_init(); // Initialize the garbage collector
40 initializeexithandler();
41 /* Create table for failed tasks */
42 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
43 (int (*)(void *,void *)) &comparetpd);
44 /* Create queue of active tasks */
45 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
46 (int (*)(void *,void *)) &comparetpd);
49 /* Process task information */
52 /* Create startup object */
53 createstartupobject(argc, argv);
55 /* Start executing the tasks */
59 void createstartupobject(int argc, char ** argv) {
62 /* Allocate startup object */
64 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
65 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
67 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
68 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
70 /* Build array of strings */
71 startupobject->___parameters___=stringarray;
73 int length=strlen(argv[i]);
75 struct ___String___ *newstring=NewString(NULL, argv[i],length);
77 struct ___String___ *newstring=NewString(argv[i],length);
79 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
82 /* Set initialized flag for startup object */
83 flagorand(startupobject,1,0xFFFFFFFF);
86 int hashCodetpd(struct taskparamdescriptor *ftd) {
87 int hash=(int)ftd->task;
89 for(i=0;i<ftd->numParameters;i++) {
90 hash^=(int)ftd->parameterArray[i];
95 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
97 if (ftd1->task!=ftd2->task)
99 for(i=0;i<ftd1->numParameters;i++)
100 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
106 /* This function sets a tag. */
108 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
110 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
112 struct ___Object___ * tagptr=obj->___tags___;
114 obj->___tags___=(struct ___Object___ *)tagd;
116 /* Have to check if it is already set */
117 if (tagptr->type==TAGTYPE) {
118 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
122 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
123 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
124 obj=(struct ___Object___ *)ptrarray[2];
125 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
126 td=(struct ___TagDescriptor___ *) obj->___tags___;
128 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
130 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
131 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
132 obj->___tags___=(struct ___Object___ *) ao;
133 ao->___cachedCode___=2;
137 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
138 for(i=0;i<ao->___cachedCode___;i++) {
139 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
143 if (ao->___cachedCode___<ao->___length___) {
144 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
145 ao->___cachedCode___++;
148 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
149 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
150 obj=(struct ___Object___ *)ptrarray[2];
151 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
152 ao=(struct ArrayObject *)obj->___tags___;
154 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
156 aonew->___cachedCode___=ao->___length___+1;
157 for(i=0;i<ao->___length___;i++) {
158 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
160 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
166 struct ___Object___ * tagset=tagd->flagptr;
170 } else if (tagset->type!=OBJECTARRAYTYPE) {
172 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
173 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
174 obj=(struct ___Object___ *)ptrarray[2];
175 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
177 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
179 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
180 ARRAYSET(ao, struct ___Object___ *, 1, obj);
181 ao->___cachedCode___=2;
182 tagd->flagptr=(struct ___Object___ *)ao;
184 struct ArrayObject *ao=(struct ArrayObject *) tagset;
185 if (ao->___cachedCode___<ao->___length___) {
186 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
190 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
191 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
192 obj=(struct ___Object___ *)ptrarray[2];
193 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
194 ao=(struct ArrayObject *)tagd->flagptr;
196 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
198 aonew->___cachedCode___=ao->___cachedCode___+1;
199 for(i=0;i<ao->___length___;i++) {
200 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
202 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
203 tagd->flagptr=(struct ___Object___ *) ao;
209 /* This function clears a tag. */
211 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
213 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
215 /* We'll assume that tag is alway there.
216 Need to statically check for this of course. */
217 struct ___Object___ * tagptr=obj->___tags___;
219 if (tagptr->type==TAGTYPE) {
220 if ((struct ___TagDescriptor___ *)tagptr==tagd)
221 obj->___tags___=NULL;
223 printf("ERROR 1 in tagclear\n");
225 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
227 for(i=0;i<ao->___cachedCode___;i++) {
228 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
230 ao->___cachedCode___--;
231 if (i<ao->___cachedCode___)
232 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
233 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
234 if (ao->___cachedCode___==0)
235 obj->___tags___=NULL;
239 printf("ERROR 2 in tagclear\n");
243 struct ___Object___ *tagset=tagd->flagptr;
244 if (tagset->type!=OBJECTARRAYTYPE) {
248 printf("ERROR 3 in tagclear\n");
250 struct ArrayObject *ao=(struct ArrayObject *) tagset;
252 for(i=0;i<ao->___cachedCode___;i++) {
253 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
255 ao->___cachedCode___--;
256 if (i<ao->___cachedCode___)
257 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
258 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
259 if (ao->___cachedCode___==0)
264 printf("ERROR 4 in tagclear\n");
272 /* This function allocates a new tag. */
274 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
275 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
277 struct ___TagDescriptor___ * allocate_tag(int index) {
278 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
287 /* This function updates the flag for object ptr. It or's the flag
288 with the or mask and and's it with the andmask. */
290 void flagbody(struct ___Object___ *ptr, int flag);
292 void flagorand(void * ptr, int ormask, int andmask) {
293 int oldflag=((int *)ptr)[1];
294 int flag=ormask|oldflag;
296 // Not sure why this was necessary
297 // if (flag==oldflag) /* Don't do anything */
303 void intflagorand(void * ptr, int ormask, int andmask) {
304 int oldflag=((int *)ptr)[1];
305 int flag=ormask|oldflag;
307 if (flag==oldflag) /* Don't do anything */
309 else flagbody(ptr, flag);
312 void flagorandinit(void * ptr, int ormask, int andmask) {
313 int oldflag=((int *)ptr)[1];
314 int flag=ormask|oldflag;
319 void flagbody(struct ___Object___ *ptr, int flag) {
320 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
323 /*Remove object from all queues */
324 while(flagptr!=NULL) {
325 struct parameterwrapper *next;
326 struct ___Object___ * tag=ptr->___tags___;
327 RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
328 RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
333 struct QueueItem *tmpptr;
334 struct parameterwrapper * parameter=objectqueues[ptr->type];
336 struct parameterwrapper * prevptr=NULL;
337 struct ___Object___ *tagptr=ptr->___tags___;
339 /* Outer loop iterates through all parameter queues an object of
340 this type could be in. */
342 while(parameter!=NULL) {
344 if (parameter->numbertags>0) {
346 goto nextloop;//that means the object has no tag but that param needs tag
347 else if(tagptr->type==TAGTYPE) {//one tag
348 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
349 for(i=0;i<parameter->numbertags;i++) {
350 //slotid is parameter->tagarray[2*i];
351 int tagid=parameter->tagarray[2*i+1];
352 if (tagid!=tagptr->flag)
353 goto nextloop; /*We don't have this tag */
355 } else {//multiple tags
356 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
357 for(i=0;i<parameter->numbertags;i++) {
358 //slotid is parameter->tagarray[2*i];
359 int tagid=parameter->tagarray[2*i+1];
361 for(j=0;j<ao->___cachedCode___;j++) {
362 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
373 for(i=0;i<parameter->numberofterms;i++) {
374 int andmask=parameter->intarray[i*2];
375 int checkmask=parameter->intarray[i*2+1];
376 if ((flag&andmask)==checkmask) {
377 enqueuetasks(parameter, prevptr, ptr);
383 parameter=parameter->next;
385 ptr->flagptr=prevptr;
389 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
390 void * taskpointerarray[MAXTASKPARAMS];
392 int numparams=parameter->task->numParameters;
393 int numiterators=parameter->task->numTotal-1;
395 struct taskdescriptor * task=parameter->task;
397 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);//this add the object to parameterwrapper
399 /* Add enqueued object to parameter vector */
400 taskpointerarray[parameter->slot]=ptr;
402 /* Reset iterators */
403 for(j=0;j<numiterators;j++) {
404 toiReset(¶meter->iterators[j]);
407 /* Find initial state */
408 for(j=0;j<numiterators;j++) {
410 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
411 toiNext(¶meter->iterators[j], taskpointerarray);
413 /* Need to backtrack */
414 toiReset(¶meter->iterators[j]);
418 /* Nothing to enqueue */
425 /* Enqueue current state */
426 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
428 tpd->numParameters=numiterators+1;
429 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
430 for(j=0;j<=numiterators;j++)
431 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
434 if (!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd)) {
435 genputtable(activetasks, tpd, tpd);
437 RUNFREE(tpd->parameterArray);
441 /* This loop iterates to the next parameter combination */
445 for(j=numiterators-1; j<numiterators;j++) {
447 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
448 toiNext(¶meter->iterators[j], taskpointerarray);
450 /* Need to backtrack */
451 toiReset(¶meter->iterators[j]);
455 /* Nothing more to enqueue */
462 /* Handler for signals. The signals catch null pointer errors and
463 arithmatic errors. */
465 void myhandler(int sig, siginfo_t *info, void *uap) {
467 printf("sig=%d\n",sig);
470 longjmp(error_handler,1);
475 struct RuntimeHash *fdtoobject;
477 void addreadfd(int fd) {
480 FD_SET(fd, &readfds);
483 void removereadfd(int fd) {
484 FD_CLR(fd, &readfds);
485 if (maxreadfd==(fd+1)) {
487 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
498 void executetasks() {
499 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
501 /* Set up signal handlers */
502 struct sigaction sig;
503 sig.sa_sigaction=&myhandler;
504 sig.sa_flags=SA_SIGINFO;
505 sigemptyset(&sig.sa_mask);
507 /* Catch bus errors, segmentation faults, and floating point exceptions*/
508 sigaction(SIGBUS,&sig,0);
509 sigaction(SIGSEGV,&sig,0);
510 sigaction(SIGFPE,&sig,0);
511 sigaction(SIGPIPE,&sig,0);
516 fdtoobject=allocateRuntimeHash(100);
518 /* Map first block of memory to protected, anonymous page */
519 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
522 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
524 /* Check if any filedescriptors have IO pending */
527 struct timeval timeout={0,0};
531 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
533 /* Process ready fd's */
535 for(fd=0;fd<maxreadfd;fd++) {
536 if (FD_ISSET(fd, &tmpreadfds)) {
537 /* Set ready flag on object */
539 // printf("Setting fd %d\n",fd);
540 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
541 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
548 /* See if there are any active tasks */
549 if (hashsize(activetasks)>0) {
551 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
552 genfreekey(activetasks, currtpd);
554 /* Check if this task has failed */
555 if (gencontains(failedtasks, currtpd)) {
556 // Free up task parameter descriptor
557 RUNFREE(currtpd->parameterArray);
561 int numparams=currtpd->task->numParameters;
562 int numtotal=currtpd->task->numTotal;
564 /* Make sure that the parameters are still in the queues */
565 for(i=0;i<numparams;i++) {
566 void * parameter=currtpd->parameterArray[i];
567 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
568 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
570 /* Check that object is still in queue */
571 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
572 RUNFREE(currtpd->parameterArray);
576 /* Check that object still has necessary tags */
577 for(j=0;j<pd->numbertags;j++) {
578 int slotid=pd->tagarray[2*j]+numparams;
579 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
580 if (!containstag(parameter, tagd)) {
581 RUNFREE(currtpd->parameterArray);
587 taskpointerarray[i+OFFSET]=parameter;
590 for(;i<numtotal;i++) {
591 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
595 /* Checkpoint the state */
596 forward=allocateRuntimeHash(100);
597 reverse=allocateRuntimeHash(100);
598 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
600 if (x=setjmp(error_handler)) {
604 printf("Fatal Error=%d, Recovering!\n",x);
606 genputtable(failedtasks,currtpd,currtpd);
607 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
608 /*where we have to insert the code for optional tasks
609 all the pointers I need are in currtpd->parameterArray */
610 freeRuntimeHash(forward);
611 freeRuntimeHash(reverse);
616 if (injectfailures) {
617 if ((((double)random())/RAND_MAX)<failurechance) {
618 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
619 longjmp(error_handler,10);
622 /* Actually call task */
624 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
625 taskpointerarray[1]=NULL;
629 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
630 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
631 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
633 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
634 freeRuntimeHash(forward);
635 freeRuntimeHash(reverse);
637 // Free up task parameter descriptor
638 RUNFREE(currtpd->parameterArray);
648 /* This function processes an objects tags */
649 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
652 for(i=0;i<pd->numbertags;i++) {
653 int slotid=pd->tagarray[2*i];
654 int tagid=pd->tagarray[2*i+1];
656 if (statusarray[slotid+numparams]==0) {
657 parameter->iterators[*iteratorcount].istag=1;
658 parameter->iterators[*iteratorcount].tagid=tagid;
659 parameter->iterators[*iteratorcount].slot=slotid+numparams;
660 parameter->iterators[*iteratorcount].tagobjectslot=index;
661 statusarray[slotid+numparams]=1;
668 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
671 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
673 parameter->iterators[*iteratorcount].istag=0;
674 parameter->iterators[*iteratorcount].slot=index;
675 parameter->iterators[*iteratorcount].objectset=objectset;
676 statusarray[index]=1;
678 for(i=0;i<pd->numbertags;i++) {
679 int slotid=pd->tagarray[2*i];
680 int tagid=pd->tagarray[2*i+1];
681 if (statusarray[slotid+numparams]!=0) {
682 /* This tag has already been enqueued, use it to narrow search */
683 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
687 parameter->iterators[*iteratorcount].numtags=tagcount;
692 /* This function builds the iterators for a task & parameter */
694 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
695 int statusarray[MAXTASKPARAMS];
697 int numparams=task->numParameters;
699 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
701 statusarray[index]=1; /* Initial parameter */
702 /* Process tags for initial iterator */
704 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
708 /* Check for objects with existing tags */
709 for(i=0;i<numparams;i++) {
710 if (statusarray[i]==0) {
711 struct parameterdescriptor *pd=task->descriptorarray[i];
713 for(j=0;j<pd->numbertags;j++) {
714 int slotid=pd->tagarray[2*j];
715 if(statusarray[slotid+numparams]!=0) {
716 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
717 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
724 /* Next do objects w/ unbound tags*/
726 for(i=0;i<numparams;i++) {
727 if (statusarray[i]==0) {
728 struct parameterdescriptor *pd=task->descriptorarray[i];
729 if (pd->numbertags>0) {
730 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
731 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
737 /* Nothing with a tag enqueued */
739 for(i=0;i<numparams;i++) {
740 if (statusarray[i]==0) {
741 struct parameterdescriptor *pd=task->descriptorarray[i];
742 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
743 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
756 /* This function processes the task information to create queues for
757 each parameter type. */
759 void processtasks() {
761 for(i=0;i<numtasks;i++) {
762 struct taskdescriptor * task=taskarray[i];
765 for(j=0;j<task->numParameters;j++) {
766 struct parameterdescriptor *param=task->descriptorarray[j];
767 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
768 struct parameterwrapper ** ptr=&objectqueues[param->type];
770 param->queue=parameter;
771 parameter->objectset=allocateRuntimeHash(10);
772 parameter->numberofterms=param->numberterms;
773 parameter->intarray=param->intarray;
774 parameter->numbertags=param->numbertags;
775 parameter->tagarray=param->tagarray;
776 parameter->task=task;
777 /* Link new queue in */
783 /* Build iterators for parameters */
784 for(j=0;j<task->numParameters;j++) {
785 struct parameterdescriptor *param=task->descriptorarray[j];
786 struct parameterwrapper *parameter=param->queue;
788 builditerators(task, j, parameter);
793 void toiReset(struct tagobjectiterator * it) {
796 } else if (it->numtags>0) {
799 RuntimeHashiterator(it->objectset, &it->it);
803 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
806 /* Get object with tags */
807 struct ___Object___ *obj=objectarray[it->tagobjectslot];
808 struct ___Object___ *tagptr=obj->___tags___;
809 if (tagptr->type==TAGTYPE) {
810 if ((it->tagobjindex==0)&& /* First object */
811 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
816 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
817 int tagindex=it->tagobjindex;
818 for(;tagindex<ao->___cachedCode___;tagindex++) {
819 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
820 if (td->flag==it->tagid) {
821 it->tagobjindex=tagindex; /* Found right type of tag */
827 } else if (it->numtags>0) {
828 /* Use tags to locate appropriate objects */
829 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
830 struct ___Object___ *objptr=tag->flagptr;
832 if (objptr->type!=OBJECTARRAYTYPE) {
833 if (it->tagobjindex>0)
835 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
837 for(i=1;i<it->numtags;i++) {
838 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
839 if (!containstag(objptr,tag2))
844 struct ArrayObject *ao=(struct ArrayObject *) objptr;
847 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
848 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
849 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
851 for(i=1;i<it->numtags;i++) {
852 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
853 if (!containstag(objptr,tag2))
860 it->tagobjindex=tagindex;
864 return RunhasNext(&it->it);
868 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
870 struct ___Object___ * objptr=tag->flagptr;
871 if (objptr->type==OBJECTARRAYTYPE) {
872 struct ArrayObject *ao=(struct ArrayObject *)objptr;
873 for(j=0;j<ao->___cachedCode___;j++) {
874 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
882 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
883 /* hasNext has all of the intelligence */
886 /* Get object with tags */
887 struct ___Object___ *obj=objectarray[it->tagobjectslot];
888 struct ___Object___ *tagptr=obj->___tags___;
889 if (tagptr->type==TAGTYPE) {
891 objectarray[it->slot]=tagptr;
893 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
894 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
896 } else if (it->numtags>0) {
897 /* Use tags to locate appropriate objects */
898 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
899 struct ___Object___ *objptr=tag->flagptr;
900 if (objptr->type!=OBJECTARRAYTYPE) {
902 objectarray[it->slot]=objptr;
904 struct ArrayObject *ao=(struct ArrayObject *) objptr;
905 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
909 objectarray[it->slot]=(void *)Runkey(&it->it);