3 #include "structdefs.h"
5 #include "SimpleHash.h"
7 #include "GenericHashtable.h"
9 #if defined(THREADS) || defined(DSTM) || defined(STM)
25 #define INITIALHEAPSIZE 32*1024*1024
26 #define GCPOINT(x) ((int)((x)*0.9))
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)/0.6))+y)
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 (key>=curr_heapbase&&key<curr_heaptop) {
160 SENQUEUE(curr->val, curr->val);
162 //rewrite transaction cache entry
164 int type=((int *)ptr)[0];
165 unsigned int *pointer=pointerarray[type];
167 //array of primitives - do nothing
168 } else if (((int)pointer)==1) {
170 struct ArrayObject *ao=(struct ArrayObject *) ptr;
171 int length=ao->___length___;
173 for(i=0; i<length; i++) {
174 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
175 ENQUEUE(objptr, ((void **)(((char *)&ao->___length___)+sizeof(int)))[i]);
180 for(i=1; i<=size; i++) {
181 unsigned int offset=pointer[i];
182 void * objptr=*((void **)(((int)ptr)+offset));
183 ENQUEUE(objptr, *((void **)(((int)ptr)+offset)));
189 index = (((unsigned int)key) & mask) >>1;
191 curr->key=(unsigned int)key;
193 // Insert into the new table
195 tmp->key = curr->key;
196 tmp->val = curr->val;
201 curr->next=tmp->next;
214 if ((head==tail)&&(tailindex==headindex))
220 struct pointerblock *taghead=NULL;
223 void enqueuetag(struct ___TagDescriptor___ *ptr) {
224 if (tagindex==NUMPTRS) {
225 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
230 taghead->ptrs[tagindex++]=ptr;
235 void collect(struct garbagelist * stackptr) {
236 #if defined(THREADS)||defined(DSTM)||defined(STM)
238 pthread_mutex_lock(&gclistlock);
240 if ((listcount+1)==threadcount) {
241 break; /* Have all other threads stopped */
243 pthread_cond_wait(&gccond, &gclistlock);
250 head=tail=malloc(sizeof(struct pointerblock));
256 taghead=malloc(sizeof(struct pointerblock));
261 /* Check current stack */
262 #if defined(THREADS)||defined(DSTM)||defined(STM)
264 struct listitem *listptr=list;
268 while(stackptr!=NULL) {
270 for(i=0; i<stackptr->size; i++) {
271 void * orig=stackptr->array[i];
272 ENQUEUE(orig, stackptr->array[i]);
274 stackptr=stackptr->next;
276 #if defined(THREADS)||defined(DSTM)||defined(STM)
277 /* Go to next thread */
280 void * orig=listptr->locklist;
281 ENQUEUE(orig, listptr->locklist);
284 fixtable(listptr->tc_table, listptr->tc_size);
286 stackptr=listptr->stackptr;
287 listptr=listptr->next;
295 ENQUEUE(___fcrevert___, ___fcrevert___);
300 /* Update objectsets */
302 for(i=0; i<NUMCLASSES; i++) {
303 #if !defined(MULTICORE)
304 struct parameterwrapper * p=objectqueues[i];
306 struct ObjectHash * set=p->objectset;
307 struct ObjectNode * ptr=set->listhead;
309 void *orig=(void *)ptr->key;
310 ENQUEUE(orig, *((void **)(&ptr->key)));
313 ObjectHashrehash(set); /* Rehash the table */
322 struct cnode * ptr=forward->listhead;
324 void * orig=(void *)ptr->key;
325 ENQUEUE(orig, *((void **)(&ptr->key)));
328 crehash(forward); /* Rehash the table */
332 struct cnode * ptr=reverse->listhead;
334 void *orig=(void *)ptr->val;
335 ENQUEUE(orig, *((void**)(&ptr->val)));
342 struct RuntimeNode * ptr=fdtoobject->listhead;
344 void *orig=(void *)ptr->data;
345 ENQUEUE(orig, *((void**)(&ptr->data)));
351 /* Update current task descriptor */
353 for(i=0; i<currtpd->numParameters; i++) {
354 void *orig=currtpd->parameterArray[i];
355 ENQUEUE(orig, currtpd->parameterArray[i]);
360 /* Update active tasks */
362 struct genpointerlist * ptr=activetasks->list;
364 struct taskparamdescriptor *tpd=ptr->src;
366 for(i=0; i<tpd->numParameters; i++) {
367 void * orig=tpd->parameterArray[i];
368 ENQUEUE(orig, tpd->parameterArray[i]);
372 genrehash(activetasks);
375 /* Update failed tasks */
377 struct genpointerlist * ptr=failedtasks->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(failedtasks);
392 void * ptr=dequeue();
393 void *cpy=((void **)ptr)[1];
394 int type=((int *)cpy)[0];
395 unsigned int * pointer;
399 /* Nothing is inside */
404 pointer=pointerarray[type];
406 /* Array of primitives */
408 #if defined(DSTM)||defined(FASTCHECK)
409 struct ArrayObject *ao=(struct ArrayObject *) ptr;
410 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
411 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
412 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
414 } else if (((int)pointer)==1) {
415 /* Array of pointers */
416 struct ArrayObject *ao=(struct ArrayObject *) ptr;
417 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
418 #if (defined(DSTM)||defined(FASTCHECK))
419 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
420 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
422 int length=ao->___length___;
424 for(i=0; i<length; i++) {
425 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
426 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
431 for(i=1; i<=size; i++) {
432 unsigned int offset=pointer[i];
433 void * objptr=*((void **)(((int)ptr)+offset));
434 ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
442 #if defined(THREADS)||defined(DSTM)||defined(STM)
444 pthread_mutex_unlock(&gclistlock);
449 /* Fix up the references from tags. This can't be done earlier,
450 because we don't want tags to keep objects alive */
452 while(taghead!=NULL) {
454 struct pointerblock *tmp=taghead->next;
455 for(i=0; i<tagindex; i++) {
456 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
457 struct ___Object___ *obj=tagd->flagptr;
458 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
460 /* Zero object case */
461 } else if (obj->type==-1) {
462 /* Single object case */
463 copy->flagptr=((struct ___Object___**)obj)[1];
464 } else if (obj->type==OBJECTARRAYTYPE) {
466 struct ArrayObject *ao=(struct ArrayObject *) obj;
470 struct ArrayObject *aonew;
472 /* Count live objects */
473 for(j=0; j<ao->___cachedCode___; j++) {
474 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
479 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
480 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
481 memcpy(aonew, ao, sizeof(struct ArrayObject));
482 aonew->type=OBJECTARRAYTYPE;
483 aonew->___length___=livecount;
485 for(j=0; j<ao->___cachedCode___; j++) {
486 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
487 if (tobj->type==-1) {
488 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
489 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
492 aonew->___cachedCode___=k;
493 for(; k<livecount; k++) {
494 ARRAYSET(aonew, struct ___Object___*, k, NULL);
497 /* No object live anymore */
508 void * tomalloc(int size) {
509 void * ptr=to_heapptr;
516 #if defined(THREADS)||defined(DSTM)||defined(STM)
517 void checkcollect(void * ptr) {
518 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
519 pthread_mutex_lock(&gclock); // Wait for GC
521 pthread_mutex_unlock(&gclock);
525 void checkcollect2(void * ptr) {
526 int ptrarray[]={1, (int)ptr, (int) revertlist};
527 struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
528 pthread_mutex_lock(&gclock); // Wait for GC
530 pthread_mutex_unlock(&gclock);
531 revertlist=(struct ___Object___*)ptrarray[2];
536 struct listitem * stopforgc(struct garbagelist * ptr) {
537 struct listitem * litem=malloc(sizeof(struct listitem));
540 litem->locklist=pthread_getspecific(threadlocks);
543 litem->tc_size=c_size;
544 litem->tc_table=&c_table;
547 pthread_mutex_lock(&gclistlock);
553 pthread_cond_signal(&gccond);
554 pthread_mutex_unlock(&gclistlock);
558 void restartaftergc(struct listitem * litem) {
559 pthread_mutex_lock(&gclistlock);
561 pthread_setspecific(threadlocks, litem->locklist);
563 if (litem->prev==NULL) {
566 litem->prev->next=litem->next;
568 if (litem->next!=NULL) {
569 litem->next->prev=litem->prev;
572 pthread_mutex_unlock(&gclistlock);
577 void * mygcmalloc(struct garbagelist * stackptr, int size) {
579 #if defined(THREADS)||defined(DSTM)||defined(STM)
580 if (pthread_mutex_trylock(&gclock)!=0) {
581 struct listitem *tmp=stopforgc(stackptr);
582 pthread_mutex_lock(&gclock);
590 if (curr_heapptr>curr_heapgcpoint) {
591 if (curr_heapbase==0) {
592 /* Need to allocate base heap */
593 curr_heapbase=malloc(INITIALHEAPSIZE);
594 bzero(curr_heapbase, INITIALHEAPSIZE);
595 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
596 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
597 curr_heapptr=curr_heapbase+size;
599 to_heapbase=malloc(INITIALHEAPSIZE);
600 to_heaptop=to_heapbase+INITIALHEAPSIZE;
601 to_heapptr=to_heapbase;
603 #if defined(THREADS)||defined(DSTM)||defined(STM)
604 pthread_mutex_unlock(&gclock);
609 /* Grow the to heap if necessary */
611 int curr_heapsize=curr_heaptop-curr_heapbase;
612 int to_heapsize=to_heaptop-to_heapbase;
615 last_heapsize=HEAPSIZE(lastgcsize, size);
616 if ((last_heapsize&7)!=0)
617 last_heapsize+=(8-(last_heapsize%8));
619 if (curr_heapsize>last_heapsize)
620 last_heapsize=curr_heapsize;
621 if (last_heapsize>to_heapsize) {
623 to_heapbase=malloc(last_heapsize);
624 if (to_heapbase==NULL) {
625 printf("Error Allocating enough memory\n");
628 to_heaptop=to_heapbase+last_heapsize;
629 to_heapptr=to_heapbase;
633 /* Do our collection */
636 /* Update stat on previous gc size */
637 lastgcsize=(to_heapptr-to_heapbase)+size;
639 /* Flip to/curr heaps */
641 void * tmp=to_heapbase;
642 to_heapbase=curr_heapbase;
646 to_heaptop=curr_heaptop;
650 curr_heapptr=to_heapptr+size;
651 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
652 to_heapptr=to_heapbase;
654 /* Not enough room :(, redo gc */
655 if (curr_heapptr>curr_heapgcpoint) {
656 #if defined(THREADS)||defined(DSTM)||defined(STM)
657 pthread_mutex_unlock(&gclock);
659 return mygcmalloc(stackptr, size);
662 bzero(tmp, curr_heaptop-tmp);
663 #if defined(THREADS)||defined(DSTM)||defined(STM)
664 pthread_mutex_unlock(&gclock);
669 #if defined(THREADS)||defined(DSTM)||defined(STM)
670 pthread_mutex_unlock(&gclock);
677 int gc_createcopy(void * orig, void ** copy_ptr) {
682 int type=((int *)orig)[0];
684 *copy_ptr=((void **)orig)[1];
687 if (type<NUMCLASSES) {
688 /* We have a normal object */
689 int size=classsize[type];
690 void *newobj=tomalloc(size);
691 memcpy(newobj, orig, size);
693 ((void **)orig)[1]=newobj;
697 /* We have an array */
698 struct ArrayObject *ao=(struct ArrayObject *)orig;
699 int elementsize=classsize[type];
700 int length=ao->___length___;
701 int size=sizeof(struct ArrayObject)+length*elementsize;
702 void *newobj=tomalloc(size);
703 memcpy(newobj, orig, size);
705 ((void **)orig)[1]=newobj;