3 #include "structdefs.h"
5 #include "checkpoint.h"
7 #include "SimpleHash.h"
8 #include "GenericHashtable.h"
9 #include "optionalstruct.h"
10 #include <sys/select.h>
11 #include <sys/types.h>
16 extern int injectfailures;
17 extern float failurechance;
22 #include "instrument.h"
25 struct genhashtable * activetasks;
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 struct genhashtable * failedtasks;
28 struct taskparamdescriptor * currtpd;
29 struct RuntimeHash * forward;
30 struct RuntimeHash * reverse;
33 int main(int argc, char **argv) {
35 GC_init(); // Initialize the garbage collector
41 initializeexithandler();
42 /* Create table for failed tasks */
43 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
44 (int (*)(void *,void *)) &comparetpd);
45 /* Create queue of active tasks */
46 activetasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
47 (int (*)(void *,void *)) &comparetpd);
50 /* Process task information */
53 /* Create startup object */
54 createstartupobject(argc, argv);
56 /* Start executing the tasks */
60 void createstartupobject(int argc, char ** argv) {
63 /* Allocate startup object */
65 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
66 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
68 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
69 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
71 /* Build array of strings */
72 startupobject->___parameters___=stringarray;
74 int length=strlen(argv[i]);
76 struct ___String___ *newstring=NewString(NULL, argv[i],length);
78 struct ___String___ *newstring=NewString(argv[i],length);
80 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
83 /* Set initialized flag for startup object */
84 flagorand(startupobject,1,0xFFFFFFFF);
87 int hashCodetpd(struct taskparamdescriptor *ftd) {
88 int hash=(int)ftd->task;
90 for(i=0;i<ftd->numParameters;i++) {
91 hash^=(int)ftd->parameterArray[i];
96 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
98 if (ftd1->task!=ftd2->task)
100 for(i=0;i<ftd1->numParameters;i++)
101 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
107 /* This function sets a tag. */
109 void tagset(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
111 void tagset(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
113 struct ___Object___ * tagptr=obj->___tags___;
115 obj->___tags___=(struct ___Object___ *)tagd;
117 /* Have to check if it is already set */
118 if (tagptr->type==TAGTYPE) {
119 struct ___TagDescriptor___ * td=(struct ___TagDescriptor___ *) tagptr;
123 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
124 struct ArrayObject * ao=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL);
125 obj=(struct ___Object___ *)ptrarray[2];
126 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
127 td=(struct ___TagDescriptor___ *) obj->___tags___;
129 struct ArrayObject * ao=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL);
131 ARRAYSET(ao, struct ___TagDescriptor___ *, 0, td);
132 ARRAYSET(ao, struct ___TagDescriptor___ *, 1, tagd);
133 obj->___tags___=(struct ___Object___ *) ao;
134 ao->___cachedCode___=2;
138 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
139 for(i=0;i<ao->___cachedCode___;i++) {
140 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___*, i);
144 if (ao->___cachedCode___<ao->___length___) {
145 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, tagd);
146 ao->___cachedCode___++;
149 int ptrarray[]={2,(int) ptr, (int) obj, (int) tagd};
150 struct ArrayObject * aonew=allocate_newarray(&ptrarray,TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
151 obj=(struct ___Object___ *)ptrarray[2];
152 tagd=(struct ___TagDescriptor___ *) ptrarray[3];
153 ao=(struct ArrayObject *)obj->___tags___;
155 struct ArrayObject * aonew=allocate_newarray(TAGARRAYTYPE,TAGARRAYINTERVAL+ao->___length___);
157 aonew->___cachedCode___=ao->___length___+1;
158 for(i=0;i<ao->___length___;i++) {
159 ARRAYSET(aonew, struct ___TagDescriptor___*, i, ARRAYGET(ao, struct ___TagDescriptor___*, i));
161 ARRAYSET(aonew, struct ___TagDescriptor___ *, ao->___length___, tagd);
167 struct ___Object___ * tagset=tagd->flagptr;
171 } else if (tagset->type!=OBJECTARRAYTYPE) {
173 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
174 struct ArrayObject * ao=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
175 obj=(struct ___Object___ *)ptrarray[2];
176 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
178 struct ArrayObject * ao=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
180 ARRAYSET(ao, struct ___Object___ *, 0, tagd->flagptr);
181 ARRAYSET(ao, struct ___Object___ *, 1, obj);
182 ao->___cachedCode___=2;
183 tagd->flagptr=(struct ___Object___ *)ao;
185 struct ArrayObject *ao=(struct ArrayObject *) tagset;
186 if (ao->___cachedCode___<ao->___length___) {
187 ARRAYSET(ao, struct ___Object___*, ao->___cachedCode___++, obj);
191 int ptrarray[]={2, (int) ptr, (int) obj, (int)tagd};
192 struct ArrayObject * aonew=allocate_newarray(&ptrarray,OBJECTARRAYTYPE,OBJECTARRAYINTERVAL+ao->___length___);
193 obj=(struct ___Object___ *)ptrarray[2];
194 tagd=(struct ___TagDescriptor___ *)ptrarray[3];
195 ao=(struct ArrayObject *)tagd->flagptr;
197 struct ArrayObject * aonew=allocate_newarray(OBJECTARRAYTYPE,OBJECTARRAYINTERVAL);
199 aonew->___cachedCode___=ao->___cachedCode___+1;
200 for(i=0;i<ao->___length___;i++) {
201 ARRAYSET(aonew, struct ___Object___*, i, ARRAYGET(ao, struct ___Object___*, i));
203 ARRAYSET(aonew, struct ___Object___ *, ao->___cachedCode___, obj);
204 tagd->flagptr=(struct ___Object___ *) ao;
210 /* This function clears a tag. */
212 void tagclear(void *ptr, struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
214 void tagclear(struct ___Object___ * obj, struct ___TagDescriptor___ * tagd) {
216 /* We'll assume that tag is alway there.
217 Need to statically check for this of course. */
218 struct ___Object___ * tagptr=obj->___tags___;
220 if (tagptr->type==TAGTYPE) {
221 if ((struct ___TagDescriptor___ *)tagptr==tagd)
222 obj->___tags___=NULL;
224 printf("ERROR 1 in tagclear\n");
226 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
228 for(i=0;i<ao->___cachedCode___;i++) {
229 struct ___TagDescriptor___ * td=ARRAYGET(ao, struct ___TagDescriptor___ *, i);
231 ao->___cachedCode___--;
232 if (i<ao->___cachedCode___)
233 ARRAYSET(ao, struct ___TagDescriptor___ *, i, ARRAYGET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___));
234 ARRAYSET(ao, struct ___TagDescriptor___ *, ao->___cachedCode___, NULL);
235 if (ao->___cachedCode___==0)
236 obj->___tags___=NULL;
240 printf("ERROR 2 in tagclear\n");
244 struct ___Object___ *tagset=tagd->flagptr;
245 if (tagset->type!=OBJECTARRAYTYPE) {
249 printf("ERROR 3 in tagclear\n");
251 struct ArrayObject *ao=(struct ArrayObject *) tagset;
253 for(i=0;i<ao->___cachedCode___;i++) {
254 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, i);
256 ao->___cachedCode___--;
257 if (i<ao->___cachedCode___)
258 ARRAYSET(ao, struct ___Object___ *, i, ARRAYGET(ao, struct ___Object___ *, ao->___cachedCode___));
259 ARRAYSET(ao, struct ___Object___ *, ao->___cachedCode___, NULL);
260 if (ao->___cachedCode___==0)
265 printf("ERROR 4 in tagclear\n");
273 /* This function allocates a new tag. */
275 struct ___TagDescriptor___ * allocate_tag(void *ptr, int index) {
276 struct ___TagDescriptor___ * v=(struct ___TagDescriptor___ *) mygcmalloc((struct garbagelist *) ptr, classsize[TAGTYPE]);
278 struct ___TagDescriptor___ * allocate_tag(int index) {
279 struct ___TagDescriptor___ * v=FREEMALLOC(classsize[TAGTYPE]);
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. */
291 void flagbody(struct ___Object___ *ptr, int flag);
293 void flagorand(void * ptr, int ormask, int andmask) {
294 int oldflag=((int *)ptr)[1];
295 int flag=ormask|oldflag;
297 // Not sure why this was necessary
298 // if (flag==oldflag) /* Don't do anything */
304 void intflagorand(void * ptr, int ormask, int andmask) {
305 int oldflag=((int *)ptr)[1];
306 int flag=ormask|oldflag;
308 if (flag==oldflag) /* Don't do anything */
310 else flagbody(ptr, flag);
313 void flagorandinit(void * ptr, int ormask, int andmask) {
314 int oldflag=((int *)ptr)[1];
315 int flag=ormask|oldflag;
320 void flagbody(struct ___Object___ *ptr, int flag) {
321 struct parameterwrapper *flagptr=(struct parameterwrapper *)ptr->flagptr;
324 /*Remove object from all queues */
325 while(flagptr!=NULL) {
326 struct parameterwrapper *next;
327 struct ___Object___ * tag=ptr->___tags___;
328 RuntimeHashget(flagptr->objectset, (int) ptr, (int *) &next);
329 RuntimeHashremove(flagptr->objectset, (int)ptr, (int) next);
334 struct QueueItem *tmpptr;
335 struct parameterwrapper * parameter=objectqueues[ptr->type];
337 struct parameterwrapper * prevptr=NULL;
338 struct ___Object___ *tagptr=ptr->___tags___;
340 /* Outer loop iterates through all parameter queues an object of
341 this type could be in. */
343 while(parameter!=NULL) {
345 if (parameter->numbertags>0) {
347 goto nextloop;//that means the object has no tag but that param needs tag
348 else if(tagptr->type==TAGTYPE) {//one tag
349 struct ___TagDescriptor___ * tag=(struct ___TagDescriptor___*) tagptr;
350 for(i=0;i<parameter->numbertags;i++) {
351 //slotid is parameter->tagarray[2*i];
352 int tagid=parameter->tagarray[2*i+1];
353 if (tagid!=tagptr->flag)
354 goto nextloop; /*We don't have this tag */
356 } else {//multiple tags
357 struct ArrayObject * ao=(struct ArrayObject *) tagptr;
358 for(i=0;i<parameter->numbertags;i++) {
359 //slotid is parameter->tagarray[2*i];
360 int tagid=parameter->tagarray[2*i+1];
362 for(j=0;j<ao->___cachedCode___;j++) {
363 if (tagid==ARRAYGET(ao, struct ___TagDescriptor___*, i)->flag)
374 for(i=0;i<parameter->numberofterms;i++) {
375 int andmask=parameter->intarray[i*2];
376 int checkmask=parameter->intarray[i*2+1];
377 if ((flag&andmask)==checkmask) {
378 enqueuetasks(parameter, prevptr, ptr);
384 parameter=parameter->next;
386 ptr->flagptr=prevptr;
390 void enqueuetasks(struct parameterwrapper *parameter, struct parameterwrapper *prevptr, struct ___Object___ *ptr) {
391 void * taskpointerarray[MAXTASKPARAMS];
393 int numparams=parameter->task->numParameters;
394 int numiterators=parameter->task->numTotal-1;
396 struct taskdescriptor * task=parameter->task;
398 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);//this add the object to parameterwrapper
400 /* Add enqueued object to parameter vector */
401 taskpointerarray[parameter->slot]=ptr;
403 /* Reset iterators */
404 for(j=0;j<numiterators;j++) {
405 toiReset(¶meter->iterators[j]);
408 /* Find initial state */
409 for(j=0;j<numiterators;j++) {
411 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
412 toiNext(¶meter->iterators[j], taskpointerarray);
414 /* Need to backtrack */
415 toiReset(¶meter->iterators[j]);
419 /* Nothing to enqueue */
426 /* Enqueue current state */
427 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
429 tpd->numParameters=numiterators+1;
430 tpd->parameterArray=RUNMALLOC(sizeof(void *)*(numiterators+1));
431 for(j=0;j<=numiterators;j++)
432 tpd->parameterArray[j]=taskpointerarray[j];//store the actual parameters
435 if (!gencontains(failedtasks, tpd)&&!gencontains(activetasks,tpd)) {
436 genputtable(activetasks, tpd, tpd);
438 RUNFREE(tpd->parameterArray);
442 /* This loop iterates to the next parameter combination */
446 for(j=numiterators-1; j<numiterators;j++) {
448 if(toiHasNext(¶meter->iterators[j], taskpointerarray))
449 toiNext(¶meter->iterators[j], taskpointerarray);
451 /* Need to backtrack */
452 toiReset(¶meter->iterators[j]);
456 /* Nothing more to enqueue */
463 /* Handler for signals. The signals catch null pointer errors and
464 arithmatic errors. */
466 void myhandler(int sig, siginfo_t *info, void *uap) {
468 printf("sig=%d\n",sig);
471 longjmp(error_handler,1);
476 struct RuntimeHash *fdtoobject;
478 void addreadfd(int fd) {
481 FD_SET(fd, &readfds);
484 void removereadfd(int fd) {
485 FD_CLR(fd, &readfds);
486 if (maxreadfd==(fd+1)) {
488 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
499 void executetasks() {
500 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
502 /* Set up signal handlers */
503 struct sigaction sig;
504 sig.sa_sigaction=&myhandler;
505 sig.sa_flags=SA_SIGINFO;
506 sigemptyset(&sig.sa_mask);
508 /* Catch bus errors, segmentation faults, and floating point exceptions*/
509 sigaction(SIGBUS,&sig,0);
510 sigaction(SIGSEGV,&sig,0);
511 sigaction(SIGFPE,&sig,0);
512 sigaction(SIGPIPE,&sig,0);
517 fdtoobject=allocateRuntimeHash(100);
519 /* Map first block of memory to protected, anonymous page */
520 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
523 while((hashsize(activetasks)>0)||(maxreadfd>0)) {
525 /* Check if any filedescriptors have IO pending */
528 struct timeval timeout={0,0};
532 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
534 /* Process ready fd's */
536 for(fd=0;fd<maxreadfd;fd++) {
537 if (FD_ISSET(fd, &tmpreadfds)) {
538 /* Set ready flag on object */
540 // printf("Setting fd %d\n",fd);
541 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
542 intflagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
549 /* See if there are any active tasks */
550 if (hashsize(activetasks)>0) {
552 currtpd=(struct taskparamdescriptor *) getfirstkey(activetasks);
553 genfreekey(activetasks, currtpd);
555 /* Check if this task has failed */
556 if (gencontains(failedtasks, currtpd)) {
557 // Free up task parameter descriptor
558 RUNFREE(currtpd->parameterArray);
562 int numparams=currtpd->task->numParameters;
563 int numtotal=currtpd->task->numTotal;
565 /* Make sure that the parameters are still in the queues */
566 for(i=0;i<numparams;i++) {
567 void * parameter=currtpd->parameterArray[i];
568 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
569 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
571 /* Check that object is still in queue */
572 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
573 RUNFREE(currtpd->parameterArray);
577 /* Check that object still has necessary tags */
578 for(j=0;j<pd->numbertags;j++) {
579 int slotid=pd->tagarray[2*j]+numparams;
580 struct ___TagDescriptor___ *tagd=currtpd->parameterArray[slotid];
581 if (!containstag(parameter, tagd)) {
582 RUNFREE(currtpd->parameterArray);
588 taskpointerarray[i+OFFSET]=parameter;
591 for(;i<numtotal;i++) {
592 taskpointerarray[i+OFFSET]=currtpd->parameterArray[i];
596 /* Checkpoint the state */
597 forward=allocateRuntimeHash(100);
598 reverse=allocateRuntimeHash(100);
599 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
601 if (x=setjmp(error_handler)) {
605 printf("Fatal Error=%d, Recovering!\n",x);
607 genputtable(failedtasks,currtpd,currtpd);
608 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
609 /*where we have to insert the code for optional tasks
610 all the pointers I need are in currtpd->parameterArray */
611 freeRuntimeHash(forward);
612 freeRuntimeHash(reverse);
617 if (injectfailures) {
618 if ((((double)random())/RAND_MAX)<failurechance) {
619 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
620 longjmp(error_handler,10);
623 /* Actually call task */
625 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
626 taskpointerarray[1]=NULL;
630 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
631 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
632 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
634 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
635 freeRuntimeHash(forward);
636 freeRuntimeHash(reverse);
638 // Free up task parameter descriptor
639 RUNFREE(currtpd->parameterArray);
649 /* This function processes an objects tags */
650 void processtags(struct parameterdescriptor *pd, int index, struct parameterwrapper *parameter, int * iteratorcount, int *statusarray, int numparams) {
653 for(i=0;i<pd->numbertags;i++) {
654 int slotid=pd->tagarray[2*i];
655 int tagid=pd->tagarray[2*i+1];
657 if (statusarray[slotid+numparams]==0) {
658 parameter->iterators[*iteratorcount].istag=1;
659 parameter->iterators[*iteratorcount].tagid=tagid;
660 parameter->iterators[*iteratorcount].slot=slotid+numparams;
661 parameter->iterators[*iteratorcount].tagobjectslot=index;
662 statusarray[slotid+numparams]=1;
669 void processobject(struct parameterwrapper *parameter, int index, struct parameterdescriptor *pd, int *iteratorcount, int * statusarray, int numparams) {
672 struct RuntimeHash * objectset=((struct parameterwrapper *)pd->queue)->objectset;
674 parameter->iterators[*iteratorcount].istag=0;
675 parameter->iterators[*iteratorcount].slot=index;
676 parameter->iterators[*iteratorcount].objectset=objectset;
677 statusarray[index]=1;
679 for(i=0;i<pd->numbertags;i++) {
680 int slotid=pd->tagarray[2*i];
681 int tagid=pd->tagarray[2*i+1];
682 if (statusarray[slotid+numparams]!=0) {
683 /* This tag has already been enqueued, use it to narrow search */
684 parameter->iterators[*iteratorcount].tagbindings[tagcount]=slotid+numparams;
688 parameter->iterators[*iteratorcount].numtags=tagcount;
693 /* This function builds the iterators for a task & parameter */
695 void builditerators(struct taskdescriptor * task, int index, struct parameterwrapper * parameter) {
696 int statusarray[MAXTASKPARAMS];
698 int numparams=task->numParameters;
700 for(i=0;i<MAXTASKPARAMS;i++) statusarray[i]=0;
702 statusarray[index]=1; /* Initial parameter */
703 /* Process tags for initial iterator */
705 processtags(task->descriptorarray[index], index, parameter, & iteratorcount, statusarray, numparams);
709 /* Check for objects with existing tags */
710 for(i=0;i<numparams;i++) {
711 if (statusarray[i]==0) {
712 struct parameterdescriptor *pd=task->descriptorarray[i];
714 for(j=0;j<pd->numbertags;j++) {
715 int slotid=pd->tagarray[2*j];
716 if(statusarray[slotid+numparams]!=0) {
717 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
718 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
725 /* Next do objects w/ unbound tags*/
727 for(i=0;i<numparams;i++) {
728 if (statusarray[i]==0) {
729 struct parameterdescriptor *pd=task->descriptorarray[i];
730 if (pd->numbertags>0) {
731 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
732 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
738 /* Nothing with a tag enqueued */
740 for(i=0;i<numparams;i++) {
741 if (statusarray[i]==0) {
742 struct parameterdescriptor *pd=task->descriptorarray[i];
743 processobject(parameter, i, pd, &iteratorcount, statusarray, numparams);
744 processtags(pd, i, parameter, &iteratorcount, statusarray, numparams);
757 /* This function processes the task information to create queues for
758 each parameter type. */
760 void processtasks() {
762 for(i=0;i<numtasks;i++) {
763 struct taskdescriptor * task=taskarray[i];
766 for(j=0;j<task->numParameters;j++) {
767 struct parameterdescriptor *param=task->descriptorarray[j];
768 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
769 struct parameterwrapper ** ptr=&objectqueues[param->type];
771 param->queue=parameter;
772 parameter->objectset=allocateRuntimeHash(10);
773 parameter->numberofterms=param->numberterms;
774 parameter->intarray=param->intarray;
775 parameter->numbertags=param->numbertags;
776 parameter->tagarray=param->tagarray;
777 parameter->task=task;
778 /* Link new queue in */
784 /* Build iterators for parameters */
785 for(j=0;j<task->numParameters;j++) {
786 struct parameterdescriptor *param=task->descriptorarray[j];
787 struct parameterwrapper *parameter=param->queue;
789 builditerators(task, j, parameter);
794 void toiReset(struct tagobjectiterator * it) {
797 } else if (it->numtags>0) {
800 RuntimeHashiterator(it->objectset, &it->it);
804 int toiHasNext(struct tagobjectiterator *it, void ** objectarray) {
807 /* Get object with tags */
808 struct ___Object___ *obj=objectarray[it->tagobjectslot];
809 struct ___Object___ *tagptr=obj->___tags___;
810 if (tagptr->type==TAGTYPE) {
811 if ((it->tagobjindex==0)&& /* First object */
812 (it->tagid==((struct ___TagDescriptor___ *)tagptr)->flag)) /* Right tag type */
817 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
818 int tagindex=it->tagobjindex;
819 for(;tagindex<ao->___cachedCode___;tagindex++) {
820 struct ___TagDescriptor___ *td=ARRAYGET(ao, struct ___TagDescriptor___ *, tagindex);
821 if (td->flag==it->tagid) {
822 it->tagobjindex=tagindex; /* Found right type of tag */
828 } else if (it->numtags>0) {
829 /* Use tags to locate appropriate objects */
830 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
831 struct ___Object___ *objptr=tag->flagptr;
833 if (objptr->type!=OBJECTARRAYTYPE) {
834 if (it->tagobjindex>0)
836 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
838 for(i=1;i<it->numtags;i++) {
839 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
840 if (!containstag(objptr,tag2))
845 struct ArrayObject *ao=(struct ArrayObject *) objptr;
848 for(tagindex=it->tagobjindex;tagindex<ao->___cachedCode___;tagindex++) {
849 struct ___Object___ *objptr=ARRAYGET(ao, struct ___Object___*, tagindex);
850 if (!RuntimeHashcontainskey(it->objectset, (int) objptr))
852 for(i=1;i<it->numtags;i++) {
853 struct ___TagDescriptor___ *tag2=objectarray[it->tagbindings[i]];
854 if (!containstag(objptr,tag2))
861 it->tagobjindex=tagindex;
865 return RunhasNext(&it->it);
869 int containstag(struct ___Object___ *ptr, struct ___TagDescriptor___ *tag) {
871 struct ___Object___ * objptr=tag->flagptr;
872 if (objptr->type==OBJECTARRAYTYPE) {
873 struct ArrayObject *ao=(struct ArrayObject *)objptr;
874 for(j=0;j<ao->___cachedCode___;j++) {
875 if (ptr==ARRAYGET(ao, struct ___Object___*, j))
883 void toiNext(struct tagobjectiterator *it , void ** objectarray) {
884 /* hasNext has all of the intelligence */
887 /* Get object with tags */
888 struct ___Object___ *obj=objectarray[it->tagobjectslot];
889 struct ___Object___ *tagptr=obj->___tags___;
890 if (tagptr->type==TAGTYPE) {
892 objectarray[it->slot]=tagptr;
894 struct ArrayObject *ao=(struct ArrayObject *) tagptr;
895 objectarray[it->slot]=ARRAYGET(ao, struct ___TagDescriptor___ *, it->tagobjindex++);
897 } else if (it->numtags>0) {
898 /* Use tags to locate appropriate objects */
899 struct ___TagDescriptor___ *tag=objectarray[it->tagbindings[0]];
900 struct ___Object___ *objptr=tag->flagptr;
901 if (objptr->type!=OBJECTARRAYTYPE) {
903 objectarray[it->slot]=objptr;
905 struct ArrayObject *ao=(struct ArrayObject *) objptr;
906 objectarray[it->slot]=ARRAYGET(ao, struct ___Object___ *, it->tagobjindex++);
910 objectarray[it->slot]=(void *)Runkey(&it->it);