41f23645093baaedfb143e6fc8002f88a2e62738
[IRC.git] / Robust / src / Runtime / bamboo / pmc_forward.c
1 #include "pmc_forward.h"
2
3 void pmc_count() {
4   for(int i=0;i<NUMPMCUNITS;i++) {
5     if (!tmc_spin_mutex_trylock(&pmc_heapptr->units[i].lock)) {
6       //got lock
7       void *unitbase=gcbaseva+i*UNITSIZE;
8       void *unittop=unitbase+UNITSIZE;
9       pmc_countbytes(&pmc_heapptr->unit[i], unitbase, unittop);
10     }
11   }
12 }
13
14 //Comment: should build dummy byte arrays to allow skipping data...
15 void pmc_countbytes(struct pmc_unit * unit, void *bottomptr, void *topptr) {
16   void *tmpptr=bottomptr;
17   unsigned int totalbytes=0;
18   while(tmpptr<topptr) {
19     unsigned int type;
20     unsigned int size;
21     gettype_size(tmpptr, &type, &size);
22     if (!type) {
23       tmpptr+=ALIGNMENTSIZE;
24       continue;
25     }
26     size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
27     if (((struct ___Object___ *)tmpptr)->mark)
28       totalbytes+=size;
29     tmpptr+=size;
30   }
31   unit->numbytes=totalbytes;
32 }
33
34 void pmc_processunits() {
35   unsigned int livebytes=0;
36   for(int i=0;i<NUMPMCUNITS;i++) {
37     livebytes+=pmc_heapptr->units[i].numbytes;
38   }
39   //make sure to round up
40   unsigned int livebytespercore=((livebytes-1)/NUMCORES4GC)+1;
41   unsigned int regionnum=0;
42   int totalbytes=0;
43   int numregions=0;
44
45   for(int i=0;i<NUMPMCUNITS;i++) {
46     if (numregions>0&&(totalbytes+pmc_heapptr->units[i].numbytes)>livebytespercore) {
47       regionnum++;
48       totalbytes-=livebytespercore;
49       numregions=0;
50     }
51     numregions++;
52     pmc_heapptr->units[i].regionnum=regionnum;
53     tmc_spin_mutex_init(&pmc_heapptr->units[i].lock);
54     totalbytes+=pmc_heapptr->units[i].numbytes;
55   }
56 }
57
58
59 void pmc_forward(struct pmc_region *region, unsigned int totalbytes, void *bottomptr, void *topptr, bool fwddirection) {
60   void *tmpptr=bottomptr;
61   void *forwardptr=fwddirection?bottomptr:(topptr-totalbytes);
62   struct ___Object___ *lastobj=NULL;
63
64   while(tmpptr>topptr) {
65     unsigned int type;
66     unsigned int size;
67     gettype_size(tmpptr, &type, &size);
68     if (!type) {
69       tmpptr+=ALIGNMENTSIZE;
70       continue;
71     }
72     size=((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
73
74     if (((struct ___Object___ *)tmpptr)->mark) {
75       ((struct ___Object___ *)tmpptr)->mark=forwardptr;
76       forwardptr+=size;
77       if (lastobj&&!fwddirection) {
78         tmpptr->backward=lastobj;
79         lastobj=(struct ___Object___ *)tmpptr;
80       }
81     }
82     tmpptr+=size;
83   }
84   region->lastobj=lastobj;
85 }