next roundof bug fixes
[IRC.git] / Robust / src / Runtime / bamboo / multicoremem.c
1 #ifdef MULTICORE
2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
4
5 #ifdef MULTICORE_GC
6 #include "multicoregarbage.h"
7 #include "multicorehelper.h"
8 #include "multicoremem_helper.h"
9
10 INLINE void * mallocmem(int tofindb,
11                         int totest,
12                         int size,
13                         int * allocsize) {
14   void * mem = NULL;
15   // find suitable block
16   return mem;
17 }
18
19 // Only allocate local mem chunks to each core.
20 // If a core has used up its local shared memory, start gc.
21 void * localmalloc_I(int coren,
22                      int isize,
23                      int * allocsize) {
24   void * mem=globalmalloc_I(coren,isize,allocsize);
25   return mem;
26
27
28 #ifdef SMEMF
29 // Allocate the local shared memory to each core with the highest priority,
30 // if a core has used up its local shared memory, try to allocate the 
31 // shared memory that belong to its neighbours, if also failed, start gc.
32 void * fixedmalloc_I(int coren,
33                      int isize,
34                      int * allocsize) {
35   void * mem=globalmalloc_I(coren,isize,allocsize);
36   return mem;
37
38 #endif 
39
40 #ifdef SMEMM
41 // Allocate the local shared memory to each core with the highest priority,
42 // if a core has used up its local shared memory, try to allocate the 
43 // shared memory that belong to its neighbours first, if failed, check 
44 // current memory allocation rate, if it has already reached the threshold,
45 // start gc, otherwise, allocate the shared memory globally.  If all the 
46 // shared memory has been used up, start gc.
47 void * mixedmalloc_I(int coren,
48                      int isize,
49                      int * allocsize) {
50   void * mem=globalmalloc_I(coren,isize,allocsize);
51   return mem;
52
53 #endif 
54
55 // Allocate all the memory chunks globally, do not consider the host cores
56 // When all the shared memory are used up, start gc.
57 void * globalmalloc_I(int coren, unsigned INTPTR memcheck, int * allocsize) {
58   block_t firstfree=NOFREEBLOCK;
59   block_t lowestblock=allocationinfo.lowestfreeblock;
60
61   for(block_t searchblock=lowestblock;searchblock<GCNUMBLOCK;searchblock++) {
62     struct blockrecord * block=&allocationinfo.blocktable[searchblock];
63     if (block->status==BS_FREE) {
64       if(firstfree==NOFREEBLOCK)
65         firstfree=searchblock;
66       unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
67       if (freespace>=memcheck) {
68         //we have a block
69         //mark block as used
70         block->status=BS_USED;
71         void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
72         unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
73         allocationinfo.lowestfreeblock=firstfree;
74         void *startaddr=blockptr+usedspace;
75         *allocsize=freespace;
76         return startaddr;
77       }
78     }
79   }
80   return NULL;
81
82
83 void * smemalloc(int coren, int isize, int * allocsize) {
84   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
85   void *retval=smemalloc_I(coren, isize, allocsize);
86   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
87   return retval;
88 }
89
90 // malloc from the shared memory
91 void * smemalloc_I(int coren, int isize, int * allocsize) {
92 #ifdef SMEML
93   void *mem = localmalloc_I(coren, isize, allocsize);
94 #elif defined(SMEMF)
95   void *mem = fixedmalloc_I(coren, isize, allocsize);
96 #elif defined(SMEMM)
97   void *mem = mixedmalloc_I(coren, isize, allocsize);
98 #elif defined(SMEMG)
99   void *mem = globalmalloc_I(coren, isize, allocsize);
100 #endif
101   if(mem == NULL) {
102     // no enough shared global memory
103     // trigger gc
104     if(!gcflag) {
105       gcflag = true;
106       if(!gc_status_info.gcprocessing) {
107         // inform other cores to stop and wait for gc
108         gcprecheck = true;
109         for(int i = 0; i < NUMCORESACTIVE; i++) {
110           // reuse the gcnumsendobjs & gcnumreceiveobjs
111           gcnumsendobjs[0][i] = 0;
112           gcnumreceiveobjs[0][i] = 0;
113         }
114         GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
115       }
116     }
117     return NULL;
118   }
119   return mem;
120 }
121 #else
122 // malloc from the shared memory
123 void * smemalloc_I(int coren,
124                    int size,
125                    int * allocsize) {
126   void * mem = NULL;
127   int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
128   if(toallocate > bamboo_free_smem_size) {
129     // no enough mem
130     mem = NULL;
131   } else {
132     mem = (void *)bamboo_free_smemp;
133     bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
134     bamboo_free_smem_size -= toallocate;
135   }
136   *allocsize = toallocate;
137   if(mem == NULL) {
138     // no enough shared global memory
139     *allocsize = 0;
140     BAMBOO_EXIT();
141   }
142   return mem;
143
144 #endif // MULTICORE_GC
145
146 #endif // MULTICORE