changes.
[IRC.git] / Robust / src / Runtime / taskgarbage.c
1 #include "garbage.h"
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include "SimpleHash.h"
5 #include "GenericHashtable.h"
6 #include <string.h>
7
8 #ifdef TASK
9
10 extern struct genhashtable * activetasks;
11 extern struct genhashtable * failedtasks;
12 extern struct taskparamdescriptor *currtpd;
13 extern struct ctable *forward;
14 extern struct ctable *reverse;
15 extern struct RuntimeHash *fdtoobject;
16
17 #ifndef MULTICORE
18 extern struct parameterwrapper * objectqueues[NUMCLASSES];
19 #endif
20
21
22 void searchtaskroots() {
23   {
24     /* Update objectsets */
25     int i;
26     for(i=0; i<NUMCLASSES; i++) {
27 #if !defined(MULTICORE)
28       struct parameterwrapper * p=objectqueues[i];
29       while(p!=NULL) {
30         struct ObjectHash * set=p->objectset;
31         struct ObjectNode * ptr=set->listhead;
32         while(ptr!=NULL) {
33           void *orig=(void *)ptr->key;
34           ENQUEUE(orig, *((void **)(&ptr->key)));
35           ptr=ptr->lnext;
36         }
37         ObjectHashrehash(set); /* Rehash the table */
38         p=p->next;
39       }
40 #endif
41     }
42   }
43
44 #ifndef FASTCHECK
45   if (forward!=NULL) {
46     struct cnode * ptr=forward->listhead;
47     while(ptr!=NULL) {
48       void * orig=(void *)ptr->key;
49       ENQUEUE(orig, *((void **)(&ptr->key)));
50       ptr=ptr->lnext;
51     }
52     crehash(forward); /* Rehash the table */
53   }
54
55   if (reverse!=NULL) {
56     struct cnode * ptr=reverse->listhead;
57     while(ptr!=NULL) {
58       void *orig=(void *)ptr->val;
59       ENQUEUE(orig, *((void**)(&ptr->val)));
60       ptr=ptr->lnext;
61     }
62   }
63 #endif
64
65   {
66     struct RuntimeNode * ptr=fdtoobject->listhead;
67     while(ptr!=NULL) {
68       void *orig=(void *)ptr->data;
69       ENQUEUE(orig, *((void**)(&ptr->data)));
70       ptr=ptr->lnext;
71     }
72   }
73
74   {
75     /* Update current task descriptor */
76     int i;
77     for(i=0; i<currtpd->numParameters; i++) {
78       void *orig=currtpd->parameterArray[i];
79       ENQUEUE(orig, currtpd->parameterArray[i]);
80     }
81
82   }
83
84   /* Update active tasks */
85   {
86     struct genpointerlist * ptr=activetasks->list;
87     while(ptr!=NULL) {
88       struct taskparamdescriptor *tpd=ptr->src;
89       int i;
90       for(i=0; i<tpd->numParameters; i++) {
91         void * orig=tpd->parameterArray[i];
92         ENQUEUE(orig, tpd->parameterArray[i]);
93       }
94       ptr=ptr->inext;
95     }
96     genrehash(activetasks);
97   }
98
99   /* Update failed tasks */
100   {
101     struct genpointerlist * ptr=failedtasks->list;
102     while(ptr!=NULL) {
103       struct taskparamdescriptor *tpd=ptr->src;
104       int i;
105       for(i=0; i<tpd->numParameters; i++) {
106         void * orig=tpd->parameterArray[i];
107         ENQUEUE(orig, tpd->parameterArray[i]);
108       }
109       ptr=ptr->inext;
110     }
111     genrehash(failedtasks);
112   }
113 }
114
115 struct pointerblock *taghead=NULL;
116 int tagindex=0;
117
118 void enqueuetag(struct ___TagDescriptor___ *ptr) {
119   if (tagindex==NUMPTRS) {
120     struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
121     tmp->next=taghead;
122     taghead=tmp;
123     tagindex=0;
124   }
125   taghead->ptrs[tagindex++]=ptr;
126 }
127
128 /* Fix up the references from tags.  This can't be done earlier,
129    because we don't want tags to keep objects alive */
130 void fixtags() {
131   while(taghead!=NULL) {
132     int i;
133     struct pointerblock *tmp=taghead->next;
134     for(i=0; i<tagindex; i++) {
135       struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
136       struct ___Object___ *obj=tagd->flagptr;
137       struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
138       if (obj==NULL) {
139         /* Zero object case */
140       } else if (obj->type==-1) {
141         /* Single object case */
142         copy->flagptr=((struct ___Object___**)obj)[1];
143       } else if (obj->type==OBJECTARRAYTYPE) {
144         /* Array case */
145         struct ArrayObject *ao=(struct ArrayObject *) obj;
146         int livecount=0;
147         int j;
148         int k=0;
149         struct ArrayObject *aonew;
150
151         /* Count live objects */
152         for(j=0; j<ao->___cachedCode___; j++) {
153           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
154           if (tobj->type==-1)
155             livecount++;
156         }
157
158         livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
159         aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
160         memcpy(aonew, ao, sizeof(struct ArrayObject));
161         aonew->type=OBJECTARRAYTYPE;
162         aonew->___length___=livecount;
163         copy->flagptr=aonew;
164         for(j=0; j<ao->___cachedCode___; j++) {
165           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
166           if (tobj->type==-1) {
167             struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
168             ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
169           }
170         }
171         aonew->___cachedCode___=k;
172         for(; k<livecount; k++) {
173           ARRAYSET(aonew, struct ___Object___*, k, NULL);
174         }
175       } else {
176         /* No object live anymore */
177         copy->flagptr=NULL;
178       }
179     }
180     free(taghead);
181     taghead=tmp;
182     tagindex=NUMPTRS;
183   }
184 }
185 #endif