add batch-mode script
[IRC.git] / Robust / src / Runtime / mem.c
index 8dd5c892b00c16ab084af998837e2098b8bd1e7d..80caa39228eeeab4645cdfd751faae86188eadcd 100644 (file)
 
 #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
 
 #ifdef MULTICORE_GC
+#include "multicoremem.h"
+#include "multicoregarbage.h"
+#endif
+#ifdef PMC_GC
+#include "multicoregc.h"
+#include "pmc_garbage.h"
+#endif
+
+#if defined(MULTICORE_GC)||defined(PMC_GC)
 extern volatile bool gcflag;
-void * mycalloc_share(struct garbagelist * stackptr,
-                      int m,
-                      int size) {
+void * mycalloc_share(struct garbagelist * stackptr, int size) {
   void * p = NULL;
-  //int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
-  int isize = (size & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE);
+  int isize = ((size-1)&(~(ALIGNMENTSIZE-1)))+ALIGNMENTSIZE;
   int hasgc = 0;
-memalloc:
-  BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
-  while(gcflag) {
-    BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
-    gc(stackptr);
-    BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
-  }
-  p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
-  if(p == NULL) {
+  int loopcount = 0;
+
+  while(true) {
+    p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
+
+    if(p != NULL) 
+      return p;
+    
     // no more global shared memory
-    BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
-    if(hasgc < 5) {
+    if(hasgc < 30) {
       // start gc
-      while(gcflag) {
-        gc(stackptr);
+      if(gcflag) {
+       gc(stackptr);
+       hasgc++;
       }
-      hasgc++;
     } else {
       // no more global shared memory
-      BAMBOO_EXIT(0xc001);
+      printf("Did %u collections without getting memory\n", hasgc);
+      BAMBOO_EXIT();
     }
-
-    // try to malloc again
-    goto memalloc;
+    loopcount++;
+    if (loopcount>10000000)
+      tprintf("Loopcount in mycalloc_share hit %u\n",loopcount);
   }
-  BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
-  void * alignedp =
-    (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
-  BAMBOO_MEMSET_WH(p, -2, (alignedp - p));
-  BAMBOO_MEMSET_WH(alignedp + size, -2, p + isize - alignedp - size);
-  return alignedp;
+
+  BAMBOO_EXIT();
+  return NULL;
 }
+
 #else
-void * mycalloc_share(int m,
-                      int size) {
-  void * p = NULL;
-  //int isize = 2*BAMBOO_CACHE_LINE_SIZE-4+(size-1)&(~BAMBOO_CACHE_LINE_MASK);
-  int isize = (size & (~(BAMBOO_CACHE_LINE_MASK))) + (BAMBOO_CACHE_LINE_SIZE);
-  BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
-  p = BAMBOO_SHARE_MEM_CALLOC_I(m, isize); // calloc(m, isize);
+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) {
     // no more global shared memory
-    BAMBOO_EXIT(0xc002);
+    BAMBOO_EXIT();
   }
-  BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
-  return
-    (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
+  return (void *)(BAMBOO_CACHE_LINE_SIZE+((int)p-1)&(~BAMBOO_CACHE_LINE_MASK));
 }
 #endif
 
-void * mycalloc(int m,
-                int size,
-                char * file,
-                int line) {
-  void * p = NULL;
-  int isize = size;
+void * mycalloc(int size, char * file, int line) {
   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
-#ifdef MULTICORE_GC
-  extern bool gc_localheap_s;
-inermycalloc_i:
-  p = gc_localheap_s ? BAMBOO_LOCAL_MEM_CALLOC_S(m, isize) :
-      BAMBOO_LOCAL_MEM_CALLOC(m, isize);
-#else
-  p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
-#endif
-  if(p == NULL) {
-#ifdef MULTICORE_GC
-    if(!gc_localheap_s) {
-      gc_localheap_s = true;
-      goto inermycalloc_i;
-    }
-#endif
-    printf("mycalloc %s %d \n", file, line);
-    BAMBOO_EXIT(0xc003);
-  }
+  void * p = mycalloc_i(size, file, line);
   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
   return p;
 }
 
 
-void * mycalloc_i(int m,
-                  int size,
-                  char * file,
-                  int line) {
-  void * p = NULL;
-  int isize = size;
-#ifdef MULTICORE_GC
-  extern bool gc_localheap_s;
-inermycalloc_i:
-  p = gc_localheap_s ? BAMBOO_LOCAL_MEM_CALLOC_S(m, isize) :
-      BAMBOO_LOCAL_MEM_CALLOC(m, isize);
-#else
-  p = BAMBOO_LOCAL_MEM_CALLOC(m, isize); // calloc(m, isize);
-#endif
+void * mycalloc_i(int size, char * file, int line) {
+  void * p = BAMBOO_LOCAL_MEM_CALLOC(size);
   if(p == NULL) {
-#ifdef MULTICORE_GC
-    if(!gc_localheap_s) {
-      gc_localheap_s = true;
-      goto inermycalloc_i;
-    }
-#endif
-    tprintf("macalloc_i %s %d \n", file, line);
-    BAMBOO_EXIT(0xc004);
+    tprintf("mycalloc_i %s %d \n", file, line);
+    BAMBOO_EXIT();
   }
   return p;
 }
 
 void myfree(void * ptr) {
-#ifdef MULTICORE_GC
-  if(ptr >= BAMBOO_LOCAL_HEAP_START_VA ) {
-#endif
+  BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
   BAMBOO_LOCAL_MEM_FREE(ptr);
-#ifdef MULTICORE_GC
-} else if(ptr >= BAMBOO_LOCAL_HEAP_START_VA_S) {
-  BAMBOO_LOCAL_MEM_FREE_S(ptr);
+  BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
+  return;
 }
-#endif
+
+void myfree_i(void * ptr) {
+  BAMBOO_LOCAL_MEM_FREE(ptr);
   return;
 }