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