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 pthread_mutex_unlock(&threadtable);
88 pthread_mutex_unlock(&gclistlock);
89 break; /* Have all other threads stopped */
91 pthread_mutex_unlock(&threadtable);
92 pthread_cond_wait(&gccond, &gclistlock);
93 pthread_mutex_unlock(&gclistlock);
100 head=tail=malloc(sizeof(struct pointerblock));
102 /* Check current stack */
105 struct listitem *listptr=list;
106 while(stackptr!=NULL) {
109 while(stackptr!=NULL) {
111 for(i=0;i<stackptr->size;i++) {
112 void * orig=stackptr->array[i];
114 if (gc_createcopy(orig,©))
116 stackptr->array[i]=copy;
118 stackptr=stackptr->next;
121 /* Go to next thread */
123 stackptr=listptr->stackptr;
124 listptr=listptr->next;
132 /* Update objectsets */
134 for(i=0;i<NUMCLASSES;i++) {
135 struct parameterwrapper * p=objectqueues[i];
137 struct RuntimeHash * set=p->objectset;
138 struct RuntimeNode * ptr=set->listhead;
140 void *orig=(void *)ptr->key;
142 if (gc_createcopy(orig, ©))
148 RuntimeHashrehash(set); /* Rehash the table */
155 struct RuntimeNode * ptr=forward->listhead;
157 void * orig=(void *)ptr->key;
159 if (gc_createcopy(orig, ©))
165 RuntimeHashrehash(forward); /* Rehash the table */
169 struct RuntimeNode * ptr=reverse->listhead;
171 void *orig=(void *)ptr->data;
173 if (gc_createcopy(orig, ©))
182 struct RuntimeNode * ptr=fdtoobject->listhead;
184 void *orig=(void *)ptr->data;
186 if (gc_createcopy(orig, ©))
196 /* Update active tasks */
197 struct QueueItem * ptr=activetasks->head;
199 struct taskparamdescriptor *tpd=ptr->objectptr;
201 for(i=0;i<tpd->numParameters;i++) {
202 void *orig=tpd->parameterArray[i];
204 if (gc_createcopy(orig, ©))
206 tpd->parameterArray[i]=copy;
211 /* Update failed tasks */
213 struct genpointerlist * ptr=failedtasks->list;
217 if (gc_createcopy(orig, ©))
223 genrehash(failedtasks);
228 void * ptr=dequeue();
229 void *cpy=((void **)ptr)[1];
230 int type=((int *)cpy)[0];
231 int * pointer=pointerarray[type];
233 /* Array of primitives */
235 } else if (((int)pointer)==1) {
236 /* Array of pointers */
237 struct ArrayObject *ao=(struct ArrayObject *) ptr;
238 struct ArrayObject *ao_cpy=(struct ArrayObject *) cpy;
239 int length=ao->___length___;
241 for(i=0;i<length;i++) {
242 void *objptr=((void **)(((char *)& ao->___length___)+sizeof(int)))[i];
244 if (gc_createcopy(objptr, ©))
246 ((void **)(((char *)& ao_cpy->___length___)+sizeof(int)))[i]=copy;
251 for(i=1;i<=size;i++) {
252 int offset=pointer[i];
253 void * objptr=*((void **)(((int)ptr)+offset));
255 if (gc_createcopy(objptr, ©))
257 *((void **) (((int)cpy)+offset))=copy;
266 void * curr_heapbase=0;
267 void * curr_heapptr=0;
268 void * curr_heapgcpoint=0;
269 void * curr_heaptop=0;
271 void * to_heapbase=0;
276 void * tomalloc(int size) {
277 void * ptr=to_heapptr;
286 void checkcollect(void * ptr) {
288 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
289 pthread_mutex_lock(&gclock);
291 pthread_mutex_unlock(&gclock);
295 struct listitem * stopforgc(struct garbagelist * ptr) {
296 struct listitem * litem=malloc(sizeof(struct listitem));
299 pthread_mutex_lock(&gclistlock);
305 pthread_cond_signal(&gccond);
306 pthread_mutex_unlock(&gclistlock);
310 void restartaftergc(struct listitem * litem) {
311 pthread_mutex_lock(&gclistlock);
312 if (litem->prev==NULL) {
315 litem->prev->next=litem->next;
317 if (litem->next!=NULL) {
318 litem->next->prev=litem->prev;
321 pthread_mutex_unlock(&gclistlock);
326 void * mygcmalloc(struct garbagelist * stackptr, int size) {
329 if (pthread_mutex_trylock(&gclock)!=0) {
330 struct listitem *tmp=stopforgc(stackptr);
331 pthread_mutex_lock(&gclock);
339 if (curr_heapptr>curr_heapgcpoint) {
340 if (curr_heapbase==0) {
341 /* Need to allocate base heap */
342 curr_heapbase=malloc(INITIALHEAPSIZE);
343 bzero(curr_heapbase, INITIALHEAPSIZE);
344 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
345 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
346 curr_heapptr=curr_heapbase+size;
348 to_heapbase=malloc(INITIALHEAPSIZE);
349 to_heaptop=to_heapbase+INITIALHEAPSIZE;
350 to_heapptr=to_heapbase;
353 pthread_mutex_unlock(&gclock);
358 /* Grow the to heap if necessary */
360 int curr_heapsize=curr_heaptop-curr_heapbase;
361 int to_heapsize=to_heaptop-to_heapbase;
364 last_heapsize=HEAPSIZE(lastgcsize, size);
365 if ((last_heapsize%4)!=0)
366 last_heapsize+=(4-(last_heapsize%4));
368 if (curr_heapsize>last_heapsize)
369 last_heapsize=curr_heapsize;
370 if (last_heapsize>to_heapsize) {
372 to_heapbase=malloc(last_heapsize);
373 to_heaptop=to_heapbase+last_heapsize;
374 to_heapptr=to_heapbase;
378 /* Do our collection */
381 /* Update stat on previous gc size */
382 lastgcsize=(to_heapptr-to_heapbase)+size;
384 /* Flip to/curr heaps */
386 void * tmp=to_heapbase;
387 to_heapbase=curr_heapbase;
391 to_heaptop=curr_heaptop;
395 curr_heapptr=to_heapptr+size;
396 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
397 to_heapptr=to_heapbase;
399 /* Not enough room :(, redo gc */
400 if (curr_heapptr>curr_heapgcpoint) {
402 pthread_mutex_unlock(&gclock);
404 return mygcmalloc(stackptr, size);
407 bzero(tmp, curr_heaptop-tmp);
409 pthread_mutex_unlock(&gclock);
415 pthread_mutex_unlock(&gclock);
422 int gc_createcopy(void * orig, void ** copy_ptr) {
427 int type=((int *)orig)[0];
429 *copy_ptr=((void **)orig)[1];
431 } if (type<NUMCLASSES) {
432 /* We have a normal object */
433 int size=classsize[type];
434 void *newobj=tomalloc(size);
435 memcpy(newobj, orig, size);
437 ((void **)orig)[1]=newobj;
441 /* We have an array */
442 struct ArrayObject *ao=(struct ArrayObject *)orig;
443 int elementsize=classsize[type];
444 int length=ao->___length___;
445 int size=sizeof(struct ArrayObject)+length*elementsize;
446 void *newobj=tomalloc(size);
447 memcpy(newobj, orig, size);
449 ((void **)orig)[1]=newobj;