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