40d7f437ed917b15b6cf2ae16694f0d116ded9fd
[IRC.git] / Robust / src / Runtime / bamboo / pmc_mem.c
1 #include <stdlib.h>
2 #include "multicoregc.h"
3 #include "multicoreruntime.h"
4 #include "pmc_garbage.h"
5 #include "pmc_mem.h"
6 #include "runtime_arch.h"
7 #include "multicoremsg.h"
8
9 void * pmc_alloc(unsigned int * numbytesallocated, unsigned int minimumbytes) {
10   unsigned int memcheck=minimumbytes>PMC_MINALLOC?minimumbytes:PMC_MINALLOC;
11
12   for(int i=0;i<NUMCORES4GC;i+=2) {
13     void *startptr=pmc_heapptr->regions[i].lastptr;
14     void *finishptr=pmc_heapptr->regions[i+1].lastptr;
15
16     if ((finishptr-startptr)>memcheck) {
17       struct pmc_region *region=&pmc_heapptr->regions[i];
18       tmc_spin_mutex_lock(&region->lock);
19       startptr=region->lastptr;
20       do {
21         //double check that we have the space
22         if ((finishptr-startptr)<memcheck)
23           break;
24         unsigned int startindex=region->lowunit;
25         unsigned int endindex=pmc_heapptr->regions[i+1].highunit;
26         void * newstartptr=startptr+memcheck;
27         
28         //update unit end points
29         for(unsigned int index=startindex;index<(endindex-1);index++) {
30           void *ptr=pmc_unitend(index);
31           if ((ptr>startptr)&&(ptr<newstartptr)) {
32             pmc_heapptr->units[index].endptr=newstartptr;
33           }
34           if (ptr>newstartptr)
35             break;
36         }
37         region->lastptr=newstartptr;
38
39         *numbytesallocated=(unsigned int)(newstartptr-startptr);
40         tmc_spin_mutex_unlock(&region->lock);
41         return startptr;
42       } while(0);
43       tmc_spin_mutex_unlock(&region->lock);
44     }
45   }
46   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
47     if (!gcflag) {
48       gcflag = true;
49       for(int i=0;i<NUMCORES4GC;i++) {
50         if (i!=STARTUPCORE)
51           send_msg_1(i, GCSTARTPRE);
52       }
53     }
54   } else {
55     send_msg_1(STARTUPCORE,GCINVOKE);
56   }
57   return NULL;
58 }