more odd core count bugs
[IRC.git] / Robust / src / Runtime / bamboo / pmc_forward.c
index d50053a3236d7f22311877ef7ac532d053693602..c60d7ce5c4060770d4893c89559eaaaedf316910 100644 (file)
@@ -1,8 +1,9 @@
+#include "multicoregc.h"
 #include "pmc_forward.h"
 #include "runtime_arch.h"
 #include "bambooalign.h"
 #include "pmc_garbage.h"
-#include "multicoregc.h"
+
 
 void pmc_count() {
   for(int i=0;i<NUMPMCUNITS;i++) {
@@ -10,6 +11,7 @@ void pmc_count() {
       //got lock
       void *unitbase=(i==0)?gcbaseva:pmc_heapptr->units[i-1].endptr;
       void *unittop=pmc_heapptr->units[i].endptr;
+      //tprintf("Cnt: %x - %x\n", unitbase, unittop);
       pmc_countbytes(&pmc_heapptr->units[i], unitbase, unittop);
     }
   }
@@ -17,20 +19,32 @@ void pmc_count() {
 
 //Comment: should build dummy byte arrays to allow skipping data...
 void pmc_countbytes(struct pmc_unit * unit, void *bottomptr, void *topptr) {
-  tprintf("%x--%x\n",bottomptr, topptr);
   void *tmpptr=bottomptr;
   unsigned int totalbytes=0;
+  void * lastunmarked=NULL;
+  bool padokay=false;
   while(tmpptr<topptr) {
     unsigned int type;
     unsigned int size;
+    //tprintf("C:%x\n",tmpptr);
     gettype_size(tmpptr, &type, &size);
     if (!type) {
       tmpptr+=ALIGNMENTSIZE;
       continue;
     }
     size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
-    if (((struct ___Object___ *)tmpptr)->marked)
+    if (((struct ___Object___ *)tmpptr)->marked) {
+      if (lastunmarked!=NULL) {
+       if (padokay)
+         padspace(lastunmarked, (unsigned INTPTR) (tmpptr-lastunmarked));
+       padokay=false;
+       lastunmarked=NULL;
+      }
       totalbytes+=size;
+    } else if (lastunmarked!=NULL)
+      lastunmarked=tmpptr;
+    else
+      padokay=true;
     tmpptr+=size;
   }
   unit->numbytes=totalbytes;
@@ -47,8 +61,16 @@ void pmc_processunits() {
   int totalbytes=0;
   int numregions=0;
 
+  pmc_heapptr->regions[0].startptr=gcbaseva;
+  pmc_heapptr->regions[0].lowunit=0;
+
   for(int i=0;i<NUMPMCUNITS;i++) {
     if (numregions>0&&(totalbytes+pmc_heapptr->units[i].numbytes)>livebytespercore) {
+      pmc_heapptr->regions[regionnum].highunit=i;
+      pmc_heapptr->regions[regionnum].endptr=pmc_heapptr->units[i-1].endptr;
+
+      pmc_heapptr->regions[regionnum+1].startptr=pmc_heapptr->units[i-1].endptr;
+      pmc_heapptr->regions[regionnum+1].lowunit=i;
       regionnum++;
       totalbytes-=livebytespercore;
       numregions=0;
@@ -58,6 +80,17 @@ void pmc_processunits() {
     tmc_spin_mutex_init(&pmc_heapptr->units[i].lock);
     totalbytes+=pmc_heapptr->units[i].numbytes;
   }
+  pmc_heapptr->regions[regionnum].highunit=NUMPMCUNITS;
+  pmc_heapptr->regions[regionnum].endptr=pmc_heapptr->units[NUMPMCUNITS-1].endptr;
+  regionnum++;
+  for(;regionnum<NUMCORES4GC;regionnum++) {
+    pmc_heapptr->regions[regionnum].highunit=NUMPMCUNITS;
+    pmc_heapptr->regions[regionnum].endptr=pmc_heapptr->units[NUMPMCUNITS-1].endptr;
+    if ((regionnum+1)<NUMCORES4GC) {
+      pmc_heapptr->regions[regionnum+1].startptr=pmc_heapptr->units[NUMPMCUNITS-1].endptr;
+      pmc_heapptr->regions[regionnum+1].lowunit=NUMPMCUNITS;
+    }
+  }
 }
 
 void pmc_doforward() {
@@ -65,63 +98,50 @@ void pmc_doforward() {
   int endregion=-1;
   unsigned int totalbytes=0;
   struct pmc_region * region=&pmc_heapptr->regions[BAMBOO_NUM_OF_CORE];
-  for(int i=0;i<NUMPMCUNITS;i++) {
-    if (startregion==-1&&BAMBOO_NUM_OF_CORE==pmc_heapptr->units[i].regionnum) {
-      startregion=i;
-    }
-    if (BAMBOO_NUM_OF_CORE<pmc_heapptr->units[i].regionnum) {
-      endregion=i;
-      break;
-    }
-    if (startregion!=-1) {
-      totalbytes+=pmc_heapptr->units[i].numbytes;
-    }
+  for(int index=region->lowunit; index<region->highunit;index++) {
+    totalbytes+=pmc_heapptr->units[index].numbytes;
   }
-  if (startregion==-1) 
-    return;
-  if (endregion==-1)
-    endregion=NUMPMCUNITS;
-  region->lowunit=startregion;
-  region->highunit=endregion;
-  region->startptr=(startregion==0)?gcbaseva:pmc_heapptr->units[startregion-1].endptr;
-  region->endptr=pmc_heapptr->units[endregion-1].endptr;
-  tprintf("startregion=%u gcbaseva=%x\n", startregion, gcbaseva);
-  tprintf("totalbytes=%u\n", totalbytes);
+
   if (BAMBOO_NUM_OF_CORE&1) {
     //upward in memory
     region->lastptr=region->endptr-totalbytes;
-    tprintf("(up) Assigning lastptr for %u to %x ep=%x\n", BAMBOO_NUM_OF_CORE, region->lastptr, region->endptr);
   } else {
     //downward in memory
     region->lastptr=region->startptr+totalbytes;
-    tprintf("(down) Assigning lastptr for %u to %x sp=%x\n", BAMBOO_NUM_OF_CORE, region->lastptr, region->startptr);
   }
-
   pmc_forward(region, totalbytes, region->startptr, region->endptr, !(BAMBOO_NUM_OF_CORE&1));
 }
 
 
 //fwddirection=1 means move things to lower addresses
-void pmc_forward(struct pmc_region *region, unsigned int totalbytes, void *bottomptr, void *topptr, bool fwddirection) {
+void pmc_forward(struct pmc_region *region, unsigned int totalbytes, void *bottomptr, void *topptr, bool lower) {
   void *tmpptr=bottomptr;
-  void *forwardptr=fwddirection?bottomptr:(topptr-totalbytes);
+  void *forwardptr=lower?bottomptr:(topptr-totalbytes);
   struct ___Object___ *lastobj=NULL;
   unsigned int currunit=region->lowunit;
   void *endunit=pmc_unitend(currunit);
+  bool internal=!(BAMBOO_NUM_OF_CORE&1);
+  int highbound=internal?region->highunit:region->highunit-1;
+
+
+  if (!lower) {
+    //We're resetting the boundaries of units at the low address end of the region...
+    //Be sure not to reset the boundary of our last unit...it is shared with another region
 
-  if (!fwddirection) {
-    //reset boundaries of beginning units
-    while(endunit<forwardptr) {
+    while(endunit<=region->lastptr&&(currunit<highbound)) {
       pmc_heapptr->units[currunit].endptr=endunit;
+      //tprintf("Ch2: %u -> %x\n", currunit, endunit);
       currunit++;
       endunit=pmc_unitend(currunit);
     }
   } else {
-    //reset boundaries of end units
-    unsigned int lastunit=region->highunit-1;
+    //We're resetting the boundaries of units at the high address end of the region...
+    //Very top most unit defines boundary of region...we can't move that right now
+    unsigned int lastunit=internal?region->highunit-1:region->highunit-2;
     void * lastunitend=pmc_unitend(lastunit);
-    while(lastunitend>forwardptr) {
+    while(lastunitend>=region->lastptr) {
       pmc_heapptr->units[lastunit].endptr=lastunitend;
+      //tprintf("Ch3: %u -> %x\n", lastunit, lastunitend);
       lastunit--;
       lastunitend=pmc_unitend(lastunit);
     }
@@ -139,20 +159,25 @@ void pmc_forward(struct pmc_region *region, unsigned int totalbytes, void *botto
 
     if (((struct ___Object___ *)tmpptr)->marked) {
       ((struct ___Object___ *)tmpptr)->marked=forwardptr;
+      //tprintf("Forwarding %x->%x\n", tmpptr, forwardptr);
       void *newforwardptr=forwardptr+size;
-      while(newforwardptr>endunit) {
-       pmc_heapptr->regions[currunit].endptr=newforwardptr;
+      //Need to make sure that unit boundaries do not fall in the middle of an object...
+      //Unless the unit boundary is at the end of the region...then just ignore it.
+      while(newforwardptr>=endunit&&currunit<highbound) {
+       pmc_heapptr->units[currunit].endptr=newforwardptr;
+       //tprintf("Ch4: %u -> %x\n", currunit, newforwardptr);
        currunit++;
        endunit=pmc_unitend(currunit);
       }
 
       forwardptr=newforwardptr;
-      if (lastobj&&!fwddirection) {
+      if (!lower) {
        ((struct ___Object___ *)tmpptr)->backward=lastobj;
        lastobj=(struct ___Object___ *)tmpptr;
       }
     }
     tmpptr+=size;
   }
+  //tprintf("forwardptr=%x\n",forwardptr);
   region->lastobj=lastobj;
 }