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,
13 unsigned int memcheck,
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 for(block_t lblock=0;lblock<MAXNEIGHBORALLOC;lblock++) {
50 for(int i=0;i<NUM_CORES2TEST;i++) {
51 int neighborcore=core2test[corenum][i];
52 if (neighborcore!=-1) {
53 block_t globalblockindex=BLOCKINDEX2(neighborcore, lblock);
54 struct blockrecord * block=&allocationinfo.blocktable[globalblockindex];
55 if (block->status==BS_FREE) {
56 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
57 if (memcheck<=freespace) {
60 block->status=BS_USED;
61 void *blockptr=OFFSET2BASEVA(globalblockindex)+gcbaseva;
62 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
64 return blockptr+usedspace;
76 // Allocate the local shared memory to each core with the highest priority,
77 // if a core has used up its local shared memory, try to allocate the
78 // shared memory that belong to its neighbours first, if failed, check
79 // current memory allocation rate, if it has already reached the threshold,
80 // start gc, otherwise, allocate the shared memory globally. If all the
81 // shared memory has been used up, start gc.
82 void * mixedmalloc_I(int coren,
85 void * mem=fixedmalloc_I(coren,isize,allocsize);
89 //try global allocator instead
90 return globalmalloc_I(coren, isize, allocsize);
94 // Allocate all the memory chunks globally, do not consider the host cores
95 // When all the shared memory are used up, start gc.
96 void * globalmalloc_I(int coren, unsigned INTPTR memcheck, int * allocsize) {
97 block_t firstfree=NOFREEBLOCK;
98 block_t lowestblock=allocationinfo.lowestfreeblock;
100 for(block_t searchblock=lowestblock;searchblock<GCNUMBLOCK;searchblock++) {
101 struct blockrecord * block=&allocationinfo.blocktable[searchblock];
102 if (block->status==BS_FREE) {
103 if(firstfree==NOFREEBLOCK)
104 firstfree=searchblock;
105 unsigned INTPTR freespace=block->freespace&~BAMBOO_CACHE_LINE_MASK;
106 if (freespace>=memcheck) {
109 block->status=BS_USED;
110 void *blockptr=OFFSET2BASEVA(searchblock)+gcbaseva;
111 unsigned INTPTR usedspace=((block->usedspace-1)&~BAMBOO_CACHE_LINE_MASK)+BAMBOO_CACHE_LINE_SIZE;
112 allocationinfo.lowestfreeblock=firstfree;
113 void *startaddr=blockptr+usedspace;
114 *allocsize=freespace;
122 void * smemalloc(int coren, int isize, int * allocsize) {
123 BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
124 void *retval=smemalloc_I(coren, isize, allocsize);
125 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
129 // malloc from the shared memory
130 void * smemalloc_I(int coren, int isize, int * allocsize) {
132 void *mem = localmalloc_I(coren, isize, allocsize);
134 void *mem = fixedmalloc_I(coren, isize, allocsize);
136 void *mem = mixedmalloc_I(coren, isize, allocsize);
138 void *mem = globalmalloc_I(coren, isize, allocsize);
142 // not enough shared global memory
146 if(!gc_status_info.gcprocessing) {
147 // inform other cores to stop and wait for gc
149 for(int i = 0; i < NUMCORESACTIVE; i++) {
150 // reuse the gcnumsendobjs & gcnumreceiveobjs
151 gcnumsendobjs[0][i] = 0;
152 gcnumreceiveobjs[0][i] = 0;
154 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
162 // malloc from the shared memory
163 void * smemalloc_I(int coren,
167 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
168 if(toallocate > bamboo_free_smem_size) {
172 mem = (void *)bamboo_free_smemp;
173 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
174 bamboo_free_smem_size -= toallocate;
176 *allocsize = toallocate;
178 // no enough shared global memory
184 #endif // MULTICORE_GC