more bug fixes
[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 #include "bambooalign.h"
9 #include "multicoremem.h"
10 #include "multicoregarbage.h"
11
12
13 extern volatile bool gcflag;
14 void * mycalloc_share(struct garbagelist * stackptr, int size) {
15   void * p = NULL;
16   int isize = ((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
17   int hasgc = 0;
18
19   while(true) {
20     if(gcflag) {
21       gc(stackptr);
22     }
23     p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
24
25     if(p == NULL) {
26       // no more global shared memory
27       if(hasgc < 5) {
28         // start gc
29         if(gcflag) {
30           gc(stackptr);
31         }
32         hasgc++;
33       } else {
34         // no more global shared memory
35         BAMBOO_EXIT();
36       }
37     } else
38       break;
39   }
40   
41   return p;
42 }
43 #else
44 void * mycalloc_share(int size) {
45   int isize = ((size-1)&(~(BAMBOO_CACHE_LINE_MASK)))+(BAMBOO_CACHE_LINE_SIZE);
46   void * p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
47   if(p == NULL) {
48     // no more global shared memory
49     BAMBOO_EXIT();
50   }
51   return (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
52 }
53 #endif
54
55 void * mycalloc(int size, char * file, int line) {
56   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
57   void * p = mycalloc_i(size, file, line);
58   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
59   return p;
60 }
61
62
63 void * mycalloc_i(int size, char * file, int line) {
64   void * p = BAMBOO_LOCAL_MEM_CALLOC(size);
65   if(p == NULL) {
66     tprintf("mycalloc_i %s %d \n", file, line);
67     BAMBOO_EXIT();
68   }
69   return p;
70 }
71
72 void myfree(void * ptr) {
73   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
74   BAMBOO_LOCAL_MEM_FREE(ptr);
75   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
76   return;
77 }
78
79 void myfree_i(void * ptr) {
80   BAMBOO_LOCAL_MEM_FREE(ptr);
81   return;
82 }
83
84 #endif