Fixed bugs..Atomic2.java testcase works fine
[IRC.git] / Robust / src / Runtime / DSTM / interface / objstr.c
1 #include "dstm.h"
2
3 objstr_t *objstrCreate(unsigned int size)
4 {
5         objstr_t *tmp = malloc(sizeof(objstr_t) + size);
6         tmp->size = size;
7         tmp->top = tmp + 1; //points to end of objstr_t structure!
8         return tmp;
9 }
10
11 //free entire list, starting at store
12 void objstrDelete(objstr_t *store)
13 {
14         objstr_t *tmp;
15         while (store != NULL)
16         {
17                 tmp = store->next;
18                 free(store);
19                 store = tmp;
20         }
21         return;
22 }
23
24 void *objstrAlloc(objstr_t *store, unsigned int size)
25 {
26         void *tmp;
27         while (1)
28         {
29                 if (((unsigned int)store->top - (unsigned int)store - sizeof(objstr_t) + size) <= store->size)
30                 {  //store not full
31                         tmp = store->top;
32                         store->top += size;
33                         return tmp;
34                 }
35                 //store full
36                 if (store->next == NULL)
37                 {  //end of list, all full
38                         if (size > DEFAULT_OBJ_STORE_SIZE) //in case of large objects
39                         {
40                                 store->next = (objstr_t *)malloc(sizeof(objstr_t) + size);
41                                 if (store->next == NULL)
42                                         return NULL;
43                                 store = store->next;
44                                 store->size = size;
45                         }
46                         else
47                         {
48                                 store->next = malloc(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE);
49                                 if (store->next == NULL)
50                                         return NULL;
51                                 store = store->next;
52                                 store->size = DEFAULT_OBJ_STORE_SIZE;
53                         }
54                         store->top = (void *)((unsigned int)store + sizeof(objstr_t) + size);
55                         return (void *)((unsigned int)store + sizeof(objstr_t));
56                 }
57                 else  //try the next one
58                         store = store->next;
59         }
60 }
61