about to make lots of changes to system, just committing this stable compilation...
[IRC.git] / Robust / src / Runtime / runtime.c
index 1ad9651b2f210598ab8975bb4f87475a1e0fd7f0..c175242faf4de4d6beb6b940a4b3cfcf725f36b2 100644 (file)
 #include "runtime.h"
 #include "structdefs.h"
-#include <string.h>
 #include <signal.h>
 #include "mem.h"
-#include<fcntl.h>
-#include<sys/types.h>
-#include<sys/mman.h>
-#include<errno.h>
-#include<signal.h>
-#include<stdio.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdio.h>
+#include "option.h"
+#include "methodheaders.h"
+#ifdef DSTM
+#include "dstm.h"
+#include "prelookup.h"
+#include "prefetch.h"
+#endif
+#ifdef STM
+#include "tm.h"
+#include <pthread.h>
+#endif
+
+#if defined(THREADS)||defined(STM)
+/* Global barrier for STM */
+pthread_barrier_t barrier;
+pthread_barrierattr_t attr;
+#endif
+
+#include <string.h>
 
 extern int classsize[];
+extern int typearray[];
+extern int typearray2[];
 jmp_buf error_handler;
 int instructioncount;
 
-#ifdef TASK
-#include "checkpoint.h"
-#include "Queue.h"
-#include "SimpleHash.h"
-#include "GenericHashtable.h"
-#include <sys/select.h>
-
-#ifdef CONSCHECK
-#include "instrument.h"
-#endif
-
-struct Queue * activetasks;
-struct parameterwrapper * objectqueues[NUMCLASSES];
-struct genhashtable * failedtasks;
-/*struct genhashtable * failedobjects;*/
-extern char ** environ;
 char *options;
 int injectfailures=0;
 float failurechance=0;
+int errors=0;
 int debugtask=0;
 int injectinstructionfailures;
 int failurecount;
 float instfailurechance=0;
+int numfailures;
+int instaccum=0;
+#ifdef DMALLOC
+#include "dmalloc.h"
+#endif
 
