code to compute unit boundaries and allocation function
[IRC.git] / Robust / src / Runtime / bamboo / pmc_mem.c
1 #include "pmc_mem.h"
2
3 void * pmc_alloc(unsigned int * numbytesallocated, unsigned int minimumbytes) {
4   unsigned int memcheck=minimumbytes>PMC_MINALLOC?minimumbytes:PMC_MINALLOC;
5
6   for(int i=0;i<NUMCORES4GC;i+=2) {
7     void *startptr=pmc_heapptr->regions[i].lastptr;
8     void *finishptr=pmc_heapptr->regions[i+1].lastptr;
9
10     if ((finishptr-startptr)>memcheck) {
11       struct pmc_region *region=&pmc_heapptr->regions[i];
12       tmc_spin_mutex_lock(&region->lock);
13       startptr=region->lastptr;
14       do {
15         //double check that we have the space
16         if ((finishptr-startptr)<memcheck)
17           break;
18         unsigned int startindex=region->lowunit;
19         unsigned int endindex=pmc_heapptr->regions[i+1]->highunit;
20         void * newstartptr=startptr+memcheck;
21         
22         //update unit end points
23         for(unsigned int index=startindex;index<endindex;index++) {
24           void *ptr=pmc_unitend(index);
25           if ((ptr>startptr)&&(ptr<newstartptr)) {
26             pmc_heapptr->units[index].endptr=newstartptr;
27           }
28           if (ptr>newstartptr)
29             break;
30         }
31         region->lastptr=newstartptr;
32
33         *numbytesallocated=(unsigned int)(newstartptr-startptr);
34         tmc_spin_mutex_unlock(&region->lock);
35         return startptr;
36       } while(0);
37       tmc_spin_mutex_unlock(&region->lock);
38     }
39   }
40   return NULL;
41 }