try to simplify garbage collector lock
[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) || defined(STM)
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 #ifdef STM
20 #include "tm.h"
21 #endif
22
23 #define NUMPTRS 100
24
25 #define INITIALHEAPSIZE 128*1024*1024
26 #define GCPOINT(x) ((int)((x)*0.95))
27 /* This define takes in how full the heap is initially and returns a new heap size to use */
28 #define HEAPSIZE(x,y) ((int)(x+y))*2
29
30 #ifdef TASK
31 extern struct genhashtable * activetasks;
32 #ifndef MULTICORE
33 extern struct parameterwrapper * objectqueues[NUMCLASSES];
34 #endif
35 extern struct genhashtable * failedtasks;
36 extern struct taskparamdescriptor *currtpd;
37 extern struct ctable *forward;
38 extern struct ctable *reverse;
39 extern struct RuntimeHash *fdtoobject;
40 #endif
41
42 #if defined(THREADS) || defined(DSTM) || defined(STM)
43 int needtocollect=0;
44 struct listitem * list=NULL;
45 int listcount=0;
46 #ifndef MAC
47 __thread struct listitem litem;
48 #endif
49 #endif
50
51 //Need to check if pointers are transaction pointers
52 //this also catches the special flag value of 1 for local copies
53 #ifdef DSTM
54 #define ENQUEUE(orig, dst) \
55   if ((!(((unsigned int)orig)&0x1))) { \
56     if (orig>=curr_heapbase&&orig<curr_heaptop) { \
57       void *copy; \
58       if (gc_createcopy(orig,&copy)) \
59         enqueue(orig);\
60       dst=copy; \
61     } \
62   }
63 #elif defined(STM)
64 #define ENQUEUE(orig, dst) \
65   if (orig>=curr_heapbase&&orig<curr_heaptop) { \
66     void *copy; \
67     if (gc_createcopy(orig,&copy)) \
68       enqueue(orig);\
69     dst=copy; \
70   }
71 #define SENQUEUE(orig, dst) \
72   { \
73     void *copy; \
74     if (gc_createcopy(orig,&copy)) \
75       enqueue(orig);\
76     dst=copy; \
77   }
78 #elif defined(FASTCHECK)
79 #define ENQUEUE(orig, dst) \
80   if (((unsigned int)orig)!=1) { \
81     void *copy; \
82     if (gc_createcopy(orig,&copy)) \
83       enqueue(orig);\
84     dst=copy; }
85 #else
86 #define ENQUEUE(orig, dst) \
87   void *copy; \
88   if (gc_createcopy(orig,&copy)) \
89     enqueue(orig);\
90   dst=copy
91 #endif
92
93 struct pointerblock {
94   void * ptrs[NUMPTRS];
95   struct pointerblock *next;
96 };
97
98 void * curr_heapbase=0;
99 void * curr_heapptr=0;
100 void * curr_heapgcpoint=0;
101 void * curr_heaptop=0;
102
103 void * to_heapbase=0;
104 void * to_heapptr=0;
105 void * to_heaptop=0;
106 long lastgcsize=0;
107
108 struct pointerblock *head=NULL;
109 int headindex=0;
110 struct pointerblock *tail=NULL;
111 int tailindex=0;
112 struct pointerblock *spare=NULL;
113
114 void enqueue(void *ptr) {
115   if (headindex==NUMPTRS) {
116     struct pointerblock * tmp;
117     if (spare!=NULL) {
118       tmp=spare;
119       spare=NULL;
120     } else
121       tmp=malloc(sizeof(struct pointerblock));
122     head->next=tmp;
123     head=tmp;
124     headindex=0;
125   }
126   head->ptrs[headindex++]=ptr;
127 }
128
129 void * dequeue() {
130   if (tailindex==NUMPTRS) {
131     struct pointerblock *tmp=tail;
132     tail=tail->next;
133     tailindex=0;
134     if (spare!=NULL)
135       free(tmp);
136     else
137       spare=tmp;
138   }
139   return tail->ptrs[tailindex++];
140 }
141
142 #ifdef STM
143 void fixobjlist(struct objlist * ptr) {
144   while(ptr!=NULL) {
145     int i;
146     for(i=0;i<ptr->offset;i++) {
147       SENQUEUE(ptr->objs[i], ptr->objs[i]);
148     }
149     ptr=ptr->next;
150   }
151 }
152
153 void fixtable(chashlistnode_t ** tc_table, chashlistnode_t **tc_list, cliststruct_t **cstr, unsigned int tc_size) {
154   unsigned int mask=(tc_size<<4)-1;
155   chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
156   chashlistnode_t *ptr=*tc_table;
157   chashlistnode_t *curr;
158   unsigned int i;
159   unsigned int index;
160   int isfirst;
161   chashlistnode_t *newlist=NULL;
162   for(i=0;i<tc_size;i++) {
163     curr=&ptr[i];
164     isfirst=1;
165     do {                      //Inner loop to go through linked lists
166       void * key;
167       chashlistnode_t *tmp,*next;
168       
169       if ((key=(void *)curr->key) == 0) {             //Exit inner loop if there the first element is 0
170         break;                  //key = val =0 for element if not present within the hash table
171       }
172       SENQUEUE(key, key);
173       if (curr->val>=curr_heapbase&&curr->val<curr_heaptop) {
174         SENQUEUE(curr->val, curr->val);
175       } else {
176         //rewrite transaction cache entry
177         void *vptr=curr->val;
178         int type=((int *)vptr)[0];
179         unsigned INTPTR *pointer=pointerarray[type];
180         if (pointer==0) {
181           //array of primitives - do nothing
182           struct ArrayObject *ao=(struct ArrayObject *) vptr;
183           SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
184         } else if (((INTPTR)pointer)==1) {
185           //array of pointers
186           struct ArrayObject *ao=(struct ArrayObject *) vptr;
187           int length=ao->___length___;
188           int i;
189           SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
190           for(i=0; i<length; i++) {
191             void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
192             SENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
193           }
194         } else {
195           INTPTR size=pointer[0];
196           int i;
197           for(i=1; i<=size; i++) {
198             unsigned int offset=pointer[i];
199             void * objptr=*((void **)(((char *)vptr)+offset));
200             SENQUEUE(objptr, *((void **)(((char *)vptr)+offset)));
201           }
202         }
203       }
204
205       next = curr->next;
206       index = (((unsigned INTPTR)key) & mask) >>4;
207
208       curr->key=key;
209       tmp=&node[index];
210       // Insert into the new table
211       if(tmp->key == 0) {
212         tmp->key = curr->key;
213         tmp->val = curr->val;
214         tmp->lnext=newlist;
215         newlist=tmp;
216       } else if (isfirst) {
217         chashlistnode_t *newnode;
218         if ((*cstr)->num<NUMCLIST) {
219           newnode=&(*cstr)->array[(*cstr)->num];
220           (*cstr)->num++;
221         } else {
222           //get new list
223           cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
224           tcl->next=*cstr;
225           *cstr=tcl;
226           newnode=&tcl->array[0];
227           tcl->num=1;
228         }
229         newnode->key = curr->key;
230         newnode->val = curr->val;
231         newnode->next = tmp->next;
232         newnode->lnext=newlist;
233         newlist=newnode;
234         tmp->next=newnode;
235       } else {
236         curr->lnext=newlist;
237         newlist=curr;
238         curr->next=tmp->next;
239         tmp->next=curr;
240       }
241       isfirst = 0;
242       curr = next;
243     } while(curr!=NULL);
244   }
245   free(ptr);
246   (*tc_table)=node;
247   (*tc_list)=newlist;
248 }
249 #endif
250
251 int moreItems() {
252   if ((head==tail)&&(tailindex==headindex))
253     return 0;
254   return 1;
255 }
256
257 #ifdef TASK
258 struct pointerblock *taghead=NULL;
259 int tagindex=0;
260
261 void enqueuetag(struct ___TagDescriptor___ *ptr) {
262   if (tagindex==NUMPTRS) {
263     struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
264     tmp->next=taghead;
265     taghead=tmp;
266     tagindex=0;
267   }
268   taghead->ptrs[tagindex++]=ptr;
269 }
270 #endif
271
272 #if defined(STM)||defined(THREADS)
273 __thread char * memorybase=NULL;
274 __thread char * memorytop=NULL;
275 #endif
276
277
278 void collect(struct garbagelist * stackptr) {
279 #if defined(THREADS)||defined(DSTM)||defined(STM)
280   needtocollect=1;
281   pthread_mutex_lock(&gclistlock);
282   while(1) {
283     if ((listcount+1)==threadcount) {
284       break; /* Have all other threads stopped */
285     }
286     pthread_cond_wait(&gccond, &gclistlock);
287   }
288 #endif
289
290   if (head==NULL) {
291     headindex=0;
292     tailindex=0;
293     head=tail=malloc(sizeof(struct pointerblock));
294   }
295
296 #ifdef TASK
297   if (taghead==NULL) {
298     tagindex=0;
299     taghead=malloc(sizeof(struct pointerblock));
300     taghead->next=NULL;
301   }
302 #endif
303
304 #ifdef STM
305   if (c_table!=NULL) {
306     fixtable(&c_table, &c_list, &c_structs, c_size);
307     fixobjlist(newobjs);
308 #ifdef STMSTATS
309     fixobjlist(lockedobjs);
310 #endif
311   }
312   memorybase=NULL;
313 #endif
314
315   /* Check current stack */
316 #if defined(THREADS)||defined(DSTM)||defined(STM)
317   {
318     struct listitem *listptr=list;
319     while(1) {
320 #endif
321
322   while(stackptr!=NULL) {
323     int i;
324     for(i=0; i<stackptr->size; i++) {
325       void * orig=stackptr->array[i];
326       ENQUEUE(orig, stackptr->array[i]);
327     }
328     stackptr=stackptr->next;
329   }
330 #if defined(THREADS)||defined(DSTM)||defined(STM)
331   /* Go to next thread */
332   if (listptr!=NULL) {
333 #ifdef THREADS
334     void * orig=listptr->locklist;
335     ENQUEUE(orig, listptr->locklist);
336 #endif
337 #ifdef STM
338     if ((*listptr->tc_table)!=NULL) {
339       fixtable(listptr->tc_table, listptr->tc_list, listptr->tc_structs, listptr->tc_size);
340       fixobjlist(listptr->objlist);
341 #ifdef STMSTATS
342       fixobjlist(listptr->lockedlist);
343 #endif
344     }
345     *(listptr->base)=NULL;
346 #endif
347     stackptr=listptr->stackptr;
348     listptr=listptr->next;
349   } else
350     break;
351 }
352 }
353 #endif
354
355 #ifdef FASTCHECK
356   ENQUEUE(___fcrevert___, ___fcrevert___);
357 #endif
358
359 #ifdef TASK
360   {
361     /* Update objectsets */
362     int i;
363     for(i=0; i<NUMCLASSES; i++) {
364 #if !defined(MULTICORE)
365       struct parameterwrapper * p=objectqueues[i];
366       while(p!=NULL) {
367         struct ObjectHash * set=p->objectset;
368         struct ObjectNode * ptr=set->listhead;
369         while(ptr!=NULL) {
370           void *orig=(void *)ptr->key;
371           ENQUEUE(orig, *((void **)(&ptr->key)));
372           ptr=ptr->lnext;
373         }
374         ObjectHashrehash(set); /* Rehash the table */
375         p=p->next;
376       }
377 #endif
378     }
379   }
380
381 #ifndef FASTCHECK
382   if (forward!=NULL) {
383     struct cnode * ptr=forward->listhead;
384     while(ptr!=NULL) {
385       void * orig=(void *)ptr->key;
386       ENQUEUE(orig, *((void **)(&ptr->key)));
387       ptr=ptr->lnext;
388     }
389     crehash(forward); /* Rehash the table */
390   }
391
392   if (reverse!=NULL) {
393     struct cnode * ptr=reverse->listhead;
394     while(ptr!=NULL) {
395       void *orig=(void *)ptr->val;
396       ENQUEUE(orig, *((void**)(&ptr->val)));
397       ptr=ptr->lnext;
398     }
399   }
400 #endif
401
402   {
403     struct RuntimeNode * ptr=fdtoobject->listhead;
404     while(ptr!=NULL) {
405       void *orig=(void *)ptr->data;
406       ENQUEUE(orig, *((void**)(&ptr->data)));
407       ptr=ptr->lnext;
408     }
409   }
410
411   {
412     /* Update current task descriptor */
413     int i;
414     for(i=0; i<currtpd->numParameters; i++) {
415       void *orig=currtpd->parameterArray[i];
416       ENQUEUE(orig, currtpd->parameterArray[i]);
417     }
418
419   }
420
421   /* Update active tasks */
422   {
423     struct genpointerlist * ptr=activetasks->list;
424     while(ptr!=NULL) {
425       struct taskparamdescriptor *tpd=ptr->src;
426       int i;
427       for(i=0; i<tpd->numParameters; i++) {
428         void * orig=tpd->parameterArray[i];
429         ENQUEUE(orig, tpd->parameterArray[i]);
430       }
431       ptr=ptr->inext;
432     }
433     genrehash(activetasks);
434   }
435
436   /* Update failed tasks */
437   {
438     struct genpointerlist * ptr=failedtasks->list;
439     while(ptr!=NULL) {
440       struct taskparamdescriptor *tpd=ptr->src;
441       int i;
442       for(i=0; i<tpd->numParameters; i++) {
443         void * orig=tpd->parameterArray[i];
444         ENQUEUE(orig, tpd->parameterArray[i]);
445       }
446       ptr=ptr->inext;
447     }
448     genrehash(failedtasks);
449   }
450 #endif
451
452   while(moreItems()) {
453     void * ptr=dequeue();
454     void *cpy=((void **)ptr)[1];
455     int type=((int *)cpy)[0];
456     unsigned INTPTR * pointer;
457 #ifdef TASK
458     if(type==TAGTYPE) {
459       /* Enqueue Tag */
460       /* Nothing is inside */
461       enqueuetag(ptr);
462       continue;
463     }
464 #endif
465     pointer=pointerarray[type];
466     if (pointer==0) {
467       /* Array of primitives */
468       /* Do nothing */
469 #if defined(DSTM)||defined(FASTCHECK)
470       struct ArrayObject *ao=(struct ArrayObject *) ptr;
471       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
472       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
473       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
474 #endif
475 #if defined(STM)
476       struct ArrayObject *ao=(struct ArrayObject *) ptr;
477       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
478       SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
479 #endif
480     } else if (((INTPTR)pointer)==1) {
481       /* Array of pointers */
482       struct ArrayObject *ao=(struct ArrayObject *) ptr;
483       struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
484 #if (defined(DSTM)||defined(FASTCHECK))
485       ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
486       ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
487 #endif
488 #if defined(STM)
489       SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
490 #endif
491       int length=ao->___length___;
492       int i;
493       for(i=0; i<length; i++) {
494         void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
495         ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
496       }
497     } else {
498       INTPTR size=pointer[0];
499       int i;
500       for(i=1; i<=size; i++) {
501         unsigned int offset=pointer[i];
502         void * objptr=*((void **)(((char *)ptr)+offset));
503         ENQUEUE(objptr, *((void **)(((char *)cpy)+offset)));
504       }
505     }
506   }
507 #ifdef TASK
508   fixtags();
509 #endif
510
511 #if defined(THREADS)||defined(DSTM)||defined(STM)
512   needtocollect=0;
513   pthread_mutex_unlock(&gclistlock);
514 #endif
515 }
516
517 #ifdef TASK
518 /* Fix up the references from tags.  This can't be done earlier,
519    because we don't want tags to keep objects alive */
520 void fixtags() {
521   while(taghead!=NULL) {
522     int i;
523     struct pointerblock *tmp=taghead->next;
524     for(i=0; i<tagindex; i++) {
525       struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
526       struct ___Object___ *obj=tagd->flagptr;
527       struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
528       if (obj==NULL) {
529         /* Zero object case */
530       } else if (obj->type==-1) {
531         /* Single object case */
532         copy->flagptr=((struct ___Object___**)obj)[1];
533       } else if (obj->type==OBJECTARRAYTYPE) {
534         /* Array case */
535         struct ArrayObject *ao=(struct ArrayObject *) obj;
536         int livecount=0;
537         int j;
538         int k=0;
539         struct ArrayObject *aonew;
540
541         /* Count live objects */
542         for(j=0; j<ao->___cachedCode___; j++) {
543           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
544           if (tobj->type==-1)
545             livecount++;
546         }
547
548         livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
549         aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
550         memcpy(aonew, ao, sizeof(struct ArrayObject));
551         aonew->type=OBJECTARRAYTYPE;
552         aonew->___length___=livecount;
553         copy->flagptr=aonew;
554         for(j=0; j<ao->___cachedCode___; j++) {
555           struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
556           if (tobj->type==-1) {
557             struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
558             ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
559           }
560         }
561         aonew->___cachedCode___=k;
562         for(; k<livecount; k++) {
563           ARRAYSET(aonew, struct ___Object___*, k, NULL);
564         }
565       } else {
566         /* No object live anymore */
567         copy->flagptr=NULL;
568       }
569     }
570     free(taghead);
571     taghead=tmp;
572     tagindex=NUMPTRS;
573   }
574 }
575 #endif
576
577 void * tomalloc(int size) {
578   void * ptr=to_heapptr;
579   if ((size&7)!=0)
580     size+=(8-(size%8));
581   to_heapptr+=size;
582   return ptr;
583 }
584
585 #if defined(THREADS)||defined(DSTM)||defined(STM)
586 void checkcollect(void * ptr) {
587   stopforgc((struct garbagelist *)ptr);
588   pthread_mutex_lock(&gclock); // Wait for GC
589   restartaftergc();
590   pthread_mutex_unlock(&gclock);
591 }
592
593 #ifdef DSTM
594 void checkcollect2(void * ptr) {
595   int ptrarray[]={1, (int)ptr, (int) revertlist};
596   stopforgc((struct garbagelist *)ptrarray);
597   pthread_mutex_lock(&gclock); // Wait for GC
598   restartaftergc();
599   pthread_mutex_unlock(&gclock);
600   revertlist=(struct ___Object___*)ptrarray[2];
601 }
602 #endif
603
604 void stopforgc(struct garbagelist * ptr) {
605 #ifndef MAC
606   litem.stackptr=ptr;
607 #ifdef THREADS
608   litem.locklist=pthread_getspecific(threadlocks);
609 #endif
610 #ifdef STM
611   litem.tc_size=c_size;
612   litem.tc_table=&c_table;
613   litem.tc_list=&c_list;
614   litem.tc_structs=&c_structs;
615   litem.objlist=newobjs;
616 #ifdef STMSTATS
617   litem.lockedlist=lockedobjs;
618 #endif
619   litem.base=&memorybase;
620 #endif
621 #else
622   //handle MAC
623   struct listitem *litem=pthread_getspecific(litemkey);
624   litem->stackptr=ptr;
625 #ifdef THREADS
626   litem->locklist=pthread_getspecific(threadlocks);
627 #endif
628 #endif
629   pthread_mutex_lock(&gclistlock);
630   listcount++;
631   pthread_cond_signal(&gccond);
632   pthread_mutex_unlock(&gclistlock);
633 }
634
635 void restartaftergc() {
636   pthread_mutex_lock(&gclistlock);
637   listcount--;
638   pthread_mutex_unlock(&gclistlock);
639 }
640 #endif
641
642 #if defined(STM)||defined(THREADS)
643 #define MEMORYBLOCK 65536
644 void * helper(struct garbagelist *, int);
645 void * mygcmalloc(struct garbagelist * stackptr, int size) {
646   if ((size&7)!=0)
647     size=(size&~7)+8;
648   if (memorybase==NULL||(memorybase+size)>memorytop) {
649     int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
650     memorybase=helper(stackptr, toallocate);
651     memorytop=memorybase+toallocate;
652   }
653   char *retvalue=memorybase;
654   memorybase+=size;
655   return retvalue;
656 }
657
658 void * helper(struct garbagelist * stackptr, int size) {
659 #else
660 void * mygcmalloc(struct garbagelist * stackptr, int size) {
661 #endif
662   void *ptr;
663 #if defined(THREADS)||defined(DSTM)||defined(STM)
664   if (pthread_mutex_trylock(&gclock)!=0) {
665     stopforgc(stackptr);
666     pthread_mutex_lock(&gclock);
667     restartaftergc();
668   }
669 #endif
670   ptr=curr_heapptr;
671   if ((size&7)!=0)
672     size=(size&~7)+8;
673   curr_heapptr+=size;
674   if (curr_heapptr>curr_heapgcpoint) {
675     if (curr_heapbase==0) {
676       /* Need to allocate base heap */
677       curr_heapbase=malloc(INITIALHEAPSIZE);
678       if (curr_heapbase==NULL) {
679         printf("malloc failed\n");
680         exit(-1);
681       }
682       bzero(curr_heapbase, INITIALHEAPSIZE);
683       curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
684       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
685       curr_heapptr=curr_heapbase+size;
686
687       to_heapbase=malloc(INITIALHEAPSIZE);
688       if (to_heapbase==NULL) {
689         printf("malloc failed\n");
690         exit(-1);
691       }
692       to_heaptop=to_heapbase+INITIALHEAPSIZE;
693       to_heapptr=to_heapbase;
694       ptr=curr_heapbase;
695 #if defined(THREADS)||defined(DSTM)||defined(STM)
696       pthread_mutex_unlock(&gclock);
697 #endif
698       return ptr;
699     }
700
701     /* Grow the to heap if necessary */
702     {
703       int curr_heapsize=curr_heaptop-curr_heapbase;
704       int to_heapsize=to_heaptop-to_heapbase;
705       int last_heapsize=0;
706       if (lastgcsize>0) {
707         last_heapsize=HEAPSIZE(lastgcsize, size);
708         if ((last_heapsize&7)!=0)
709           last_heapsize+=(8-(last_heapsize%8));
710       }
711       if (curr_heapsize>last_heapsize)
712         last_heapsize=curr_heapsize;
713       if (last_heapsize>to_heapsize) {
714         free(to_heapbase);
715         to_heapbase=malloc(last_heapsize);
716         if (to_heapbase==NULL) {
717           printf("Error Allocating enough memory\n");
718           exit(-1);
719         }
720         to_heaptop=to_heapbase+last_heapsize;
721         to_heapptr=to_heapbase;
722       }
723     }
724
725     /* Do our collection */
726     collect(stackptr);
727
728     /* Update stat on previous gc size */
729     lastgcsize=(to_heapptr-to_heapbase)+size;
730
731 #ifdef GARBAGESTATS
732     printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
733     printf("New space: %u\n", to_heapptr-to_heapbase);
734     printf("Total space: %u\n", to_heaptop-to_heapbase);
735 #endif
736     /* Flip to/curr heaps */
737     {
738       void * tmp=to_heapbase;
739       to_heapbase=curr_heapbase;
740       curr_heapbase=tmp;
741
742       tmp=to_heaptop;
743       to_heaptop=curr_heaptop;
744       curr_heaptop=tmp;
745
746       tmp=to_heapptr;
747       curr_heapptr=to_heapptr+size;
748       curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
749       to_heapptr=to_heapbase;
750
751       /* Not enough room :(, redo gc */
752       if (curr_heapptr>curr_heapgcpoint) {
753 #if defined(THREADS)||defined(DSTM)||defined(STM)
754         pthread_mutex_unlock(&gclock);
755 #endif
756         return mygcmalloc(stackptr, size);
757       }
758
759       bzero(tmp, curr_heaptop-tmp);
760 #if defined(THREADS)||defined(DSTM)||defined(STM)
761       pthread_mutex_unlock(&gclock);
762 #endif
763       return tmp;
764     }
765   } else {
766 #if defined(THREADS)||defined(DSTM)||defined(STM)
767     pthread_mutex_unlock(&gclock);
768 #endif
769     return ptr;
770   }
771 }
772
773
774 int gc_createcopy(void * orig, void ** copy_ptr) {
775   if (orig==0) {
776     *copy_ptr=NULL;
777     return 0;
778   } else {
779     int type=((int *)orig)[0];
780     if (type==-1) {
781       *copy_ptr=((void **)orig)[1];
782       return 0;
783     }
784     if (type<NUMCLASSES) {
785       /* We have a normal object */
786 #ifdef STM
787       int size=classsize[type]+sizeof(objheader_t);
788       void *newobj=tomalloc(size);
789       memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
790       newobj=((char *)newobj)+sizeof(objheader_t);
791 #else
792       int size=classsize[type];
793       void *newobj=tomalloc(size);
794       memcpy(newobj, orig, size);
795 #endif
796       ((int *)orig)[0]=-1;
797       ((void **)orig)[1]=newobj;
798       *copy_ptr=newobj;
799       return 1;
800     } else {
801       /* We have an array */
802       struct ArrayObject *ao=(struct ArrayObject *)orig;
803       int elementsize=classsize[type];
804       int length=ao->___length___;
805 #ifdef STM
806       int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
807       void *newobj=tomalloc(size);
808       memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
809       newobj=((char *)newobj)+sizeof(objheader_t);
810 #else
811       int size=sizeof(struct ArrayObject)+length*elementsize;
812       void *newobj=tomalloc(size);
813       memcpy(newobj, orig, size);
814 #endif
815
816       ((int *)orig)[0]=-1;
817       ((void **)orig)[1]=newobj;
818       *copy_ptr=newobj;
819       return 1;
820     }
821   }
822 }