3 #include "structdefs.h"
5 #include "SimpleHash.h"
7 #include "GenericHashtable.h"
9 #if defined(THREADS) || defined(DSTM) || defined(STM)
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
31 extern struct genhashtable * activetasks;
33 extern struct parameterwrapper * objectqueues[NUMCLASSES];
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;
42 #if defined(THREADS) || defined(DSTM) || defined(STM)
44 struct listitem * list=NULL;
47 __thread struct listitem litem;
51 //Need to check if pointers are transaction pointers
52 //this also catches the special flag value of 1 for local copies
54 #define ENQUEUE(orig, dst) \
55 if ((!(((unsigned int)orig)&0x1))) { \
56 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
58 if (gc_createcopy(orig,©)) \
64 #define ENQUEUE(orig, dst) \
65 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
67 if (gc_createcopy(orig,©)) \
71 #define SENQUEUE(orig, dst) \
74 if (gc_createcopy(orig,©)) \
78 #elif defined(FASTCHECK)
79 #define ENQUEUE(orig, dst) \
80 if (((unsigned int)orig)!=1) { \
82 if (gc_createcopy(orig,©)) \
86 #define ENQUEUE(orig, dst) \
88 if (gc_createcopy(orig,©)) \
95 struct pointerblock *next;
98 void * curr_heapbase=0;
99 void * curr_heapptr=0;
100 void * curr_heapgcpoint=0;
101 void * curr_heaptop=0;
103 void * to_heapbase=0;
108 struct pointerblock *head=NULL;
110 struct pointerblock *tail=NULL;
112 struct pointerblock *spare=NULL;
114 void enqueue(void *ptr) {
115 if (headindex==NUMPTRS) {
116 struct pointerblock * tmp;
121 tmp=malloc(sizeof(struct pointerblock));
126 head->ptrs[headindex++]=ptr;
130 if (tailindex==NUMPTRS) {
131 struct pointerblock *tmp=tail;
139 return tail->ptrs[tailindex++];
143 void fixobjlist(struct objlist * ptr) {
146 for(i=0;i<ptr->offset;i++) {
147 SENQUEUE(ptr->objs[i], ptr->objs[i]);
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;
161 chashlistnode_t *newlist=NULL;
162 for(i=0;i<tc_size;i++) {
165 do { //Inner loop to go through linked lists
167 chashlistnode_t *tmp,*next;
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
173 if (curr->val>=curr_heapbase&&curr->val<curr_heaptop) {
174 SENQUEUE(curr->val, curr->val);
176 //rewrite transaction cache entry
177 void *vptr=curr->val;
178 int type=((int *)vptr)[0];
179 unsigned INTPTR *pointer=pointerarray[type];
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) {
186 struct ArrayObject *ao=(struct ArrayObject *) vptr;
187 int length=ao->___length___;
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]);
195 INTPTR size=pointer[0];
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)));
206 index = (((unsigned INTPTR)key) & mask) >>4;
210 // Insert into the new table
212 tmp->key = curr->key;
213 tmp->val = curr->val;
216 } else if (isfirst) {
217 chashlistnode_t *newnode;
218 if ((*cstr)->num<NUMCLIST) {
219 newnode=&(*cstr)->array[(*cstr)->num];
223 cliststruct_t *tcl=calloc(1,sizeof(cliststruct_t));
226 newnode=&tcl->array[0];
229 newnode->key = curr->key;
230 newnode->val = curr->val;
231 newnode->next = tmp->next;
232 newnode->lnext=newlist;
238 curr->next=tmp->next;
252 if ((head==tail)&&(tailindex==headindex))
258 struct pointerblock *taghead=NULL;
261 void enqueuetag(struct ___TagDescriptor___ *ptr) {
262 if (tagindex==NUMPTRS) {
263 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
268 taghead->ptrs[tagindex++]=ptr;
272 #if defined(STM)||defined(THREADS)
273 __thread char * memorybase=NULL;
274 __thread char * memorytop=NULL;
278 void collect(struct garbagelist * stackptr) {
279 #if defined(THREADS)||defined(DSTM)||defined(STM)
281 pthread_mutex_lock(&gclistlock);
283 if ((listcount+1)==threadcount) {
284 break; /* Have all other threads stopped */
286 pthread_cond_wait(&gccond, &gclistlock);
293 head=tail=malloc(sizeof(struct pointerblock));
299 taghead=malloc(sizeof(struct pointerblock));
306 fixtable(&c_table, &c_list, &c_structs, c_size);
309 fixobjlist(lockedobjs);
315 /* Check current stack */
316 #if defined(THREADS)||defined(DSTM)||defined(STM)
318 struct listitem *listptr=list;
322 while(stackptr!=NULL) {
324 for(i=0; i<stackptr->size; i++) {
325 void * orig=stackptr->array[i];
326 ENQUEUE(orig, stackptr->array[i]);
328 stackptr=stackptr->next;
330 #if defined(THREADS)||defined(DSTM)||defined(STM)
331 /* Go to next thread */
334 void * orig=listptr->locklist;
335 ENQUEUE(orig, listptr->locklist);
338 if ((*listptr->tc_table)!=NULL) {
339 fixtable(listptr->tc_table, listptr->tc_list, listptr->tc_structs, listptr->tc_size);
340 fixobjlist(listptr->objlist);
342 fixobjlist(listptr->lockedlist);
345 *(listptr->base)=NULL;
347 stackptr=listptr->stackptr;
348 listptr=listptr->next;
356 ENQUEUE(___fcrevert___, ___fcrevert___);
361 /* Update objectsets */
363 for(i=0; i<NUMCLASSES; i++) {
364 #if !defined(MULTICORE)
365 struct parameterwrapper * p=objectqueues[i];
367 struct ObjectHash * set=p->objectset;
368 struct ObjectNode * ptr=set->listhead;
370 void *orig=(void *)ptr->key;
371 ENQUEUE(orig, *((void **)(&ptr->key)));
374 ObjectHashrehash(set); /* Rehash the table */
383 struct cnode * ptr=forward->listhead;
385 void * orig=(void *)ptr->key;
386 ENQUEUE(orig, *((void **)(&ptr->key)));
389 crehash(forward); /* Rehash the table */
393 struct cnode * ptr=reverse->listhead;
395 void *orig=(void *)ptr->val;
396 ENQUEUE(orig, *((void**)(&ptr->val)));
403 struct RuntimeNode * ptr=fdtoobject->listhead;
405 void *orig=(void *)ptr->data;
406 ENQUEUE(orig, *((void**)(&ptr->data)));
412 /* Update current task descriptor */
414 for(i=0; i<currtpd->numParameters; i++) {
415 void *orig=currtpd->parameterArray[i];
416 ENQUEUE(orig, currtpd->parameterArray[i]);
421 /* Update active tasks */
423 struct genpointerlist * ptr=activetasks->list;
425 struct taskparamdescriptor *tpd=ptr->src;
427 for(i=0; i<tpd->numParameters; i++) {
428 void * orig=tpd->parameterArray[i];
429 ENQUEUE(orig, tpd->parameterArray[i]);
433 genrehash(activetasks);
436 /* Update failed tasks */
438 struct genpointerlist * ptr=failedtasks->list;
440 struct taskparamdescriptor *tpd=ptr->src;
442 for(i=0; i<tpd->numParameters; i++) {
443 void * orig=tpd->parameterArray[i];
444 ENQUEUE(orig, tpd->parameterArray[i]);
448 genrehash(failedtasks);
453 void * ptr=dequeue();
454 void *cpy=((void **)ptr)[1];
455 int type=((int *)cpy)[0];
456 unsigned INTPTR * pointer;
460 /* Nothing is inside */
465 pointer=pointerarray[type];
467 /* Array of primitives */
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___));
476 struct ArrayObject *ao=(struct ArrayObject *) ptr;
477 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
478 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
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___));
489 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
491 int length=ao->___length___;
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]);
498 INTPTR size=pointer[0];
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)));
511 #if defined(THREADS)||defined(DSTM)||defined(STM)
513 pthread_mutex_unlock(&gclistlock);
518 /* Fix up the references from tags. This can't be done earlier,
519 because we don't want tags to keep objects alive */
521 while(taghead!=NULL) {
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];
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) {
535 struct ArrayObject *ao=(struct ArrayObject *) obj;
539 struct ArrayObject *aonew;
541 /* Count live objects */
542 for(j=0; j<ao->___cachedCode___; j++) {
543 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
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;
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);
561 aonew->___cachedCode___=k;
562 for(; k<livecount; k++) {
563 ARRAYSET(aonew, struct ___Object___*, k, NULL);
566 /* No object live anymore */
577 void * tomalloc(int size) {
578 void * ptr=to_heapptr;
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
590 pthread_mutex_unlock(&gclock);
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
599 pthread_mutex_unlock(&gclock);
600 revertlist=(struct ___Object___*)ptrarray[2];
604 void stopforgc(struct garbagelist * ptr) {
608 litem.locklist=pthread_getspecific(threadlocks);
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;
617 litem.lockedlist=lockedobjs;
619 litem.base=&memorybase;
623 struct listitem *litem=pthread_getspecific(litemkey);
626 litem->locklist=pthread_getspecific(threadlocks);
629 pthread_mutex_lock(&gclistlock);
631 pthread_cond_signal(&gccond);
632 pthread_mutex_unlock(&gclistlock);
635 void restartaftergc() {
636 pthread_mutex_lock(&gclistlock);
638 pthread_mutex_unlock(&gclistlock);
642 #if defined(STM)||defined(THREADS)
643 #define MEMORYBLOCK 65536
644 void * helper(struct garbagelist *, int);
645 void * mygcmalloc(struct garbagelist * stackptr, int size) {
648 if (memorybase==NULL||(memorybase+size)>memorytop) {
649 int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
650 memorybase=helper(stackptr, toallocate);
651 memorytop=memorybase+toallocate;
653 char *retvalue=memorybase;
658 void * helper(struct garbagelist * stackptr, int size) {
660 void * mygcmalloc(struct garbagelist * stackptr, int size) {
663 #if defined(THREADS)||defined(DSTM)||defined(STM)
664 if (pthread_mutex_trylock(&gclock)!=0) {
666 pthread_mutex_lock(&gclock);
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");
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;
687 to_heapbase=malloc(INITIALHEAPSIZE);
688 if (to_heapbase==NULL) {
689 printf("malloc failed\n");
692 to_heaptop=to_heapbase+INITIALHEAPSIZE;
693 to_heapptr=to_heapbase;
695 #if defined(THREADS)||defined(DSTM)||defined(STM)
696 pthread_mutex_unlock(&gclock);
701 /* Grow the to heap if necessary */
703 int curr_heapsize=curr_heaptop-curr_heapbase;
704 int to_heapsize=to_heaptop-to_heapbase;
707 last_heapsize=HEAPSIZE(lastgcsize, size);
708 if ((last_heapsize&7)!=0)
709 last_heapsize+=(8-(last_heapsize%8));
711 if (curr_heapsize>last_heapsize)
712 last_heapsize=curr_heapsize;
713 if (last_heapsize>to_heapsize) {
715 to_heapbase=malloc(last_heapsize);
716 if (to_heapbase==NULL) {
717 printf("Error Allocating enough memory\n");
720 to_heaptop=to_heapbase+last_heapsize;
721 to_heapptr=to_heapbase;
725 /* Do our collection */
728 /* Update stat on previous gc size */
729 lastgcsize=(to_heapptr-to_heapbase)+size;
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);
736 /* Flip to/curr heaps */
738 void * tmp=to_heapbase;
739 to_heapbase=curr_heapbase;
743 to_heaptop=curr_heaptop;
747 curr_heapptr=to_heapptr+size;
748 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
749 to_heapptr=to_heapbase;
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);
756 return mygcmalloc(stackptr, size);
759 bzero(tmp, curr_heaptop-tmp);
760 #if defined(THREADS)||defined(DSTM)||defined(STM)
761 pthread_mutex_unlock(&gclock);
766 #if defined(THREADS)||defined(DSTM)||defined(STM)
767 pthread_mutex_unlock(&gclock);
774 int gc_createcopy(void * orig, void ** copy_ptr) {
779 int type=((int *)orig)[0];
781 *copy_ptr=((void **)orig)[1];
784 if (type<NUMCLASSES) {
785 /* We have a normal object */
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);
792 int size=classsize[type];
793 void *newobj=tomalloc(size);
794 memcpy(newobj, orig, size);
797 ((void **)orig)[1]=newobj;
801 /* We have an array */
802 struct ArrayObject *ao=(struct ArrayObject *)orig;
803 int elementsize=classsize[type];
804 int length=ao->___length___;
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);
811 int size=sizeof(struct ArrayObject)+length*elementsize;
812 void *newobj=tomalloc(size);
813 memcpy(newobj, orig, size);
817 ((void **)orig)[1]=newobj;