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;
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 fixtable(chashlistnode_t ** tc_table, unsigned int tc_size) {
141 unsigned int mask=(tc_size<<1)-1;
142 chashlistnode_t *node=calloc(tc_size, sizeof(chashlistnode_t));
143 chashlistnode_t *ptr=*tc_table;
144 chashlistnode_t *curr;
148 for(i=0;i<tc_size;i++) {
151 do { //Inner loop to go through linked lists
153 chashlistnode_t *tmp,*next;
155 if ((key=(void *)curr->key) == 0) { //Exit inner loop if there the first element is 0
156 break; //key = val =0 for element if not present within the hash table
159 if (curr->val>=curr_heapbase&&curr->val<curr_heaptop) {
160 SENQUEUE(curr->val, curr->val);
162 //rewrite transaction cache entry
163 void *vptr=curr->val;
164 int type=((int *)vptr)[0];
165 unsigned int *pointer=pointerarray[type];
167 //array of primitives - do nothing
168 struct ArrayObject *ao=(struct ArrayObject *) vptr;
169 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
170 } else if (((int)pointer)==1) {
172 struct ArrayObject *ao=(struct ArrayObject *) vptr;
173 int length=ao->___length___;
175 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao->___objlocation___));
176 for(i=0; i<length; i++) {
177 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
178 SENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
183 for(i=1; i<=size; i++) {
184 unsigned int offset=pointer[i];
185 void * objptr=*((void **)(((int)vptr)+offset));
186 SENQUEUE(objptr, *((void **)(((int)vptr)+offset)));
192 index = (((unsigned int)key) & mask) >>1;
194 curr->key=(unsigned int)key;
196 // Insert into the new table
198 tmp->key = curr->key;
199 tmp->val = curr->val;
203 } else if (isfirst) {
204 chashlistnode_t *newnode= calloc(1, sizeof(chashlistnode_t));
205 newnode->key = curr->key;
206 newnode->val = curr->val;
207 newnode->next = tmp->next;
210 curr->next=tmp->next;
223 if ((head==tail)&&(tailindex==headindex))
229 struct pointerblock *taghead=NULL;
232 void enqueuetag(struct ___TagDescriptor___ *ptr) {
233 if (tagindex==NUMPTRS) {
234 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
239 taghead->ptrs[tagindex++]=ptr;
244 void collect(struct garbagelist * stackptr) {
245 #if defined(THREADS)||defined(DSTM)||defined(STM)
247 pthread_mutex_lock(&gclistlock);
249 if ((listcount+1)==threadcount) {
250 break; /* Have all other threads stopped */
252 pthread_cond_wait(&gccond, &gclistlock);
259 head=tail=malloc(sizeof(struct pointerblock));
265 taghead=malloc(sizeof(struct pointerblock));
272 fixtable(&c_table, c_size);
275 /* Check current stack */
276 #if defined(THREADS)||defined(DSTM)||defined(STM)
278 struct listitem *listptr=list;
282 while(stackptr!=NULL) {
284 for(i=0; i<stackptr->size; i++) {
285 void * orig=stackptr->array[i];
286 ENQUEUE(orig, stackptr->array[i]);
288 stackptr=stackptr->next;
290 #if defined(THREADS)||defined(DSTM)||defined(STM)
291 /* Go to next thread */
294 void * orig=listptr->locklist;
295 ENQUEUE(orig, listptr->locklist);
298 if ((*listptr->tc_table)!=NULL)
299 fixtable(listptr->tc_table, listptr->tc_size);
301 stackptr=listptr->stackptr;
302 listptr=listptr->next;
310 ENQUEUE(___fcrevert___, ___fcrevert___);
315 /* Update objectsets */
317 for(i=0; i<NUMCLASSES; i++) {
318 #if !defined(MULTICORE)
319 struct parameterwrapper * p=objectqueues[i];
321 struct ObjectHash * set=p->objectset;
322 struct ObjectNode * ptr=set->listhead;
324 void *orig=(void *)ptr->key;
325 ENQUEUE(orig, *((void **)(&ptr->key)));
328 ObjectHashrehash(set); /* Rehash the table */
337 struct cnode * ptr=forward->listhead;
339 void * orig=(void *)ptr->key;
340 ENQUEUE(orig, *((void **)(&ptr->key)));
343 crehash(forward); /* Rehash the table */
347 struct cnode * ptr=reverse->listhead;
349 void *orig=(void *)ptr->val;
350 ENQUEUE(orig, *((void**)(&ptr->val)));
357 struct RuntimeNode * ptr=fdtoobject->listhead;
359 void *orig=(void *)ptr->data;
360 ENQUEUE(orig, *((void**)(&ptr->data)));
366 /* Update current task descriptor */
368 for(i=0; i<currtpd->numParameters; i++) {
369 void *orig=currtpd->parameterArray[i];
370 ENQUEUE(orig, currtpd->parameterArray[i]);
375 /* Update active tasks */
377 struct genpointerlist * ptr=activetasks->list;
379 struct taskparamdescriptor *tpd=ptr->src;
381 for(i=0; i<tpd->numParameters; i++) {
382 void * orig=tpd->parameterArray[i];
383 ENQUEUE(orig, tpd->parameterArray[i]);
387 genrehash(activetasks);
390 /* Update failed tasks */
392 struct genpointerlist * ptr=failedtasks->list;
394 struct taskparamdescriptor *tpd=ptr->src;
396 for(i=0; i<tpd->numParameters; i++) {
397 void * orig=tpd->parameterArray[i];
398 ENQUEUE(orig, tpd->parameterArray[i]);
402 genrehash(failedtasks);
407 void * ptr=dequeue();
408 void *cpy=((void **)ptr)[1];
409 int type=((int *)cpy)[0];
410 unsigned int * pointer;
414 /* Nothing is inside */
419 pointer=pointerarray[type];
421 /* Array of primitives */
423 #if defined(DSTM)||defined(FASTCHECK)
424 struct ArrayObject *ao=(struct ArrayObject *) ptr;
425 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
426 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
427 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
430 struct ArrayObject *ao=(struct ArrayObject *) ptr;
431 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
432 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
434 } else if (((int)pointer)==1) {
435 /* Array of pointers */
436 struct ArrayObject *ao=(struct ArrayObject *) ptr;
437 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
438 #if (defined(DSTM)||defined(FASTCHECK))
439 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
440 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
443 SENQUEUE((void *)ao->___objlocation___, *((void **)&ao_cpy->___objlocation___));
445 int length=ao->___length___;
447 for(i=0; i<length; i++) {
448 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
449 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
454 for(i=1; i<=size; i++) {
455 unsigned int offset=pointer[i];
456 void * objptr=*((void **)(((int)ptr)+offset));
457 ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
465 #if defined(THREADS)||defined(DSTM)||defined(STM)
467 pthread_mutex_unlock(&gclistlock);
472 /* Fix up the references from tags. This can't be done earlier,
473 because we don't want tags to keep objects alive */
475 while(taghead!=NULL) {
477 struct pointerblock *tmp=taghead->next;
478 for(i=0; i<tagindex; i++) {
479 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
480 struct ___Object___ *obj=tagd->flagptr;
481 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
483 /* Zero object case */
484 } else if (obj->type==-1) {
485 /* Single object case */
486 copy->flagptr=((struct ___Object___**)obj)[1];
487 } else if (obj->type==OBJECTARRAYTYPE) {
489 struct ArrayObject *ao=(struct ArrayObject *) obj;
493 struct ArrayObject *aonew;
495 /* Count live objects */
496 for(j=0; j<ao->___cachedCode___; j++) {
497 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
502 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
503 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
504 memcpy(aonew, ao, sizeof(struct ArrayObject));
505 aonew->type=OBJECTARRAYTYPE;
506 aonew->___length___=livecount;
508 for(j=0; j<ao->___cachedCode___; j++) {
509 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
510 if (tobj->type==-1) {
511 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
512 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
515 aonew->___cachedCode___=k;
516 for(; k<livecount; k++) {
517 ARRAYSET(aonew, struct ___Object___*, k, NULL);
520 /* No object live anymore */
531 void * tomalloc(int size) {
532 void * ptr=to_heapptr;
539 #if defined(THREADS)||defined(DSTM)||defined(STM)
540 void checkcollect(void * ptr) {
541 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
542 pthread_mutex_lock(&gclock); // Wait for GC
544 pthread_mutex_unlock(&gclock);
548 void checkcollect2(void * ptr) {
549 int ptrarray[]={1, (int)ptr, (int) revertlist};
550 struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
551 pthread_mutex_lock(&gclock); // Wait for GC
553 pthread_mutex_unlock(&gclock);
554 revertlist=(struct ___Object___*)ptrarray[2];
559 struct listitem * stopforgc(struct garbagelist * ptr) {
560 struct listitem * litem=malloc(sizeof(struct listitem));
563 litem->locklist=pthread_getspecific(threadlocks);
566 litem->tc_size=c_size;
567 litem->tc_table=&c_table;
570 pthread_mutex_lock(&gclistlock);
576 pthread_cond_signal(&gccond);
577 pthread_mutex_unlock(&gclistlock);
581 void restartaftergc(struct listitem * litem) {
582 pthread_mutex_lock(&gclistlock);
584 pthread_setspecific(threadlocks, litem->locklist);
586 if (litem->prev==NULL) {
589 litem->prev->next=litem->next;
591 if (litem->next!=NULL) {
592 litem->next->prev=litem->prev;
595 pthread_mutex_unlock(&gclistlock);
600 void * mygcmalloc(struct garbagelist * stackptr, int size) {
602 #if defined(THREADS)||defined(DSTM)||defined(STM)
603 if (pthread_mutex_trylock(&gclock)!=0) {
604 struct listitem *tmp=stopforgc(stackptr);
605 pthread_mutex_lock(&gclock);
613 if (curr_heapptr>curr_heapgcpoint) {
614 if (curr_heapbase==0) {
615 /* Need to allocate base heap */
616 curr_heapbase=malloc(INITIALHEAPSIZE);
617 bzero(curr_heapbase, INITIALHEAPSIZE);
618 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
619 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
620 curr_heapptr=curr_heapbase+size;
622 to_heapbase=malloc(INITIALHEAPSIZE);
623 to_heaptop=to_heapbase+INITIALHEAPSIZE;
624 to_heapptr=to_heapbase;
626 #if defined(THREADS)||defined(DSTM)||defined(STM)
627 pthread_mutex_unlock(&gclock);
632 /* Grow the to heap if necessary */
634 int curr_heapsize=curr_heaptop-curr_heapbase;
635 int to_heapsize=to_heaptop-to_heapbase;
638 last_heapsize=HEAPSIZE(lastgcsize, size);
639 if ((last_heapsize&7)!=0)
640 last_heapsize+=(8-(last_heapsize%8));
642 if (curr_heapsize>last_heapsize)
643 last_heapsize=curr_heapsize;
644 if (last_heapsize>to_heapsize) {
646 to_heapbase=malloc(last_heapsize);
647 if (to_heapbase==NULL) {
648 printf("Error Allocating enough memory\n");
651 to_heaptop=to_heapbase+last_heapsize;
652 to_heapptr=to_heapbase;
656 /* Do our collection */
659 /* Update stat on previous gc size */
660 lastgcsize=(to_heapptr-to_heapbase)+size;
663 printf("Garbage collected: Old bytes: %u\n", curr_heapptr-curr_heapbase);
664 printf("New space: %u\n", to_heapptr-to_heapbase);
665 printf("Total space: %u\n", to_heaptop-to_heapbase);
667 /* Flip to/curr heaps */
669 void * tmp=to_heapbase;
670 to_heapbase=curr_heapbase;
674 to_heaptop=curr_heaptop;
678 curr_heapptr=to_heapptr+size;
679 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
680 to_heapptr=to_heapbase;
682 /* Not enough room :(, redo gc */
683 if (curr_heapptr>curr_heapgcpoint) {
684 #if defined(THREADS)||defined(DSTM)||defined(STM)
685 pthread_mutex_unlock(&gclock);
687 return mygcmalloc(stackptr, size);
690 bzero(tmp, curr_heaptop-tmp);
691 #if defined(THREADS)||defined(DSTM)||defined(STM)
692 pthread_mutex_unlock(&gclock);
697 #if defined(THREADS)||defined(DSTM)||defined(STM)
698 pthread_mutex_unlock(&gclock);
705 int gc_createcopy(void * orig, void ** copy_ptr) {
710 int type=((int *)orig)[0];
712 *copy_ptr=((void **)orig)[1];
715 if (type<NUMCLASSES) {
716 /* We have a normal object */
718 int size=classsize[type]+sizeof(objheader_t);
719 void *newobj=tomalloc(size);
720 memcpy(newobj,((char *) orig)-sizeof(objheader_t), size);
721 newobj=((char *)newobj)+sizeof(objheader_t);
723 int size=classsize[type];
724 void *newobj=tomalloc(size);
725 memcpy(newobj, orig, size);
728 ((void **)orig)[1]=newobj;
732 /* We have an array */
733 struct ArrayObject *ao=(struct ArrayObject *)orig;
734 int elementsize=classsize[type];
735 int length=ao->___length___;
737 int size=sizeof(struct ArrayObject)+length*elementsize+sizeof(objheader_t);
738 void *newobj=tomalloc(size);
739 memcpy(newobj, ((char*)orig)-sizeof(objheader_t), size);
740 newobj=((char *)newobj)+sizeof(objheader_t);
742 int size=sizeof(struct ArrayObject)+length*elementsize;
743 void *newobj=tomalloc(size);
744 memcpy(newobj, orig, size);
748 ((void **)orig)[1]=newobj;