2 #include "runtime_arch.h"
3 #include "multicoreruntime.h"
6 #include "multicoregarbage.h"
7 #include "multicorehelper.h"
8 #include "multicoremem_helper.h"
10 INLINE void * mallocmem(int tofindb,
15 // find suitable block
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,
24 void * mem=globalmalloc_I(coren,isize,allocsize);
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,
35 void * mem=globalmalloc_I(coren,isize,allocsize);
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,
50 void * mem=globalmalloc_I(coren,isize,allocsize);
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;
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) {
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;
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();
90 // malloc from the shared memory
91 void * smemalloc_I(int coren, int isize, int * allocsize) {
93 void *mem = localmalloc_I(coren, isize, allocsize);
95 void *mem = fixedmalloc_I(coren, isize, allocsize);
97 void *mem = mixedmalloc_I(coren, isize, allocsize);
99 void *mem = globalmalloc_I(coren, isize, allocsize);
102 // no enough shared global memory
106 if(!gc_status_info.gcprocessing) {
107 // inform other cores to stop and wait for gc
109 for(int i = 0; i < NUMCORESACTIVE; i++) {
110 // reuse the gcnumsendobjs & gcnumreceiveobjs
111 gcnumsendobjs[0][i] = 0;
112 gcnumreceiveobjs[0][i] = 0;
114 GC_SEND_MSG_1_TO_CLIENT(GCSTARTPRE);
122 // malloc from the shared memory
123 void * smemalloc_I(int coren,
127 int toallocate = (size>(BAMBOO_SMEM_SIZE)) ? (size) : (BAMBOO_SMEM_SIZE);
128 if(toallocate > bamboo_free_smem_size) {
132 mem = (void *)bamboo_free_smemp;
133 bamboo_free_smemp = ((void*)bamboo_free_smemp) + toallocate;
134 bamboo_free_smem_size -= toallocate;
136 *allocsize = toallocate;
138 // no enough shared global memory
144 #endif // MULTICORE_GC