bug fixes
[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 = calloc(1, (sizeof(objstr_t) + size));
6         tmp->size = size;
7         tmp->next = NULL;
8         tmp->top = tmp + 1; //points to end of objstr_t structure!
9         return tmp;
10 }
11
12 //free entire list, starting at store
13 void objstrDelete(objstr_t *store)
14 {
15         objstr_t *tmp;
16         while (store != NULL)
17         {
18                 tmp = store->next;
19                 free(store);
20                 store = tmp;
21         }
22         return;
23 }
24
25 void *objstrAlloc(objstr_t *store, unsigned int size)
26 {
27         void *tmp;
28         while (1)
29         {
30                 if (((unsigned int)store->top - (unsigned int)store - sizeof(objstr_t) + size) <= store->size)
31                 {  //store not full
32                         tmp = store->top;
33                         store->top += size;
34                         return tmp;
35                 }
36                 //store full
37                 if (store->next == NULL)
38                 {  //end of list, all full
39                         if (size > DEFAULT_OBJ_STORE_SIZE) //in case of large objects
40                         {
41                                 store->next = (objstr_t *)calloc(1,(sizeof(objstr_t) + size));
42                                 if (store->next == NULL)
43                                         return NULL;
44                                 store = store->next;
45                                 store->size = size;
46                         }
47                         else
48                         {
49                                 store->next = calloc(1,(sizeof(objstr_t) + DEFAULT_OBJ_STORE_SIZE));
50                                 if (store->next == NULL)
51                                         return NULL;
52                                 store = store->next;
53                                 store->next = NULL;
54                                 store->size = DEFAULT_OBJ_STORE_SIZE;
55                         }
56                         store->top = (void *)((unsigned int)store + sizeof(objstr_t) + size);
57                         return (void *)((unsigned int)store + sizeof(objstr_t));
58                 }
59                 else  //try the next one
60                         store = store->next;
61         }
62 }
63