3 #include "structdefs.h"
5 #include "SimpleHash.h"
7 #include "GenericHashtable.h"
9 #if defined(THREADS) || defined(DSTM) || defined(STM)
25 #define INITIALHEAPSIZE 64*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;
48 //Need to check if pointers are transaction pointers
49 //this also catches the special flag value of 1 for local copies
51 #define ENQUEUE(orig, dst) \
52 if ((!(((unsigned int)orig)&0x1))) { \
53 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
55 if (gc_createcopy(orig,©)) \
61 #define ENQUEUE(orig, dst) \
62 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
64 if (gc_createcopy(orig,©)) \
68 #define SENQUEUE(orig, dst) \
71 if (gc_createcopy(orig,©)) \
75 #elif defined(FASTCHECK)
76 #define ENQUEUE(orig, dst) \
77 if (((unsigned int)orig)!=1) { \
79 if (gc_createcopy(orig,©)) \
83 #define ENQUEUE(orig, dst) \
85 if (gc_createcopy(orig,©)) \
92 struct pointerblock *next;
95 void * curr_heapbase=0;
96 void * curr_heapptr=0;
97 void * curr_heapgcpoint=0;
98 void * curr_heaptop=0;
100 void * to_heapbase=0;
105 struct pointerblock *head=NULL;
107 struct pointerblock *tail=NULL;
109 struct pointerblock *spare=NULL;
111 void enqueue(void *ptr) {
112 if (headindex==NUMPTRS) {
113 struct pointerblock * tmp;
118 tmp=malloc(sizeof(struct pointerblock));
123 head->ptrs[headindex++]=ptr;
127 if (tailindex==NUMPTRS) {
128 struct pointerblock *tmp=tail;
136 return tail->ptrs[tailindex++];
140 void fixobjlist(struct objlist * ptr) {
143 for(i=0;i<ptr->offset;i++) {
144 SENQUEUE(ptr->objs[i], ptr->objs[i]);
150 void fixtable(chashlistnode_t ** tc_table, unsigned int tc_size) {
151 unsigned int mask=(tc_size<<3)-1;
152 chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
153 chashlistnode_t *ptr=*tc_table;
154 chashlistnode_t *curr;
158 for(i=0;i<tc_size;i++) {
161 do { //Inner loop to go through linked lists
163 chashlistnode_t *tmp,*next;
165 if ((key=(void *)curr->key) == 0) { //Exit inner loop if there the first element is 0
166 break; //key = val =0 for element if not present within the hash table
169 if (curr->val>=curr_heapbase&&curr->val<curr_heaptop) {
170 SENQUEUE(curr->val, curr->val);
172 //rewrite transaction cache entry
173 void *vptr=curr->val;
174 int type=((int *)vptr)[0];
175 unsigned int *pointer=pointerarray[type];
177 //array of primitives - do nothing
178 struct ArrayObject *ao=(struct ArrayObject *) vptr;
179 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
180 } else if (((int)pointer)==1) {
182 struct ArrayObject *ao=(struct ArrayObject *) vptr;
183 int length=ao->___length___;
185 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
186 for(i=0; i<length; i++) {
187 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
188 SENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
193 for(i=1; i<=size; i++) {
194 unsigned int offset=pointer[i];
195 void * objptr=*((void **)(((int)vptr)+offset));
196 SENQUEUE(objptr, *((void **)(((int)vptr)+offset)));
202 index = (((unsigned int)key) & mask) >>3;
204 curr->key=(unsigned int)key;
206 // Insert into the new table
208 tmp->key = curr->key;
209 tmp->val = curr->val;
213 } else if (isfirst) {
214 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
215 newnode->key = curr->key;
216 newnode->val = curr->val;
217 newnode->next = tmp->next;
220 curr->next=tmp->next;
233 if ((head==tail)&&(tailindex==headindex))
239 struct pointerblock *taghead=NULL;
242 void enqueuetag(struct ___TagDescriptor___ *ptr) {
243 if (tagindex==NUMPTRS) {
244 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
249 taghead->ptrs[tagindex++]=ptr;
254 __thread char * memorybase=NULL;
255 __thread char * memorytop=NULL;
259 void collect(struct garbagelist * stackptr) {
260 #if defined(THREADS)||defined(DSTM)||defined(STM)
262 pthread_mutex_lock(&gclistlock);
264 if ((listcount+1)==threadcount) {
265 break; /* Have all other threads stopped */
267 pthread_cond_wait(&gccond, &gclistlock);
274 head=tail=malloc(sizeof(struct pointerblock));
280 taghead=malloc(sizeof(struct pointerblock));
287 fixtable(&c_table, c_size);
293 /* Check current stack */
294 #if defined(THREADS)||defined(DSTM)||defined(STM)
296 struct listitem *listptr=list;
300 while(stackptr!=NULL) {
302 for(i=0; i<stackptr->size; i++) {
303 void * orig=stackptr->array[i];
304 ENQUEUE(orig, stackptr->array[i]);
306 stackptr=stackptr->next;
308 #if defined(THREADS)||defined(DSTM)||defined(STM)
309 /* Go to next thread */
312 void * orig=listptr->locklist;
313 ENQUEUE(orig, listptr->locklist);
316 if ((*listptr->tc_table)!=NULL) {
317 fixtable(listptr->tc_table, listptr->tc_size);
318 fixobjlist(listptr->objlist);
320 *(listptr->base)=NULL;
322 stackptr=listptr->stackptr;
323 listptr=listptr->next;
331 ENQUEUE(___fcrevert___, ___fcrevert___);
336 /* Update objectsets */
338 for(i=0; i<NUMCLASSES; i++) {
339 #if !defined(MULTICORE)
340 struct parameterwrapper * p=objectqueues[i];
342 struct ObjectHash * set=p->objectset;
343 struct ObjectNode * ptr=set->listhead;
345 void *orig=(void *)ptr->key;
346 ENQUEUE(orig, *((void **)(&ptr->key)));
349 ObjectHashrehash(set); /* Rehash the table */
358 struct cnode * ptr=forward->listhead;
360 void * orig=(void *)ptr->key;
361 ENQUEUE(orig, *((void **)(&ptr->key)));
364 crehash(forward); /* Rehash the table */
368 struct cnode * ptr=reverse->listhead;
370 void *orig=(void *)ptr->val;
371 ENQUEUE(orig, *((void**)(&ptr->val)));
378 struct RuntimeNode * ptr=fdtoobject->listhead;
380 void *orig=(void *)ptr->data;
381 ENQUEUE(orig, *((void**)(&ptr->data)));
387 /* Update current task descriptor */
389 for(i=0; i<currtpd->numParameters; i++) {
390 void *orig=currtpd->parameterArray[i];
391 ENQUEUE(orig, currtpd->parameterArray[i]);
396 /* Update active tasks */
398 struct genpointerlist * ptr=activetasks->list;
400 struct taskparamdescriptor *tpd=ptr->src;
402 for(i=0; i<tpd->numParameters; i++) {
403 void * orig=tpd->parameterArray[i];
404 ENQUEUE(orig, tpd->parameterArray[i]);
408 genrehash(activetasks);
411 /* Update failed tasks */
413 struct genpointerlist * ptr=failedtasks->list;
415 struct taskparamdescriptor *tpd=ptr->src;
417 for(i=0; i<tpd->numParameters; i++) {
418 void * orig=tpd->parameterArray[i];
419 ENQUEUE(orig, tpd->parameterArray[i]);
423 genrehash(failedtasks);
428 void * ptr=dequeue();
429 void *cpy=((void **)ptr)[1];
430 int type=((int *)cpy)[0];
431 unsigned int * pointer;
435 /* Nothing is inside */
440 pointer=pointerarray[type];
442 /* Array of primitives */
444 #if defined(DSTM)||defined(FASTCHECK)
445 struct ArrayObject *ao=(struct ArrayObject *) ptr;
446 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
447 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
448 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
451 struct ArrayObject *ao=(struct ArrayObject *) ptr;
452 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
453 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
455 } else if (((int)pointer)==1) {
456 /* Array of pointers */
457 struct ArrayObject *ao=(struct ArrayObject *) ptr;
458 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
459 #if (defined(DSTM)||defined(FASTCHECK))
460 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
461 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
464 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
466 int length=ao->___length___;
468 for(i=0; i<length; i++) {
469 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
470 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
475 for(i=1; i<=size; i++) {
476 unsigned int offset=pointer[i];
477 void * objptr=*((void **)(((int)ptr)+offset));
478 ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
486 #if defined(THREADS)||defined(DSTM)||defined(STM)
488 pthread_mutex_unlock(&gclistlock);
493 /* Fix up the references from tags. This can't be done earlier,
494 because we don't want tags to keep objects alive */
496 while(taghead!=NULL) {
498 struct pointerblock *tmp=taghead->next;
499 for(i=0; i<tagindex; i++) {
500 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
501 struct ___Object___ *obj=tagd->flagptr;
502 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
504 /* Zero object case */
505 } else if (obj->type==-1) {
506 /* Single object case */
507 copy->flagptr=((struct ___Object___**)obj)[1];
508 } else if (obj->type==OBJECTARRAYTYPE) {
510 struct ArrayObject *ao=(struct ArrayObject *) obj;
514 struct ArrayObject *aonew;
516 /* Count live objects */
517 for(j=0; j<ao->___cachedCode___; j++) {
518 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
523 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
524 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
525 memcpy(aonew, ao, sizeof(struct ArrayObject));
526 aonew->type=OBJECTARRAYTYPE;
527 aonew->___length___=livecount;
529 for(j=0; j<ao->___cachedCode___; j++) {
530 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
531 if (tobj->type==-1) {
532 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
533 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
536 aonew->___cachedCode___=k;
537 for(; k<livecount; k++) {
538 ARRAYSET(aonew, struct ___Object___*, k, NULL);
541 /* No object live anymore */
552 void * tomalloc(int size) {
553 void * ptr=to_heapptr;
560 #if defined(THREADS)||defined(DSTM)||defined(STM)
561 void checkcollect(void * ptr) {
562 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
563 pthread_mutex_lock(&gclock); // Wait for GC
565 pthread_mutex_unlock(&gclock);
569 void checkcollect2(void * ptr) {
570 int ptrarray[]={1, (int)ptr, (int) revertlist};
571 struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
572 pthread_mutex_lock(&gclock); // Wait for GC
574 pthread_mutex_unlock(&gclock);
575 revertlist=(struct ___Object___*)ptrarray[2];
579 struct listitem * stopforgc(struct garbagelist * ptr) {
580 struct listitem * litem=malloc(sizeof(struct listitem));
583 litem->locklist=pthread_getspecific(threadlocks);
586 litem->tc_size=c_size;
587 litem->tc_table=&c_table;
588 litem->objlist=newobjs;
589 litem->base=&memorybase;
592 pthread_mutex_lock(&gclistlock);
598 pthread_cond_signal(&gccond);
599 pthread_mutex_unlock(&gclistlock);
603 void restartaftergc(struct listitem * litem) {
604 pthread_mutex_lock(&gclistlock);
606 pthread_setspecific(threadlocks, litem->locklist);
608 if (litem->prev==NULL) {
611 litem->prev->next=litem->next;
613 if (litem->next!=NULL) {
614 litem->next->prev=litem->prev;
617 pthread_mutex_unlock(&gclistlock);
623 #define MEMORYBLOCK 65536
624 void * helper(struct garbagelist *, int);
625 void * mygcmalloc(struct garbagelist * stackptr, int size) {
628 if (memorybase==NULL||(memorybase+size)>memorytop) {
629 int toallocate=(size>MEMORYBLOCK)?size:MEMORYBLOCK;
630 memorybase=helper(stackptr, toallocate);
631 memorytop=memorybase+toallocate;
633 char *retvalue=memorybase;
638 void * helper(struct garbagelist * stackptr, int size) {
640 void * mygcmalloc(struct garbagelist * stackptr, int size) {
643 #if defined(THREADS)||defined(DSTM)||defined(STM)
644 if (pthread_mutex_trylock(&gclock)!=0) {
645 struct listitem *tmp=stopforgc(stackptr);
646 pthread_mutex_lock(&gclock);
654 if (curr_heapptr>curr_heapgcpoint) {
655 if (curr_heapbase==0) {
656 /* Need to allocate base heap */
657 curr_heapbase=malloc(INITIALHEAPSIZE);
658 if (curr_heapbase==NULL) {
659 printf("malloc failed\n");
662 bzero(curr_heapbase, INITIALHEAPSIZE);
663 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
664 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
665 curr_heapptr=curr_heapbase+size;
667 to_heapbase=malloc(INITIALHEAPSIZE);
668 if (to_heapbase==NULL) {
669 printf("malloc failed\n");
672 to_heaptop=to_heapbase+INITIALHEAPSIZE;
673 to_heapptr=to_heapbase;
675 #if defined(THREADS)||defined(DSTM)||defined(STM)
676 pthread_mutex_unlock(&gclock);
681 /* Grow the to heap if necessary */
683 int curr_heapsize=curr_heaptop-curr_heapbase;
684 int to_heapsize=to_heaptop-to_heapbase;
687 last_heapsize=HEAPSIZE(lastgcsize, size);
688 if ((last_heapsize&7)!=0)
689 last_heapsize+=(8-(last_heapsize%8));
691 if (curr_heapsize>last_heapsize)
692 last_heapsize=curr_heapsize;
693 if (last_heapsize>to_heapsize) {
695 to_heapbase=malloc(last_heapsize);
696 if (to_heapbase==NULL) {
697 printf("Error Allocating enough memory\n");
700 to_heaptop=to_heapbase+last_heapsize;
701 to_heapptr=to_heapbase;
705 /* Do our collection */
708 /* Update stat on previous gc size */
709 lastgcsize=(to_heapptr-to_heapbase)+size;
712 printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
713 printf("New space: %u\n", to_heapptr-to_heapbase);
714 printf("Total space: %u\n", to_heaptop-to_heapbase);
716 /* Flip to/curr heaps */
718 void * tmp=to_heapbase;
719 to_heapbase=curr_heapbase;
723 to_heaptop=curr_heaptop;
727 curr_heapptr=to_heapptr+size;
728 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
729 to_heapptr=to_heapbase;
731 /* Not enough room :(, redo gc */
732 if (curr_heapptr>curr_heapgcpoint) {
733 #if defined(THREADS)||defined(DSTM)||defined(STM)
734 pthread_mutex_unlock(&gclock);
736 return mygcmalloc(stackptr, size);
739 bzero(tmp, curr_heaptop-tmp);
740 #if defined(THREADS)||defined(DSTM)||defined(STM)
741 pthread_mutex_unlock(&gclock);
746 #if defined(THREADS)||defined(DSTM)||defined(STM)
747 pthread_mutex_unlock(&gclock);
754 int gc_createcopy(void * orig, void ** copy_ptr) {
759 int type=((int *)orig)[0];
761 *copy_ptr=((void **)orig)[1];
764 if (type<NUMCLASSES) {
765 /* We have a normal object */
767 int size=classsize[type]+sizeof(objheader_t);
768 void *newobj=tomalloc(size);
769 memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
770 newobj=((char *)newobj)+sizeof(objheader_t);
772 int size=classsize[type];
773 void *newobj=tomalloc(size);
774 memcpy(newobj, orig, size);
777 ((void **)orig)[1]=newobj;
781 /* We have an array */
782 struct ArrayObject *ao=(struct ArrayObject *)orig;
783 int elementsize=classsize[type];
784 int length=ao->___length___;
786 int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
787 void *newobj=tomalloc(size);
788 memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
789 newobj=((char *)newobj)+sizeof(objheader_t);
791 int size=sizeof(struct ArrayObject)+length*elementsize;
792 void *newobj=tomalloc(size);
793 memcpy(newobj, orig, size);
797 ((void **)orig)[1]=newobj;