3 #include "structdefs.h"
5 #include "SimpleHash.h"
7 #include "GenericHashtable.h"
9 #if defined(THREADS) || defined(DSTM)
22 #define INITIALHEAPSIZE 8192*1024
23 #define GCPOINT(x) ((int)((x)*0.9))
24 /* This define takes in how full the heap is initially and returns a new heap size to use */
25 #define HEAPSIZE(x,y) (((int)((x)/0.6))+y)
28 extern struct genhashtable * activetasks;
30 extern struct parameterwrapper * objectqueues[NUMCLASSES];
32 extern struct genhashtable * failedtasks;
33 extern struct taskparamdescriptor *currtpd;
34 extern struct ctable *forward;
35 extern struct ctable *reverse;
36 extern struct RuntimeHash *fdtoobject;
39 #if defined(THREADS) || defined(DSTM)
41 struct listitem * list=NULL;
45 //Need to check if pointers are transaction pointers
47 #define ENQUEUE(orig, dst) \
48 if ((!(((unsigned int)orig)&0x1))) { \
49 if (orig>=curr_heapbase&&orig<curr_heaptop) { \
51 if (gc_createcopy(orig,©)) \
57 #define ENQUEUE(orig, dst) \
59 if (gc_createcopy(orig,©)) \
66 struct pointerblock *next;
69 void * curr_heapbase=0;
70 void * curr_heapptr=0;
71 void * curr_heapgcpoint=0;
72 void * curr_heaptop=0;
79 struct pointerblock *head=NULL;
81 struct pointerblock *tail=NULL;
83 struct pointerblock *spare=NULL;
85 void enqueue(void *ptr) {
86 if (headindex==NUMPTRS) {
87 struct pointerblock * tmp;
92 tmp=malloc(sizeof(struct pointerblock));
97 head->ptrs[headindex++]=ptr;
101 if (tailindex==NUMPTRS) {
102 struct pointerblock *tmp=tail;
110 return tail->ptrs[tailindex++];
114 if ((head==tail)&&(tailindex==headindex))
120 struct pointerblock *taghead=NULL;
123 void enqueuetag(struct ___TagDescriptor___ *ptr) {
124 if (tagindex==NUMPTRS) {
125 struct pointerblock * tmp=malloc(sizeof(struct pointerblock));
130 taghead->ptrs[tagindex++]=ptr;
135 void collect(struct garbagelist * stackptr) {
136 #if defined(THREADS)||defined(DSTM)
138 pthread_mutex_lock(&gclistlock);
140 if ((listcount+1)==threadcount) {
141 break; /* Have all other threads stopped */
143 pthread_cond_wait(&gccond, &gclistlock);
150 head=tail=malloc(sizeof(struct pointerblock));
156 taghead=malloc(sizeof(struct pointerblock));
161 /* Check current stack */
162 #if defined(THREADS)||defined(DSTM)
164 struct listitem *listptr=list;
168 while(stackptr!=NULL) {
170 for(i=0; i<stackptr->size; i++) {
171 void * orig=stackptr->array[i];
172 ENQUEUE(orig, stackptr->array[i]);
174 stackptr=stackptr->next;
176 #if defined(THREADS)||defined(DSTM)
177 /* Go to next thread */
179 void * orig=listptr->locklist;
180 ENQUEUE(orig, listptr->locklist);
181 stackptr=listptr->stackptr;
182 listptr=listptr->next;
191 /* Update objectsets */
193 for(i=0; i<NUMCLASSES; i++) {
196 struct parameterwrapper * p=objectqueues[i];
198 struct ObjectHash * set=p->objectset;
199 struct ObjectNode * ptr=set->listhead;
201 void *orig=(void *)ptr->key;
202 ENQUEUE(orig, *((void **)(&ptr->key)));
205 ObjectHashrehash(set); /* Rehash the table */
213 struct cnode * ptr=forward->listhead;
215 void * orig=(void *)ptr->key;
216 ENQUEUE(orig, *((void **)(&ptr->key)));
219 crehash(forward); /* Rehash the table */
223 struct cnode * ptr=reverse->listhead;
225 void *orig=(void *)ptr->val;
226 ENQUEUE(orig, *((void**)(&ptr->val)));
232 struct RuntimeNode * ptr=fdtoobject->listhead;
234 void *orig=(void *)ptr->data;
235 ENQUEUE(orig, *((void**)(&ptr->data)));
241 /* Update current task descriptor */
243 for(i=0; i<currtpd->numParameters; i++) {
244 void *orig=currtpd->parameterArray[i];
245 ENQUEUE(orig, currtpd->parameterArray[i]);
250 /* Update active tasks */
252 struct genpointerlist * ptr=activetasks->list;
254 struct taskparamdescriptor *tpd=ptr->src;
256 for(i=0; i<tpd->numParameters; i++) {
257 void * orig=tpd->parameterArray[i];
258 ENQUEUE(orig, tpd->parameterArray[i]);
262 genrehash(activetasks);
265 /* Update failed tasks */
267 struct genpointerlist * ptr=failedtasks->list;
269 struct taskparamdescriptor *tpd=ptr->src;
271 for(i=0; i<tpd->numParameters; i++) {
272 void * orig=tpd->parameterArray[i];
273 ENQUEUE(orig, tpd->parameterArray[i]);
277 genrehash(failedtasks);
282 void * ptr=dequeue();
283 void *cpy=((void **)ptr)[1];
284 int type=((int *)cpy)[0];
285 unsigned int * pointer;
289 /* Nothing is inside */
294 pointer=pointerarray[type];
296 /* Array of primitives */
299 struct ArrayObject *ao=(struct ArrayObject *) ptr;
300 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
301 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
302 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
304 } else if (((int)pointer)==1) {
305 /* Array of pointers */
306 struct ArrayObject *ao=(struct ArrayObject *) ptr;
307 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
309 ENQUEUE((void *)ao->___nextobject___, *((void **)&ao_cpy->___nextobject___));
310 ENQUEUE((void *)ao->___localcopy___, *((void **)&ao_cpy->___localcopy___));
312 int length=ao->___length___;
314 for(i=0; i<length; i++) {
315 void *objptr=((void **)(((char *)&ao->___length___)+sizeof(int)))[i];
316 ENQUEUE(objptr, ((void **)(((char *)&ao_cpy->___length___)+sizeof(int)))[i]);
321 for(i=1; i<=size; i++) {
322 unsigned int offset=pointer[i];
323 void * objptr=*((void **)(((int)ptr)+offset));
324 ENQUEUE(objptr, *((void **)(((int)cpy)+offset)));
332 #if defined(THREADS)||defined(DSTM)
334 pthread_mutex_unlock(&gclistlock);
340 /* Fix up the references from tags. This can't be done earlier,
341 because we don't want tags to keep objects alive */
343 while(taghead!=NULL) {
345 struct pointerblock *tmp=taghead->next;
346 for(i=0; i<tagindex; i++) {
347 struct ___TagDescriptor___ *tagd=taghead->ptrs[i];
348 struct ___Object___ *obj=tagd->flagptr;
349 struct ___TagDescriptor___ *copy=((struct ___TagDescriptor___**)tagd)[1];
351 /* Zero object case */
352 } else if (obj->type==-1) {
353 /* Single object case */
354 copy->flagptr=((struct ___Object___**)obj)[1];
355 } else if (obj->type==OBJECTARRAYTYPE) {
357 struct ArrayObject *ao=(struct ArrayObject *) obj;
361 struct ArrayObject *aonew;
363 /* Count live objects */
364 for(j=0; j<ao->___cachedCode___; j++) {
365 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
370 livecount=((livecount-1)/OBJECTARRAYINTERVAL+1)*OBJECTARRAYINTERVAL;
371 aonew=(struct ArrayObject *) tomalloc(sizeof(struct ArrayObject)+sizeof(struct ___Object___*)*livecount);
372 memcpy(aonew, ao, sizeof(struct ArrayObject));
373 aonew->type=OBJECTARRAYTYPE;
374 aonew->___length___=livecount;
376 for(j=0; j<ao->___cachedCode___; j++) {
377 struct ___Object___ * tobj=ARRAYGET(ao, struct ___Object___ *, j);
378 if (tobj->type==-1) {
379 struct ___Object___ * tobjcpy=((struct ___Object___**)tobj)[1];
380 ARRAYSET(aonew, struct ___Object___*, k++,tobjcpy);
383 aonew->___cachedCode___=k;
384 for(; k<livecount; k++) {
385 ARRAYSET(aonew, struct ___Object___*, k, NULL);
388 /* No object live anymore */
399 void * tomalloc(int size) {
400 void * ptr=to_heapptr;
407 #if defined(THREADS)||defined(DSTM)
408 void checkcollect(void * ptr) {
410 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
411 pthread_mutex_lock(&gclock); // Wait for GC
413 pthread_mutex_unlock(&gclock);
419 void checkcollect2(void * ptr, transrecord_t *trans) {
421 int ptrarray[]={1, (int)ptr, (int) trans->revertlist};
422 struct listitem * tmp=stopforgc((struct garbagelist *)ptrarray);
423 pthread_mutex_lock(&gclock); // Wait for GC
425 pthread_mutex_unlock(&gclock);
426 trans->revertlist=(struct ___Object___*)ptrarray[2];
432 struct listitem * stopforgc(struct garbagelist * ptr) {
433 struct listitem * litem=malloc(sizeof(struct listitem));
435 litem->locklist=pthread_getspecific(threadlocks);
437 pthread_mutex_lock(&gclistlock);
443 pthread_cond_signal(&gccond);
444 pthread_mutex_unlock(&gclistlock);
448 void restartaftergc(struct listitem * litem) {
449 pthread_mutex_lock(&gclistlock);
450 pthread_setspecific(threadlocks, litem->locklist);
451 if (litem->prev==NULL) {
454 litem->prev->next=litem->next;
456 if (litem->next!=NULL) {
457 litem->next->prev=litem->prev;
460 pthread_mutex_unlock(&gclistlock);
465 void * mygcmalloc(struct garbagelist * stackptr, int size) {
467 #if defined(THREADS)||defined(DSTM)
468 if (pthread_mutex_trylock(&gclock)!=0) {
469 struct listitem *tmp=stopforgc(stackptr);
470 pthread_mutex_lock(&gclock);
478 if (curr_heapptr>curr_heapgcpoint) {
479 if (curr_heapbase==0) {
480 /* Need to allocate base heap */
481 curr_heapbase=malloc(INITIALHEAPSIZE);
482 bzero(curr_heapbase, INITIALHEAPSIZE);
483 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
484 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
485 curr_heapptr=curr_heapbase+size;
487 to_heapbase=malloc(INITIALHEAPSIZE);
488 to_heaptop=to_heapbase+INITIALHEAPSIZE;
489 to_heapptr=to_heapbase;
491 #if defined(THREADS)||defined(DSTM)
492 pthread_mutex_unlock(&gclock);
497 /* Grow the to heap if necessary */
499 int curr_heapsize=curr_heaptop-curr_heapbase;
500 int to_heapsize=to_heaptop-to_heapbase;
503 last_heapsize=HEAPSIZE(lastgcsize, size);
504 if ((last_heapsize%4)!=0)
505 last_heapsize+=(4-(last_heapsize%4));
507 if (curr_heapsize>last_heapsize)
508 last_heapsize=curr_heapsize;
509 if (last_heapsize>to_heapsize) {
511 to_heapbase=malloc(last_heapsize);
512 to_heaptop=to_heapbase+last_heapsize;
513 to_heapptr=to_heapbase;
517 /* Do our collection */
520 /* Update stat on previous gc size */
521 lastgcsize=(to_heapptr-to_heapbase)+size;
523 /* Flip to/curr heaps */
525 void * tmp=to_heapbase;
526 to_heapbase=curr_heapbase;
530 to_heaptop=curr_heaptop;
534 curr_heapptr=to_heapptr+size;
535 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
536 to_heapptr=to_heapbase;
538 /* Not enough room :(, redo gc */
539 if (curr_heapptr>curr_heapgcpoint) {
540 #if defined(THREADS)||defined(DSTM)
541 pthread_mutex_unlock(&gclock);
543 return mygcmalloc(stackptr, size);
546 bzero(tmp, curr_heaptop-tmp);
547 #if defined(THREADS)||defined(DSTM)
548 pthread_mutex_unlock(&gclock);
553 #if defined(THREADS)||defined(DSTM)
554 pthread_mutex_unlock(&gclock);
561 int gc_createcopy(void * orig, void ** copy_ptr) {
566 int type=((int *)orig)[0];
568 *copy_ptr=((void **)orig)[1];
571 if (type<NUMCLASSES) {
572 /* We have a normal object */
573 int size=classsize[type];
574 void *newobj=tomalloc(size);
575 memcpy(newobj, orig, size);
577 ((void **)orig)[1]=newobj;
581 /* We have an array */
582 struct ArrayObject *ao=(struct ArrayObject *)orig;
583 int elementsize=classsize[type];
584 int length=ao->___length___;
585 int size=sizeof(struct ArrayObject)+length*elementsize;
586 void *newobj=tomalloc(size);
587 memcpy(newobj, orig, size);
589 ((void **)orig)[1]=newobj;