3 #include "structdefs.h"
5 #include "SimpleHash.h"
6 #include "GenericHashtable.h"
15 #define INITIALHEAPSIZE 10*1024
16 #define GCPOINT(x) ((int)((x)*0.9))
17 /* This define takes in how full the heap is initially and returns a new heap size to use */
18 #define HEAPSIZE(x,y) (((int)((x)/0.6))+y)
21 extern struct Queue * activetasks;
22 extern struct parameterwrapper * objectqueues[NUMCLASSES];
23 extern struct genhashtable * failedtasks;
24 extern struct RuntimeHash *forward;
25 extern struct RuntimeHash *reverse;
26 extern struct RuntimeHash *fdtoobject;
31 struct listitem * list=NULL;
37 struct pointerblock *next;
40 struct pointerblock *head=NULL;
42 struct pointerblock *tail=NULL;
44 struct pointerblock *spare=NULL;
46 void enqueue(void *ptr) {
47 if (headindex==NUMPTRS) {
48 struct pointerblock * tmp;
53 tmp=malloc(sizeof(struct pointerblock));
58 head->ptrs[headindex++]=ptr;
62 if (tailindex==NUMPTRS) {
63 struct pointerblock *tmp=tail;
71 return tail->ptrs[tailindex++];
75 if ((head==tail)&&(tailindex==headindex))
80 void collect(struct garbagelist * stackptr) {
84 pthread_mutex_lock(&gclistlock);
85 pthread_mutex_lock(&threadtable);
86 if ((listcount+1)==threadcount) {
87 break; /* Have all other threads stopped */
89 pthread_mutex_unlock(&threadtable);
90 pthread_cond_wait(&gccond, &gclistlock);
91 pthread_mutex_unlock(&gclistlock);
98 head=tail=malloc(sizeof(struct pointerblock));
100 /* Check current stack */
103 struct listitem *listptr=list;
104 while(stackptr!=NULL) {
107 while(stackptr!=NULL) {
109 for(i=0;i<stackptr->size;i++) {
110 void * orig=stackptr->array[i];
112 if (gc_createcopy(orig,©))
114 stackptr->array[i]=copy;
116 stackptr=stackptr->next;
119 /* Go to next thread */
121 stackptr=listptr->stackptr;
122 listptr=listptr->next;
130 /* Update objectsets */
132 for(i=0;i<NUMCLASSES;i++) {
133 struct parameterwrapper * p=objectqueues[i];
135 struct RuntimeHash * set=p->objectset;
136 struct RuntimeNode * ptr=set->listhead;
138 void *orig=(void *)ptr->key;
140 if (gc_createcopy(orig, ©))
146 RuntimeHashrehash(set); /* Rehash the table */
153 struct RuntimeNode * ptr=forward->listhead;
155 void * orig=(void *)ptr->key;
157 if (gc_createcopy(orig, ©))
163 RuntimeHashrehash(forward); /* Rehash the table */
167 struct RuntimeNode * ptr=reverse->listhead;
169 void *orig=(void *)ptr->data;
171 if (gc_createcopy(orig, ©))
180 struct RuntimeNode * ptr=fdtoobject->listhead;
182 void *orig=(void *)ptr->data;
184 if (gc_createcopy(orig, ©))
194 /* Update active tasks */
195 struct QueueItem * ptr=activetasks->head;
197 struct taskparamdescriptor *tpd=ptr->objectptr;
199 for(i=0;i<tpd->numParameters;i++) {
200 void *orig=tpd->parameterArray[i];
202 if (gc_createcopy(orig, ©))
204 tpd->parameterArray[i]=copy;
209 /* Update failed tasks */
211 struct genpointerlist * ptr=failedtasks->list;
215 if (gc_createcopy(orig, ©))
221 genrehash(failedtasks);
226 void * ptr=dequeue();
227 void *cpy=((void **)ptr)[1];
228 int type=((int *)cpy)[0];
229 int * pointer=pointerarray[type];
231 /* Array of primitives */
233 } else if (((int)pointer)==1) {
234 /* Array of pointers */
235 struct ArrayObject *ao=(struct ArrayObject *) ptr;
236 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
237 int length=ao->___length___;
239 for(i=0;i<length;i++) {
240 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
242 if (gc_createcopy(objptr, ©))
244 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=copy;
249 for(i=1;i<=size;i++) {
250 int offset=pointer[i];
251 void * objptr=*((void **)(((int)ptr)+offset));
253 if (gc_createcopy(objptr, ©))
255 *((void **) (((int)cpy)+offset))=copy;
264 void * curr_heapbase=0;
265 void * curr_heapptr=0;
266 void * curr_heapgcpoint=0;
267 void * curr_heaptop=0;
269 void * to_heapbase=0;
274 void * tomalloc(int size) {
275 void * ptr=to_heapptr;
284 void checkcollect(void * ptr) {
286 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
287 pthread_mutex_lock(&gclock);
292 struct listitem * stopforgc(struct garbagelist * ptr) {
293 struct listitem * litem=malloc(sizeof(struct listitem));
296 pthread_mutex_lock(&gclistlock);
302 pthread_cond_signal(&gccond);
303 pthread_mutex_unlock(&gclistlock);
307 void restartaftergc(struct listitem * litem) {
308 pthread_mutex_lock(&gclistlock);
309 if (litem->prev==NULL) {
312 litem->prev->next=litem->next;
314 if (litem->next!=NULL) {
315 litem->next->prev=litem->prev;
318 pthread_mutex_unlock(&gclistlock);
323 void * mygcmalloc(struct garbagelist * stackptr, int size) {
326 if (pthread_mutex_trylock(&gclock)!=0) {
327 struct listitem *tmp=stopforgc(stackptr);
328 pthread_mutex_lock(&gclock);
336 if (curr_heapptr>curr_heapgcpoint) {
337 if (curr_heapbase==0) {
338 /* Need to allocate base heap */
339 curr_heapbase=malloc(INITIALHEAPSIZE);
340 bzero(curr_heapbase, INITIALHEAPSIZE);
341 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
342 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
343 curr_heapptr=curr_heapbase+size;
345 to_heapbase=malloc(INITIALHEAPSIZE);
346 to_heaptop=to_heapbase+INITIALHEAPSIZE;
347 to_heapptr=to_heapbase;
350 pthread_mutex_unlock(&gclock);
355 /* Grow the to heap if necessary */
357 int curr_heapsize=curr_heaptop-curr_heapbase;
358 int to_heapsize=to_heaptop-to_heapbase;
361 last_heapsize=HEAPSIZE(lastgcsize, size);
362 if ((last_heapsize%4)!=0)
363 last_heapsize+=(4-(last_heapsize%4));
365 if (curr_heapsize>last_heapsize)
366 last_heapsize=curr_heapsize;
367 if (last_heapsize>to_heapsize) {
369 to_heapbase=malloc(last_heapsize);
370 to_heaptop=to_heapbase+last_heapsize;
371 to_heapptr=to_heapbase;
375 /* Do our collection */
378 /* Update stat on previous gc size */
379 lastgcsize=(to_heapptr-to_heapbase)+size;
381 /* Flip to/curr heaps */
383 void * tmp=to_heapbase;
384 to_heapbase=curr_heapbase;
388 to_heaptop=curr_heaptop;
392 curr_heapptr=to_heapptr+size;
393 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
394 to_heapptr=to_heapbase;
396 /* Not enough room :(, redo gc */
397 if (curr_heapptr>curr_heapgcpoint) {
399 pthread_mutex_unlock(&gclock);
401 return mygcmalloc(stackptr, size);
404 bzero(tmp, curr_heaptop-tmp);
406 pthread_mutex_unlock(&gclock);
412 pthread_mutex_unlock(&gclock);
419 int gc_createcopy(void * orig, void ** copy_ptr) {
424 int type=((int *)orig)[0];
426 *copy_ptr=((void **)orig)[1];
428 } if (type<NUMCLASSES) {
429 /* We have a normal object */
430 int size=classsize[type];
431 void *newobj=tomalloc(size);
432 memcpy(newobj, orig, size);
434 ((void **)orig)[1]=newobj;
438 /* We have an array */
439 struct ArrayObject *ao=(struct ArrayObject *)orig;
440 int elementsize=classsize[type];
441 int length=ao->___length___;
442 int size=sizeof(struct ArrayObject)+length*elementsize;
443 void *newobj=tomalloc(size);
444 memcpy(newobj, orig, size);
446 ((void **)orig)[1]=newobj;