7587b36c53c292482a2d6ad54ecaee3cebbd4bf5
[IRC.git] / Robust / src / Runtime / bamboo / pmc_refupdate.c
1 #include <stdlib.h>
2 #include "structdefs.h"
3 #include "bambooalign.h"
4 #include "runtime_arch.h"
5 #include "pmc_forward.h"
6 #include "pmc_refupdate.h"
7 #include "multicoremgc.h"
8 #include "runtime.h"
9 #include "thread.h"
10
11
12 #define pmcupdateObj(objptr) ((void *)((struct ___Object___ *)objptr)->marked)
13
14 #define PMCUPDATEOBJ(obj) {void *updatetmpptr=obj; if (updatetmpptr!=NULL) {obj=pmcupdateObj(updatetmpptr);}}
15
16 //if (obj==NULL) {tprintf("BAD REF UPDATE %x->%x in %u\n",updatetmpptr,obj,__LINE__);}}}
17
18 #define PMCUPDATEOBJNONNULL(obj) {void *updatetmpptr=obj; obj=pmcupdateObj(updatetmpptr);}
19 //if (obj==NULL) {tprintf("BAD REF UPDATE in %x->%x %u\n",updatetmpptr,obj,__LINE__);}}
20
21 void pmc_updatePtrs(void *ptr, int type) {
22   unsigned int * pointer=pointerarray[type];
23   //tprintf("Updating pointers in %x\n", ptr);
24   if (pointer==0) {
25     /* Array of primitives */
26   } else if (((unsigned int)pointer)==1) {
27     /* Array of pointers */
28     struct ArrayObject *ao=(struct ArrayObject *) ptr;
29     int length=ao->___length___;
30     for(int j=0; j<length; j++) {
31       PMCUPDATEOBJ(((void **)(((char *)&ao->___length___)+sizeof(int)))[j]);
32     }
33   } else {
34     unsigned int size=pointer[0];
35     
36     for(int i=1; i<=size; i++) {
37       unsigned int offset=pointer[i];
38       PMCUPDATEOBJ(*((void **)(((char *)ptr)+offset)));
39     }
40   }
41   //tprintf("done\n");
42 }
43
44 void pmc_updategarbagelist(struct garbagelist *listptr) {
45   for(;listptr!=NULL; listptr=listptr->next) {
46     for(int i=0; i<listptr->size; i++) {
47       PMCUPDATEOBJ(listptr->array[i]);
48     }
49   }
50 }
51
52 void pmc_updateRuntimePtrs(struct garbagelist * stackptr) {
53   // update current stack
54   pmc_updategarbagelist(stackptr);
55
56   // update static pointers global_defs_p
57   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
58     pmc_updategarbagelist((struct garbagelist *)global_defs_p);
59   }
60
61 #ifdef TASK
62   // update objectsets
63   if(BAMBOO_NUM_OF_CORE < NUMCORESACTIVE) {
64     for(int i=0; i<NUMCLASSES; i++) {
65       struct parameterwrapper ** queues = objectqueues[BAMBOO_NUM_OF_CORE][i];
66       int length = numqueues[BAMBOO_NUM_OF_CORE][i];
67       for(int j = 0; j < length; ++j) {
68         struct parameterwrapper * parameter = queues[j];
69         struct ObjectHash * set=parameter->objectset;
70         for(struct ObjectNode * ptr=set->listhead;ptr!=NULL;ptr=ptr->lnext) {
71           PMCUPDATEOBJNONNULL(ptr->key);
72         }
73         ObjectHashrehash(set);
74       }
75     }
76   }
77
78   // update current task descriptor
79   if(currtpd != NULL) {
80     for(int i=0; i<currtpd->numParameters; i++) {
81       // the parameter can not be NULL
82       PMCUPDATEOBJNONNULL(currtpd->parameterArray[i]);
83     }
84   }
85
86   // update active tasks
87   if(activetasks != NULL) {
88     for(struct genpointerlist * ptr=activetasks->list;ptr!=NULL;ptr=ptr->inext){
89       struct taskparamdescriptor *tpd=ptr->src;
90       for(int i=0; i<tpd->numParameters; i++) {
91         // the parameter can not be NULL
92         PMCUPDATEOBJNONNULL(tpd->parameterArray[i]);
93       }
94     }
95     genrehash(activetasks);
96   }
97
98   // update cached transferred obj
99   for(struct QueueItem * tmpobjptr =  getHead(&objqueue);tmpobjptr != NULL;tmpobjptr = getNextQueueItem(tmpobjptr)) {
100     struct transObjInfo * objInfo=(struct transObjInfo *)(tmpobjptr->objectptr);
101     // the obj can not be NULL
102     PMCUPDATEOBJNONNULL(objInfo->objptr);
103   }
104
105   // update cached objs to be transferred
106   for(struct QueueItem * item = getHead(totransobjqueue);item != NULL;item = getNextQueueItem(item)) {
107     struct transObjInfo * totransobj = (struct transObjInfo *)(item->objectptr);
108     // the obj can not be NULL
109     PMCUPDATEOBJNONNULL(totransobj->objptr);
110   }  
111
112   // enqueue lock related info
113   for(int i = 0; i < runtime_locklen; ++i) {
114     PMCUPDATEOBJ(runtime_locks[i].redirectlock);
115     PMCUPDATEOBJ(runtime_locks[i].value);
116   }
117 #endif
118
119 #ifdef MGC
120   // update the bamboo_threadlocks
121   for(int i = 0; i < bamboo_threadlocks.index; i++) {
122     // the locked obj can not be NULL
123     PMCUPDATEOBJNONNULL(bamboo_threadlocks.locks[i].object);
124   }
125
126   // update the bamboo_current_thread
127   PMCUPDATEOBJ(bamboo_current_thread);
128
129   // update global thread queue
130   if(STARTUPCORE == BAMBOO_NUM_OF_CORE) {
131     unsigned int thread_counter = *((unsigned int*)(bamboo_thread_queue+1));
132     if(thread_counter > 0) {
133       unsigned int start = *((unsigned int*)(bamboo_thread_queue+2));
134       for(int i = thread_counter; i > 0; i--) {
135         // the thread obj can not be NULL
136         PMCUPDATEOBJNONNULL(*((void **)&bamboo_thread_queue[4+start]));
137         start = (start+1)&bamboo_max_thread_num_mask;
138       }
139     }
140     unlockthreadqueue();
141   }
142 #endif
143 }
144
145
146 void pmc_doreferenceupdate(struct garbagelist *stackptr) {
147   struct pmc_region * region=&pmc_heapptr->regions[BAMBOO_NUM_OF_CORE];
148   pmc_updateRuntimePtrs(stackptr);
149   pmc_referenceupdate(region->startptr, region->endptr);
150 }
151
152 void pmc_referenceupdate(void *bottomptr, void *topptr) {
153   void *tmpptr=bottomptr;
154   //tprintf("%x -- %x\n", bottomptr, topptr);
155   while(tmpptr<topptr) {
156     unsigned int type;
157     unsigned int size;
158     gettype_size(tmpptr, &type, &size);
159     size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
160     if (!type) {
161       tmpptr+=ALIGNMENTSIZE;
162       continue;
163     }
164     //if marked we update the pointers
165     if (((struct ___Object___ *) tmpptr)->marked) {
166       pmc_updatePtrs(tmpptr, type);
167     }
168     tmpptr+=size;
169   }
170 }
171
172 void pmc_docompact() {
173   struct pmc_region * region=&pmc_heapptr->regions[BAMBOO_NUM_OF_CORE];
174   pmc_compact(region, !(BAMBOO_NUM_OF_CORE&1), region->startptr, region->endptr);
175 }
176
177
178 void pmc_compact(struct pmc_region * region, int forward, void *bottomptr, void *topptr) {
179   if (forward) {
180     void *tmpptr=bottomptr;
181     while(tmpptr<topptr) {
182       unsigned int type;
183       unsigned int size;
184       gettype_size(tmpptr, &type, &size);
185       size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
186       if (!type) {
187         tmpptr+=ALIGNMENTSIZE;
188         continue;
189       }
190       //if marked we update the pointers
191       void *forwardptr=(void *)((struct ___Object___ *) tmpptr)->marked;
192       ((struct ___Object___ *) tmpptr)->marked=NULL;
193       if (forwardptr) {
194         //tprintf("Compacting %x\n",tmpptr);
195         memmove(forwardptr, tmpptr, size);
196       }
197       tmpptr+=size;
198     }
199   } else {
200     struct ___Object___ *backward=region->lastobj;
201     struct ___Object___ *lastobj=NULL;
202     while(backward) {
203       lastobj=backward;
204       backward=backward->backward;
205       unsigned int type;
206       unsigned int size;
207       gettype_size(lastobj, &type, &size);
208       size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
209       void *forwardptr=(void *)((struct ___Object___ *) lastobj)->marked;
210       ((struct ___Object___ *) lastobj)->marked=NULL;
211       //tprintf("Compacting %x\n",lastobj);
212       memmove(forwardptr, lastobj, size);
213     }
214   }
215 }