change to use static mapping of core # and block # in multicore garbage modul
[IRC.git] / Robust / src / Runtime / mem.c
1 #include "mem.h"
2
3 #ifdef MULTICORE
4 #include "runtime.h"
5 #include "runtime_arch.h"
6
7 void * mycalloc(int m, 
8                             int size) {
9   void * p = NULL;
10   int isize = size; 
11   BAMBOO_START_CRITICAL_SECTION_MEM();
12   p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
13   if(p == NULL) {
14           BAMBOO_EXIT(0xc001);
15   }
16   BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
17   return p;
18 }
19
20 #ifdef MULTICORE_GC
21 void * mycalloc_share(struct garbagelist * stackptr, 
22                                   int m, 
23                                                                                         int size) {
24         void * p = NULL;
25   int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
26         bool hasgc = false;
27 memalloc:
28   BAMBOO_START_CRITICAL_SECTION_MEM();
29   p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
30 #ifdef DEBUG
31         tprintf("new obj in shared mem: %x, %x \n", p, isize);
32 #endif
33   if(p == NULL) {
34                 // no more global shared memory
35                 BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
36                 if(!hasgc) {
37                         // start gc
38                         gc(stackptr);
39                         hasgc = true;
40                 } else {
41                         // no more global shared memory
42                         BAMBOO_EXIT(0xc002);
43                 }
44
45                 // try to malloc again
46                 goto memalloc;
47   }
48   BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
49         void * alignedp = 
50                 (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
51         memset(p, -2, (alignedp - p));
52   memset(alignedp + size, -2, p + isize - alignedp - size);
53         return alignedp;
54 }
55 #else
56 void * mycalloc_share(int m, 
57                                   int size) {
58   void * p = NULL;
59   int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
60   BAMBOO_START_CRITICAL_SECTION_MEM();
61   p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
62   if(p == NULL) {
63                 // no more global shared memory
64                 BAMBOO_EXIT(0xc003);
65   }
66   BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
67   return 
68                 (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
69 }
70 #endif
71
72 void * mycalloc_i(int m, 
73                               int size) {
74   void * p = NULL;
75   int isize = size; 
76   p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
77   if(p == NULL) {
78           BAMBOO_EXIT(0xc004);
79   }
80   return p;
81 }
82
83 void myfree(void * ptr) {
84   BAMBOO_LOCAL_MEM_FREE(ptr);
85   return;
86 }
87
88 #endif