changes
[IRC.git] / Robust / src / Runtime / garbage.c
1 #include "garbage.h"
2 #include "runtime.h"
3 #include "structdefs.h"
4 #include "Queue.h"
5 #include "SimpleHash.h"
6 #include "chash.h"
7 #include "GenericHashtable.h"
8 #include <string.h>
9 #if defined(THREADS) || defined(DSTM)
10 #include "thread.h"
11 #endif
12
13 #ifdef DMALLOC
14 #include "dmalloc.h"
15 #endif
16 #ifdef DSTM
17 #include "dstm.h"
18 #endif
19
20 #define NUMPTRS 100
21
22 #define INITIALHEAPSIZE 32*1024*1024
23 #define GCPOINT(x) ((int)((x)*0.9))
24 /* This define takes in how full the heap is initially and returns a new heap size to use */
25 #define HEAPSIZE(x,y) (((int)((x)/0.6))+y)
26
27 #ifdef TASK
28 extern struct genhashtable * activetasks;
29 #ifndef MULTICORE
30 extern struct parameterwrapper * objectqueues[NUMCLASSES];
31 #endif
32 extern struct genhashtable * failedtasks;
33 extern struct taskparamdescriptor *currtpd;
34 extern struct ctable *forward;
35 extern struct ctable *reverse;
36 extern struct RuntimeHash *fdtoobject;
37 #endif
38
39 #if defined(THREADS) || defined(DSTM)
40 int needtocollect=0;
41 struct listitem * list=NULL;
42 int listcount=0;
43 #endif
44
45 //Need to check if pointers are transaction pointers
46 //this also catches the special flag value of 1 for local copies
47 #ifdef DSTM
48 #define ENQUEUE(orig, dst) \
49   if ((!(((unsigned int)orig)&0x1))) { \
50     if (orig>=curr_heapbase&&orig<curr_heaptop) { \
51       void *copy; \
52       if (gc_createcopy(orig,&copy)) \
53         enqueue(orig);\
54       dst=copy; \
55     } \
56   }
57 #elif defined(FASTCHECK)
58 #define ENQUEUE(orig, dst) \
59   if (((unsigned int)orig)!=1) { \
60   void *copy; \
61   if (gc_createcopy(orig,&copy)) \
62     enqueue(orig);\
63   dst=copy; }
64 #else
65 #define ENQUEUE(orig, dst) \
66   void *copy; \
67   if (gc_createcopy(orig,&copy)) \
68     enqueue(orig);\
69   dst=copy
70 #endif
71
72 struct pointerblock {
73   void * ptrs[NUMPTRS];
74   struct pointerblock *next;
75 };
76
77 void * curr_heapbase=0;
78 void * curr_heapptr=0;
79 void * curr_heapgcpoint=0;
80 void * curr_heaptop=0;
81
82 void * to_heapbase=0;
83 void * to_heapptr=0;
84 void * to_heaptop=0;
85 long lastgcsize=0;
86
87 struct pointerblock *head=NULL;
88 int headindex=0;
89 struct pointerblock *tail=NULL;
90 int tailindex=0;
91 struct pointerblock *spare=NULL;
92
93 void enqueue(void *ptr) {
94   if (headindex==NUMPTRS) {
95     struct pointerblock * tmp;
96     if (spare!=NULL) {
97       tmp=spare;
98       spare=NULL;
99     } else
100       tmp=malloc(sizeof(struct pointerblock));
101     head->next=tmp;
102     head=tmp;
103     headindex=0;
104   }
105   head->ptrs[headindex++]=ptr;
106 }
107
108 void * dequeue() {
109   if (tailindex==NUMPTRS) {
110     struct pointerblock *tmp=tail;
111     tail=tail->next;
112     tailindex=0;
113     if (spare!=NULL)
114       free(tmp);
115     else
116       spare=tmp;
117   }
118   return tail->ptrs[tailindex++];
119 }
120
121 int moreItems() {
122   if ((head==tail)&&(tailindex==headindex))
123     return 0;
124   return 1;
125 }
126
127 #ifdef TASK
128 struct pointerblock *taghead=NULL;
129 int tagindex=0;
130
131 void enqueuetag(struct ___TagDescriptor___ *ptr) {
132   if (tagindex==NUMPTRS) {
133     struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
134     tmp->next=taghead;
135     taghead=tmp;
136     tagindex=0;
137   }
138   taghead->ptrs[tagindex++]=ptr;
139 }
140 #endif
141
142
143 void collect(struct garbagelist * stackptr) {
144 #if defined(THREADS)||defined(DSTM)
145   needtocollect=1;
146   pthread_mutex_lock(&gclistlock);
147   while(1) {
148     if ((listcount+1)==threadcount) {
149       break; /* Have all other threads stopped */
150     }
151     pthread_cond_wait(&gccond, &gclistlock);
152   }
153 #endif
154
155   if (head==NULL) {
156     headindex=0;
157     tailindex=0;
158     head=tail=malloc(sizeof(struct pointerblock));
159   }
160
161 #ifdef TASK
162   if (taghead==NULL) {
163     tagindex=0;
164     taghead=malloc(sizeof(struct pointerblock));
165     taghead->next=NULL;
166   }
167 #endif
168
169   /* Check current stack */
170 #if defined(THREADS)||defined(DSTM)
171   {
172     struct listitem *listptr=list;
173     while(1) {
174 #endif
175
176   while(stackptr!=NULL) {
177     int i;
178     for(i=0; i<stackptr->size; i++) {
179       void * orig=stackptr->array[i];
180       ENQUEUE(orig, stackptr->array[i]);
181     }
182     stackptr=stackptr->next;
183   }
184 #if defined(THREADS)||defined(DSTM)
185   /* Go to next thread */
186   if (listptr!=NULL) {
187     void * orig=listptr->locklist;
188     ENQUEUE(orig, listptr->locklist);
189     stackptr=listptr->stackptr;
190     listptr=listptr->next;
191   } else
192     break;
193 }
194 }
195 #endif
196
197 #ifdef FASTCHECK
198   ENQUEUE(___fcrevert___, ___fcrevert___);
199 #endif
200
201 #ifdef TASK
202   {
203     /* Update objectsets */
204     int i;
205     for(i=0; i<NUMCLASSES; i++) {
206 #ifdef MULTICORE
207 #else
208       struct parameterwrapper * p=objectqueues[i];
209       while(p!=NULL) {
210         struct ObjectHash * set=p->objectset;
211         struct ObjectNode * ptr=set->listhead;
212         while(ptr!=NULL) {
213           void *orig=(void *)ptr->key;
214           ENQUEUE(orig, *((void **)(&ptr->key)));
215           ptr=ptr->lnext;
216         }
217         ObjectHashrehash(set); /* Rehash the table */
218         p=p->next;
219       }
220 #endif
221     }
222   }
223
224 #ifndef FASTCHECK
225   if (forward!=NULL) {
226     struct cnode * ptr=forward->listhead;
227     while(ptr!=NULL) {
228       void * orig=(void *)ptr->key;
229       ENQUEUE(orig, *((void **)(&ptr->key)));
230       ptr=ptr->lnext;
231     }
232     crehash(forward); /* Rehash the table */
233   }
234
235   if (reverse!=NULL) {
236     struct cnode * ptr=reverse->listhead;
237     while(ptr!=NULL) {
238       void *orig=(void *)ptr->val;
239       ENQUEUE(orig, *((void**)(&ptr->val)));
240       ptr=ptr->lnext;
241     }
242   }
243 #endif
244
245   {
246     struct RuntimeNode * ptr=fdtoobject->listhead;
247     while(ptr!=NULL) {
248       void *orig=(void *)ptr->data;
249       ENQUEUE(orig, *((void**)(&ptr->data)));
250       ptr=ptr->lnext;
251     }
252   }
253
254   {
255     /* Update current task descriptor */
256     int i;
257     for(i=0; i<currtpd->numParameters; i++) {
258       void *orig=currtpd->parameterArray[i];
259       ENQUEUE(orig, currtpd->parameterArray[i]);
260     }
261
262   }
263
264   /* Update active tasks */
265   {
266     struct genpointerlist * ptr=activetasks->list;
267     while(ptr!=NULL) {
268       struct taskparamdescriptor *tpd=ptr->src;
269       int i;
270       for(i=0; i<tpd->numParameters; i++) {
271         void * orig=tpd->parameterArray[i];
272         ENQUEUE(orig, tpd->parameterArray[i]);
273       }
274       ptr=ptr->inext;
275     }
276     genrehash(activetasks);
277   }
278
279   /* Update failed tasks */
280   {
281     struct genpointerlist * ptr=failedtasks->list;
282     while(ptr!=NULL) {
283       struct taskparamdescriptor *tpd=ptr->src;
284       int i;
285       for(i=0; i<tpd->numParameters; i++) {
286         void * orig=tpd->parameterArray[i];
287         ENQUEUE(orig, tpd->parameterArray[i]);
288       }
289       ptr=ptr->inext;
290     }
291     genrehash(failedtasks);
292   }
293 #endif
294
295   while(moreItems()) {
296     void * ptr=dequeue();
297     void *cpy=((void **)ptr)[1];
298     int type=((int *)cpy)[0];
299     unsigned int * pointer;
300 #ifdef TASK
301     if(type==TAGTYPE) {
302       /* Enqueue Tag */
303       /* Nothing is inside */
304       enqueuetag(ptr);
305       continue;
306     }
307 #endif
308     pointer=pointerarray[type];
309     if (pointer==0) {
310       /* Array of primitives */
311       /* Do nothing */
312 #ifdef DSTM
313       struct ArrayObject *ao=(struct ArrayObject *) ptr;
314       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
315       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
316       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
317 #endif
318     } else if (((int)pointer)==1) {
319       /* Array of pointers */
320       struct ArrayObject *ao=(struct ArrayObject *) ptr;
321       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
322 #ifdef DSTM
323       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
324       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
325 #endif
326       int length=ao->___length___;
327       int i;
328       for(i=0; i<length; i++) {
329         void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
330         ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
331       }
332     } else {
333       int size=pointer[0];
334       int i;
335       for(i=1; i<=size; i++) {
336         unsigned int offset=pointer[i];
337         void * objptr=*((void **)(((int)ptr)+offset));
338         ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
339       }
340     }
341   }
342 #ifdef TASK
343   fixtags();
344 #endif
345
346 #if defined(THREADS)||defined(DSTM)
347   needtocollect=0;
348   pthread_mutex_unlock(&gclistlock);
349 #endif
350 }
351
352 #ifdef TASK
353
354 /* Fix up the references from tags.  This can't be done earlier,
355    because we don't want tags to keep objects alive */
356 void fixtags() {
357   while(taghead!=NULL) {
358     int i;
359     struct pointerblock *tmp=taghead->next;
360     for(i=0; i<tagindex; i++) {
361       struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
362       struct ___Object___ *obj=tagd->flagptr;
363       struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
364       if (obj==NULL) {
365         /* Zero object case */
366       } else if (obj->type==-1) {
367         /* Single object case */
368         copy->flagptr=((struct ___Object___**)obj)[1];
369       } else if (obj->type==OBJECTARRAYTYPE) {
370         /* Array case */
371         struct ArrayObject *ao=(struct ArrayObject *) obj;
372         int livecount=0;
373         int j;
374         int k=0;
375         struct ArrayObject *aonew;
376
377         /* Count live objects */
378         for(j=0; j<ao->___cachedCode___; j++) {
379           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
380           if (tobj->type==-1)
381             livecount++;
382         }
383
384         livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
385         aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
386         memcpy(aonew, ao, sizeof(struct ArrayObject));
387         aonew->type=OBJECTARRAYTYPE;
388         aonew->___length___=livecount;
389         copy->flagptr=aonew;
390         for(j=0; j<ao->___cachedCode___; j++) {
391           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
392           if (tobj->type==-1) {
393             struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
394             ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
395           }
396         }
397         aonew->___cachedCode___=k;
398         for(; k<livecount; k++) {
399           ARRAYSET(aonew, struct ___Object___*, k, NULL);
400         }
401       } else {
402         /* No object live anymore */
403         copy->flagptr=NULL;
404       }
405     }
406     free(taghead);
407     taghead=tmp;
408     tagindex=NUMPTRS;
409   }
410 }
411 #endif
412
413 void * tomalloc(int size) {
414   void * ptr=to_heapptr;
415   if ((size%4)!=0)
416     size+=(4-(size%4));
417   to_heapptr+=size;
418   return ptr;
419 }
420
421 #if defined(THREADS)||defined(DSTM)
422 void checkcollect(void * ptr) {
423   struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
424   pthread_mutex_lock(&gclock); // Wait for GC
425   restartaftergc(tmp);
426   pthread_mutex_unlock(&gclock);
427 }
428
429 #ifdef DSTM
430 void checkcollect2(void * ptr, transrecord_t *trans) {
431   int ptrarray[]={1, (int)ptr, (int) trans->revertlist};
432   struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
433   pthread_mutex_lock(&gclock); // Wait for GC
434   restartaftergc(tmp);
435   pthread_mutex_unlock(&gclock);
436   trans->revertlist=(struct ___Object___*)ptrarray[2];
437 }
438 #endif
439
440
441 struct listitem * stopforgc(struct garbagelist * ptr) {
442   struct listitem * litem=malloc(sizeof(struct listitem));
443   litem->stackptr=ptr;
444   litem->locklist=pthread_getspecific(threadlocks);
445   litem->prev=NULL;
446   pthread_mutex_lock(&gclistlock);
447   litem->next=list;
448   if(list!=NULL)
449     list->prev=litem;
450   list=litem;
451   listcount++;
452   pthread_cond_signal(&gccond);
453   pthread_mutex_unlock(&gclistlock);
454   return litem;
455 }
456
457 void restartaftergc(struct listitem * litem) {
458   pthread_mutex_lock(&gclistlock);
459   pthread_setspecific(threadlocks, litem->locklist);
460   if (litem->prev==NULL) {
461     list=litem->next;
462   } else {
463     litem->prev->next=litem->next;
464   }
465   if (litem->next!=NULL) {
466     litem->next->prev=litem->prev;
467   }
468   listcount--;
469   pthread_mutex_unlock(&gclistlock);
470   free(litem);
471 }
472 #endif
473
474 void * mygcmalloc(struct garbagelist * stackptr, int size) {
475   void *ptr;
476 #if defined(THREADS)||defined(DSTM)
477   if (pthread_mutex_trylock(&gclock)!=0) {
478     struct listitem *tmp=stopforgc(stackptr);
479     pthread_mutex_lock(&gclock);
480     restartaftergc(tmp);
481   }
482 #endif
483   ptr=curr_heapptr;
484   if ((size%4)!=0)
485     size+=(4-(size%4));
486   curr_heapptr+=size;
487   if (curr_heapptr>curr_heapgcpoint) {
488     if (curr_heapbase==0) {
489       /* Need to allocate base heap */
490       curr_heapbase=malloc(INITIALHEAPSIZE);
491       bzero(curr_heapbase, INITIALHEAPSIZE);
492       curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
493       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
494       curr_heapptr=curr_heapbase+size;
495
496       to_heapbase=malloc(INITIALHEAPSIZE);
497       to_heaptop=to_heapbase+INITIALHEAPSIZE;
498       to_heapptr=to_heapbase;
499       ptr=curr_heapbase;
500 #if defined(THREADS)||defined(DSTM)
501       pthread_mutex_unlock(&gclock);
502 #endif
503       return ptr;
504     }
505
506     /* Grow the to heap if necessary */
507     {
508       int curr_heapsize=curr_heaptop-curr_heapbase;
509       int to_heapsize=to_heaptop-to_heapbase;
510       int last_heapsize=0;
511       if (lastgcsize>0) {
512         last_heapsize=HEAPSIZE(lastgcsize, size);
513         if ((last_heapsize%4)!=0)
514           last_heapsize+=(4-(last_heapsize%4));
515       }
516       if (curr_heapsize>last_heapsize)
517         last_heapsize=curr_heapsize;
518       if (last_heapsize>to_heapsize) {
519         free(to_heapbase);
520         to_heapbase=malloc(last_heapsize);
521         if (to_heapbase==NULL) {
522           printf("Error Allocating enough memory\n");
523           exit(-1);
524         }
525         to_heaptop=to_heapbase+last_heapsize;
526         to_heapptr=to_heapbase;
527       }
528     }
529
530     /* Do our collection */
531     collect(stackptr);
532
533     /* Update stat on previous gc size */
534     lastgcsize=(to_heapptr-to_heapbase)+size;
535
536     /* Flip to/curr heaps */
537     {
538       void * tmp=to_heapbase;
539       to_heapbase=curr_heapbase;
540       curr_heapbase=tmp;
541
542       tmp=to_heaptop;
543       to_heaptop=curr_heaptop;
544       curr_heaptop=tmp;
545
546       tmp=to_heapptr;
547       curr_heapptr=to_heapptr+size;
548       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
549       to_heapptr=to_heapbase;
550
551       /* Not enough room :(, redo gc */
552       if (curr_heapptr>curr_heapgcpoint) {
553 #if defined(THREADS)||defined(DSTM)
554         pthread_mutex_unlock(&gclock);
555 #endif
556         return mygcmalloc(stackptr, size);
557       }
558
559       bzero(tmp, curr_heaptop-tmp);
560 #if defined(THREADS)||defined(DSTM)
561       pthread_mutex_unlock(&gclock);
562 #endif
563       return tmp;
564     }
565   } else {
566 #if defined(THREADS)||defined(DSTM)
567     pthread_mutex_unlock(&gclock);
568 #endif
569     return ptr;
570   }
571 }
572
573
574 int gc_createcopy(void * orig, void ** copy_ptr) {
575   if (orig==0) {
576     *copy_ptr=NULL;
577     return 0;
578   } else {
579     int type=((int *)orig)[0];
580     if (type==-1) {
581       *copy_ptr=((void **)orig)[1];
582       return 0;
583     }
584     if (type<NUMCLASSES) {
585       /* We have a normal object */
586       int size=classsize[type];
587       void *newobj=tomalloc(size);
588       memcpy(newobj, orig, size);
589       ((int *)orig)[0]=-1;
590       ((void **)orig)[1]=newobj;
591       *copy_ptr=newobj;
592       return 1;
593     } else {
594       /* We have an array */
595       struct ArrayObject *ao=(struct ArrayObject *)orig;
596       int elementsize=classsize[type];
597       int length=ao->___length___;
598       int size=sizeof(struct ArrayObject)+length*elementsize;
599       void *newobj=tomalloc(size);
600       memcpy(newobj, orig, size);
601       ((int *)orig)[0]=-1;
602       ((void **)orig)[1]=newobj;
603       *copy_ptr=newobj;
604       return 1;
605     }
606   }
607 }