small bug fix
[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=(i+1)<NUMCORES4GC?pmc_heapptr->regions[i+1].lastptr:pmc_heapptr->regions[i].endptr;
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=(i+1)<NUMCORES4GC?pmc_heapptr->regions[i+1].highunit:pmc_heapptr->regions[i].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             //tprintf("Ch: %u -> %x\n", index, newstartptr);
33             pmc_heapptr->units[index].endptr=newstartptr;
34           }
35
36           if (ptr>newstartptr)
37             break;
38         }
39         region->lastptr=newstartptr;
40
41         *numbytesallocated=(unsigned int)(newstartptr-startptr);
42         tmc_spin_mutex_unlock(&region->lock);
43
44         return startptr;
45       } while(0);
46       tmc_spin_mutex_unlock(&region->lock);
47     }
48   }
49   if (BAMBOO_NUM_OF_CORE==STARTUPCORE) {
50     if (!gcflag) {
51       gcflag = true;
52       for(int i=0;i<NUMCORES4GC;i++) {
53         if (i!=STARTUPCORE)
54           send_msg_1(i, GCSTARTPRE);
55       }
56     }
57   } else {
58     send_msg_1(STARTUPCORE,GCINVOKE);
59   }
60   return NULL;
61 }