From b66d1a9e8e8ea441c1cd69aa7debf91d2920959d Mon Sep 17 00:00:00 2001
From: bdemsky <bdemsky>
Date: Mon, 13 Apr 2009 20:41:31 +0000
Subject: [PATCH] avoid resizing...

---
 Robust/src/Runtime/STM/stm.c       | 55 +++++++++++++++++++++---------
 Robust/src/Runtime/STM/stmlookup.c | 16 +++++++++
 Robust/src/Runtime/STM/stmlookup.h |  1 +
 Robust/src/Runtime/STM/tm.h        |  7 ++--
 Robust/src/Runtime/thread.c        | 12 ++++++-
 5 files changed, 71 insertions(+), 20 deletions(-)

diff --git a/Robust/src/Runtime/STM/stm.c b/Robust/src/Runtime/STM/stm.c
index abdc0f7e..1c3a0147 100644
--- a/Robust/src/Runtime/STM/stm.c
+++ b/Robust/src/Runtime/STM/stm.c
@@ -14,6 +14,7 @@
 #include "garbage.h"
 /* Thread transaction variables */
 __thread objstr_t *t_cache;
+__thread objstr_t *t_reserve;
 __thread struct objlist * newobjs;
 
 #ifdef TRANSSTATS
@@ -51,6 +52,16 @@ objstr_t *objstrCreate(unsigned int size) {
   return tmp;
 }
 
