2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
6 #include "multicoregarbage.h"
7 #include "multicorehelper.h"
8 #include "multicoremem_helper.h"
10 // Only allocate local mem chunks to each core.
11 // If a core has used up its local shared memory, start gc.
12 void * localmalloc_I(int coren,
15 for(block_t localblocknum=0;localblocknum<GCNUMLOCALBLOCK;localblocknum++) {
16 block_t searchblock=BLOCKINDEX2(coren, localblocknum);
17 struct blockrecord * block=&allocationinfo.blocktable[searchblock];
18 if (block->status==BS_FREE) {
19 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
20 if (freespace>=memcheck) {
23 block->status=BS_USED;
24 void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
25 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
26 void *startaddr=blockptr+usedspace;
36 // Allocate the local shared memory to each core with the highest priority,
37 // if a core has used up its local shared memory, try to allocate the
38 // shared memory that belong to its neighbours, if also failed, start gc.
39 void * fixedmalloc_I(int coren,
40 unsigned int memcheck,
43 void * mem=localmalloc_I(coren,memcheck,allocsize);
47 //failed try neighbors...in a round robin fashion
49 int minblockindex=allocationinfo.lowestfreeblock/NUMCORES4GC;
50 unsigned INTPTR threshold=(desiredmem<MINMEMORYCHUNKSIZE)? desiredmem: MINMEMORYCHUNKSIZE;
51 unsigned INTPTR memcheck=requiredmem>threshold?requiredmem:threshold;
53 for(block_t lblock=0;lblock<MAXNEIGHBORALLOC;lblock++) {
54 for(int i=0;i<NUM_CORES2TEST;i++) {
55 int neighborcore=core2test[corenum][i];
56 if (neighborcore!=-1) {
57 block_t globalblockindex=BLOCKINDEX2(neighborcore, lblock);
58 struct blockrecord * block=&allocationinfo.blocktable[globalblockindex];
59 if (block->status==BS_FREE) {
60 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
61 if (memcheck<=freespace) {
64 block->status=BS_USED;
65 void *blockptr=OFFSET2BASEVA(globalblockindex)+gcbaseva;
66 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
68 return blockptr+usedspace;
80 // Allocate the local shared memory to each core with the highest priority,
81 // if a core has used up its local shared memory, try to allocate the
82 // shared memory that belong to its neighbours first, if failed, check
83 // current memory allocation rate, if it has already reached the threshold,
84 // start gc, otherwise, allocate the shared memory globally. If all the
85 // shared memory has been used up, start gc.
86 void * mixedmalloc_I(int coren,
89 void * mem=fixedmalloc_I(coren,isize,allocsize);
93 //try global allocator instead
94 return globalmalloc_I(coren, isize, allocsite);
98 // Allocate all the memory chunks globally, do not consider the host cores
99 // When all the shared memory are used up, start gc.
100 void * globalmalloc_I(int coren, unsigned INTPTR memcheck, int * allocsize) {
101 block_t firstfree=NOFREEBLOCK;
102 block_t lowestblock=allocationinfo.lowestfreeblock;
104 for(block_t searchblock=lowestblock;searchblock<GCNUMBLOCK;searchblock++) {
105 struct blockrecord * block=&allocationinfo.blocktable[searchblock];
106 if (block->status==BS_FREE) {
107 if(firstfree==NOFREEBLOCK)
108 firstfree=searchblock;
109 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
110 if (freespace>=memcheck) {
113 block->status=BS_USED;
114 void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
115 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
116 allocationinfo.lowestfreeblock=firstfree;
117 void *startaddr=blockptr+usedspace;
118 *allocsize=freespace;
126 void * smemalloc(int coren, int isize, int * allocsize) {
127 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
128 void *retval=smemalloc_I(coren, isize, allocsize);
129 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
133 // malloc from the shared memory
134 void * smemalloc_I(int coren, int isize, int * allocsize) {
136 void *mem = localmalloc_I(coren, isize, allocsize);
138 void *mem = fixedmalloc_I(coren, isize, allocsize);
140 void *mem = mixedmalloc_I(coren, isize, allocsize);
142 void *mem = globalmalloc_I(coren, isize, allocsize);
146 // not enough shared global memory
150 if(!gc_status_info.gcprocessing) {
151 // inform other cores to stop and wait for gc
153 for(int i = 0; i < NUMCORESACTIVE; i++) {
154 // reuse the gcnumsendobjs & gcnumreceiveobjs
155 gcnumsendobjs[0][i] = 0;
156 gcnumreceiveobjs[0][i] = 0;
158 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
166 // malloc from the shared memory
167 void * smemalloc_I(int coren,
171 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
172 if(toallocate > bamboo_free_smem_size) {
176 mem = (void *)bamboo_free_smemp;
177 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
178 bamboo_free_smem_size -= toallocate;
180 *allocsize = toallocate;
182 // no enough shared global memory
188 #endif // MULTICORE_GC