-void processOptions() {
-  int i;
-  options=NULL;
-  for(i=0;environ[i]!=0;i++) {
-    if (strncmp(environ[i],"BRISTLECONE=",12)==0) {
-      options=environ[i]+12;
-      break;
+int instanceof(struct ___Object___ *ptr, int type) {
+  int i=ptr->type;
+  do {
+    if (i==type)
+      return 1;
+    i=typearray[i];
+  } while(i!=-1);
+  i=ptr->type;
+  if (i>NUMCLASSES) {
+    do {
+      if (i==type)
+       return 1;
+      i=typearray2[i-NUMCLASSES];
+    } while(i!=-1);
+  }
+  return 0;
+}
+
+void exithandler(int sig, siginfo_t *info, void * uap) {
+  exit(0);
+}
+
+void initializeexithandler() {
+  struct sigaction sig;
+  sig.sa_sigaction=&exithandler;
+  sig.sa_flags=SA_SIGINFO;
+  sigemptyset(&sig.sa_mask);
+  sigaction(SIGUSR2, &sig, 0);
+}
+
+
+/* This function inject failures */
+
+void injectinstructionfailure() {
+#ifdef TASK
+  if (injectinstructionfailures) {
+    if (numfailures==0)
+      return;
+    instructioncount=failurecount;
+    instaccum+=failurecount;
+    if ((((double)random())/RAND_MAX)<instfailurechance) {
+      if (numfailures>0)
+       numfailures--;
+      printf("FAILURE!!! %d\n",numfailures);
+      longjmp(error_handler,11);
     }
   }
-  
-  while(options!=NULL) {
-    if (strncmp(options,"-injectfailures",sizeof("-injectfailures")-1)==0) {
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-      if (options==NULL)
-       break;
-      sscanf(options, "%f", &failurechance);
-      injectfailures=1;
-      printf("Injecting errors with chance=%f\n",failurechance);
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-    } else if (strncmp(options,"-injectinstructionfailures",sizeof("-injectinstructionfailures")-1)==0) {
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-      if (options==NULL)
-       break;
-      sscanf(options, "%d", &failurecount);
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-      if (options==NULL)
-       break;
-      sscanf(options, "%f", &instfailurechance);
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-      instructioncount=failurecount;
-      injectinstructionfailures=1;
-      printf("Injecting errors with count=%d\n",failurecount);
-      printf("Injecting errors with chance=%f\n",instfailurechance);
-
-    } else if (strncmp(options, "-debugtask",sizeof("-debugtask")-1)==0) {
-      options=strchr(options,' ');
-      if (options!=NULL) options++;
-      debugtask=1;
-      printf("Debug task option on.\n");
-    } else
-      break;
+#else
+#ifdef THREADS
+  if (injectinstructionfailures) {
+    if (numfailures==0)
+      return;
+    instaccum+=failurecount;
+    if ((((double)random())/RAND_MAX)<instfailurechance) {
+      if (numfailures>0)
+       numfailures--;
+      printf("FAILURE!!! %d\n",numfailures);
+      threadexit();
+    }
   }
+#endif
+#endif
 }
 
-int main(int argc, char **argv) {
-  GC_init(); // Initialize the garbage collector
-#ifdef CONSCHECK
-  initializemmap();
+#ifdef D___Double______nativeparsedouble____L___String___
+double CALL01(___Double______nativeparsedouble____L___String___,struct ___String___ * ___str___) {
+  int length=VAR(___str___)->___count___;
+  int maxlength=(length>60)?60:length;
+  char str[maxlength+1];
+  struct ArrayObject * chararray=VAR(___str___)->___value___;
+  int i;
+  int offset=VAR(___str___)->___offset___;
+  for(i=0; i<maxlength; i++) {
+    str[i]=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
+  }
+  str[i]=0;
+  double d=atof(str);
+  return d;
+}
 #endif
-  processOptions();
 
-  {
+#ifdef D___String______convertdoubletochar____D__AR_C
+int CALL12(___String______convertdoubletochar____D__AR_C, double ___val___, double ___val___, struct ArrayObject ___chararray___) {
+  int length=VAR(___chararray___)->___length___;
+  char str[length];
   int i;
-  /* Allocate startup object */
-  struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
-  struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1); 
-  failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
-                                  (int (*)(void *,void *)) &comparetpd);
-  /*  failedobjects=genallocatehashtable(NULL,NULL);*/
-  
-  activetasks=createQueue();
-
-  /* Set flags */
-  processtasks();
-  flagorand(startupobject,1,0xFFFFFFFF);
-
-  /* Build array of strings */
-
-  startupobject->___parameters___=stringarray;
-
-  for(i=1;i<argc;i++) {
-    int length=strlen(argv[i]);
-    struct ___String___ *newstring=NewString(argv[i],length);
-    ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
-  }
-  executetasks();
+  int num=snprintf(str, length, "%f",___val___);
+  if (num>=length)
+    num=length-1;
+  for(i=0; i<length; i++) {
+    ((short *)(((char *)&VAR(___chararray___)->___length___)+sizeof(int)))[i]=(short)str[i];
   }
+  return num;
 }
+#endif
 
-int hashCodetpd(struct taskparamdescriptor *ftd) {
-  int hash=(int)ftd->task;
+void CALL11(___System______exit____I,int ___status___, int ___status___) {
+#ifdef TRANSSTATS
+  printf("numTransCommit = %d\n", numTransCommit);
+  printf("numTransAbort = %d\n", numTransAbort);
+  printf("nSoftAbort = %d\n", nSoftAbort);
+#ifdef STM
+  printf("nSoftAbortCommit = %d\n", nSoftAbortCommit);
+  printf("nSoftAbortAbort = %d\n", nSoftAbortAbort);
+#ifdef STMSTATS
   int i;
-  for(i=0;i<ftd->numParameters;i++) {
-    hash^=(int)ftd->parameterArray[i];
+  for(i=0; i<TOTALNUMCLASSANDARRAY; i++) {
+    printf("typesCausingAbort[%d]= %d\n", i, typesCausingAbort[i]);
   }
-  return hash;
+#endif
+#endif
+#endif
+  exit(___status___);
 }
 
-int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
+#ifdef D___Vector______removeElement_____AR_L___Object____I_I
+void CALL23(___Vector______removeElement_____AR_L___Object____I_I, int ___index___, int ___size___, struct ArrayObject * ___array___, int ___index___, int ___size___) {
+  char* offset=((char *)(&VAR(___array___)->___length___))+sizeof(unsigned int)+sizeof(void *)*___index___;
+  memmove(offset, offset+sizeof(void *),(___size___-___index___-1)*sizeof(void *));
+}
+#endif
+
+void CALL11(___System______printI____I,int ___status___, int ___status___) {
+  printf("%d\n",___status___);
+}
+
+long long CALL00(___System______currentTimeMillis____) {
+  struct timeval tv; long long retval;
+  gettimeofday(&tv, NULL);
+  retval = tv.tv_sec; /* seconds */
+  retval*=1000; /* milliseconds */
+  retval+= (tv.tv_usec/1000); /* adjust milliseconds & add them in */
+  return retval;
+}
+
+void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
+  struct ArrayObject * chararray=VAR(___s___)->___value___;
   int i;
-  if (ftd1->task!=ftd2->task)
-    return 0;
-  for(i=0;i<ftd1->numParameters;i++)
-    if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
-      return 0;
-  return 1;
+  int offset=VAR(___s___)->___offset___;
+  for(i=0; i<VAR(___s___)->___count___; i++) {
+    short sc=((short *)(((char *)&chararray->___length___)+sizeof(int)))[i+offset];
+    putchar(sc);
+  }
 }
 
-void flagorand(void * ptr, int ormask, int andmask) {
-  int flag=((int *)ptr)[1];
-  struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
-  flag|=ormask;
-  flag&=andmask;
-  ((int*)ptr)[1]=flag;
-  /*Remove from all queues */
-  while(flagptr!=NULL) {
-    struct RuntimeHash *next;
-    RuntimeHashget(flagptr, (int) ptr, (int *) &next);
-    RuntimeHashremove(flagptr, (int)ptr, (int) next);
-    flagptr=next;
+#ifdef DSTM
+void CALL00(___System______clearPrefetchCache____) {
+  prehashClear();
+}
+
+#ifdef RANGEPREFETCH
+void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
+  /* Manual Prefetches to be inserted */
+  //printf("DEBUG-> %s() ___Object___ * ___o___ = %x\n", __func__, VAR(___o___));
+  //printf("DEBUG-> %s() ArrayObject * = %x\n", __func__, VAR(___offsets___));
+  int numoffset=VAR(___offsets___)->___length___;
+  int i;
+  short offArry[numoffset+2];
+  offArry[0] = 0;
+  offArry[1] = 0;
+  for(i = 2; i<(numoffset+2); i++) {
+    offArry[i] = *((short *)(((char *)&VAR(___offsets___)->___length___) + sizeof(int) + (i-2) * sizeof(short)));
+    //printf("DEBUG-> offArry[%d] = %d\n", i, offArry[i]);
   }
-  
-  {
-    struct QueueItem *tmpptr;
-    struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
-    int i;
-    struct RuntimeHash * prevptr=NULL;
-    while(parameter!=NULL) {
-      for(i=0;i<parameter->numberofterms;i++) {
-       int andmask=parameter->intarray[i*2];
-       int checkmask=parameter->intarray[i*2+1];
-       if ((flag&andmask)==checkmask) {
-         RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
-         prevptr=parameter->objectset;
-         {
-           struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
-           void * taskpointerarray[MAXTASKPARAMS];
-           int j;
-           int numparams=parameter->task->numParameters;
-           int done=1;
-           struct taskdescriptor * task=parameter->task;
-           int newindex=-1;
-           for(j=0;j<numparams;j++) {
-             struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
-             if (parameter==pw) {
-               taskpointerarray[j]=ptr;
-               newindex=j;
-             } else {
-               RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
-               if (RunhasNext(&iteratorarray[j])) {
-                 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
-                 Runnext(&iteratorarray[j]);
-               } else {
-                 done=0;
-                 break; /* No tasks to dispatch */
-               }
-             }
-           }
-           /* Queue task items... */
-
-           while(done) {
-             struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
-             tpd->task=task;
-             tpd->numParameters=numparams;
-             tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
-             for(j=0;j<numparams;j++)
-               tpd->parameterArray[j]=taskpointerarray[j];
-             /* Queue task */
-             if (!gencontains(failedtasks, tpd))
-               addNewItem(activetasks, tpd);
-             
-             /* This loop iterates to the next parameter combination */
-             for(j=0;j<numparams;j++) {
-               if (j==newindex) {
-                 if ((j+1)==numparams)
-                   done=0;
-                 continue;
-               }
-               if (RunhasNext(&iteratorarray[j])) {
-                 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
-                 Runnext(&iteratorarray[j]);
-                 break;
-               } else if ((j+1)!=numparams) {
-                 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
-               } else {
-                 done=0;
-                 break;
-               }
-             }
-           }
-         }
-         break;
-       }
-      }
-      parameter=parameter->next;
-    }
-    ((struct RuntimeHash **)ptr)[2]=prevptr;
+  unsigned int oid;
+  if(((unsigned int)(VAR(___o___)) & 1) != 0) { //odd
+    oid =  (unsigned int) VAR(___o___); //outside transaction therefore just an oid
+  } else { //even
+    oid = (unsigned int) COMPOID(VAR(___o___)); //inside transaction therefore a pointer to oid
   }
+  rangePrefetch(oid, (short)(numoffset+2), offArry);
 }
+#else
+void CALL02(___System______rangePrefetch____L___Object_____AR_S, struct ___Object___ * ___o___, struct ArrayObject * ___offsets___) {
+  return;
+}
+#endif
 
-/* Handler for signals */
-void myhandler(int sig, struct __siginfo *info, void *uap) {
-#ifdef DEBUG
-  printf("sig=%d\n",sig);
-  printf("signal\n");
 #endif
-  longjmp(error_handler,1);
+
+/* STM Barrier constructs */
+#ifdef D___Barrier______setBarrier____I
+void CALL11(___Barrier______setBarrier____I, int nthreads, int nthreads) {
+  // Barrier initialization
+  int ret;
+  if((ret = pthread_barrier_init(&barrier, NULL, nthreads)) != 0) {
+    printf("%s() Could not create a barrier: numthreads = 0 in %s\n", __func__, __FILE__);
+    exit(-1);
+  }
 }
+#endif
 
+#ifdef D___Barrier______enterBarrier____
+void CALL00(___Barrier______enterBarrier____) {
+  // Synchronization point
+  int ret;
+#ifdef PRECISE_GC
+  stopforgc((struct garbagelist *)___params___);
+#endif
+  ret = pthread_barrier_wait(&barrier);
+#ifdef PRECISE_GC
+  restartaftergc();
+#endif
+  if(ret != 0 && ret != PTHREAD_BARRIER_SERIAL_THREAD) {
+    printf("%s() Could not wait on barrier: error %d in %s\n", __func__, errno, __FILE__);
+    exit(-1);
+  }
+}
+#endif
 
-fd_set readfds;
-int maxreadfd;
-struct RuntimeHash *fdtoobject;
+/* Object allocation function */
 
-void addreadfd(int fd) {
-  if (fd>=maxreadfd)
-    maxreadfd=fd+1;
-  FD_SET(fd, &readfds);
+#ifdef DSTM
+__attribute__((malloc)) void * allocate_newglobal(int type) {
+  struct ___Object___ * v=(struct ___Object___ *) transCreateObj(classsize[type]);
+  v->type=type;
+#ifdef THREADS
+  v->tid=0;
+  v->lockentry=0;
+  v->lockcount=0;
+#endif
+  return v;
 }
 
-void removereadfd(int fd) {
-  FD_CLR(fd, &readfds);
-  if (maxreadfd==(fd+1)) {
-    maxreadfd--;
-    while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
-      maxreadfd--;
-  }
-}
+/* Array allocation function */
 
-/*
-void restoreObject(void * obj) {
-  if (gencontains(failedobjects, obj)) {
-    struct tpdlist *tpd=gengettable(failedobjects, obj);
-    genfreekey(failedobjects, obj);
-    while(tpd!=NULL) {
-      int i;
-      struct taskparamdescriptor *task=tpd->task;
-      genfreekey(failedtasks, task);
-      for(i=0;i<task->numParameters;i++) {
-       void *objother=task->parameterArray[i];
-       struct tpdlist *tpdother=gengettable(failedobjects, objother);
-       struct tpdlist *tmpptr;
-       genfreekey(failedobjects, objother);
-       struct tpdlist **tpdptr=&tpdother;
-       while((*tpdptr)->task!=task)
-         tpdptr=&((*tpdptr)->next);
-       tmpptr=*tpdptr;
-       (*tpdptr)=(*tpdptr)->next;
-       RUNFREE(tmpptr);
-       if (tpdother!=NULL)
-         genputtable(failedobjects, objother, tpdother);
-      }
-      RUNFREE(task);
-      {
-       struct tpdlist *old=tpd;
-       tpd=tpd->next;
-       RUNFREE(old);
-      }
-    }
+__attribute__((malloc)) struct ArrayObject * allocate_newarrayglobal(int type, int length) {
+  struct ArrayObject * v=(struct ArrayObject *)transCreateObj(sizeof(struct ArrayObject)+length*classsize[type]);
+  if (length<0) {
+    printf("ERROR: negative array\n");
+    return NULL;
   }
+  v->type=type;
+  v->___length___=length;
+#ifdef THREADS
+  v->tid=0;
+  v->lockentry=0;
+  v->lockcount=0;
+#endif
+  return v;
 }
-*/
+#endif
 
-void executetasks() {
-  void * taskpointerarray[MAXTASKPARAMS];
 
-  /* Set up signal handlers */
-  struct sigaction sig;
-  sig.sa_sigaction=&myhandler;
-  sig.sa_flags=SA_SIGINFO;
-  sigemptyset(&sig.sa_mask);
+#ifdef STM
+// STM Versions of allocation functions
 
-  /* Catch bus errors, segmentation faults, and floating point exceptions*/
-  sigaction(SIGBUS,&sig,0);
-  sigaction(SIGSEGV,&sig,0);
-  sigaction(SIGFPE,&sig,0);
-
-  /* Zero fd set */
-  FD_ZERO(&readfds);
-  maxreadfd=0;
-  fdtoobject=allocateRuntimeHash(100);
-
-  /* Map first block of memory to protected, anonymous page */
-  mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
-
-  newtask:
-  while(!isEmpty(activetasks)||(maxreadfd>0)) {
-
-    if (maxreadfd>0) {
-      int i;
-      struct timeval timeout={0,0};
-      fd_set tmpreadfds;
-      int numselect;
-      FD_COPY(&readfds, &tmpreadfds);
-      numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
-      if (numselect>0) {
-       /* Process ready fd's */
-       int fd;
-       for(fd=0;fd<maxreadfd;fd++) {
-         if (FD_ISSET(fd, &tmpreadfds)) {
-           /* Set ready flag on object */
-           void * objptr;
-           if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
-             flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
-           }
-         }
-       }
-      }
-    }
+/* Object allocation function */
+__attribute__((malloc)) void * allocate_newtrans(void * ptr, int type) {
+  struct ___Object___ * v=(struct ___Object___ *) transCreateObj(ptr, classsize[type]);
+  v->type=type;
+  v->___objlocation___=v;
+  return v;
+}
 
-    if (!isEmpty(activetasks)) {
-      int i;
-      struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
-      struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
-      removeItem(activetasks, qi);
-      
-      for(i=0;i<tpd->task->numParameters;i++) {
-       void * parameter=tpd->parameterArray[i];
-       struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
-       struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
-       if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
-         goto newtask;
-       taskpointerarray[i]=parameter;
-      }
-      {
-       struct RuntimeHash * forward=allocateRuntimeHash(100);
-       struct RuntimeHash * reverse=allocateRuntimeHash(100);
-       void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
-       int x;
-       if (x=setjmp(error_handler)) {
-         /* Recover */
-         int h;
-#ifdef DEBUG
-         printf("Fatal Error=%d, Recovering!\n",x);
-#endif
-         genputtable(failedtasks,tpd,tpd);
-       /* for(i=0;i<tpd->task->numParameters;i++) {
-           void *parameter=tpd->parameterArray[i];
-           {
-             // Create mapping from object -> failed tasks 
-             struct tpdlist * tpnew=RUNMALLOC(sizeof(struct tpdlist));
-             tpnew->task=tpd;
-             if (gencontains(failedobjects, parameter)) {
-               struct tpdlist * tpdptr=gengettable(failedobjects, parameter);
-               tpnew->next=tpdptr->next;
-               tpdptr->next=tpnew;
-             } else {
-               tpnew->next=NULL;
-               genputtable(failedobjects, parameter, tpnew);
-             }
-           }
-         }  */
-         restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
-       } else {
-         if (injectfailures) {
-           if ((((double)random())/RAND_MAX)<failurechance) {
-             printf("\nINJECTING TASK FAILURE to %s\n", tpd->task->name);
-             longjmp(error_handler,10);
-           }
-         }
-         /* Actually call task */
-         if (debugtask) {
-           printf("ENTER %s\n",tpd->task->name);
-           ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
-           printf("EXIT %s\n",tpd->task->name);
-         } else
-           ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
-       }
-      }
-    }
+/* Array allocation function */
+__attribute__((malloc)) struct ArrayObject * allocate_newarraytrans(void * ptr, int type, int length) {
+  struct ArrayObject * v=(struct ArrayObject *)transCreateObj(ptr, sizeof(struct ArrayObject)+length*classsize[type]);
+  if (length<0) {
+    printf("ERROR: negative array\n");
+    return NULL;
   }
+  v->___objlocation___=(struct ___Object___*)v;
+  v->type=type;
+  v->___length___=length;
+  return v;
+}
+__attribute__((malloc)) void * allocate_new(void * ptr, int type) {
+  objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, classsize[type]+sizeof(objheader_t));
+  struct ___Object___ * v=(struct ___Object___ *) &tmp[1];
+  initdsmlocks(&tmp->lock);
+  tmp->version = 1;
+  v->___objlocation___=v;
+  v->type = type;
+  return v;
 }
 
-void processtasks() {
-  int i;
-  for(i=0;i<numtasks;i++) {
-    struct taskdescriptor * task=taskarray[i];
-    int j;
-
-    for(j=0;j<task->numParameters;j++) {
-      struct parameterdescriptor *param=task->descriptorarray[j];
-      struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
-      struct parameterwrapper ** ptr=&objectqueues[param->type];
-
-      param->queue=parameter;
-      parameter->objectset=allocateRuntimeHash(10);
-      parameter->numberofterms=param->numberterms;
-      parameter->intarray=param->intarray;
-      parameter->task=task;
-      /* Link new queue in */
-      while((*ptr)!=NULL)
-       ptr=&((*ptr)->next);
-      (*ptr)=parameter;
-    }
+/* Array allocation function */
+
+__attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
+  objheader_t *tmp=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]+sizeof(objheader_t));
+  struct ArrayObject * v=(struct ArrayObject *) &tmp[1];
+  initdsmlocks(&tmp->lock);
+  tmp->version=1;
+  v->type=type;
+  if (length<0) {
+    printf("ERROR: negative array %d\n", length);
+    return NULL;
   }
+  v->___objlocation___=(struct ___Object___ *)v;
+  v->___length___=length;
+  return v;
 }
-
 #endif
 
-
-void injectinstructionfailure() {
-#ifdef TASK
-  if (injectinstructionfailures) {
-    instructioncount=failurecount;    
-    if ((((double)random())/RAND_MAX)<instfailurechance) {
-      printf("FAILURE!!!\n");
-      longjmp(error_handler,11);
-    }
-  }
-#else
+#ifndef STM
+#if defined(PRECISE_GC)
+__attribute__((malloc)) void * allocate_new(void * ptr, int type) {
+  struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
+  v->type=type;
+#ifdef THREADS
+  v->tid=0;
+  v->lockentry=0;
+  v->lockcount=0;
+#endif
+#ifdef OPTIONAL
+  v->fses=0;
 #endif
+  return v;
 }
 
-int ___Object______hashcode____(struct ___Object___ * ___this___) {
-  return (int) ___this___;
-}
+/* Array allocation function */
 
-void ___System______printString____L___String___(struct ___String___ * s) {
-    struct ArrayObject * chararray=s->___value___;
-    int i;
-    int offset=s->___offset___;
-    for(i=0;i<s->___count___;i++) {
-       short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
-       putchar(s);
-    }
+__attribute__((malloc)) struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
+  struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
+  v->type=type;
+  if (length<0) {
+    printf("ERROR: negative array\n");
+    return NULL;
+  }
+  v->___length___=length;
+#ifdef THREADS
+  v->tid=0;
+  v->lockentry=0;
+  v->lockcount=0;
+#endif
+#ifdef OPTIONAL
+  v->fses=0;
+#endif
+  return v;
 }
 
-void * allocate_new(int type) {
-  void * v=FREEMALLOC(classsize[type]);
-  *((int *)v)=type;
+#else
+__attribute__((malloc)) void * allocate_new(int type) {
+  struct ___Object___ * v=FREEMALLOC(classsize[type]);
+  v->type=type;
+#ifdef OPTIONAL
+  v->fses=0;
+#endif
   return v;
 }
 
-struct ArrayObject * allocate_newarray(int type, int length) {
-  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
+/* Array allocation function */
+
+__attribute__((malloc)) struct ArrayObject * allocate_newarray(int type, int length) {
+  __attribute__((malloc))  struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
   v->type=type;
   v->___length___=length;
+#ifdef OPTIONAL
+  v->fses=0;
+#endif
   return v;
 }
+#endif
+#endif
 
-struct ___String___ * NewString(const char *str,int length) {
+/* Converts C character arrays into Java strings */
+#ifdef PRECISE_GC
+__attribute__((malloc)) struct ___String___ * NewString(void * ptr, const char *str,int length) {
+#else
+__attribute__((malloc)) struct ___String___ * NewString(const char *str,int length) {
+#endif
+  int i;
+#ifdef PRECISE_GC
+  struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
+  INTPTR ptrarray[]={1, (INTPTR) ptr, (INTPTR) chararray};
+  struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
+  chararray=(struct ArrayObject *) ptrarray[2];
+#else
   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
   struct ___String___ * strobj=allocate_new(STRINGTYPE);
-  int i;
+#endif
   strobj->___value___=chararray;
   strobj->___count___=length;
   strobj->___offset___=0;
 
-  for(i=0;i<length;i++) {
-    ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
+  for(i=0; i<length; i++) {
+    ((short *)(((char *)&chararray->___length___)+sizeof(int)))[i]=(short)str[i];
+  }
   return strobj;
 }
 
+/* Generated code calls this if we fail a bounds check */
+
 void failedboundschk() {
 #ifndef TASK
   printf("Array out of bounds\n");
-  exit(-1);
+#ifdef THREADS
+  threadexit();
 #else
-  longjmp(error_handler,2);
-#endif
-}
-
-void failednullptr() {
-#ifndef TASK
-  printf("Dereferenced a null pointer\n");
   exit(-1);
+#endif
 #else
-  longjmp(error_handler,3);
+  longjmp(error_handler,2);
 #endif
 }
 
+/* Abort task call */
 void abort_task() {
-#ifndef TASK
+#ifdef TASK
+  longjmp(error_handler,4);
+#else
   printf("Aborting\n");
   exit(-1);
-#else
-  longjmp(error_handler,4);
 #endif
 }