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) {
83 pthread_mutex_lock(&gclistlock);
85 if ((listcount+1)==threadcount) {
86 break; /* Have all other threads stopped */
88 pthread_cond_wait(&gccond, &gclistlock);
95 head=tail=malloc(sizeof(struct pointerblock));
97 /* Check current stack */
100 struct listitem *listptr=list;
101 while(stackptr!=NULL) {
104 while(stackptr!=NULL) {
106 for(i=0;i<stackptr->size;i++) {
107 void * orig=stackptr->array[i];
109 if (gc_createcopy(orig,©))
111 stackptr->array[i]=copy;
113 stackptr=stackptr->next;
116 /* Go to next thread */
118 void * orig=listptr->locklist;
120 if (gc_createcopy(orig,©))
122 listptr->locklist=copy;
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;
263 pthread_mutex_unlock(&gclistlock);
267 void * curr_heapbase=0;
268 void * curr_heapptr=0;
269 void * curr_heapgcpoint=0;
270 void * curr_heaptop=0;
272 void * to_heapbase=0;
277 void * tomalloc(int size) {
278 void * ptr=to_heapptr;
287 void checkcollect(void * ptr) {
289 struct listitem * tmp=stopforgc((struct garbagelist *)ptr);
290 pthread_mutex_lock(&gclock); // Wait for GC
292 pthread_mutex_unlock(&gclock);
297 struct listitem * stopforgc(struct garbagelist * ptr) {
298 struct listitem * litem=malloc(sizeof(struct listitem));
300 litem->locklist=pthread_getspecific(threadlocks);
302 pthread_mutex_lock(&gclistlock);
308 pthread_cond_signal(&gccond);
309 pthread_mutex_unlock(&gclistlock);
313 void restartaftergc(struct listitem * litem) {
314 pthread_mutex_lock(&gclistlock);
315 pthread_setspecific(threadlocks, litem->locklist);
316 if (litem->prev==NULL) {
319 litem->prev->next=litem->next;
321 if (litem->next!=NULL) {
322 litem->next->prev=litem->prev;
325 pthread_mutex_unlock(&gclistlock);
330 void * mygcmalloc(struct garbagelist * stackptr, int size) {
333 if (pthread_mutex_trylock(&gclock)!=0) {
334 struct listitem *tmp=stopforgc(stackptr);
335 pthread_mutex_lock(&gclock);
343 if (curr_heapptr>curr_heapgcpoint) {
344 if (curr_heapbase==0) {
345 /* Need to allocate base heap */
346 curr_heapbase=malloc(INITIALHEAPSIZE);
347 bzero(curr_heapbase, INITIALHEAPSIZE);
348 curr_heaptop=curr_heapbase+INITIALHEAPSIZE;
349 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(INITIALHEAPSIZE);
350 curr_heapptr=curr_heapbase+size;
352 to_heapbase=malloc(INITIALHEAPSIZE);
353 to_heaptop=to_heapbase+INITIALHEAPSIZE;
354 to_heapptr=to_heapbase;
357 pthread_mutex_unlock(&gclock);
362 /* Grow the to heap if necessary */
364 int curr_heapsize=curr_heaptop-curr_heapbase;
365 int to_heapsize=to_heaptop-to_heapbase;
368 last_heapsize=HEAPSIZE(lastgcsize, size);
369 if ((last_heapsize%4)!=0)
370 last_heapsize+=(4-(last_heapsize%4));
372 if (curr_heapsize>last_heapsize)
373 last_heapsize=curr_heapsize;
374 if (last_heapsize>to_heapsize) {
376 to_heapbase=malloc(last_heapsize);
377 to_heaptop=to_heapbase+last_heapsize;
378 to_heapptr=to_heapbase;
382 /* Do our collection */
385 /* Update stat on previous gc size */
386 lastgcsize=(to_heapptr-to_heapbase)+size;
388 /* Flip to/curr heaps */
390 void * tmp=to_heapbase;
391 to_heapbase=curr_heapbase;
395 to_heaptop=curr_heaptop;
399 curr_heapptr=to_heapptr+size;
400 curr_heapgcpoint=((char *) curr_heapbase)+GCPOINT(curr_heaptop-curr_heapbase);
401 to_heapptr=to_heapbase;
403 /* Not enough room :(, redo gc */
404 if (curr_heapptr>curr_heapgcpoint) {
406 pthread_mutex_unlock(&gclock);
408 return mygcmalloc(stackptr, size);
411 bzero(tmp, curr_heaptop-tmp);
413 pthread_mutex_unlock(&gclock);
419 pthread_mutex_unlock(&gclock);
426 int gc_createcopy(void * orig, void ** copy_ptr) {
431 int type=((int *)orig)[0];
433 *copy_ptr=((void **)orig)[1];
435 } if (type<NUMCLASSES) {
436 /* We have a normal object */
437 int size=classsize[type];
438 void *newobj=tomalloc(size);
439 memcpy(newobj, orig, size);
441 ((void **)orig)[1]=newobj;
445 /* We have an array */
446 struct ArrayObject *ao=(struct ArrayObject *)orig;
447 int elementsize=classsize[type];
448 int length=ao->___length___;
449 int size=sizeof(struct ArrayObject)+length*elementsize;
450 void *newobj=tomalloc(size);
451 memcpy(newobj, orig, size);
453 ((void **)orig)[1]=newobj;