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