From 2b725cdbe9252a894cd58b4922f11828efb9766e Mon Sep 17 00:00:00 2001 From: erubow Date: Sat, 3 Mar 2007 01:02:03 +0000 Subject: [PATCH] Changed structure of objstr_t: removed void *base pointer, data block now immediately follows the structure. This cuts the number of malloc() and free() calls in half. Implemented objstr_t functions in objstr.c, testobjstr.c tests them. --- Robust/src/Runtime/DSTM/interface/dstm.h | 5 +- Robust/src/Runtime/DSTM/interface/objstr.c | 59 +++++++++++++++++++ .../src/Runtime/DSTM/interface/testobjstr.c | 32 ++++++++++ 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 Robust/src/Runtime/DSTM/interface/objstr.c create mode 100644 Robust/src/Runtime/DSTM/interface/testobjstr.c diff --git a/Robust/src/Runtime/DSTM/interface/dstm.h b/Robust/src/Runtime/DSTM/interface/dstm.h index ee4b3a31..7e8df577 100644 --- a/Robust/src/Runtime/DSTM/interface/dstm.h +++ b/Robust/src/Runtime/DSTM/interface/dstm.h @@ -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 index 00000000..216a5bcf --- /dev/null +++ b/Robust/src/Runtime/DSTM/interface/objstr.c @@ -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 index 00000000..6929c56e --- /dev/null +++ b/Robust/src/Runtime/DSTM/interface/testobjstr.c @@ -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; +} + -- 2.34.1