Changed structure of objstr_t: removed void *base pointer, data block now immediately...
authorerubow <erubow>
Sat, 3 Mar 2007 01:02:03 +0000 (01:02 +0000)
committererubow <erubow>
Sat, 3 Mar 2007 01:02:03 +0000 (01:02 +0000)
Implemented objstr_t functions in objstr.c, testobjstr.c tests them.

Robust/src/Runtime/DSTM/interface/dstm.h
Robust/src/Runtime/DSTM/interface/objstr.c [new file with mode: 0644]
Robust/src/Runtime/DSTM/interface/testobjstr.c [new file with mode: 0644]

index ee4b3a316029f01784af9f4414a60867bba3d64f..7e8df57751508cdad86aae54d28389a3cd395375 100644 (file)
@@ -17,8 +17,7 @@ typedef struct objheader {
 } objheader_t;
 
 typedef struct objstr {
-       void *base;
-       unsigned int size;
+       unsigned int size; //this many bytes are allocated after this header
        void *top;
        struct objstr *next;
 } objstr_t;
@@ -51,7 +50,7 @@ void *dstmAccept(void *);
 /* Prototypes for transactions */
 transrecord_t *transStart();
 objheader_t *transRead(transrecord_t *record, unsigned int oid);
-objheader_t *transCreateObj(transrecort_t *record, unsigned short type); //returns oid
+objheader_t *transCreateObj(transrecord_t *record, unsigned short type); //returns oid
 int transCommit(transrecord_t *record); //return 0 if successful
 /* end transactions */
 
diff --git a/Robust/src/Runtime/DSTM/interface/objstr.c b/Robust/src/Runtime/DSTM/interface/objstr.c
new file mode 100644 (file)
index 0000000..216a5bc
--- /dev/null
@@ -0,0 +1,59 @@
+#include "dstm.h"
+
+#define DEFAULT_OBJ_STORE_SIZE 1048510 //1MB
+
+objstr_t *objstrCreate(unsigned int size)
+{
+       objstr_t *tmp = malloc(sizeof(objstr_t) + size);
+       tmp->size = size;
+       tmp->top = tmp + 1; //points to end of objstr_t structure!
+       return tmp;
+}
+
+//free entire list, starting at store
+void objstrDelete(objstr_t *store)
+{
+       objstr_t *tmp;
+       while (store != NULL)
+       {
+               tmp = store->next;
+               free(store);
+               store = tmp;
+       }
+       return;
+}
+
+void *objstrAlloc(objstr_t *store, unsigned int size)
+{
+       void *tmp;
+       while (1)
+       {
+               if (((unsigned int)store->top - (unsigned int)store - sizeof(objstr_t) + size) <= store->size)
+               {  //store not full
+                       tmp = store->top;
+                       store->top += size;
+                       return tmp;
+               }
+               //store full
+               if (store->next == NULL)
+               {  //end of list, all full
+                       if (size > DEFAULT_OBJ_STORE_SIZE) //in case of large objects
+                       {
+                               store->next = (objstr_t *)malloc(sizeof(objstr_t) + size);
+                               store = store->next;
+                               store->size = size;
+                       }
+                       else
+                       {
+                               store->next = malloc(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE);
+                               store = store->next;
+                               store->size = DEFAULT_OBJ_STORE_SIZE;
+                       }
+                       store->top = (void *)((unsigned int)store + sizeof(objstr_t) + size);
+                       return (void *)((unsigned int)store + sizeof(objstr_t));
+               }
+               else  //try the next one
+                       store = store->next;
+       }
+}
+
diff --git a/Robust/src/Runtime/DSTM/interface/testobjstr.c b/Robust/src/Runtime/DSTM/interface/testobjstr.c
new file mode 100644 (file)
index 0000000..6929c56
--- /dev/null
@@ -0,0 +1,32 @@
+#include "dstm.h"
+
+#define NUMITEMS 1000000 //uses four object stores
+
+int main(void)
+{
+       objstr_t *myObjStr = objstrCreate(1048510);
+       int i;
+       int *j[NUMITEMS];
+       int data[NUMITEMS];
+       int fail = 0;
+       
+       for (i = 0; i < NUMITEMS; i++)
+       {
+               j[i] = objstrAlloc(myObjStr, sizeof(int));
+               *j[i] = data[i] = i;
+       }
+       for (i = 0; i < NUMITEMS; i++)
+       {
+               if (data[i] != *j[i])
+                       fail = 1;
+       }
+
+       if (fail)
+               printf("test failed\n");
+       else
+               printf("test succeeded\n");
+       
+       objstrDelete(myObjStr);
+       return 0;
+}
+