add batch-mode script
[IRC.git] / Robust / src / Runtime / mem.c
index a52ef83446cdd5b4e6858fc9cd98b47e67447b14..80caa39228eeeab4645cdfd751faae86188eadcd 100644 (file)
@@ -2,54 +2,96 @@
 
 #ifdef MULTICORE
 #include "runtime.h"
+#include <stdio.h>
+
+#if defined(MULTICORE_GC)||defined(PMC_GC)
+#include "multicoreruntime.h"
+#include "bambooalign.h"
 #include "runtime_arch.h"
+#include "methodheaders.h"
+#endif
 
-/*void * m_calloc(int m, int size) {
-        void * p = malloc(m*size);
-        int i = 0;
-        for(i = 0; i < size; ++i) {
- *(char *)(p+i) = 0;
-        }
-        return p;
-   }*/
+#ifdef MULTICORE_GC
+#include "multicoremem.h"
+#include "multicoregarbage.h"
+#endif
+#ifdef PMC_GC
+#include "multicoregc.h"
+#include "pmc_garbage.h"
+#endif
 
-void * mycalloc(int m, int size) {
+#if defined(MULTICORE_GC)||defined(PMC_GC)
+extern volatile bool gcflag;
+void * mycalloc_share(struct garbagelist * stackptr, int size) {
   void * p = NULL;
-  int isize = size; //2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
-  BAMBOO_START_CRITICAL_SECTION_MEM();
-  p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
-  if(p == NULL) {
-         BAMBOO_EXIT(0xa024);
+  int isize = ((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
+  int hasgc = 0;
+  int loopcount = 0;
+
+  while(true) {
+    p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
+
+    if(p != NULL) 
+      return p;
+    
+    // no more global shared memory
+    if(hasgc < 30) {
+      // start gc
+      if(gcflag) {
+       gc(stackptr);
+       hasgc++;
+      }
+    } else {
+      // no more global shared memory
+      printf("Did %u collections without getting memory\n", hasgc);
+      BAMBOO_EXIT();
+    }
+    loopcount++;
+    if (loopcount>10000000)
+      tprintf("Loopcount in mycalloc_share hit %u\n",loopcount);
   }
-  BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
-  //return (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
-  return p;
+
+  BAMBOO_EXIT();
+  return NULL;
 }
 
-void * mycalloc_share(int m, int size) {
-  void * p = NULL;
-  int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
-  BAMBOO_START_CRITICAL_SECTION_MEM();
-  p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
+#else
+void * mycalloc_share(int size) {
+  int isize = ((size-1)&(~(BAMBOO_CACHE_LINE_MASK)))+(BAMBOO_CACHE_LINE_SIZE);
+  void * p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
   if(p == NULL) {
-         BAMBOO_EXIT(0xa025);
+    // no more global shared memory
+    BAMBOO_EXIT();
   }
-  BAMBOO_CLOSE_CRITICAL_SECTION_MEM();
   return (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
 }
+#endif
 
-void * mycalloc_i(int m, int size) {
-  void * p = NULL;
-  int isize = size; //2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
-  p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
+void * mycalloc(int size, char * file, int line) {
+  BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
+  void * p = mycalloc_i(size, file, line);
+  BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
+  return p;
+}
+
+
+void * mycalloc_i(int size, char * file, int line) {
+  void * p = BAMBOO_LOCAL_MEM_CALLOC(size);
   if(p == NULL) {
-         BAMBOO_EXIT(0xa026);
+    tprintf("mycalloc_i %s %d \n", file, line);
+    BAMBOO_EXIT();
   }
   return p;
-  //return (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
 }
 
 void myfree(void * ptr) {
+  BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
+  BAMBOO_LOCAL_MEM_FREE(ptr);
+  BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
+  return;
+}
+
+void myfree_i(void * ptr) {
   BAMBOO_LOCAL_MEM_FREE(ptr);
   return;
 }