+void objstrReset() {
+  while(t_cache->next!=NULL) {
+    objstr_t *next=t_cache->next;
+    t_cache->next=t_reserve;
+    t_reserve=t_cache;
+    t_cache=next;
+  }
+  t_cache->top=t_cache+1;
+}
+
 //free entire list, starting at store
 void objstrDelete(objstr_t *store) {
   objstr_t *tmp;
@@ -69,8 +80,7 @@ void objstrDelete(objstr_t *store) {
  * =================================================
  */
 void transStart() {
-  t_cache = objstrCreate(1048576);
-  t_chashCreate(CHASH_SIZE, CLOADFACTOR);
+  //Transaction start is currently free...commit and aborting is not
 }
 
 /* =======================================================
@@ -115,15 +125,15 @@ void randomdelay() {
  * - allocate space in an object store
  * ==============================================
  */
-void *objstrAlloc(objstr_t **osptr, unsigned int size) {
+void *objstrAlloc(unsigned int size) {
   void *tmp;
   int i=0;
-  objstr_t *store=*osptr;
+  objstr_t *store=t_cache;
   if ((size&7)!=0) {
     size+=(8-(size&7));
   }
 
-  for(;i<3;i++) {
+  for(;i<2;i++) {
     if (OSFREE(store)>=size) {
       tmp=store->top;
       store->top +=size;
@@ -135,13 +145,26 @@ void *objstrAlloc(objstr_t **osptr, unsigned int size) {
 
   {
     unsigned int newsize=size>DEFAULT_OBJ_STORE_SIZE?size:DEFAULT_OBJ_STORE_SIZE;
+    objstr_t **otmp=&t_reserve;
+    objstr_t *ptr;
+    while((ptr=*otmp)!=NULL) {
+      if (ptr->size>=newsize) {
+	//remove from list
+	*otmp=ptr->next;
+	ptr->next=t_cache;
+	t_cache=ptr;
+	ptr->top=((char *)(&ptr[1]))+size;
+	return &ptr[1];
+      }
+    }
+    
     objstr_t *os=(objstr_t *)calloc(1,(sizeof(objstr_t) + newsize));
-    void *ptr=&os[1];
-    os->next=*osptr;
-    (*osptr)=os;
+    void *nptr=&os[1];
+    os->next=t_cache;
+    t_cache=os;
     os->size=newsize;
-    os->top=((char *)ptr)+size;
-    return ptr;
+    os->top=((char *)nptr)+size;
+    return nptr;
   }
 }
 
@@ -165,7 +188,7 @@ __attribute__((pure)) void *transRead(void * oid) {
   objheader_t *header = (objheader_t *)(((char *)oid) - sizeof(objheader_t)); 
   GETSIZE(size, header);
   size += sizeof(objheader_t);
-  objcopy = (objheader_t *) objstrAlloc(&t_cache, size);
+  objcopy = (objheader_t *) objstrAlloc(size);
   memcpy(objcopy, header, size);
   /* Insert into cache's lookup table */
   STATUS(objcopy)=0;
@@ -206,9 +229,8 @@ int transCommit() {
       }
 #endif
       freenewobjs();
-      objstrDelete(t_cache);
-      t_cache=NULL;
-      t_chashDelete();
+      objstrReset();
+      t_chashreset();
       return TRANS_ABORT;
     }
     if(finalResponse == TRANS_COMMIT) {
@@ -219,9 +241,8 @@ int transCommit() {
       }
 #endif
       freenewobjs();
-      objstrDelete(t_cache);
-      t_cache=NULL;
-      t_chashDelete();
+      objstrReset();
+      t_chashreset();
       return 0;
     }
     /* wait a random amount of time before retrying to commit transaction*/
diff --git a/Robust/src/Runtime/STM/stmlookup.c b/Robust/src/Runtime/STM/stmlookup.c
index cc9a77e0..f9537c53 100644
--- a/Robust/src/Runtime/STM/stmlookup.c
+++ b/Robust/src/Runtime/STM/stmlookup.c
@@ -1,4 +1,5 @@
 #include "stmlookup.h"
+#include "strings.h"
 
 __thread chashlistnode_t *c_table;
 __thread unsigned int c_size;
@@ -23,6 +24,21 @@ void t_chashCreate(unsigned int size, double loadfactor) {
   c_numelements = 0; // Initial number of elements in the hash
 }
 
+void t_chashreset() {
+  chashlistnode_t *ptr = c_table;
+  int i;
+  for(i=0 ; i<c_size ; i++) {
+    chashlistnode_t * curr = ptr[i].next;
+    while(curr!=NULL) {
+      chashlistnode_t * next = curr->next;
+      free(curr);
+      curr=next;
+    }
+  }
+  c_numelements = 0;
+  bzero(c_table, sizeof(chashlistnode_t)*c_size);
+}
+
 chashtable_t *chashCreate(unsigned int size, double loadfactor) {
   chashtable_t *ctable;
   chashlistnode_t *nodes;
diff --git a/Robust/src/Runtime/STM/stmlookup.h b/Robust/src/Runtime/STM/stmlookup.h
index 7189bcdd..e7544748 100644
--- a/Robust/src/Runtime/STM/stmlookup.h
+++ b/Robust/src/Runtime/STM/stmlookup.h
@@ -39,6 +39,7 @@ void t_chashInsert(void * key, void *val);
 void * t_chashSearch(void * key);
 unsigned int t_chashResize(unsigned int newsize);
 void t_chashDelete();
+void t_chashreset();
 
 /* Prototypes for hash*/
 chashtable_t *chashCreate(unsigned int size, double loadfactor);
diff --git a/Robust/src/Runtime/STM/tm.h b/Robust/src/Runtime/STM/tm.h
index deee9282..3227d511 100644
--- a/Robust/src/Runtime/STM/tm.h
+++ b/Robust/src/Runtime/STM/tm.h
@@ -105,7 +105,6 @@ typedef struct objstr {
   unsigned int size;       //this many bytes are allocated after this header
   void *top;
   struct objstr *next;
-  struct objstr *prev;
 } objstr_t;
 
 #define MAXOBJLIST 512
@@ -116,6 +115,9 @@ struct objlist {
 };
 
 extern __thread struct objlist * newobjs;
+extern __thread objstr_t *t_cache;
+extern __thread objstr_t *t_reserve;
+
 
 #ifdef TRANSSTATS
 /***********************************
@@ -133,12 +135,13 @@ extern int nSoftAbortCommit;
  * ================================
  */
 int stmStartup();
+void objstrReset();
 void objstrDelete(objstr_t *store);
 objstr_t *objstrCreate(unsigned int size);
 void transStart();
 objheader_t *transCreateObj(void * ptr, unsigned int size);
 unsigned int getNewOID(void);
-void *objstrAlloc(objstr_t **osptr, unsigned int size);
+void *objstrAlloc(unsigned int size);
 __attribute__((pure)) void *transRead(void * oid);
 int transCommit();
 int traverseCache();
diff --git a/Robust/src/Runtime/thread.c b/Robust/src/Runtime/thread.c
index 9bd7aa0f..a7790dfc 100644
--- a/Robust/src/Runtime/thread.c
+++ b/Robust/src/Runtime/thread.c
@@ -15,6 +15,10 @@
 #ifndef RAW
 #include <stdio.h>
 #endif
+#ifdef STM
+#include "tm.h"
+#endif
+
 int threadcount;
 pthread_mutex_t gclock;
 pthread_mutex_t gclistlock;
@@ -118,8 +122,14 @@ void initthread(struct ___Thread___ * ___this___) {
   ___Thread______staticStart____L___Thread___((struct ___Thread______staticStart____L___Thread____params *)p);
 #else
   newobjs=calloc(1, sizeof(struct objlist));
+  t_cache = objstrCreate(1048576);
+  t_reserve=NULL;
+  t_chashCreate(CHASH_SIZE, CLOADFACTOR);
  ___Thread____NN____staticStart____L___Thread___((struct ___Thread____NN____staticStart____L___Thread____params *)p);
-  free(newobjs);
+ objstrDelete(t_cache);
+ objstrDelete(t_reserve);
+ t_chashDelete();
+ free(newobjs);
 #endif
   ___this___=(struct ___Thread___ *) p[2];
 #else
-- 
2.34.1