add batch-mode script
[IRC.git] / Robust / src / Runtime / mem.c
index dc502d5c37fa4d90ff62981b0f18f27d7a867a33..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-1)&(~(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
+      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);
+void * mycalloc_share(int size) {
   int isize = ((size-1)&(~(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 * p = BAMBOO_SHARE_MEM_CALLOC(isize); // calloc(m, isize);
   if(p == NULL) {
     // no more global shared memory
     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();
-  }
+  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("mycalloc_i %s %d \n", file, line);
     BAMBOO_EXIT();
   }
@@ -122,29 +86,13 @@ inermycalloc_i:
 
 void myfree(void * ptr) {
   BAMBOO_ENTER_RUNTIME_MODE_FROM_CLIENT();
-#ifdef MULTICORE_GC
-  if(ptr >= BAMBOO_LOCAL_HEAP_START_VA ) {
-#endif
   BAMBOO_LOCAL_MEM_FREE(ptr);
-#ifdef MULTICORE_GC
-} else if(ptr >= BAMBOO_LOCAL_HEAP_START_VA_S) {
-  BAMBOO_LOCAL_MEM_FREE_S(ptr);
-}
-#endif
   BAMBOO_ENTER_CLIENT_MODE_FROM_RUNTIME();
   return;
 }
 
 void myfree_i(void * ptr) {
-#ifdef MULTICORE_GC
-  if(ptr >= BAMBOO_LOCAL_HEAP_START_VA ) {
-#endif
   BAMBOO_LOCAL_MEM_FREE(ptr);
-#ifdef MULTICORE_GC
-} else if(ptr >= BAMBOO_LOCAL_HEAP_START_VA_S) {
-  BAMBOO_LOCAL_MEM_FREE_S(ptr);
-}
-#endif
   return;
 }