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) {
347 else if(tagptr->type==TAGTYPE) {
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 */
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);
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];
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 freeRuntimeHash(forward);
609 freeRuntimeHash(reverse);
614 if (injectfailures) {
615 if ((((double)random())/RAND_MAX)<failurechance) {
616 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
617 longjmp(error_handler,10);
620 /* Actually call task */
622 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
623 taskpointerarray[1]=NULL;
627 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
628 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
629 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
631 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
632 freeRuntimeHash(forward);
633 freeRuntimeHash(reverse);
635 // Free up task parameter descriptor
636 RUNFREE(currtpd->parameterArray);
646 /* This function processes an objects tags */
647 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
650 for(i=0;i<pd->numbertags;i++) {
651 int slotid=pd->tagarray[2*i];
652 int tagid=pd->tagarray[2*i+1];
654 if (statusarray[slotid+numparams]==0) {
655 parameter->iterators[*iteratorcount].istag=1;
656 parameter->iterators[*iteratorcount].tagid=tagid;
657 parameter->iterators[*iteratorcount].slot=slotid+numparams;
658 parameter->iterators[*iteratorcount].tagobjectslot=index;
659 statusarray[slotid+numparams]=1;
666 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
669 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
671 parameter->iterators[*iteratorcount].istag=0;
672 parameter->iterators[*iteratorcount].slot=index;
673 parameter->iterators[*iteratorcount].objectset=objectset;
674 statusarray[index]=1;
676 for(i=0;i<pd->numbertags;i++) {
677 int slotid=pd->tagarray[2*i];
678 int tagid=pd->tagarray[2*i+1];
679 if (statusarray[slotid+numparams]!=0) {
680 /* This tag has already been enqueued, use it to narrow search */
681 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
685 parameter->iterators[*iteratorcount].numtags=tagcount;
690 /* This function builds the iterators for a task & parameter */
692 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
693 int statusarray[MAXTASKPARAMS];
695 int numparams=task->numParameters;
697 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
699 statusarray[index]=1; /* Initial parameter */
700 /* Process tags for initial iterator */
702 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
706 /* Check for objects with existing tags */
707 for(i=0;i<numparams;i++) {
708 if (statusarray[i]==0) {
709 struct parameterdescriptor *pd=task->descriptorarray[i];
711 for(j=0;j<pd->numbertags;j++) {
712 int slotid=pd->tagarray[2*j];
713 if(statusarray[slotid+numparams]!=0) {
714 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
715 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
722 /* Next do objects w/ unbound tags*/
724 for(i=0;i<numparams;i++) {
725 if (statusarray[i]==0) {
726 struct parameterdescriptor *pd=task->descriptorarray[i];
727 if (pd->numbertags>0) {
728 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
729 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
735 /* Nothing with a tag enqueued */
737 for(i=0;i<numparams;i++) {
738 if (statusarray[i]==0) {
739 struct parameterdescriptor *pd=task->descriptorarray[i];
740 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
741 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
754 /* This function processes the task information to create queues for
755 each parameter type. */
757 void processtasks() {
759 for(i=0;i<numtasks;i++) {
760 struct taskdescriptor * task=taskarray[i];
763 for(j=0;j<task->numParameters;j++) {
764 struct parameterdescriptor *param=task->descriptorarray[j];
765 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
766 struct parameterwrapper ** ptr=&objectqueues[param->type];
768 param->queue=parameter;
769 parameter->objectset=allocateRuntimeHash(10);
770 parameter->numberofterms=param->numberterms;
771 parameter->intarray=param->intarray;
772 parameter->numbertags=param->numbertags;
773 parameter->tagarray=param->tagarray;
774 parameter->task=task;
775 /* Link new queue in */
781 /* Build iterators for parameters */
782 for(j=0;j<task->numParameters;j++) {
783 struct parameterdescriptor *param=task->descriptorarray[j];
784 struct parameterwrapper *parameter=param->queue;
786 builditerators(task, j, parameter);
791 void toiReset(struct tagobjectiterator * it) {
794 } else if (it->numtags>0) {
797 RuntimeHashiterator(it->objectset, &it->it);
801 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
804 /* Get object with tags */
805 struct ___Object___ *obj=objectarray[it->tagobjectslot];
806 struct ___Object___ *tagptr=obj->___tags___;
807 if (tagptr->type==TAGTYPE) {
808 if ((it->tagobjindex==0)&& /* First object */
809 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
814 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
815 int tagindex=it->tagobjindex;
816 for(;tagindex<ao->___cachedCode___;tagindex++) {
817 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
818 if (td->flag==it->tagid) {
819 it->tagobjindex=tagindex; /* Found right type of tag */
825 } else if (it->numtags>0) {
826 /* Use tags to locate appropriate objects */
827 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
828 struct ___Object___ *objptr=tag->flagptr;
830 if (objptr->type!=OBJECTARRAYTYPE) {
831 if (it->tagobjindex>0)
833 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
835 for(i=1;i<it->numtags;i++) {
836 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
837 if (!containstag(objptr,tag2))
842 struct ArrayObject *ao=(struct ArrayObject *) objptr;
845 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
846 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
847 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
849 for(i=1;i<it->numtags;i++) {
850 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
851 if (!containstag(objptr,tag2))
858 it->tagobjindex=tagindex;
862 return RunhasNext(&it->it);
866 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
868 struct ___Object___ * objptr=tag->flagptr;
869 if (objptr->type==OBJECTARRAYTYPE) {
870 struct ArrayObject *ao=(struct ArrayObject *)objptr;
871 for(j=0;j<ao->___cachedCode___;j++) {
872 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
880 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
881 /* hasNext has all of the intelligence */
884 /* Get object with tags */
885 struct ___Object___ *obj=objectarray[it->tagobjectslot];
886 struct ___Object___ *tagptr=obj->___tags___;
887 if (tagptr->type==TAGTYPE) {
889 objectarray[it->slot]=tagptr;
891 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
892 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
894 } else if (it->numtags>0) {
895 /* Use tags to locate appropriate objects */
896 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
897 struct ___Object___ *objptr=tag->flagptr;
898 if (objptr->type!=OBJECTARRAYTYPE) {
900 objectarray[it->slot]=objptr;
902 struct ArrayObject *ao=(struct ArrayObject *) objptr;
903 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
907 objectarray[it->slot]=(void *)Runkey(&it->it);