Remove the local mapping tbl and shared mapping tbl in gc, instead, use a big shared...
[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 #ifdef MULTICORE_GC
8 void * mycalloc_share(struct garbagelist * stackptr, 
9                               int m, 
10                                           int size) {
11         void * p = NULL;
12   //int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
13   int isize = (size & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE);
14         int hasgc = 0;
15 memalloc:
16   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
17   p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
18   if(p == NULL) {
19                 // no more global shared memory
20                 BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
21                 if(hasgc < 5) {
22                     // start gc
23                         if(gc(stackptr)) {
24                           hasgc++;
25                         }
26                 } else {
27                         // no more global shared memory
28                         BAMBOO_EXIT(0xc002);
29                 }
30
31                 // try to malloc again
32                 goto memalloc;
33   }
34   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
35         void * alignedp = 
36                 (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
37         BAMBOO_MEMSET_WH(p, -2, (alignedp - p));
38   BAMBOO_MEMSET_WH(alignedp + size, -2, p + isize - alignedp - size);
39         return alignedp;
40 }
41 #else
42 void * mycalloc_share(int m, 
43                                   int size) {
44   void * p = NULL;
45   //int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
46   int isize = (size & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE);
47   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
48   p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
49   if(p == NULL) {
50                 // no more global shared memory
51                 BAMBOO_EXIT(0xc003);
52   }
53   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
54   return 
55                 (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
56 }
57 #endif
58
59 void * mycalloc(int m, 
60                         int size) {
61   void * p = NULL;
62   int isize = size; 
63   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
64 #ifdef MULTICORE_GC
65   extern bool gc_localheap_s;
66 inermycalloc_i:
67   p = gc_localheap_s ? BAMBOO_LOCAL_MEM_CALLOC_S(m, isize) : 
68         BAMBOO_LOCAL_MEM_CALLOC(m, isize);
69 #else
70   p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
71 #endif
72   if(p == NULL) {
73 #ifdef MULTICORE_GC
74         if(!gc_localheap_s) {
75           gc_localheap_s = true;
76           goto inermycalloc_i;
77         }
78 #endif
79           BAMBOO_EXIT(0xc001);
80   }
81   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
82   return p;
83 }
84
85
86 void * mycalloc_i(int m, 
87                           int size) {
88   void * p = NULL;
89   int isize = size; 
90 #ifdef MULTICORE_GC
91   extern bool gc_localheap_s;
92 inermycalloc_i:
93   p = gc_localheap_s ? BAMBOO_LOCAL_MEM_CALLOC_S(m, isize) : 
94         BAMBOO_LOCAL_MEM_CALLOC(m, isize);
95 #else
96   p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
97 #endif
98   if(p == NULL) {
99 #ifdef MULTICORE_GC
100         if(!gc_localheap_s) {
101           gc_localheap_s = true;
102           goto inermycalloc_i;
103         }
104 #endif
105         BAMBOO_EXIT(0xc004);
106   }
107   return p;
108 }
109
110 void myfree(void * ptr) {
111 #ifdef MULTICORE_GC
112   if(ptr >= BAMBOO_LOCAL_HEAP_START_VA ) {
113 #endif
114         BAMBOO_LOCAL_MEM_FREE(ptr);
115 #ifdef MULTICORE_GC
116   } else if(ptr >= BAMBOO_LOCAL_HEAP_START_VA_S) {
117         BAMBOO_LOCAL_MEM_FREE_S(ptr);
118   }
119 #endif
120   return;
121 }
122
123 #endif