} 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;
/* 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 */
--- /dev/null
+#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;
+ }
+}
+
--- /dev/null
+#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;
+}
+